LEFT | RIGHT |
1 // Copyright 2009 The Go Authors. All rights reserved. | 1 // Copyright 2009 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 package os | 5 package os |
6 | 6 |
7 import "syscall" | 7 import ( |
| 8 » "runtime" |
| 9 » "syscall" |
| 10 ) |
8 | 11 |
9 // Options for Wait. | 12 // Options for Wait. |
10 const ( | 13 const ( |
11 WNOHANG = syscall.WNOHANG // Don't wait if no process has exited. | 14 WNOHANG = syscall.WNOHANG // Don't wait if no process has exited. |
12 WSTOPPED = syscall.WSTOPPED // If set, status of stopped subprocesses
is also reported. | 15 WSTOPPED = syscall.WSTOPPED // If set, status of stopped subprocesses
is also reported. |
13 WUNTRACED = syscall.WUNTRACED // Usually an alias for WSTOPPED. | 16 WUNTRACED = syscall.WUNTRACED // Usually an alias for WSTOPPED. |
14 WRUSAGE = 1 << 20 // Record resource usage. | 17 WRUSAGE = 1 << 20 // Record resource usage. |
15 ) | 18 ) |
16 | 19 |
17 // WRUSAGE must not be too high a bit, to avoid clashing with Linux's | 20 // WRUSAGE must not be too high a bit, to avoid clashing with Linux's |
18 // WCLONE, WALL, and WNOTHREAD flags, which sit in the top few bits of | 21 // WCLONE, WALL, and WNOTHREAD flags, which sit in the top few bits of |
19 // the options | 22 // the options |
20 | 23 |
21 // Wait waits for the Process to exit or stop, and then returns a | 24 // Wait waits for the Process to exit or stop, and then returns a |
22 // Waitmsg describing its status and an Error, if any. The options | 25 // Waitmsg describing its status and an Error, if any. The options |
23 // (WNOHANG etc.) affect the behavior of the Wait call. | 26 // (WNOHANG etc.) affect the behavior of the Wait call. |
24 func (p *Process) Wait(options int) (w *Waitmsg, err Error) { | 27 func (p *Process) Wait(options int) (w *Waitmsg, err Error) { |
| 28 if p.Pid == -1 { |
| 29 return nil, EINVAL |
| 30 } |
25 var status syscall.WaitStatus | 31 var status syscall.WaitStatus |
26 var rusage *syscall.Rusage | 32 var rusage *syscall.Rusage |
27 if options&WRUSAGE != 0 { | 33 if options&WRUSAGE != 0 { |
28 rusage = new(syscall.Rusage) | 34 rusage = new(syscall.Rusage) |
29 options ^= WRUSAGE | 35 options ^= WRUSAGE |
30 } | 36 } |
31 pid1, e := syscall.Wait4(p.Pid, &status, options, rusage) | 37 pid1, e := syscall.Wait4(p.Pid, &status, options, rusage) |
32 if e != 0 { | 38 if e != 0 { |
33 return nil, NewSyscallError("wait", e) | 39 return nil, NewSyscallError("wait", e) |
34 } | 40 } |
35 w = new(Waitmsg) | 41 w = new(Waitmsg) |
36 w.Pid = pid1 | 42 w.Pid = pid1 |
37 w.WaitStatus = status | 43 w.WaitStatus = status |
38 w.Rusage = rusage | 44 w.Rusage = rusage |
39 return w, nil | 45 return w, nil |
40 } | 46 } |
41 | 47 |
42 // Release resources associated with the Process, | 48 // Release releases any resources associated with the Process. |
43 // rendering it unusable. It returns an Error, if any. | |
44 func (p *Process) Release() Error { | 49 func (p *Process) Release() Error { |
45 // NOOP for unix. | 50 // NOOP for unix. |
| 51 p.Pid = -1 |
| 52 // no need for a finalizer anymore |
| 53 runtime.SetFinalizer(p, nil) |
46 return nil | 54 return nil |
47 } | 55 } |
48 | 56 |
49 // FindProcess looks for a running process by its pid. | 57 // FindProcess looks for a running process by its pid. |
50 // The Process it returns can be used to obtain information | 58 // The Process it returns can be used to obtain information |
51 // about the underlying operating system process. | 59 // about the underlying operating system process. |
52 // If the process cannot be found, an error is returned. | |
53 func FindProcess(pid int) (p *Process, err Error) { | 60 func FindProcess(pid int) (p *Process, err Error) { |
54 // NOOP for unix. | 61 // NOOP for unix. |
55 » return &Process{pid, 0}, nil | 62 » return newProcess(pid, 0), nil |
56 } | 63 } |
LEFT | RIGHT |