2011-03-16 23:05:44 +00:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package os
|
|
|
|
|
|
|
|
import (
|
2011-12-03 02:17:34 +00:00
|
|
|
"errors"
|
2011-03-16 23:05:44 +00:00
|
|
|
"runtime"
|
|
|
|
"syscall"
|
|
|
|
)
|
|
|
|
|
2011-12-03 02:17:34 +00:00
|
|
|
func (p *Process) Wait(options int) (w *Waitmsg, err error) {
|
2011-09-16 15:47:21 +00:00
|
|
|
s, e := syscall.WaitForSingleObject(syscall.Handle(p.handle), syscall.INFINITE)
|
2011-03-16 23:05:44 +00:00
|
|
|
switch s {
|
|
|
|
case syscall.WAIT_OBJECT_0:
|
|
|
|
break
|
|
|
|
case syscall.WAIT_FAILED:
|
|
|
|
return nil, NewSyscallError("WaitForSingleObject", e)
|
|
|
|
default:
|
2011-12-03 02:17:34 +00:00
|
|
|
return nil, errors.New("os: unexpected result from WaitForSingleObject")
|
2011-03-16 23:05:44 +00:00
|
|
|
}
|
|
|
|
var ec uint32
|
2011-09-16 15:47:21 +00:00
|
|
|
e = syscall.GetExitCodeProcess(syscall.Handle(p.handle), &ec)
|
2011-03-16 23:05:44 +00:00
|
|
|
if e != 0 {
|
|
|
|
return nil, NewSyscallError("GetExitCodeProcess", e)
|
|
|
|
}
|
2011-09-16 15:47:21 +00:00
|
|
|
p.done = true
|
2011-03-16 23:05:44 +00:00
|
|
|
return &Waitmsg{p.Pid, syscall.WaitStatus{s, ec}, new(syscall.Rusage)}, nil
|
|
|
|
}
|
|
|
|
|
2011-09-16 15:47:21 +00:00
|
|
|
// Signal sends a signal to the Process.
|
2011-12-03 02:17:34 +00:00
|
|
|
func (p *Process) Signal(sig Signal) error {
|
2011-09-16 15:47:21 +00:00
|
|
|
if p.done {
|
2011-12-03 02:17:34 +00:00
|
|
|
return errors.New("os: process already finished")
|
2011-09-16 15:47:21 +00:00
|
|
|
}
|
|
|
|
switch sig.(UnixSignal) {
|
|
|
|
case SIGKILL:
|
|
|
|
e := syscall.TerminateProcess(syscall.Handle(p.handle), 1)
|
|
|
|
return NewSyscallError("TerminateProcess", e)
|
|
|
|
}
|
|
|
|
return Errno(syscall.EWINDOWS)
|
|
|
|
}
|
|
|
|
|
2011-12-03 02:17:34 +00:00
|
|
|
func (p *Process) Release() error {
|
2011-03-16 23:05:44 +00:00
|
|
|
if p.handle == -1 {
|
|
|
|
return EINVAL
|
|
|
|
}
|
2011-09-16 15:47:21 +00:00
|
|
|
e := syscall.CloseHandle(syscall.Handle(p.handle))
|
2011-03-16 23:05:44 +00:00
|
|
|
if e != 0 {
|
|
|
|
return NewSyscallError("CloseHandle", e)
|
|
|
|
}
|
|
|
|
p.handle = -1
|
|
|
|
// no need for a finalizer anymore
|
|
|
|
runtime.SetFinalizer(p, nil)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2011-12-03 02:17:34 +00:00
|
|
|
func FindProcess(pid int) (p *Process, err error) {
|
2011-03-16 23:05:44 +00:00
|
|
|
const da = syscall.STANDARD_RIGHTS_READ |
|
|
|
|
syscall.PROCESS_QUERY_INFORMATION | syscall.SYNCHRONIZE
|
|
|
|
h, e := syscall.OpenProcess(da, false, uint32(pid))
|
|
|
|
if e != 0 {
|
|
|
|
return nil, NewSyscallError("OpenProcess", e)
|
|
|
|
}
|
|
|
|
return newProcess(pid, int(h)), nil
|
|
|
|
}
|