| 1 |
// Copyright 2009 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 |
|
| 5 |
package os |
| 6 |
|
| 7 |
import ( |
| 8 |
"errors" |
| 9 |
"internal/syscall/windows" |
| 10 |
"runtime" |
| 11 |
"syscall" |
| 12 |
"time" |
| 13 |
) |
| 14 |
|
| 15 |
// Note that Process.handle is never nil because Windows always requires |
| 16 |
// a handle. A manually-created Process literal is not valid. |
| 17 |
|
| 18 |
func (p *Process) wait() (ps *ProcessState, err error) { |
| 19 |
handle, status := p.handleTransientAcquire() |
| 20 |
switch status { |
| 21 |
case statusDone: |
| 22 |
return nil, ErrProcessDone |
| 23 |
case statusReleased: |
| 24 |
return nil, syscall.EINVAL |
| 25 |
} |
| 26 |
defer p.handleTransientRelease() |
| 27 |
|
| 28 |
s, e := syscall.WaitForSingleObject(syscall.Handle(handle), syscall.INFINITE) |
| 29 |
switch s { |
| 30 |
case syscall.WAIT_OBJECT_0: |
| 31 |
break |
| 32 |
case syscall.WAIT_FAILED: |
| 33 |
return nil, NewSyscallError("WaitForSingleObject", e) |
| 34 |
default: |
| 35 |
return nil, errors.New("os: unexpected result from WaitForSingleObject") |
| 36 |
} |
| 37 |
var ec uint32 |
| 38 |
e = syscall.GetExitCodeProcess(syscall.Handle(handle), &ec) |
| 39 |
if e != nil { |
| 40 |
return nil, NewSyscallError("GetExitCodeProcess", e) |
| 41 |
} |
| 42 |
var u syscall.Rusage |
| 43 |
e = syscall.GetProcessTimes(syscall.Handle(handle), &u.CreationTime, &u.ExitTime, &u.KernelTime, &u.UserTime) |
| 44 |
if e != nil { |
| 45 |
return nil, NewSyscallError("GetProcessTimes", e) |
| 46 |
} |
| 47 |
|
| 48 |
// For compatibility we use statusReleased here rather |
| 49 |
// than statusDone. |
| 50 |
p.doRelease(statusReleased) |
| 51 |
|
| 52 |
return &ProcessState{p.Pid, syscall.WaitStatus{ExitCode: ec}, &u}, nil |
| 53 |
} |
| 54 |
|
| 55 |
func (p *Process) signal(sig Signal) error { |
| 56 |
handle, status := p.handleTransientAcquire() |
| 57 |
switch status { |
| 58 |
case statusDone: |
| 59 |
return ErrProcessDone |
| 60 |
case statusReleased: |
| 61 |
return syscall.EINVAL |
| 62 |
} |
| 63 |
defer p.handleTransientRelease() |
| 64 |
|
| 65 |
if sig == Kill { |
| 66 |
var terminationHandle syscall.Handle |
| 67 |
e := syscall.DuplicateHandle(^syscall.Handle(0), syscall.Handle(handle), ^syscall.Handle(0), &terminationHandle, syscall.PROCESS_TERMINATE, false, 0) |
| 68 |
if e != nil { |
| 69 |
return NewSyscallError("DuplicateHandle", e) |
| 70 |
} |
| 71 |
runtime.KeepAlive(p) |
| 72 |
defer syscall.CloseHandle(terminationHandle) |
| 73 |
e = syscall.TerminateProcess(syscall.Handle(terminationHandle), 1) |
| 74 |
return NewSyscallError("TerminateProcess", e) |
| 75 |
} |
| 76 |
// TODO(rsc): Handle Interrupt too? |
| 77 |
return syscall.Errno(syscall.EWINDOWS) |
| 78 |
} |
| 79 |
|
| 80 |
func (ph *processHandle) closeHandle() { |
| 81 |
syscall.CloseHandle(syscall.Handle(ph.handle)) |
| 82 |
} |
| 83 |
|
| 84 |
func findProcess(pid int) (p *Process, err error) { |
| 85 |
const da = syscall.STANDARD_RIGHTS_READ | |
| 86 |
syscall.PROCESS_QUERY_INFORMATION | syscall.SYNCHRONIZE |
| 87 |
h, e := syscall.OpenProcess(da, false, uint32(pid)) |
| 88 |
if e != nil { |
| 89 |
return nil, NewSyscallError("OpenProcess", e) |
| 90 |
} |
| 91 |
return newHandleProcess(pid, uintptr(h)), nil |
| 92 |
} |
| 93 |
|
| 94 |
func init() { |
| 95 |
cmd := windows.UTF16PtrToString(syscall.GetCommandLine()) |
| 96 |
if len(cmd) == 0 { |
| 97 |
arg0, _ := Executable() |
| 98 |
Args = []string{arg0} |
| 99 |
} else { |
| 100 |
Args = commandLineToArgv(cmd) |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
// appendBSBytes appends n '\\' bytes to b and returns the resulting slice. |
| 105 |
func appendBSBytes(b []byte, n int) []byte { |
| 106 |
for ; n > 0; n-- { |
| 107 |
b = append(b, '\\') |
| 108 |
} |
| 109 |
return b |
| 110 |
} |
| 111 |
|
| 112 |
// readNextArg splits command line string cmd into next |
| 113 |
// argument and command line remainder. |
| 114 |
func readNextArg(cmd string) (arg []byte, rest string) { |
| 115 |
var b []byte |
| 116 |
var inquote bool |
| 117 |
var nslash int |
| 118 |
for ; len(cmd) > 0; cmd = cmd[1:] { |
| 119 |
c := cmd[0] |
| 120 |
switch c { |
| 121 |
case ' ', '\t': |
| 122 |
if !inquote { |
| 123 |
return appendBSBytes(b, nslash), cmd[1:] |
| 124 |
} |
| 125 |
case '"': |
| 126 |
b = appendBSBytes(b, nslash/2) |
| 127 |
if nslash%2 == 0 { |
| 128 |
// use "Prior to 2008" rule from |
| 129 |
// http://daviddeley.com/autohotkey/parameters/parameters.htm |
| 130 |
// section 5.2 to deal with double double quotes |
| 131 |
if inquote && len(cmd) > 1 && cmd[1] == '"' { |
| 132 |
b = append(b, c) |
| 133 |
cmd = cmd[1:] |
| 134 |
} |
| 135 |
inquote = !inquote |
| 136 |
} else { |
| 137 |
b = append(b, c) |
| 138 |
} |
| 139 |
nslash = 0 |
| 140 |
continue |
| 141 |
case '\\': |
| 142 |
nslash++ |
| 143 |
continue |
| 144 |
} |
| 145 |
b = appendBSBytes(b, nslash) |
| 146 |
nslash = 0 |
| 147 |
b = append(b, c) |
| 148 |
} |
| 149 |
return appendBSBytes(b, nslash), "" |
| 150 |
} |
| 151 |
|
| 152 |
// commandLineToArgv splits a command line into individual argument |
| 153 |
// strings, following the Windows conventions documented |
| 154 |
// at http://daviddeley.com/autohotkey/parameters/parameters.htm#WINARGV |
| 155 |
func commandLineToArgv(cmd string) []string { |
| 156 |
var args []string |
| 157 |
for len(cmd) > 0 { |
| 158 |
if cmd[0] == ' ' || cmd[0] == '\t' { |
| 159 |
cmd = cmd[1:] |
| 160 |
continue |
| 161 |
} |
| 162 |
var arg []byte |
| 163 |
arg, cmd = readNextArg(cmd) |
| 164 |
args = append(args, string(arg)) |
| 165 |
} |
| 166 |
return args |
| 167 |
} |
| 168 |
|
| 169 |
func ftToDuration(ft *syscall.Filetime) time.Duration { |
| 170 |
n := int64(ft.HighDateTime)<<32 + int64(ft.LowDateTime) // in 100-nanosecond intervals |
| 171 |
return time.Duration(n*100) * time.Nanosecond |
| 172 |
} |
| 173 |
|
| 174 |
func (p *ProcessState) userTime() time.Duration { |
| 175 |
return ftToDuration(&p.rusage.UserTime) |
| 176 |
} |
| 177 |
|
| 178 |
func (p *ProcessState) systemTime() time.Duration { |
| 179 |
return ftToDuration(&p.rusage.KernelTime) |
| 180 |
} |