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 func (p *Process) Wait(options int) (w *Waitmsg, err Error) { | 12 func (p *Process) Wait(options int) (w *Waitmsg, err Error) { |
10 s, e := syscall.WaitForSingleObject(int32(p.handle), syscall.INFINITE) | 13 s, e := syscall.WaitForSingleObject(int32(p.handle), syscall.INFINITE) |
11 switch s { | 14 switch s { |
12 case syscall.WAIT_OBJECT_0: | 15 case syscall.WAIT_OBJECT_0: |
13 break | 16 break |
14 case syscall.WAIT_FAILED: | 17 case syscall.WAIT_FAILED: |
15 return nil, NewSyscallError("WaitForSingleObject", e) | 18 return nil, NewSyscallError("WaitForSingleObject", e) |
16 default: | 19 default: |
17 return nil, ErrorString("os: unexpected result from WaitForSingl
eObject") | 20 return nil, ErrorString("os: unexpected result from WaitForSingl
eObject") |
18 } | 21 } |
19 var ec uint32 | 22 var ec uint32 |
20 if ok, e := syscall.GetExitCodeProcess(uint32(p.handle), &ec); !ok { | 23 if ok, e := syscall.GetExitCodeProcess(uint32(p.handle), &ec); !ok { |
21 return nil, NewSyscallError("GetExitCodeProcess", e) | 24 return nil, NewSyscallError("GetExitCodeProcess", e) |
22 } | 25 } |
23 return &Waitmsg{p.Pid, syscall.WaitStatus{s, ec}, new(syscall.Rusage)},
nil | 26 return &Waitmsg{p.Pid, syscall.WaitStatus{s, ec}, new(syscall.Rusage)},
nil |
24 } | 27 } |
25 | 28 |
26 func (p *Process) Release() Error { | 29 func (p *Process) Release() Error { |
27 if p.handle == -1 { | 30 if p.handle == -1 { |
28 » » return ErrorString("os: Process is already released") | 31 » » return EINVAL |
29 } | 32 } |
30 if ok, e := syscall.CloseHandle(int32(p.handle)); !ok { | 33 if ok, e := syscall.CloseHandle(int32(p.handle)); !ok { |
31 return NewSyscallError("CloseHandle", e) | 34 return NewSyscallError("CloseHandle", e) |
32 } | 35 } |
33 p.handle = -1 | 36 p.handle = -1 |
| 37 // no need for a finalizer anymore |
| 38 runtime.SetFinalizer(p, nil) |
34 return nil | 39 return nil |
35 } | 40 } |
36 | 41 |
37 func FindProcess(pid int) (p *Process, err Error) { | 42 func FindProcess(pid int) (p *Process, err Error) { |
38 const da = syscall.STANDARD_RIGHTS_READ | | 43 const da = syscall.STANDARD_RIGHTS_READ | |
39 syscall.PROCESS_QUERY_INFORMATION | syscall.SYNCHRONIZE | 44 syscall.PROCESS_QUERY_INFORMATION | syscall.SYNCHRONIZE |
40 h, e := syscall.OpenProcess(da, false, uint32(pid)) | 45 h, e := syscall.OpenProcess(da, false, uint32(pid)) |
41 if e != 0 { | 46 if e != 0 { |
42 return nil, NewSyscallError("OpenProcess", e) | 47 return nil, NewSyscallError("OpenProcess", e) |
43 } | 48 } |
44 » return &Process{pid, int(h)}, nil | 49 » return newProcess(pid, int(h)), nil |
45 } | 50 } |
LEFT | RIGHT |