doc.go 4.6 KB

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