2011-03-17 00:05:44 +01: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 03:17:34 +01:00
|
|
|
"errors"
|
2011-03-17 00:05:44 +01:00
|
|
|
"runtime"
|
2015-10-31 01:59:47 +01:00
|
|
|
"sync/atomic"
|
2011-03-17 00:05:44 +01:00
|
|
|
"syscall"
|
2012-03-02 21:01:37 +01:00
|
|
|
"time"
|
2011-12-13 00:40:51 +01:00
|
|
|
"unsafe"
|
2011-03-17 00:05:44 +01:00
|
|
|
)
|
|
|
|
|
2012-03-06 18:57:23 +01:00
|
|
|
func (p *Process) wait() (ps *ProcessState, err error) {
|
2015-10-31 01:59:47 +01:00
|
|
|
handle := atomic.LoadUintptr(&p.handle)
|
|
|
|
s, e := syscall.WaitForSingleObject(syscall.Handle(handle), syscall.INFINITE)
|
2011-03-17 00:05:44 +01:00
|
|
|
switch s {
|
|
|
|
case syscall.WAIT_OBJECT_0:
|
|
|
|
break
|
|
|
|
case syscall.WAIT_FAILED:
|
|
|
|
return nil, NewSyscallError("WaitForSingleObject", e)
|
|
|
|
default:
|
2011-12-03 03:17:34 +01:00
|
|
|
return nil, errors.New("os: unexpected result from WaitForSingleObject")
|
2011-03-17 00:05:44 +01:00
|
|
|
}
|
|
|
|
var ec uint32
|
2015-10-31 01:59:47 +01:00
|
|
|
e = syscall.GetExitCodeProcess(syscall.Handle(handle), &ec)
|
2011-12-13 00:40:51 +01:00
|
|
|
if e != nil {
|
2011-03-17 00:05:44 +01:00
|
|
|
return nil, NewSyscallError("GetExitCodeProcess", e)
|
|
|
|
}
|
2012-03-06 18:57:23 +01:00
|
|
|
var u syscall.Rusage
|
2015-10-31 01:59:47 +01:00
|
|
|
e = syscall.GetProcessTimes(syscall.Handle(handle), &u.CreationTime, &u.ExitTime, &u.KernelTime, &u.UserTime)
|
2012-03-06 18:57:23 +01:00
|
|
|
if e != nil {
|
|
|
|
return nil, NewSyscallError("GetProcessTimes", e)
|
|
|
|
}
|
2012-10-03 07:27:36 +02:00
|
|
|
p.setDone()
|
2012-03-06 18:57:23 +01:00
|
|
|
// NOTE(brainman): It seems that sometimes process is not dead
|
|
|
|
// when WaitForSingleObject returns. But we do not know any
|
|
|
|
// other way to wait for it. Sleeping for a while seems to do
|
|
|
|
// the trick sometimes. So we will sleep and smell the roses.
|
|
|
|
defer time.Sleep(5 * time.Millisecond)
|
|
|
|
defer p.Release()
|
|
|
|
return &ProcessState{p.Pid, syscall.WaitStatus{ExitCode: ec}, &u}, nil
|
2011-03-17 00:05:44 +01:00
|
|
|
}
|
|
|
|
|
2013-11-06 20:49:01 +01:00
|
|
|
func terminateProcess(pid, exitcode int) error {
|
|
|
|
h, e := syscall.OpenProcess(syscall.PROCESS_TERMINATE, false, uint32(pid))
|
|
|
|
if e != nil {
|
|
|
|
return NewSyscallError("OpenProcess", e)
|
|
|
|
}
|
|
|
|
defer syscall.CloseHandle(h)
|
|
|
|
e = syscall.TerminateProcess(h, uint32(exitcode))
|
|
|
|
return NewSyscallError("TerminateProcess", e)
|
|
|
|
}
|
|
|
|
|
2012-03-06 18:57:23 +01:00
|
|
|
func (p *Process) signal(sig Signal) error {
|
2015-10-31 01:59:47 +01:00
|
|
|
handle := atomic.LoadUintptr(&p.handle)
|
|
|
|
if handle == uintptr(syscall.InvalidHandle) {
|
2015-01-15 01:27:56 +01:00
|
|
|
return syscall.EINVAL
|
|
|
|
}
|
2012-10-03 07:27:36 +02:00
|
|
|
if p.done() {
|
2011-12-03 03:17:34 +01:00
|
|
|
return errors.New("os: process already finished")
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
2012-03-02 17:38:43 +01:00
|
|
|
if sig == Kill {
|
2017-01-14 01:05:42 +01:00
|
|
|
err := terminateProcess(p.Pid, 1)
|
|
|
|
runtime.KeepAlive(p)
|
|
|
|
return err
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
2012-03-02 17:38:43 +01:00
|
|
|
// TODO(rsc): Handle Interrupt too?
|
2011-12-13 00:40:51 +01:00
|
|
|
return syscall.Errno(syscall.EWINDOWS)
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
|
2012-03-06 18:57:23 +01:00
|
|
|
func (p *Process) release() error {
|
2015-10-31 01:59:47 +01:00
|
|
|
handle := atomic.LoadUintptr(&p.handle)
|
|
|
|
if handle == uintptr(syscall.InvalidHandle) {
|
2012-03-02 21:01:37 +01:00
|
|
|
return syscall.EINVAL
|
2011-03-17 00:05:44 +01:00
|
|
|
}
|
2015-10-31 01:59:47 +01:00
|
|
|
e := syscall.CloseHandle(syscall.Handle(handle))
|
2011-12-13 00:40:51 +01:00
|
|
|
if e != nil {
|
2011-03-17 00:05:44 +01:00
|
|
|
return NewSyscallError("CloseHandle", e)
|
|
|
|
}
|
2015-10-31 01:59:47 +01:00
|
|
|
atomic.StoreUintptr(&p.handle, uintptr(syscall.InvalidHandle))
|
2011-03-17 00:05:44 +01:00
|
|
|
// no need for a finalizer anymore
|
|
|
|
runtime.SetFinalizer(p, nil)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2012-01-25 22:54:22 +01:00
|
|
|
func findProcess(pid int) (p *Process, err error) {
|
2011-03-17 00:05:44 +01:00
|
|
|
const da = syscall.STANDARD_RIGHTS_READ |
|
|
|
|
syscall.PROCESS_QUERY_INFORMATION | syscall.SYNCHRONIZE
|
|
|
|
h, e := syscall.OpenProcess(da, false, uint32(pid))
|
2011-12-13 00:40:51 +01:00
|
|
|
if e != nil {
|
2011-03-17 00:05:44 +01:00
|
|
|
return nil, NewSyscallError("OpenProcess", e)
|
|
|
|
}
|
2012-02-09 09:19:58 +01:00
|
|
|
return newProcess(pid, uintptr(h)), nil
|
2011-03-17 00:05:44 +01:00
|
|
|
}
|
2011-12-13 00:40:51 +01:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
var argc int32
|
|
|
|
cmd := syscall.GetCommandLine()
|
|
|
|
argv, e := syscall.CommandLineToArgv(cmd, &argc)
|
|
|
|
if e != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer syscall.LocalFree(syscall.Handle(uintptr(unsafe.Pointer(argv))))
|
|
|
|
Args = make([]string, argc)
|
|
|
|
for i, v := range (*argv)[:argc] {
|
2016-07-22 20:15:38 +02:00
|
|
|
Args[i] = syscall.UTF16ToString((*v)[:])
|
2011-12-13 00:40:51 +01:00
|
|
|
}
|
|
|
|
}
|
2012-03-02 21:01:37 +01:00
|
|
|
|
2012-03-06 18:57:23 +01:00
|
|
|
func ftToDuration(ft *syscall.Filetime) time.Duration {
|
|
|
|
n := int64(ft.HighDateTime)<<32 + int64(ft.LowDateTime) // in 100-nanosecond intervals
|
|
|
|
return time.Duration(n*100) * time.Nanosecond
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *ProcessState) userTime() time.Duration {
|
|
|
|
return ftToDuration(&p.rusage.UserTime)
|
2012-03-02 21:01:37 +01:00
|
|
|
}
|
|
|
|
|
2012-03-06 18:57:23 +01:00
|
|
|
func (p *ProcessState) systemTime() time.Duration {
|
|
|
|
return ftToDuration(&p.rusage.KernelTime)
|
2012-03-02 21:01:37 +01:00
|
|
|
}
|