Home | History | Annotate | Download | only in os
      1 // Copyright 2012 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 "time"
      8 
      9 // FindProcess looks for a running process by its pid.
     10 // The Process it returns can be used to obtain information
     11 // about the underlying operating system process.
     12 func FindProcess(pid int) (p *Process, err error) {
     13 	return findProcess(pid)
     14 }
     15 
     16 // StartProcess starts a new process with the program, arguments and attributes
     17 // specified by name, argv and attr.
     18 //
     19 // StartProcess is a low-level interface. The os/exec package provides
     20 // higher-level interfaces.
     21 //
     22 // If there is an error, it will be of type *PathError.
     23 func StartProcess(name string, argv []string, attr *ProcAttr) (*Process, error) {
     24 	return startProcess(name, argv, attr)
     25 }
     26 
     27 // Release releases any resources associated with the Process p,
     28 // rendering it unusable in the future.
     29 // Release only needs to be called if Wait is not.
     30 func (p *Process) Release() error {
     31 	return p.release()
     32 }
     33 
     34 // Kill causes the Process to exit immediately.
     35 func (p *Process) Kill() error {
     36 	return p.kill()
     37 }
     38 
     39 // Wait waits for the Process to exit, and then returns a
     40 // ProcessState describing its status and an error, if any.
     41 // Wait releases any resources associated with the Process.
     42 // On most operating systems, the Process must be a child
     43 // of the current process or an error will be returned.
     44 func (p *Process) Wait() (*ProcessState, error) {
     45 	return p.wait()
     46 }
     47 
     48 // Signal sends a signal to the Process.
     49 // Sending Interrupt on Windows is not implemented.
     50 func (p *Process) Signal(sig Signal) error {
     51 	return p.signal(sig)
     52 }
     53 
     54 // UserTime returns the user CPU time of the exited process and its children.
     55 func (p *ProcessState) UserTime() time.Duration {
     56 	return p.userTime()
     57 }
     58 
     59 // SystemTime returns the system CPU time of the exited process and its children.
     60 func (p *ProcessState) SystemTime() time.Duration {
     61 	return p.systemTime()
     62 }
     63 
     64 // Exited reports whether the program has exited.
     65 func (p *ProcessState) Exited() bool {
     66 	return p.exited()
     67 }
     68 
     69 // Success reports whether the program exited successfully,
     70 // such as with exit status 0 on Unix.
     71 func (p *ProcessState) Success() bool {
     72 	return p.success()
     73 }
     74 
     75 // Sys returns system-dependent exit information about
     76 // the process.  Convert it to the appropriate underlying
     77 // type, such as syscall.WaitStatus on Unix, to access its contents.
     78 func (p *ProcessState) Sys() interface{} {
     79 	return p.sys()
     80 }
     81 
     82 // SysUsage returns system-dependent resource usage information about
     83 // the exited process.  Convert it to the appropriate underlying
     84 // type, such as *syscall.Rusage on Unix, to access its contents.
     85 // (On Unix, *syscall.Rusage matches struct rusage as defined in the
     86 // getrusage(2) manual page.)
     87 func (p *ProcessState) SysUsage() interface{} {
     88 	return p.sysUsage()
     89 }
     90 
     91 // Hostname returns the host name reported by the kernel.
     92 func Hostname() (name string, err error) {
     93 	return hostname()
     94 }
     95 
     96 // Readdir reads the contents of the directory associated with file and
     97 // returns a slice of up to n FileInfo values, as would be returned
     98 // by Lstat, in directory order. Subsequent calls on the same file will yield
     99 // further FileInfos.
    100 //
    101 // If n > 0, Readdir returns at most n FileInfo structures. In this case, if
    102 // Readdir returns an empty slice, it will return a non-nil error
    103 // explaining why. At the end of a directory, the error is io.EOF.
    104 //
    105 // If n <= 0, Readdir returns all the FileInfo from the directory in
    106 // a single slice. In this case, if Readdir succeeds (reads all
    107 // the way to the end of the directory), it returns the slice and a
    108 // nil error. If it encounters an error before the end of the
    109 // directory, Readdir returns the FileInfo read until that point
    110 // and a non-nil error.
    111 func (f *File) Readdir(n int) (fi []FileInfo, err error) {
    112 	if f == nil {
    113 		return nil, ErrInvalid
    114 	}
    115 	return f.readdir(n)
    116 }
    117 
    118 // Readdirnames reads and returns a slice of names from the directory f.
    119 //
    120 // If n > 0, Readdirnames returns at most n names. In this case, if
    121 // Readdirnames returns an empty slice, it will return a non-nil error
    122 // explaining why. At the end of a directory, the error is io.EOF.
    123 //
    124 // If n <= 0, Readdirnames returns all the names from the directory in
    125 // a single slice. In this case, if Readdirnames succeeds (reads all
    126 // the way to the end of the directory), it returns the slice and a
    127 // nil error. If it encounters an error before the end of the
    128 // directory, Readdirnames returns the names read until that point and
    129 // a non-nil error.
    130 func (f *File) Readdirnames(n int) (names []string, err error) {
    131 	if f == nil {
    132 		return nil, ErrInvalid
    133 	}
    134 	return f.readdirnames(n)
    135 }
    136