Skip to content

Commit 548547d

Browse files
committed
portable pipe
1 parent 1924f10 commit 548547d

File tree

2 files changed

+20
-16
lines changed

2 files changed

+20
-16
lines changed

lib/std/child_process.zig

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1130,6 +1130,25 @@ pub const ChildProcess = struct {
11301130
}
11311131
};
11321132

1133+
const PortPipeReturn = if (builtin.os.tag == .windows) [2]windows.HANDLE else [2]os.fd_t;
1134+
1135+
/// Portable pipe creation without handle inheritance
1136+
pub fn portablePipe() !PortPipeReturn {
1137+
// TODO think how to offer user an interface to lpSecurityDescriptor
1138+
var pipe_new: PortPipeReturn = undefined;
1139+
if (builtin.os.tag == .windows) {
1140+
const saAttr = windows.SECURITY_ATTRIBUTES{
1141+
.nLength = @sizeOf(windows.SECURITY_ATTRIBUTES),
1142+
.bInheritHandle = windows.FALSE,
1143+
.lpSecurityDescriptor = null,
1144+
};
1145+
try windowsMakeAsyncPipe(&pipe_new[os.pipe_rd], &pipe_new[os.pipe_wr], &saAttr);
1146+
} else {
1147+
pipe_new = try os.pipe2(@as(u32, os.O.CLOEXEC));
1148+
}
1149+
return pipe_new;
1150+
}
1151+
11331152
/// Expects `app_buf` to contain exactly the app name, and `dir_buf` to contain exactly the dir path.
11341153
/// After return, `app_buf` will always contain exactly the app name and `dir_buf` will always contain exactly the dir path.
11351154
/// Note: `app_buf` should not contain any leading path separators.

test/standalone/childprocess_extrapipe/parent.zig

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,7 @@ pub fn main() !void {
1919
_ = it.next() orelse unreachable; // skip binary name
2020
const child_path = it.next() orelse unreachable;
2121

22-
// use posix convention: 0 read, 1 write
23-
var pipe: if (builtin.os.tag == .windows) [2]windows.HANDLE else [2]os.fd_t = undefined;
24-
if (builtin.os.tag == .windows) {
25-
const saAttr = windows.SECURITY_ATTRIBUTES{
26-
.nLength = @sizeOf(windows.SECURITY_ATTRIBUTES),
27-
.bInheritHandle = windows.TRUE,
28-
.lpSecurityDescriptor = null,
29-
};
30-
// create pipe without inheritance
31-
try child_process.windowsMakeAsyncPipe(&pipe[pipe_rd], &pipe[pipe_wr], &saAttr);
32-
} else {
33-
// we could save setting and and unsetting 1 pipe end, but this would
34-
// 1. allow more leak time and 2. makes things less consistent with windows
35-
// TODO: benchmarks
36-
pipe = try os.pipe2(@as(u32, os.O.CLOEXEC));
37-
}
22+
var pipe = try child_process.portablePipe();
3823

3924
// write read side of pipe to string + add to spawn command
4025
var buf: [os.handleCharSize]u8 = comptime [_]u8{0} ** os.handleCharSize;

0 commit comments

Comments
 (0)