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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
|
#+private
#+build darwin, netbsd, freebsd, openbsd
package os
import "base:runtime"
import "core:time"
import "core:strings"
import kq "core:sys/kqueue"
import "core:sys/posix"
_get_uid :: proc() -> int {
return int(posix.getuid())
}
_get_euid :: proc() -> int {
return int(posix.geteuid())
}
_get_gid :: proc() -> int {
return int(posix.getgid())
}
_get_egid :: proc() -> int {
return int(posix.getegid())
}
_get_pid :: proc() -> int {
return int(posix.getpid())
}
_get_ppid :: proc() -> int {
return int(posix.getppid())
}
_process_info_by_handle :: proc(process: Process, selection: Process_Info_Fields, allocator: runtime.Allocator) -> (info: Process_Info, err: Error) {
return _process_info_by_pid(process.pid, selection, allocator)
}
_current_process_info :: proc(selection: Process_Info_Fields, allocator: runtime.Allocator) -> (info: Process_Info, err: Error) {
return _process_info_by_pid(_get_pid(), selection, allocator)
}
_process_start :: proc(desc: Process_Desc) -> (process: Process, err: Error) {
if len(desc.command) == 0 {
err = .Invalid_Path
return
}
temp_allocator := TEMP_ALLOCATOR_GUARD({})
// search PATH if just a plain name is provided.
exe_builder := strings.builder_make(temp_allocator)
exe_name := desc.command[0]
if strings.index_byte(exe_name, '/') < 0 {
path_env := get_env("PATH", temp_allocator)
path_dirs := split_path_list(path_env, temp_allocator) or_return
found: bool
for dir in path_dirs {
strings.builder_reset(&exe_builder)
strings.write_string(&exe_builder, dir)
strings.write_byte(&exe_builder, '/')
strings.write_string(&exe_builder, exe_name)
if exe_fd := posix.open(strings.to_cstring(&exe_builder) or_return, {.CLOEXEC, .EXEC}); exe_fd == -1 {
continue
} else {
posix.close(exe_fd)
found = true
break
}
}
if !found {
// check in cwd to match windows behavior
strings.builder_reset(&exe_builder)
strings.write_string(&exe_builder, desc.working_dir)
if len(desc.working_dir) > 0 && desc.working_dir[len(desc.working_dir)-1] != '/' {
strings.write_byte(&exe_builder, '/')
}
strings.write_string(&exe_builder, "./")
strings.write_string(&exe_builder, exe_name)
// "hello/./world" is fine right?
if exe_fd := posix.open(strings.to_cstring(&exe_builder) or_return, {.CLOEXEC, .EXEC}); exe_fd == -1 {
err = .Not_Exist
return
} else {
posix.close(exe_fd)
}
}
} else {
strings.builder_reset(&exe_builder)
strings.write_string(&exe_builder, exe_name)
if exe_fd := posix.open(strings.to_cstring(&exe_builder) or_return, {.CLOEXEC, .EXEC}); exe_fd == -1 {
err = .Not_Exist
return
} else {
posix.close(exe_fd)
}
}
cwd: cstring; if desc.working_dir != "" {
cwd = clone_to_cstring(desc.working_dir, temp_allocator) or_return
}
cmd := make([]cstring, len(desc.command) + 1, temp_allocator)
for part, i in desc.command {
cmd[i] = clone_to_cstring(part, temp_allocator) or_return
}
env: [^]cstring
if desc.env == nil {
// take this process's current environment
env = posix.environ
} else {
cenv := make([]cstring, len(desc.env) + 1, temp_allocator)
for env, i in desc.env {
cenv[i] = clone_to_cstring(env, temp_allocator) or_return
}
env = raw_data(cenv)
}
READ :: 0
WRITE :: 1
pipe: [2]posix.FD
if posix.pipe(&pipe) != .OK {
err = _get_platform_error()
return
}
defer posix.close(pipe[READ])
if posix.fcntl(pipe[READ], .SETFD, i32(posix.FD_CLOEXEC)) == -1 {
posix.close(pipe[WRITE])
err = _get_platform_error()
return
}
if posix.fcntl(pipe[WRITE], .SETFD, i32(posix.FD_CLOEXEC)) == -1 {
posix.close(pipe[WRITE])
err = _get_platform_error()
return
}
switch pid := posix.fork(); pid {
case -1:
posix.close(pipe[WRITE])
err = _get_platform_error()
return
case 0:
abort :: proc(parent_fd: posix.FD) -> ! {
#assert(len(posix.Errno) < max(u8))
errno := u8(posix.errno())
posix.write(parent_fd, &errno, 1)
posix.exit(126)
}
null := posix.open("/dev/null", {.RDWR})
if null == -1 { abort(pipe[WRITE]) }
stderr := (^File_Impl)(desc.stderr.impl).fd if desc.stderr != nil else null
stdout := (^File_Impl)(desc.stdout.impl).fd if desc.stdout != nil else null
stdin := (^File_Impl)(desc.stdin.impl).fd if desc.stdin != nil else null
if posix.dup2(stderr, posix.STDERR_FILENO) == -1 { abort(pipe[WRITE]) }
if posix.dup2(stdout, posix.STDOUT_FILENO) == -1 { abort(pipe[WRITE]) }
if posix.dup2(stdin, posix.STDIN_FILENO ) == -1 { abort(pipe[WRITE]) }
if cwd != nil {
if posix.chdir(cwd) != .OK { abort(pipe[WRITE]) }
}
res := posix.execve(strings.to_cstring(&exe_builder) or_return, raw_data(cmd), env)
assert(res == -1)
abort(pipe[WRITE])
case:
posix.close(pipe[WRITE])
errno: posix.Errno
for {
errno_byte: u8
switch posix.read(pipe[READ], &errno_byte, 1) {
case 1:
errno = posix.Errno(errno_byte)
case -1:
errno = posix.errno()
if errno == .EINTR {
continue
} else {
// If the read failed, something weird happened. Do not return the read
// error so the user knows to wait on it.
errno = nil
}
}
break
}
if errno != nil {
// We can assume it trapped here.
for {
info: posix.siginfo_t
wpid := posix.waitid(.P_PID, posix.id_t(process.pid), &info, {.EXITED})
if wpid == -1 && posix.errno() == .EINTR {
continue
}
break
}
err = errno
return
}
process, _ = _process_open(int(pid), {})
return
}
}
_process_wait :: proc(process: Process, timeout: time.Duration) -> (process_state: Process_State, err: Error) {
process_state.pid = process.pid
_process_handle_still_valid(process) or_return
// timeout > 0 = use kqueue to wait (with a timeout) on process exit
// timeout == 0 = use waitid with WNOHANG so it returns immediately
// timeout > 0 = use waitid without WNOHANG so it waits indefinitely
//
// at the end use waitid to actually reap the process and get it's status
if timeout > 0 {
timeout := timeout
queue := kq.kqueue() or_return
defer posix.close(queue)
changelist, eventlist: [1]kq.KEvent
changelist[0] = {
ident = uintptr(process.pid),
filter = .Proc,
flags = { .Add },
fflags = {
fproc = { .Exit },
},
}
for {
start := time.tick_now()
n, kerr := kq.kevent(queue, changelist[:], eventlist[:], &{
tv_sec = posix.time_t(timeout / time.Second),
tv_nsec = i64(timeout % time.Second),
})
if kerr == .EINTR {
timeout -= time.tick_since(start)
continue
} else if kerr != nil {
err = kerr
return
} else if n == 0 {
err = .Timeout
_process_state_update_times(process, &process_state)
return
} else {
_process_state_update_times(process, &process_state)
break
}
}
} else {
flags := posix.Wait_Flags{.EXITED, .NOWAIT}
if timeout == 0 {
flags += {.NOHANG}
}
info: posix.siginfo_t
for {
wpid := posix.waitid(.P_PID, posix.id_t(process.pid), &info, flags)
if wpid == -1 {
if errno := posix.errno(); errno == .EINTR {
continue
} else {
err = _get_platform_error()
return
}
}
break
}
_process_state_update_times(process, &process_state)
if info.si_signo == nil {
assert(timeout == 0)
err = .Timeout
return
}
}
info: posix.siginfo_t
for {
wpid := posix.waitid(.P_PID, posix.id_t(process.pid), &info, {.EXITED})
if wpid == -1 {
if errno := posix.errno(); errno == .EINTR {
continue
} else {
err = _get_platform_error()
return
}
}
break
}
switch info.si_code.chld {
case: unreachable()
case .CONTINUED, .STOPPED: unreachable()
case .EXITED:
process_state.exited = true
process_state.exit_code = int(info.si_status)
process_state.success = process_state.exit_code == 0
case .KILLED, .DUMPED, .TRAPPED:
process_state.exited = true
process_state.exit_code = int(info.si_status)
process_state.success = false
}
return
}
_process_close :: proc(process: Process) -> Error {
return nil
}
_process_kill :: proc(process: Process) -> (err: Error) {
_process_handle_still_valid(process) or_return
if posix.kill(posix.pid_t(process.pid), .SIGKILL) != .OK {
err = _get_platform_error()
}
return
}
|