github.com/kdevb0x/go@v0.0.0-20180115030120-39687051e9e7/src/os/exec.go (about)

     1  // Copyright 2009 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package os
     6  
     7  import (
     8  	"internal/testlog"
     9  	"runtime"
    10  	"sync"
    11  	"sync/atomic"
    12  	"syscall"
    13  	"time"
    14  )
    15  
    16  // Process stores the information about a process created by StartProcess.
    17  type Process struct {
    18  	Pid    int
    19  	handle uintptr      // handle is accessed atomically on Windows
    20  	isdone uint32       // process has been successfully waited on, non zero if true
    21  	sigMu  sync.RWMutex // avoid race between wait and signal
    22  }
    23  
    24  func newProcess(pid int, handle uintptr) *Process {
    25  	p := &Process{Pid: pid, handle: handle}
    26  	runtime.SetFinalizer(p, (*Process).Release)
    27  	return p
    28  }
    29  
    30  func (p *Process) setDone() {
    31  	atomic.StoreUint32(&p.isdone, 1)
    32  }
    33  
    34  func (p *Process) done() bool {
    35  	return atomic.LoadUint32(&p.isdone) > 0
    36  }
    37  
    38  // ProcAttr holds the attributes that will be applied to a new process
    39  // started by StartProcess.
    40  type ProcAttr struct {
    41  	// If Dir is non-empty, the child changes into the directory before
    42  	// creating the process.
    43  	Dir string
    44  	// If Env is non-nil, it gives the environment variables for the
    45  	// new process in the form returned by Environ.
    46  	// If it is nil, the result of Environ will be used.
    47  	Env []string
    48  	// Files specifies the open files inherited by the new process. The
    49  	// first three entries correspond to standard input, standard output, and
    50  	// standard error. An implementation may support additional entries,
    51  	// depending on the underlying operating system. A nil entry corresponds
    52  	// to that file being closed when the process starts.
    53  	Files []*File
    54  
    55  	// Operating system-specific process creation attributes.
    56  	// Note that setting this field means that your program
    57  	// may not execute properly or even compile on some
    58  	// operating systems.
    59  	Sys *syscall.SysProcAttr
    60  }
    61  
    62  // A Signal represents an operating system signal.
    63  // The usual underlying implementation is operating system-dependent:
    64  // on Unix it is syscall.Signal.
    65  type Signal interface {
    66  	String() string
    67  	Signal() // to distinguish from other Stringers
    68  }
    69  
    70  // Getpid returns the process id of the caller.
    71  func Getpid() int { return syscall.Getpid() }
    72  
    73  // Getppid returns the process id of the caller's parent.
    74  func Getppid() int { return syscall.Getppid() }
    75  
    76  // FindProcess looks for a running process by its pid.
    77  //
    78  // The Process it returns can be used to obtain information
    79  // about the underlying operating system process.
    80  //
    81  // On Unix systems, FindProcess always succeeds and returns a Process
    82  // for the given pid, regardless of whether the process exists.
    83  func FindProcess(pid int) (*Process, error) {
    84  	return findProcess(pid)
    85  }
    86  
    87  // StartProcess starts a new process with the program, arguments and attributes
    88  // specified by name, argv and attr. The argv slice will become os.Args in the
    89  // new process, so it normally starts with the program name.
    90  //
    91  // StartProcess is a low-level interface. The os/exec package provides
    92  // higher-level interfaces.
    93  //
    94  // If there is an error, it will be of type *PathError.
    95  func StartProcess(name string, argv []string, attr *ProcAttr) (*Process, error) {
    96  	testlog.Open(name)
    97  	return startProcess(name, argv, attr)
    98  }
    99  
   100  // Release releases any resources associated with the Process p,
   101  // rendering it unusable in the future.
   102  // Release only needs to be called if Wait is not.
   103  func (p *Process) Release() error {
   104  	return p.release()
   105  }
   106  
   107  // Kill causes the Process to exit immediately.
   108  func (p *Process) Kill() error {
   109  	return p.kill()
   110  }
   111  
   112  // Wait waits for the Process to exit, and then returns a
   113  // ProcessState describing its status and an error, if any.
   114  // Wait releases any resources associated with the Process.
   115  // On most operating systems, the Process must be a child
   116  // of the current process or an error will be returned.
   117  func (p *Process) Wait() (*ProcessState, error) {
   118  	return p.wait()
   119  }
   120  
   121  // Signal sends a signal to the Process.
   122  // Sending Interrupt on Windows is not implemented.
   123  func (p *Process) Signal(sig Signal) error {
   124  	return p.signal(sig)
   125  }
   126  
   127  // UserTime returns the user CPU time of the exited process and its children.
   128  func (p *ProcessState) UserTime() time.Duration {
   129  	return p.userTime()
   130  }
   131  
   132  // SystemTime returns the system CPU time of the exited process and its children.
   133  func (p *ProcessState) SystemTime() time.Duration {
   134  	return p.systemTime()
   135  }
   136  
   137  // Exited reports whether the program has exited.
   138  func (p *ProcessState) Exited() bool {
   139  	return p.exited()
   140  }
   141  
   142  // Success reports whether the program exited successfully,
   143  // such as with exit status 0 on Unix.
   144  func (p *ProcessState) Success() bool {
   145  	return p.success()
   146  }
   147  
   148  // Sys returns system-dependent exit information about
   149  // the process. Convert it to the appropriate underlying
   150  // type, such as syscall.WaitStatus on Unix, to access its contents.
   151  func (p *ProcessState) Sys() interface{} {
   152  	return p.sys()
   153  }
   154  
   155  // SysUsage returns system-dependent resource usage information about
   156  // the exited process. Convert it to the appropriate underlying
   157  // type, such as *syscall.Rusage on Unix, to access its contents.
   158  // (On Unix, *syscall.Rusage matches struct rusage as defined in the
   159  // getrusage(2) manual page.)
   160  func (p *ProcessState) SysUsage() interface{} {
   161  	return p.sysUsage()
   162  }