Source file
src/os/exec_windows.go
1
2
3
4
5 package os
6
7 import (
8 "errors"
9 "internal/syscall/windows"
10 "runtime"
11 "syscall"
12 "time"
13 )
14
15 func (p *Process) wait() (ps *ProcessState, err error) {
16 handle := p.handle.Load()
17 s, e := syscall.WaitForSingleObject(syscall.Handle(handle), syscall.INFINITE)
18 switch s {
19 case syscall.WAIT_OBJECT_0:
20 break
21 case syscall.WAIT_FAILED:
22 return nil, NewSyscallError("WaitForSingleObject", e)
23 default:
24 return nil, errors.New("os: unexpected result from WaitForSingleObject")
25 }
26 var ec uint32
27 e = syscall.GetExitCodeProcess(syscall.Handle(handle), &ec)
28 if e != nil {
29 return nil, NewSyscallError("GetExitCodeProcess", e)
30 }
31 var u syscall.Rusage
32 e = syscall.GetProcessTimes(syscall.Handle(handle), &u.CreationTime, &u.ExitTime, &u.KernelTime, &u.UserTime)
33 if e != nil {
34 return nil, NewSyscallError("GetProcessTimes", e)
35 }
36 p.setDone()
37 defer p.Release()
38 return &ProcessState{p.Pid, syscall.WaitStatus{ExitCode: ec}, &u}, nil
39 }
40
41 func (p *Process) signal(sig Signal) error {
42 handle := p.handle.Load()
43 if handle == uintptr(syscall.InvalidHandle) {
44 return syscall.EINVAL
45 }
46 if p.done() {
47 return ErrProcessDone
48 }
49 if sig == Kill {
50 var terminationHandle syscall.Handle
51 e := syscall.DuplicateHandle(^syscall.Handle(0), syscall.Handle(handle), ^syscall.Handle(0), &terminationHandle, syscall.PROCESS_TERMINATE, false, 0)
52 if e != nil {
53 return NewSyscallError("DuplicateHandle", e)
54 }
55 runtime.KeepAlive(p)
56 defer syscall.CloseHandle(terminationHandle)
57 e = syscall.TerminateProcess(syscall.Handle(terminationHandle), 1)
58 return NewSyscallError("TerminateProcess", e)
59 }
60
61 return syscall.Errno(syscall.EWINDOWS)
62 }
63
64 func (p *Process) release() error {
65 handle := p.handle.Swap(uintptr(syscall.InvalidHandle))
66 if handle == uintptr(syscall.InvalidHandle) {
67 return syscall.EINVAL
68 }
69 e := syscall.CloseHandle(syscall.Handle(handle))
70 if e != nil {
71 return NewSyscallError("CloseHandle", e)
72 }
73
74 runtime.SetFinalizer(p, nil)
75 return nil
76 }
77
78 func findProcess(pid int) (p *Process, err error) {
79 const da = syscall.STANDARD_RIGHTS_READ |
80 syscall.PROCESS_QUERY_INFORMATION | syscall.SYNCHRONIZE
81 h, e := syscall.OpenProcess(da, false, uint32(pid))
82 if e != nil {
83 return nil, NewSyscallError("OpenProcess", e)
84 }
85 return newProcess(pid, uintptr(h)), nil
86 }
87
88 func init() {
89 cmd := windows.UTF16PtrToString(syscall.GetCommandLine())
90 if len(cmd) == 0 {
91 arg0, _ := Executable()
92 Args = []string{arg0}
93 } else {
94 Args = commandLineToArgv(cmd)
95 }
96 }
97
98
99 func appendBSBytes(b []byte, n int) []byte {
100 for ; n > 0; n-- {
101 b = append(b, '\\')
102 }
103 return b
104 }
105
106
107
108 func readNextArg(cmd string) (arg []byte, rest string) {
109 var b []byte
110 var inquote bool
111 var nslash int
112 for ; len(cmd) > 0; cmd = cmd[1:] {
113 c := cmd[0]
114 switch c {
115 case ' ', '\t':
116 if !inquote {
117 return appendBSBytes(b, nslash), cmd[1:]
118 }
119 case '"':
120 b = appendBSBytes(b, nslash/2)
121 if nslash%2 == 0 {
122
123
124
125 if inquote && len(cmd) > 1 && cmd[1] == '"' {
126 b = append(b, c)
127 cmd = cmd[1:]
128 }
129 inquote = !inquote
130 } else {
131 b = append(b, c)
132 }
133 nslash = 0
134 continue
135 case '\\':
136 nslash++
137 continue
138 }
139 b = appendBSBytes(b, nslash)
140 nslash = 0
141 b = append(b, c)
142 }
143 return appendBSBytes(b, nslash), ""
144 }
145
146
147
148
149 func commandLineToArgv(cmd string) []string {
150 var args []string
151 for len(cmd) > 0 {
152 if cmd[0] == ' ' || cmd[0] == '\t' {
153 cmd = cmd[1:]
154 continue
155 }
156 var arg []byte
157 arg, cmd = readNextArg(cmd)
158 args = append(args, string(arg))
159 }
160 return args
161 }
162
163 func ftToDuration(ft *syscall.Filetime) time.Duration {
164 n := int64(ft.HighDateTime)<<32 + int64(ft.LowDateTime)
165 return time.Duration(n*100) * time.Nanosecond
166 }
167
168 func (p *ProcessState) userTime() time.Duration {
169 return ftToDuration(&p.rusage.UserTime)
170 }
171
172 func (p *ProcessState) systemTime() time.Duration {
173 return ftToDuration(&p.rusage.KernelTime)
174 }
175
View as plain text