Skip to content

Commit df6ed94

Browse files
committed
cleanups
1 parent b8b6875 commit df6ed94

File tree

3 files changed

+19
-29
lines changed

3 files changed

+19
-29
lines changed

lib/std/child_process.zig

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -889,16 +889,20 @@ pub const ChildProcess = struct {
889889
windowsDestroyPipe(g_hChildStd_IN_Rd, g_hChildStd_IN_Wr);
890890
};
891891

892+
var tmp_hChildStd_Rd: windows.HANDLE = undefined;
893+
var tmp_hChildStd_Wr: windows.HANDLE = undefined;
892894
var g_hChildStd_OUT_Rd: ?windows.HANDLE = null;
893895
var g_hChildStd_OUT_Wr: ?windows.HANDLE = null;
894896
switch (self.stdout_behavior) {
895897
StdIo.Pipe => {
896898
try windowsMakeAsyncPipe(
897-
&g_hChildStd_OUT_Rd,
898-
&g_hChildStd_OUT_Wr,
899+
&tmp_hChildStd_Rd,
900+
&tmp_hChildStd_Wr,
899901
&saAttr,
900902
PipeDirection.child_to_parent,
901903
);
904+
g_hChildStd_OUT_Rd.* = tmp_hChildStd_Rd.*;
905+
g_hChildStd_OUT_Wr.* = tmp_hChildStd_Wr.*;
902906
},
903907
StdIo.Ignore => {
904908
g_hChildStd_OUT_Wr = nul_handle;
@@ -919,11 +923,13 @@ pub const ChildProcess = struct {
919923
switch (self.stderr_behavior) {
920924
StdIo.Pipe => {
921925
try windowsMakeAsyncPipe(
922-
&g_hChildStd_ERR_Rd,
923-
&g_hChildStd_ERR_Wr,
926+
&tmp_hChildStd_Rd,
927+
&tmp_hChildStd_Wr,
924928
&saAttr,
925929
PipeDirection.child_to_parent,
926930
);
931+
g_hChildStd_ERR_Rd.* = tmp_hChildStd_Rd.*;
932+
g_hChildStd_ERR_Wr.* = tmp_hChildStd_Wr.*;
927933
},
928934
StdIo.Ignore => {
929935
g_hChildStd_ERR_Wr = nul_handle;

test/standalone/childprocess_extrapipe/child.zig

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,10 @@ pub fn main() !void {
1111
_ = it.next() orelse unreachable; // skip binary name
1212
const s_handle = it.next() orelse unreachable;
1313

14-
var file_handle: std.os.fd_t = file_handle: {
15-
if (builtin.target.os.tag == .windows) {
16-
var handle_int = try std.fmt.parseInt(usize, s_handle, 10);
17-
break :file_handle @intToPtr(windows.HANDLE, handle_int);
18-
} else {
19-
break :file_handle try std.fmt.parseInt(std.os.fd_t, s_handle, 10);
20-
}
21-
};
14+
var file_handle: std.os.fd_t = if (builtin.target.os.tag == .windows)
15+
@intToPtr(windows.HANDLE, try std.fmt.parseInt(usize, s_handle, 10))
16+
else
17+
try std.fmt.parseInt(std.os.fd_t, s_handle, 10);
2218
// TODO: Is there a way on Windows to let the Kernel disable inheritance
2319
// after it is inherited in CreateProcess???
2420
if (builtin.target.os.tag == .windows) {

test/standalone/childprocess_extrapipe/parent.zig

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ pub fn main() !void {
3232
const child_path = it.next() orelse unreachable;
3333

3434
// use posix convention: 0 read, 1 write
35-
var pipe: if (builtin.os.tag == .windows) [2]?windows.HANDLE else [2]os.fd_t = undefined;
35+
var pipe: if (builtin.os.tag == .windows) [2]windows.HANDLE else [2]os.fd_t = undefined;
3636
if (builtin.os.tag == .windows) {
3737
const saAttr = windows.SECURITY_ATTRIBUTES{
3838
.nLength = @sizeOf(windows.SECURITY_ATTRIBUTES),
@@ -47,12 +47,7 @@ pub fn main() !void {
4747

4848
// write read side of pipe to string + add to spawn command
4949
var buf: [handleCharSize]u8 = comptime [_]u8{0} ** handleCharSize;
50-
const s_handle =
51-
if (builtin.os.tag == .windows)
52-
try handleToString(pipe[0].?, &buf)
53-
else
54-
try handleToString(pipe[0], &buf);
55-
50+
const s_handle = try handleToString(pipe[0], &buf);
5651
var child_proc = ChildProcess.init(
5752
&.{ child_path, s_handle },
5853
gpa,
@@ -62,15 +57,12 @@ pub fn main() !void {
6257
if (os.hasPosixSpawn) child_proc.posix_actions = try os.posix_spawn.Actions.init();
6358
defer if (os.hasPosixSpawn) child_proc.posix_actions.?.deinit();
6459
if (os.hasPosixSpawn) try child_proc.posix_actions.?.close(pipe[1]); // TODO: This is not closed in child
65-
defer if (builtin.os.tag == .windows)
66-
os.close(pipe[0].?)
67-
else
68-
os.close(pipe[0]);
60+
defer os.close(pipe[0]);
6961

7062
try child_proc.spawn();
7163
}
7264

73-
// call fcntl on Unixes to disable handle inheritance
65+
// call fcntl on Unixes to disable handle inheritance (windows one is per default not enabled)
7466
if (builtin.os.tag != .windows) {
7567
try std.os.disableFileInheritance(pipe[1]);
7668
}
@@ -85,11 +77,7 @@ pub fn main() !void {
8577
try std.testing.expect((fcntl_flags & os.FD_CLOEXEC) != 0);
8678
}
8779

88-
var file_out = if (builtin.target.os.tag == .windows)
89-
std.fs.File{ .handle = pipe[1].? }
90-
else
91-
std.fs.File{ .handle = pipe[1] };
92-
80+
var file_out = std.fs.File{ .handle = pipe[1] };
9381
defer file_out.close();
9482
const file_out_writer = file_out.writer();
9583
try file_out_writer.writeAll("test123\x17"); // ETB = \x17

0 commit comments

Comments
 (0)