summaryrefslogtreecommitdiff
path: root/libgo/go/os/exec_windows.go
blob: 65e94ac4acc8c4dfcd7da6ca07aec9543ddc570f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// 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 (
	"runtime"
	"syscall"
)

func (p *Process) Wait(options int) (w *Waitmsg, err Error) {
	s, e := syscall.WaitForSingleObject(syscall.Handle(p.handle), syscall.INFINITE)
	switch s {
	case syscall.WAIT_OBJECT_0:
		break
	case syscall.WAIT_FAILED:
		return nil, NewSyscallError("WaitForSingleObject", e)
	default:
		return nil, NewError("os: unexpected result from WaitForSingleObject")
	}
	var ec uint32
	e = syscall.GetExitCodeProcess(syscall.Handle(p.handle), &ec)
	if e != 0 {
		return nil, NewSyscallError("GetExitCodeProcess", e)
	}
	p.done = true
	return &Waitmsg{p.Pid, syscall.WaitStatus{s, ec}, new(syscall.Rusage)}, nil
}

// Signal sends a signal to the Process.
func (p *Process) Signal(sig Signal) Error {
	if p.done {
		return NewError("os: process already finished")
	}
	switch sig.(UnixSignal) {
	case SIGKILL:
		e := syscall.TerminateProcess(syscall.Handle(p.handle), 1)
		return NewSyscallError("TerminateProcess", e)
	}
	return Errno(syscall.EWINDOWS)
}

func (p *Process) Release() Error {
	if p.handle == -1 {
		return EINVAL
	}
	e := syscall.CloseHandle(syscall.Handle(p.handle))
	if e != 0 {
		return NewSyscallError("CloseHandle", e)
	}
	p.handle = -1
	// no need for a finalizer anymore
	runtime.SetFinalizer(p, nil)
	return nil
}

func FindProcess(pid int) (p *Process, err Error) {
	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
}