Source file
src/os/exec_posix.go
1
2
3
4
5
6
7 package os
8
9 import (
10 "internal/itoa"
11 "internal/syscall/execenv"
12 "runtime"
13 "syscall"
14 )
15
16
17
18
19
20
21 var (
22 Interrupt Signal = syscall.SIGINT
23 Kill Signal = syscall.SIGKILL
24 )
25
26 func startProcess(name string, argv []string, attr *ProcAttr) (p *Process, err error) {
27
28
29
30 if attr != nil && attr.Sys == nil && attr.Dir != "" {
31 if _, err := Stat(attr.Dir); err != nil {
32 pe := err.(*PathError)
33 pe.Op = "chdir"
34 return nil, pe
35 }
36 }
37
38 sysattr := &syscall.ProcAttr{
39 Dir: attr.Dir,
40 Env: attr.Env,
41 Sys: attr.Sys,
42 }
43 if sysattr.Env == nil {
44 sysattr.Env, err = execenv.Default(sysattr.Sys)
45 if err != nil {
46 return nil, err
47 }
48 }
49 sysattr.Files = make([]uintptr, 0, len(attr.Files))
50 for _, f := range attr.Files {
51 sysattr.Files = append(sysattr.Files, f.Fd())
52 }
53
54 pid, h, e := syscall.StartProcess(name, argv, sysattr)
55
56
57 runtime.KeepAlive(attr)
58
59 if e != nil {
60 return nil, &PathError{Op: "fork/exec", Path: name, Err: e}
61 }
62
63 return newProcess(pid, h), nil
64 }
65
66 func (p *Process) kill() error {
67 return p.Signal(Kill)
68 }
69
70
71 type ProcessState struct {
72 pid int
73 status syscall.WaitStatus
74 rusage *syscall.Rusage
75 }
76
77
78 func (p *ProcessState) Pid() int {
79 return p.pid
80 }
81
82 func (p *ProcessState) exited() bool {
83 return p.status.Exited()
84 }
85
86 func (p *ProcessState) success() bool {
87 return p.status.ExitStatus() == 0
88 }
89
90 func (p *ProcessState) sys() any {
91 return p.status
92 }
93
94 func (p *ProcessState) sysUsage() any {
95 return p.rusage
96 }
97
98 func (p *ProcessState) String() string {
99 if p == nil {
100 return "<nil>"
101 }
102 status := p.Sys().(syscall.WaitStatus)
103 res := ""
104 switch {
105 case status.Exited():
106 code := status.ExitStatus()
107 if runtime.GOOS == "windows" && uint(code) >= 1<<16 {
108 res = "exit status " + uitox(uint(code))
109 } else {
110 res = "exit status " + itoa.Itoa(code)
111 }
112 case status.Signaled():
113 res = "signal: " + status.Signal().String()
114 case status.Stopped():
115 res = "stop signal: " + status.StopSignal().String()
116 if status.StopSignal() == syscall.SIGTRAP && status.TrapCause() != 0 {
117 res += " (trap " + itoa.Itoa(status.TrapCause()) + ")"
118 }
119 case status.Continued():
120 res = "continued"
121 }
122 if status.CoreDump() {
123 res += " (core dumped)"
124 }
125 return res
126 }
127
128
129
130 func (p *ProcessState) ExitCode() int {
131
132 if p == nil {
133 return -1
134 }
135 return p.status.ExitStatus()
136 }
137
View as plain text