-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathjobobj.go
58 lines (48 loc) · 1.52 KB
/
jobobj.go
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
// Copyright 2013 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 ps
import (
"syscall"
"github.com/alexbrainman/ps/winapi"
)
// JobObject
type JobObject struct {
Handle syscall.Handle
}
// CreateJobObject creates new job object named name.
func CreateJobObject(name string) (*JobObject, error) {
h, err := winapi.CreateJobObject(nil, &(syscall.StringToUTF16(name))[0])
if err != nil {
return nil, err
}
return &JobObject{h}, nil
}
// OpenJobObject opens existing job object named name.
func OpenJobObject(name string, access uint32) (*JobObject, error) {
h, err := winapi.OpenJobObject(access, false, &(syscall.StringToUTF16(name))[0])
if err != nil {
return nil, err
}
return &JobObject{h}, nil
}
// Close closes job object handle. The job is destroyed when its last
// handle has been closed and all associated processes have exited.
func (jo *JobObject) Close() error {
return syscall.CloseHandle(jo.Handle)
}
// AddProcess adds process idntified by process handle p to job object jo.
func (jo *JobObject) AddProcess(p syscall.Handle) error {
return winapi.AssignProcessToJobObject(jo.Handle, p)
}
// AddCurrentProcess adds current process to job object jo.
func (jo *JobObject) AddCurrentProcess() error {
p, err := syscall.GetCurrentProcess()
if err != nil {
return err
}
return jo.AddProcess(p)
}
func (jo *JobObject) Terminate(exitcode uint32) error {
return winapi.TerminateJobObject(jo.Handle, exitcode)
}