...
Run Format

Source file src/os/exec.go

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

View as plain text