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 attrSys, shouldDupPidfd := ensurePidfd(attr.Sys)
39 sysattr := &syscall.ProcAttr{
40 Dir: attr.Dir,
41 Env: attr.Env,
42 Sys: attrSys,
43 }
44 if sysattr.Env == nil {
45 sysattr.Env, err = execenv.Default(sysattr.Sys)
46 if err != nil {
47 return nil, err
48 }
49 }
50 sysattr.Files = make([]uintptr, 0, len(attr.Files))
51 for _, f := range attr.Files {
52 sysattr.Files = append(sysattr.Files, f.Fd())
53 }
54
55 pid, h, e := syscall.StartProcess(name, argv, sysattr)
56
57
58 runtime.KeepAlive(attr)
59
60 if e != nil {
61 return nil, &PathError{Op: "fork/exec", Path: name, Err: e}
62 }
63
64
65 if runtime.GOOS != "windows" {
66 var ok bool
67 h, ok = getPidfd(sysattr.Sys, shouldDupPidfd)
68 if !ok {
69 return newPIDProcess(pid), nil
70 }
71 }
72
73 return newHandleProcess(pid, h), nil
74 }
75
76 func (p *Process) kill() error {
77 return p.Signal(Kill)
78 }
79
80
81 type ProcessState struct {
82 pid int
83 status syscall.WaitStatus
84 rusage *syscall.Rusage
85 }
86
87
88 func (p *ProcessState) Pid() int {
89 return p.pid
90 }
91
92 func (p *ProcessState) exited() bool {
93 return p.status.Exited()
94 }
95
96 func (p *ProcessState) success() bool {
97 return p.status.ExitStatus() == 0
98 }
99
100 func (p *ProcessState) sys() any {
101 return p.status
102 }
103
104 func (p *ProcessState) sysUsage() any {
105 return p.rusage
106 }
107
108 func (p *ProcessState) String() string {
109 if p == nil {
110 return "<nil>"
111 }
112 status := p.Sys().(syscall.WaitStatus)
113 res := ""
114 switch {
115 case status.Exited():
116 code := status.ExitStatus()
117 if runtime.GOOS == "windows" && uint(code) >= 1<<16 {
118 res = "exit status " + itoa.Uitox(uint(code))
119 } else {
120 res = "exit status " + itoa.Itoa(code)
121 }
122 case status.Signaled():
123 res = "signal: " + status.Signal().String()
124 case status.Stopped():
125 res = "stop signal: " + status.StopSignal().String()
126 if status.StopSignal() == syscall.SIGTRAP && status.TrapCause() != 0 {
127 res += " (trap " + itoa.Itoa(status.TrapCause()) + ")"
128 }
129 case status.Continued():
130 res = "continued"
131 }
132 if status.CoreDump() {
133 res += " (core dumped)"
134 }
135 return res
136 }
137
138
139
140 func (p *ProcessState) ExitCode() int {
141
142 if p == nil {
143 return -1
144 }
145 return p.status.ExitStatus()
146 }
147
View as plain text