Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 3.2k
std.child_process: enable non-standard streams#14152
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -288,6 +288,27 @@ pub fn close(fd: fd_t) void { | ||
| } | ||
| } | ||
| pub const windowsPtrDigits = 19; // log10(max(usize)) | ||
| pub const unixoidPtrDigits = 10; // log10(max(u32)) + 1 for sign | ||
| pub const handleCharSize = if (builtin.target.os.tag == .windows) windowsPtrDigits else unixoidPtrDigits; | ||
| pub fn handleToString(handle: fd_t, buf: []u8) std.fmt.BufPrintError![]u8 { | ||
| var s_handle: []u8 = undefined; | ||
| const handle_int = | ||
| // handle is *anyopaque or an integer on unix-likes Kernels. | ||
| if (builtin.target.os.tag == .windows) @ptrToInt(handle) else handle; | ||
| s_handle = try std.fmt.bufPrint(buf[0..], "{d}", .{handle_int}); | ||
| return s_handle; | ||
| } | ||
| pub fn stringToHandle(s_handle: []const u8) std.fmt.ParseIntError!std.os.fd_t { | ||
| var handle: std.os.fd_t = if (builtin.target.os.tag == .windows) | ||
| @intToPtr(windows.HANDLE, try std.fmt.parseInt(usize, s_handle, 10)) | ||
| else | ||
| try std.fmt.parseInt(std.os.fd_t, s_handle, 10); | ||
| return handle; | ||
| } | ||
| pub const FChmodError = error{ | ||
| AccessDenied, | ||
| InputOutput, | ||
| @@ -4866,6 +4887,44 @@ pub fn lseek_CUR_get(fd: fd_t) SeekError!u64 { | ||
| } | ||
| } | ||
| const IsInheritableError = FcntlError || windows.GetHandleInformationError; | ||
| /// Whether inheritence is enabled or CLOEXEC is not set. | ||
| pub inline fn isInheritable(handle: fd_t) IsInheritableError!bool { | ||
| if (builtin.os.tag == .windows) { | ||
| var handle_flags: windows.DWORD = undefined; | ||
| try windows.GetHandleInformation(handle, &handle_flags); | ||
| return handle_flags & windows.HANDLE_FLAG_INHERIT != 0; | ||
| } else { | ||
| const fcntl_flags = try fcntl(handle, F.GETFD, 0); | ||
| return fcntl_flags & FD_CLOEXEC == 0; | ||
| } | ||
| } | ||
| const EnableInheritanceError = FcntlError || windows.SetHandleInformationError; | ||
matu3ba marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| /// Enables inheritence or sets CLOEXEC. | ||
| pub inline fn enableInheritance(handle: fd_t) EnableInheritanceError!void { | ||
| if (builtin.os.tag == .windows) { | ||
| try windows.SetHandleInformation(handle, windows.HANDLE_FLAG_INHERIT, 1); | ||
| } else { | ||
| var flags = try fcntl(handle, F.GETFD, 0); | ||
| flags &= ~@as(u32, FD_CLOEXEC); | ||
| _ = try fcntl(handle, F.SETFD, flags); | ||
| } | ||
| } | ||
| const DisableInheritanceError = FcntlError || windows.SetHandleInformationError; | ||
| /// Disables inheritence or unsets CLOEXEC. | ||
| pub inline fn disableInheritance(handle: fd_t) DisableInheritanceError!void { | ||
| if (builtin.os.tag == .windows) { | ||
| try windows.SetHandleInformation(handle, windows.HANDLE_FLAG_INHERIT, 0); | ||
| } else { | ||
| _ = try fcntl(handle, F.SETFD, FD_CLOEXEC); | ||
| } | ||
| } | ||
| pub const FcntlError = error{ | ||
| PermissionDenied, | ||
| FileBusy, | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -228,6 +228,16 @@ pub fn DeviceIoControl( | ||
| } | ||
| } | ||
| pub const GetHandleInformationError = error{Unexpected}; | ||
| pub fn GetHandleInformation(h: HANDLE, flags: *DWORD) GetHandleInformationError!void { | ||
| if (kernel32.GetHandleInformation(h, flags) == 0) { | ||
| switch (kernel32.GetLastError()) { | ||
| else => |err| return unexpectedError(err), | ||
| } | ||
| } | ||
| } | ||
| pub fn GetOverlappedResult(h: HANDLE, overlapped: *OVERLAPPED, wait: bool) !DWORD { | ||
| var bytes: DWORD = undefined; | ||
| if (kernel32.GetOverlappedResult(h, overlapped, &bytes, @boolToInt(wait)) == 0) { | ||
| @@ -1590,6 +1600,45 @@ pub fn GetEnvironmentVariableW(lpName: LPWSTR, lpBuffer: [*]u16, nSize: DWORD) G | ||
| return rc; | ||
| } | ||
| // zig fmt: off | ||
ContributorAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I kept these inside for #14251, but they are generally useful for other users as well short of advanced list attributes. | ||
| pub const PROCESS_CREATION_FLAGS = enum(u32) { | ||
| // <- gap here -> | ||
| DEBUG_PROCESS = 0x0000_0001, | ||
| DEBUG_ONLY_THIS_PROCESS = 0x0000_0002, | ||
| CREATE_SUSPENDED = 0x0000_0004, | ||
| DETACHED_PROCESS = 0x0000_0008, | ||
| CREATE_NEW_CONSOLE = 0x0000_0010, | ||
| NORMAL_PRIORITY_CLASS = 0x0000_0020, | ||
| IDLE_PRIORITY_CLASS = 0x0000_0040, | ||
| HIGH_PRIORITY_CLASS = 0x0000_0080, | ||
| REALTIME_PRIORITY_CLASS = 0x0000_0100, | ||
| CREATE_NEW_PROCESS_GROUP = 0x0000_0200, | ||
| CREATE_UNICODE_ENVIRONMENT = 0x0000_0400, | ||
| CREATE_SEPARATE_WOW_VDM = 0x0000_0800, | ||
| CREATE_SHARED_WOW_VDM = 0x0000_1000, | ||
| CREATE_FORCEDOS = 0x0000_2000, | ||
| BELOW_NORMAL_PRIORITY_CLASS = 0x0000_4000, | ||
| ABOVE_NORMAL_PRIORITY_CLASS = 0x0000_8000, | ||
| INHERIT_PARENT_AFFINITY = 0x0001_0000, | ||
| INHERIT_CALLER_PRIORITY = 0x0002_0000, | ||
| CREATE_PROTECTED_PROCESS = 0x0004_0000, | ||
| EXTENDED_STARTUPINFO_PRESENT = 0x0008_0000, | ||
| PROCESS_MODE_BACKGROUND_BEGIN = 0x0010_0000, | ||
| PROCESS_MODE_BACKGROUND_END = 0x0020_0000, | ||
| CREATE_SECURE_PROCESS = 0x0040_0000, | ||
| // <- gap here -> | ||
| CREATE_BREAKAWAY_FROM_JOB = 0x0100_0000, | ||
| CREATE_PRESERVE_CODE_AUTHZ_LEVEL = 0x0200_0000, | ||
| CREATE_DEFAULT_ERROR_MODE = 0x0400_0000, | ||
| CREATE_NO_WINDOW = 0x0800_0000, | ||
| PROFILE_USER = 0x1000_0000, | ||
| PROFILE_KERNEL = 0x2000_0000, | ||
| PROFILE_SERVER = 0x4000_0000, | ||
| CREATE_IGNORE_SYSTEM_DEFAULT = 0x8000_0000, | ||
| _, | ||
| }; | ||
| // zig fmt: on | ||
| pub const CreateProcessError = error{ | ||
| FileNotFound, | ||
| AccessDenied, | ||
| @@ -2940,8 +2989,6 @@ pub const COORD = extern struct { | ||
| Y: SHORT, | ||
| }; | ||
| pub const CREATE_UNICODE_ENVIRONMENT = 1024; | ||
| pub const TLS_OUT_OF_INDEXES = 4294967295; | ||
| pub const IMAGE_TLS_DIRECTORY = extern struct { | ||
| StartAddressOfRawData: usize, | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| const Builder = @import("std").build.Builder; | ||
| pub fn build(b: *Builder) void { | ||
| const target = b.standardTargetOptions(.{}); | ||
| const optimize = b.standardOptimizeOption(.{}); | ||
| const child = b.addExecutable(.{ | ||
| .name = "child", | ||
| .root_source_file = .{ .path = "child.zig" }, | ||
| .target = target, | ||
| .optimize = optimize, | ||
| }); | ||
| const parent = b.addExecutable(.{ | ||
| .name = "parent", | ||
| .root_source_file = .{ .path = "parent.zig" }, | ||
| .target = target, | ||
| .optimize = optimize, | ||
| }); | ||
| const run_cmd = parent.run(); | ||
| run_cmd.addArtifactArg(child); | ||
| const test_step = b.step("test", "Test it"); | ||
| test_step.dependOn(&run_cmd.step); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| const std = @import("std"); | ||
| const builtin = @import("builtin"); | ||
| const windows = std.os.windows; | ||
| pub fn main() !void { | ||
| var general_purpose_allocator = std.heap.GeneralPurposeAllocator(.{}){}; | ||
| defer if (general_purpose_allocator.deinit()) @panic("found memory leaks"); | ||
| const gpa = general_purpose_allocator.allocator(); | ||
| var it = try std.process.argsWithAllocator(gpa); | ||
| defer it.deinit(); | ||
| _ = it.next() orelse unreachable; // skip binary name | ||
| const s_handle = it.next() orelse unreachable; | ||
| var file_handle = try std.os.stringToHandle(s_handle); | ||
| defer std.os.close(file_handle); | ||
| // child inherited the handle, so inheritance must be enabled | ||
| const is_inheritable = try std.os.isInheritable(file_handle); | ||
| std.debug.assert(is_inheritable); | ||
| try std.os.disableInheritance(file_handle); | ||
| var file_in = std.fs.File{ .handle = file_handle }; // read side of pipe | ||
| const file_in_reader = file_in.reader(); | ||
| const message = try file_in_reader.readUntilDelimiterAlloc(gpa, '\x17', 20_000); | ||
| defer gpa.free(message); | ||
| try std.testing.expectEqualSlices(u8, message, "test123"); | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I dont like this solution. These are very frequent operations, so it would be nice to have the sizes next to fd_t on each platform or to have a LUT. Opinions?