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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
|
//! Command launches sub-processes. This is an alternate implementation to the
//! Zig std.process.Child since at the time of authoring this, std.process.Child
//! didn't support the options necessary to spawn a shell attached to a pty.
//!
//! Consequently, I didn't implement a lot of features that std.process.Child
//! supports because we didn't need them. Cross-platform subprocessing is not
//! a trivial thing to implement (I've done it in three separate languages now)
//! so if we want to replatform onto std.process.Child I'd love to do that.
//! This was just the fastest way to get something built.
//!
//! Issues with std.process.Child:
//!
//! * No pre_exec callback for logic after fork but before exec.
//! * posix_spawn is used for Mac, but doesn't support the necessary
//! features for tty setup.
//!
//!
//! TODO: This may have changed a lot now with the new I/O implementations in
//! >= 0.16.0, so this might warrant a recheck.
const Command = @This();
const std = @import("std");
const builtin = @import("builtin");
const configpkg = @import("config.zig");
const global = @import("global.zig");
const internal_os = @import("os/main.zig");
const windows = internal_os.windows;
const TempDir = internal_os.TempDir;
const mem = std.mem;
const linux = std.os.linux;
const posix = std.posix;
const debug = std.debug;
const testing = std.testing;
const Allocator = std.mem.Allocator;
const File = std.Io.File;
const EnvMap = std.process.Environ.Map;
const apprt = @import("apprt.zig");
/// Function prototype for a function executed /in the child process/ after the
/// fork, but before exec'ing the command. If the function returns a u8, the
/// child process will be exited with that error code.
const PreExecFn = fn (*Command) ?u8;
/// Allowable set of errors that can be returned by a post fork function. Any
/// errors will result in the failure to create the surface.
pub const PostForkError = error{PostForkError};
/// Function prototype for a function executed /in the parent process/
/// after the fork.
const PostForkFn = fn (*Command) PostForkError!void;
/// Path to the command to run. This doesn't have to be an absolute path,
/// because use exec functions that search the PATH, if necessary.
///
/// This field is null-terminated to avoid a copy for the sake of
/// adding a null terminator since POSIX systems are so common.
path: [:0]const u8,
/// Command-line arguments. It is the responsibility of the caller to set
/// args[0] to the command. If args is empty then args[0] will automatically
/// be set to equal path.
args: []const [:0]const u8,
/// Environment variables for the child process. If this is null, inherits
/// the environment variables from this process. These are the exact
/// environment variables to set; these are /not/ merged.
env: ?*const EnvMap = null,
/// Working directory to change to in the child process. If not set, the
/// working directory of the calling process is preserved.
cwd: ?[:0]const u8 = null,
/// The file handle to set for stdin/out/err. If this isn't set, we do
/// nothing explicitly so it is up to the behavior of the operating system.
stdin: ?File = null,
stdout: ?File = null,
stderr: ?File = null,
/// If set, this will be executed /in the child process/ after fork but
/// before exec. This is useful to setup some state in the child before the
/// exec process takes over, such as signal handlers, setsid, setuid, etc.
os_pre_exec: ?*const PreExecFn,
/// If set, this will be executed /in the child process/ after fork but
/// before exec. This is useful to setup some state in the child before the
/// exec process takes over, such as signal handlers, setsid, setuid, etc.
rt_pre_exec: ?*const PreExecFn,
/// Configuration information needed by the apprt pre exec function. Note
/// that this should be a trivially copyable struct and not require any
/// allocation/deallocation.
rt_pre_exec_info: RtPreExecInfo,
/// If set, this will be executed in the /in the parent process/ after the fork.
rt_post_fork: ?*const PostForkFn,
/// Configuration information needed by the apprt post fork function. Note
/// that this should be a trivially copyable struct and not require any
/// allocation/deallocation.
rt_post_fork_info: RtPostForkInfo,
/// If set, then the process will be created attached to this pseudo console.
/// `stdin`, `stdout`, and `stderr` will be ignored if set.
pseudo_console: if (builtin.os.tag == .windows) ?windows.HPCON else void =
if (builtin.os.tag == .windows) null else {},
/// User data that is sent to the callback. Set with setData and getData
/// for a more user-friendly API.
data: ?*anyopaque = null,
/// Process ID is set after start is called.
pid: ?posix.system.pid_t = null,
/// The various methods a process may exit.
pub const Exit = if (builtin.os.tag == .windows) union(enum) {
Exited: u32,
} else union(enum) {
/// Exited by normal exit call, value is exit status
Exited: u8,
/// Exited by a signal, value is the signal
Signal: u32,
/// Exited by a stop signal, value is signal
Stopped: u32,
/// Unknown exit reason, value is the status from waitpid
Unknown: u32,
pub fn init(status: u32) Exit {
return if (posix.W.IFEXITED(status))
Exit{ .Exited = posix.W.EXITSTATUS(status) }
else if (posix.W.IFSIGNALED(status))
Exit{ .Signal = @intFromEnum(posix.W.TERMSIG(status)) }
else if (posix.W.IFSTOPPED(status))
Exit{ .Stopped = @intFromEnum(posix.W.STOPSIG(status)) }
else
Exit{ .Unknown = status };
}
};
/// Configuration information needed by the apprt pre exec function. Note
/// that this should be a trivially copyable struct and not require any
/// allocation/deallocation.
pub const RtPreExecInfo = if (@hasDecl(apprt.runtime, "pre_exec")) apprt.runtime.pre_exec.PreExecInfo else struct {
pub inline fn init(_: *const configpkg.Config) @This() {
return .{};
}
};
/// Configuration information needed by the apprt post fork function. Note
/// that this should be a trivially copyable struct and not require any
/// allocation/deallocation.
pub const RtPostForkInfo = if (@hasDecl(apprt.runtime, "post_fork")) apprt.runtime.post_fork.PostForkInfo else struct {
pub inline fn init(_: *const configpkg.Config) @This() {
return .{};
}
};
/// Start the subprocess. This returns immediately once the child is started.
///
/// After this is successful, self.pid is available.
pub fn start(self: *Command, alloc: Allocator) !void {
// Use an arena allocator for the temporary allocations we need in this func.
// IMPORTANT: do all allocation prior to the fork(). I believe it is undefined
// behavior if you malloc between fork and exec. The source of the Zig
// stdlib seems to verify this as well as Go.
var arena_allocator = std.heap.ArenaAllocator.init(alloc);
defer arena_allocator.deinit();
const arena = arena_allocator.allocator();
switch (builtin.os.tag) {
.windows => try self.startWindows(arena),
else => try self.startPosix(arena),
}
}
fn startPosix(self: *Command, arena: Allocator) !void {
// Null-terminate all our arguments
const argsZ = try arena.allocSentinel(?[*:0]const u8, self.args.len, null);
for (self.args, 0..) |arg, i| argsZ[i] = arg.ptr;
// Determine our env vars
const envp = if (self.env) |env_map|
(try createNullDelimitedEnvMap(arena, env_map)).ptr
else if (builtin.link_libc)
std.c.environ
else
@compileError("missing env vars");
// Fork.
const pid = try fork();
if (pid != 0) {
// Parent, return immediately.
self.pid = @intCast(pid);
if (self.rt_post_fork) |f| try f(self);
return;
}
// We are the child.
// Setup our file descriptors for std streams.
if (self.stdin) |f| setupFd(f.handle, posix.STDIN_FILENO) catch
return error.ExecFailedInChild;
if (self.stdout) |f| setupFd(f.handle, posix.STDOUT_FILENO) catch
return error.ExecFailedInChild;
if (self.stderr) |f| setupFd(f.handle, posix.STDERR_FILENO) catch
return error.ExecFailedInChild;
// Setup our working directory.
//
// NOTE: this can fail if we don't have permission to go to this directory
// or if due to race conditions it doesn't exist or any various other
// reasons. We don't want to crash the entire process if this fails so we
// ignore it. We don't log because that'll show up in the output.
if (self.cwd) |cwd| _ = posix.system.chdir(cwd);
// Restore any rlimits that were set by Ghostty. This might fail but
// any failures are ignored (its best effort).
global.rlimits().restore();
// If there are pre exec callbacks, call them now.
if (self.os_pre_exec) |f| if (f(self)) |exitcode| posix.system.exit(exitcode);
if (self.rt_pre_exec) |f| if (f(self)) |exitcode| posix.system.exit(exitcode);
const err: posix.E = execve: {
// This functionality has been taken from Zig stdlib, a simplified
// version of the exec bits with PATH search so that we can just
// offload to execve below.
const file_slice = std.mem.sliceTo(self.path, 0);
if (std.mem.findScalar(u8, file_slice, '/') != null) {
break :execve posix.errno(posix.system.execve(self.path, argsZ, envp));
}
var path_expanded_buf: [std.fs.max_path_bytes]u8 = undefined;
const PATH = global.environ().getPosix("PATH") orelse "/usr/local/bin:/bin/:/usr/bin";
var it = std.mem.tokenizeScalar(u8, PATH, ':');
var err: posix.system.E = .NOENT;
var seen_eacces = false;
while (it.next()) |search_path| {
const path_len = search_path.len + file_slice.len + 1;
if (path_expanded_buf.len < path_len + 1) break :execve .NAMETOOLONG;
@memcpy(path_expanded_buf[0..search_path.len], search_path);
path_expanded_buf[search_path.len] = '/';
@memcpy(path_expanded_buf[search_path.len + 1 ..][0..file_slice.len], file_slice);
path_expanded_buf[path_len] = 0;
const full_path = path_expanded_buf[0..path_len :0].ptr;
// Replace here, switch on error (any error means that replace
// failed, but we might need to retry).
err = posix.errno(posix.system.execve(full_path, argsZ, envp));
switch (err) {
.ACCES => seen_eacces = true,
.NOENT, .NOTDIR => {},
else => break :execve err,
}
}
if (seen_eacces) break :execve .ACCES;
break :execve err;
};
// If we are executing this code, the exec failed. We're in the
// child process so there isn't much we can do. We try to output
// something reasonable. Its important to note we MUST NOT return
// any other error condition from here on out.
var stderr_buf: [1024]u8 = undefined;
var stderr_writer = std.Io.File.stderr().writer(global.io(), &stderr_buf);
const stderr = &stderr_writer.interface;
switch (err) {
posix.system.E.NOENT => stderr.print(
\\Requested executable not found. Please verify the command is on
\\the PATH and try again.
\\
,
.{},
) catch {},
else => |e| stderr.print(
\\exec syscall failed with unexpected error: E{s}
\\
,
.{@tagName(e)},
) catch {},
}
stderr.flush() catch {};
// We return a very specific error that can be detected to determine
// we're in the child.
return error.ExecFailedInChild;
}
/// Wrapper for the raw fork syscall. This preserves the error handling from
/// the std.posix wrapper that was removed in Zig 0.16.
fn fork() !posix.pid_t {
const rc = posix.system.fork();
switch (posix.errno(rc)) {
.SUCCESS => return @intCast(rc),
.AGAIN, .NOMEM => return error.SystemResources,
else => |err| return posix.unexpectedErrno(err),
}
}
fn startWindows(self: *Command, arena: Allocator) !void {
const cwd_w = if (self.cwd) |cwd| try std.unicode.utf8ToUtf16LeAllocZ(arena, cwd) else null;
// Pass null for lpApplicationName and put the program as the first
// token of lpCommandLine. This lets CreateProcessW perform the
// standard program search (parent-app dir, CWD, system dirs, PATH)
// and append ".exe" when the name has no extension, which is what
// users expect for bare commands like `wsl ~` or `pwsh.exe`.
// It also preserves the child's argv[0] as written by the caller
// rather than replacing it with the resolved absolute path.
const command_line = if (self.args.len > 0)
try windowsCreateCommandLine(arena, self.args)
else
try windowsCreateCommandLine(arena, &.{self.path});
const command_line_w = try std.unicode.utf8ToUtf16LeAllocZ(arena, command_line);
const env_w = if (self.env) |env_map| try createWindowsEnvBlock(arena, env_map) else null;
const any_null_fd = self.stdin == null or self.stdout == null or self.stderr == null;
const null_fd = if (any_null_fd) null_fd: {
// path = "\Device\Null"
const path = [_]u16{ '\\', 'D', 'e', 'v', 'i', 'c', 'e', '\\', 'N', 'u', 'l', 'l' };
var path_unicode_string: windows.UNICODE_STRING = .init(&path);
var attrs: windows.OBJECT_ATTRIBUTES = .{ .ObjectName = &path_unicode_string };
var fd: windows.HANDLE = undefined;
var io_status: windows.IO_STATUS_BLOCK = undefined; // unused
const result = windows.exp.ntdll.NtCreateFile(
&fd,
.{ .GENERIC = .{ .READ = true }, .STANDARD = .{ .SYNCHRONIZE = true } },
&attrs,
&io_status,
null,
windows.FILE_ATTRIBUTE_NORMAL,
windows.FILE_SHARE_READ,
windows.OPEN_EXISTING,
windows.FILE_NON_DIRECTORY_FILE,
null,
0,
);
if (result != .SUCCESS) {
return windows.unexpectedStatus(result);
}
break :null_fd fd;
} else null;
defer {
if (null_fd) |fd| _ = windows.exp.kernel32.CloseHandle(fd);
}
// TODO: In the case of having FDs instead of pty, need to set up
// attributes such that the child process only inherits these handles,
// then set bInheritsHandles below.
const attribute_list, const stdin, const stdout, const stderr = if (self.pseudo_console) |pseudo_console| b: {
var attribute_list_size: usize = undefined;
_ = windows.exp.kernel32.InitializeProcThreadAttributeList(
null,
1,
0,
&attribute_list_size,
);
const attribute_list_buf = try arena.alloc(u8, attribute_list_size);
if (windows.exp.kernel32.InitializeProcThreadAttributeList(
attribute_list_buf.ptr,
1,
0,
&attribute_list_size,
) == windows.FALSE) return windows.unexpectedError(windows.GetLastError());
if (windows.exp.kernel32.UpdateProcThreadAttribute(
attribute_list_buf.ptr,
0,
windows.PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE,
pseudo_console,
@sizeOf(windows.HPCON),
null,
null,
) == windows.FALSE) return windows.unexpectedError(windows.GetLastError());
break :b .{ attribute_list_buf.ptr, null, null, null };
} else b: {
const stdin = if (self.stdin) |f| f.handle else null_fd.?;
const stdout = if (self.stdout) |f| f.handle else null_fd.?;
const stderr = if (self.stderr) |f| f.handle else null_fd.?;
break :b .{ null, stdin, stdout, stderr };
};
var startup_info_ex = windows.STARTUPINFOEX{
.StartupInfo = .{
.cb = if (attribute_list != null) @sizeOf(windows.STARTUPINFOEX) else @sizeOf(windows.STARTUPINFOW),
.hStdError = stderr,
.hStdOutput = stdout,
.hStdInput = stdin,
.dwFlags = windows.STARTF_USESTDHANDLES,
.lpReserved = null,
.lpDesktop = null,
.lpTitle = null,
.dwX = 0,
.dwY = 0,
.dwXSize = 0,
.dwYSize = 0,
.dwXCountChars = 0,
.dwYCountChars = 0,
.dwFillAttribute = 0,
.wShowWindow = 0,
.cbReserved2 = 0,
.lpReserved2 = null,
},
.lpAttributeList = attribute_list,
};
var flags: windows.DWORD = windows.CREATE_UNICODE_ENVIRONMENT;
if (attribute_list != null) flags |= windows.EXTENDED_STARTUPINFO_PRESENT;
var process_information: windows.PROCESS_INFORMATION = undefined;
if (windows.exp.kernel32.CreateProcessW(
null,
command_line_w.ptr,
null,
null,
windows.TRUE,
flags,
if (env_w) |w| w.ptr else null,
if (cwd_w) |w| w.ptr else null,
@ptrCast(&startup_info_ex.StartupInfo),
&process_information,
) == windows.FALSE) return windows.unexpectedError(windows.GetLastError());
self.pid = process_information.hProcess;
}
fn setupFd(src: File.Handle, target: i32) !void {
const PosixCall = struct {
fn f(func: anytype, args: anytype) !usize {
while (true) {
const rc = @call(.auto, func, args);
switch (posix.errno(rc)) {
.SUCCESS => return @intCast(rc),
.INTR => continue,
.AGAIN, .ACCES => return error.Locked,
.BADF => unreachable,
.BUSY => return error.FileBusy,
.INVAL => unreachable, // invalid parameters
.PERM => return error.PermissionDenied,
.MFILE => return error.ProcessFdQuotaExceeded,
.NOTDIR => unreachable, // invalid parameter
.DEADLK => return error.DeadLock,
.NOLCK => return error.LockedRegionLimitExceeded,
else => |err| return posix.unexpectedErrno(err),
}
}
}
};
switch (builtin.os.tag) {
.linux => {
// We use dup3 so that we can clear CLO_ON_EXEC. We do NOT want this
// file descriptor to be closed on exec since we're exactly exec-ing after
// this.
_ = try PosixCall.f(linux.dup3, .{ src, target, 0 });
},
.freebsd, .ios, .macos => {
// Mac doesn't support dup3 so we use dup2. We purposely clear
// CLO_ON_EXEC for this fd.
const flags = try PosixCall.f(posix.system.fcntl, .{ src, posix.F.GETFD });
if (flags & posix.FD_CLOEXEC != 0) {
_ = try PosixCall.f(
posix.system.fcntl,
.{ src, posix.F.SETFD, flags & ~@as(u32, posix.FD_CLOEXEC) },
);
}
_ = try PosixCall.f(posix.system.dup2, .{ src, target });
},
else => @compileError("unsupported platform"),
}
}
/// Wait for the command to exit and return information about how it exited.
pub fn wait(self: Command, block: bool) !Exit {
if (comptime builtin.os.tag == .windows) {
// Block until the process exits. This returns immediately if the
// process already exited.
//
// NOTE: We can use the pid directly as posix.system.pid_t is still an
// alias for a handle under Windows. We might want to keep an eye on if
// this changes, though.
const result = windows.exp.kernel32.WaitForSingleObject(self.pid.?, windows.INFINITE);
if (result == windows.WAIT_FAILED) {
return windows.unexpectedError(windows.GetLastError());
}
var exit_code: windows.DWORD = undefined;
const has_code = windows.exp.kernel32.GetExitCodeProcess(self.pid.?, &exit_code) != windows.FALSE;
if (!has_code) {
return windows.unexpectedError(windows.GetLastError());
}
return .{ .Exited = exit_code };
}
const status: u32 = if (block) wait_block: {
var status: if (builtin.link_libc) c_int else u32 = undefined;
_ = try waitPid(self.pid.?, &status, 0);
break :wait_block @bitCast(status);
} else wait_nohang: {
// We specify NOHANG because its not our fault if the process we launch
// for the tty doesn't properly waitpid its children. We don't want
// to hang the terminal over it.
// When NOHANG is specified, waitpid will return a pid of 0 if the process
// doesn't have a status to report. When that happens, it is as though the
// wait call has not been performed, so we need to keep trying until we get
// a non-zero pid back, otherwise we end up with zombie processes.
while (true) {
var status: if (builtin.link_libc) c_int else u32 = undefined;
const pid = try waitPid(self.pid.?, &status, posix.system.W.NOHANG);
if (pid != 0) break :wait_nohang @bitCast(status);
}
};
return .init(status);
}
/// Wrapper for the raw waitpid syscall. Status is only initialized on success;
/// interrupted waits are retried and all other errors are propagated.
fn waitPid(
pid: posix.pid_t,
status: *if (builtin.link_libc) c_int else u32,
flags: u32,
) !posix.pid_t {
while (true) {
const rc = posix.system.waitpid(pid, status, @intCast(flags));
switch (posix.errno(rc)) {
.SUCCESS => return @intCast(rc),
.INTR => continue,
.CHILD => return error.NoChildProcess,
.INVAL => return error.InvalidWaitOptions,
else => |err| return posix.unexpectedErrno(err),
}
}
}
/// Sets command->data to data.
pub fn setData(self: *Command, pointer: ?*anyopaque) void {
self.data = pointer;
}
/// Returns command->data.
pub fn getData(self: Command, comptime DT: type) ?*DT {
return if (self.data) |ptr| @ptrCast(@alignCast(ptr)) else null;
}
// Copied from Zig. This is a publicly exported function but there is no
// way to get it from the std package.
fn createNullDelimitedEnvMap(arena: mem.Allocator, env_map: *const EnvMap) ![:null]?[*:0]u8 {
const envp_count = env_map.count();
const envp_buf = try arena.allocSentinel(?[*:0]u8, envp_count, null);
var it = env_map.iterator();
var i: usize = 0;
while (it.next()) |pair| : (i += 1) {
const env_buf = try arena.allocSentinel(u8, pair.key_ptr.len + pair.value_ptr.len + 1, 0);
@memcpy(env_buf[0..pair.key_ptr.len], pair.key_ptr.*);
env_buf[pair.key_ptr.len] = '=';
@memcpy(env_buf[pair.key_ptr.len + 1 ..], pair.value_ptr.*);
envp_buf[i] = env_buf.ptr;
}
std.debug.assert(i == envp_count);
return envp_buf;
}
// Copied from Zig. This is a publicly exported function but there is no
// way to get it from the std package.
fn createWindowsEnvBlock(allocator: mem.Allocator, env_map: *const EnvMap) ![]u16 {
// count bytes needed
const max_chars_needed = x: {
var max_chars_needed: usize = 4; // 4 for the final 4 null bytes
var it = env_map.iterator();
while (it.next()) |pair| {
// +1 for '='
// +1 for null byte
max_chars_needed += pair.key_ptr.len + pair.value_ptr.len + 2;
}
break :x max_chars_needed;
};
const result = try allocator.alloc(u16, max_chars_needed);
errdefer allocator.free(result);
var it = env_map.iterator();
var i: usize = 0;
while (it.next()) |pair| {
i += try std.unicode.utf8ToUtf16Le(result[i..], pair.key_ptr.*);
result[i] = '=';
i += 1;
i += try std.unicode.utf8ToUtf16Le(result[i..], pair.value_ptr.*);
result[i] = 0;
i += 1;
}
result[i] = 0;
i += 1;
result[i] = 0;
i += 1;
result[i] = 0;
i += 1;
result[i] = 0;
i += 1;
return try allocator.realloc(result, i);
}
/// Copied from Zig. This function could be made public in child_process.zig instead.
fn windowsCreateCommandLine(allocator: mem.Allocator, argv: []const []const u8) ![:0]u8 {
var buf: std.Io.Writer.Allocating = .init(allocator);
defer buf.deinit();
const writer = &buf.writer;
for (argv, 0..) |arg, arg_i| {
if (arg_i != 0) try writer.writeByte(' ');
if (mem.indexOfAny(u8, arg, " \t\n\"") == null) {
try writer.writeAll(arg);
continue;
}
try writer.writeByte('"');
var backslash_count: usize = 0;
for (arg) |byte| {
switch (byte) {
'\\' => backslash_count += 1,
'"' => {
try writer.splatByteAll('\\', backslash_count * 2 + 1);
try writer.writeByte('"');
backslash_count = 0;
},
else => {
try writer.splatByteAll('\\', backslash_count);
try writer.writeByte(byte);
backslash_count = 0;
},
}
}
try writer.splatByteAll('\\', backslash_count * 2);
try writer.writeByte('"');
}
return buf.toOwnedSliceSentinel(0);
}
test "createNullDelimitedEnvMap" {
const allocator = testing.allocator;
var envmap = EnvMap.init(allocator);
defer envmap.deinit();
try envmap.put("HOME", "/home/ifreund");
try envmap.put("WAYLAND_DISPLAY", "wayland-1");
try envmap.put("DISPLAY", ":1");
try envmap.put("DEBUGINFOD_URLS", " ");
try envmap.put("XCURSOR_SIZE", "24");
var arena = std.heap.ArenaAllocator.init(allocator);
defer arena.deinit();
const environ = try createNullDelimitedEnvMap(arena.allocator(), &envmap);
try testing.expectEqual(@as(usize, 5), environ.len);
inline for (.{
"HOME=/home/ifreund",
"WAYLAND_DISPLAY=wayland-1",
"DISPLAY=:1",
"DEBUGINFOD_URLS= ",
"XCURSOR_SIZE=24",
}) |target| {
for (environ) |variable| {
if (mem.eql(u8, mem.span(variable orelse continue), target)) break;
} else {
try testing.expect(false); // Environment variable not found
}
}
}
test "Command: os pre exec 1" {
if (builtin.os.tag == .windows) return error.SkipZigTest;
var cmd: Command = .{
.path = "/bin/sh",
.args = &.{ "/bin/sh", "-v" },
.os_pre_exec = (struct {
fn do(_: *Command) ?u8 {
// This runs in the child, so we can exit and it won't
// kill the test runner.
posix.system.exit(42);
}
}).do,
.rt_pre_exec = null,
.rt_post_fork = null,
.rt_pre_exec_info = undefined,
.rt_post_fork_info = undefined,
};
try cmd.testingStart();
try testing.expect(cmd.pid != null);
const exit = try cmd.wait(true);
try testing.expect(exit == .Exited);
try testing.expect(exit.Exited == 42);
}
test "Command: os pre exec 2" {
if (builtin.os.tag == .windows) return error.SkipZigTest;
var cmd: Command = .{
.path = "/bin/sh",
.args = &.{ "/bin/sh", "-v" },
.os_pre_exec = (struct {
fn do(_: *Command) ?u8 {
// This runs in the child, so we can exit and it won't
// kill the test runner.
return 42;
}
}).do,
.rt_pre_exec = null,
.rt_post_fork = null,
.rt_pre_exec_info = undefined,
.rt_post_fork_info = undefined,
};
try cmd.testingStart();
try testing.expect(cmd.pid != null);
const exit = try cmd.wait(true);
try testing.expect(exit == .Exited);
try testing.expect(exit.Exited == 42);
}
test "Command: rt pre exec 1" {
if (builtin.os.tag == .windows) return error.SkipZigTest;
var cmd: Command = .{
.path = "/bin/sh",
.args = &.{ "/bin/sh", "-v" },
.os_pre_exec = null,
.rt_pre_exec = (struct {
fn do(_: *Command) ?u8 {
// This runs in the child, so we can exit and it won't
// kill the test runner.
posix.system.exit(42);
}
}).do,
.rt_post_fork = null,
.rt_pre_exec_info = undefined,
.rt_post_fork_info = undefined,
};
try cmd.testingStart();
try testing.expect(cmd.pid != null);
const exit = try cmd.wait(true);
try testing.expect(exit == .Exited);
try testing.expect(exit.Exited == 42);
}
test "Command: rt pre exec 2" {
if (builtin.os.tag == .windows) return error.SkipZigTest;
var cmd: Command = .{
.path = "/bin/sh",
.args = &.{ "/bin/sh", "-v" },
.os_pre_exec = null,
.rt_pre_exec = (struct {
fn do(_: *Command) ?u8 {
// This runs in the child, so we can exit and it won't
// kill the test runner.
return 42;
}
}).do,
.rt_post_fork = null,
.rt_pre_exec_info = undefined,
.rt_post_fork_info = undefined,
};
try cmd.testingStart();
try testing.expect(cmd.pid != null);
const exit = try cmd.wait(true);
try testing.expect(exit == .Exited);
try testing.expect(exit.Exited == 42);
}
test "Command: rt post fork 1" {
if (builtin.os.tag == .windows) return error.SkipZigTest;
var cmd: Command = .{
.path = "/bin/sh",
.args = &.{ "/bin/sh", "-c", "sleep 1" },
.os_pre_exec = null,
.rt_pre_exec = null,
.rt_post_fork = (struct {
fn do(_: *Command) PostForkError!void {
return error.PostForkError;
}
}).do,
.rt_pre_exec_info = undefined,
.rt_post_fork_info = undefined,
};
try testing.expectError(error.PostForkError, cmd.testingStart());
}
fn createTestStdout(io: std.Io, dir: std.Io.Dir) !File {
const file = try dir.createFile(io, "stdout.txt", .{ .read = true });
if (builtin.os.tag == .windows) {
if (windows.exp.kernel32.SetHandleInformation(
file.handle,
windows.HANDLE_FLAG_INHERIT,
windows.HANDLE_FLAG_INHERIT,
) == windows.FALSE) {
return windows.unexpectedError(windows.GetLastError());
}
}
return file;
}
fn createTestStderr(io: std.Io, dir: std.Io.Dir) !File {
const file = try dir.createFile(io, "stderr.txt", .{ .read = true });
if (builtin.os.tag == .windows) {
if (windows.exp.kernel32.SetHandleInformation(
file.handle,
windows.HANDLE_FLAG_INHERIT,
windows.HANDLE_FLAG_INHERIT,
) == windows.FALSE) {
return windows.unexpectedError(windows.GetLastError());
}
}
return file;
}
test "Command: redirect stdout to file" {
var td = try TempDir.init();
defer td.deinit();
var stdout = try createTestStdout(testing.io, td.dir);
defer stdout.close(testing.io);
var cmd: Command = if (builtin.os.tag == .windows) .{
.path = "C:\\Windows\\System32\\whoami.exe",
.args = &.{"C:\\Windows\\System32\\whoami.exe"},
.stdout = stdout,
.os_pre_exec = null,
.rt_pre_exec = null,
.rt_post_fork = null,
.rt_pre_exec_info = undefined,
.rt_post_fork_info = undefined,
} else .{
.path = "/bin/sh",
.args = &.{ "/bin/sh", "-c", "echo hello" },
.stdout = stdout,
.os_pre_exec = null,
.rt_pre_exec = null,
.rt_post_fork = null,
.rt_pre_exec_info = undefined,
.rt_post_fork_info = undefined,
};
try cmd.testingStart();
try testing.expect(cmd.pid != null);
const exit = try cmd.wait(true);
try testing.expect(exit == .Exited);
try testing.expectEqual(@as(u32, 0), @as(u32, exit.Exited));
// Read our stdout
const contents = contents: {
const size = (try stdout.stat(testing.io)).size;
const data = try testing.allocator.alloc(u8, size);
errdefer testing.allocator.free(data);
try testing.expectEqual(size, try stdout.readPositionalAll(testing.io, data, 0));
break :contents data;
};
defer testing.allocator.free(contents);
try testing.expect(contents.len > 0);
}
test "Command: custom env vars" {
var td = try TempDir.init();
defer td.deinit();
var stdout = try createTestStdout(testing.io, td.dir);
defer stdout.close(testing.io);
var env = EnvMap.init(testing.allocator);
defer env.deinit();
try env.put("VALUE", "hello");
var cmd: Command = if (builtin.os.tag == .windows) .{
.path = "C:\\Windows\\System32\\cmd.exe",
.args = &.{ "C:\\Windows\\System32\\cmd.exe", "/C", "echo %VALUE%" },
.stdout = stdout,
.env = &env,
.os_pre_exec = null,
.rt_pre_exec = null,
.rt_post_fork = null,
.rt_pre_exec_info = undefined,
.rt_post_fork_info = undefined,
} else .{
.path = "/bin/sh",
.args = &.{ "/bin/sh", "-c", "echo $VALUE" },
.stdout = stdout,
.env = &env,
.os_pre_exec = null,
.rt_pre_exec = null,
.rt_post_fork = null,
.rt_pre_exec_info = undefined,
.rt_post_fork_info = undefined,
};
try cmd.testingStart();
try testing.expect(cmd.pid != null);
const exit = try cmd.wait(true);
try testing.expect(exit == .Exited);
try testing.expect(exit.Exited == 0);
// Read our stdout
const contents = contents: {
const size = (try stdout.stat(testing.io)).size;
const data = try testing.allocator.alloc(u8, size);
errdefer testing.allocator.free(data);
try testing.expectEqual(size, try stdout.readPositionalAll(testing.io, data, 0));
break :contents data;
};
defer testing.allocator.free(contents);
if (builtin.os.tag == .windows) {
try testing.expectEqualStrings("hello\r\n", contents);
} else {
try testing.expectEqualStrings("hello\n", contents);
}
}
test "Command: custom working directory" {
var td = try TempDir.init();
defer td.deinit();
var stdout = try createTestStdout(testing.io, td.dir);
defer stdout.close(testing.io);
var cmd: Command = if (builtin.os.tag == .windows) .{
.path = "C:\\Windows\\System32\\cmd.exe",
.args = &.{ "C:\\Windows\\System32\\cmd.exe", "/C", "cd" },
.stdout = stdout,
.cwd = "C:\\Windows\\System32",
.os_pre_exec = null,
.rt_pre_exec = null,
.rt_post_fork = null,
.rt_pre_exec_info = undefined,
.rt_post_fork_info = undefined,
} else .{
.path = "/bin/sh",
.args = &.{ "/bin/sh", "-c", "pwd" },
.stdout = stdout,
.cwd = "/tmp",
.os_pre_exec = null,
.rt_pre_exec = null,
.rt_post_fork = null,
.rt_pre_exec_info = undefined,
.rt_post_fork_info = undefined,
};
try cmd.testingStart();
try testing.expect(cmd.pid != null);
const exit = try cmd.wait(true);
try testing.expect(exit == .Exited);
try testing.expect(exit.Exited == 0);
// Read our stdout
const contents = contents: {
const size = (try stdout.stat(testing.io)).size;
const data = try testing.allocator.alloc(u8, size);
errdefer testing.allocator.free(data);
try testing.expectEqual(size, try stdout.readPositionalAll(testing.io, data, 0));
break :contents data;
};
defer testing.allocator.free(contents);
if (builtin.os.tag == .windows) {
try testing.expectEqualStrings("C:\\Windows\\System32\r\n", contents);
} else if (builtin.os.tag == .macos) {
try testing.expectEqualStrings("/private/tmp\n", contents);
} else {
try testing.expectEqualStrings("/tmp\n", contents);
}
}
// Test validate an execveZ failure correctly terminates when error.ExecFailedInChild is correctly handled
//
// Incorrectly handling an error.ExecFailedInChild results in a second copy of the test process running.
// Duplicating the test process leads to weird behavior
// zig build test will hang
// test binary created via -Demit-test-exe will run 2 copies of the test suite
test "Command: posix fork handles execveZ failure" {
if (builtin.os.tag == .windows) {
return error.SkipZigTest;
}
var td = try TempDir.init();
defer td.deinit();
var stdout = try createTestStdout(testing.io, td.dir);
defer stdout.close(testing.io);
var stderr = try createTestStderr(testing.io, td.dir);
defer stderr.close(testing.io);
var cmd: Command = .{
.path = "/not/a/binary",
.args = &.{ "/not/a/binary", "" },
.stdout = stdout,
.stderr = stderr,
.cwd = "/bin",
.os_pre_exec = null,
.rt_pre_exec = null,
.rt_post_fork = null,
.rt_pre_exec_info = undefined,
.rt_post_fork_info = undefined,
};
try cmd.testingStart();
try testing.expect(cmd.pid != null);
const exit = try cmd.wait(true);
try testing.expect(exit == .Exited);
try testing.expect(exit.Exited == 1);
}
// If cmd.start fails with error.ExecFailedInChild it's the _child_ process that is running. If it does not
// terminate in response to that error both the parent and child will continue as if they _are_ the test suite
// process.
fn testingStart(self: *Command) !void {
self.start(testing.allocator) catch |err| {
if (err == error.ExecFailedInChild) {
// I am a child process, I must not get confused and continue running the rest of the test suite.
posix.system.exit(1);
}
return err;
};
}
|