authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-01-18 11:49:42-08:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2021-01-18 11:49:42-08:00
log0353c9601a7a72cf453738818d0fba4e980512ba
tree833e668796e537ddb51bbd59df2612068ffbe9e3
parentc3dadfa95b01d140460eb3d3d47d13859302f298
parent6418f9ae91e2444da0743ea0fb0c81fc07ea86a9
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #7814 from LemonBoy/fix-7760

std: Fixed pipe2 fallback

7 files changed, 47 insertions(+), 20 deletions(-)

lib/std/c.zig-1
...@@ -108,7 +108,6 @@ pub extern "c" fn fork() c_int;...@@ -108,7 +108,6 @@ pub extern "c" fn fork() c_int;
108pub extern "c" fn access(path: [*:0]const u8, mode: c_uint) c_int;108pub extern "c" fn access(path: [*:0]const u8, mode: c_uint) c_int;
109pub extern "c" fn faccessat(dirfd: fd_t, path: [*:0]const u8, mode: c_uint, flags: c_uint) c_int;109pub extern "c" fn faccessat(dirfd: fd_t, path: [*:0]const u8, mode: c_uint, flags: c_uint) c_int;
110pub extern "c" fn pipe(fds: *[2]fd_t) c_int;110pub extern "c" fn pipe(fds: *[2]fd_t) c_int;
111pub extern "c" fn pipe2(fds: *[2]fd_t, flags: u32) c_int;
112pub extern "c" fn mkdir(path: [*:0]const u8, mode: c_uint) c_int;111pub extern "c" fn mkdir(path: [*:0]const u8, mode: c_uint) c_int;
113pub extern "c" fn mkdirat(dirfd: fd_t, path: [*:0]const u8, mode: u32) c_int;112pub extern "c" fn mkdirat(dirfd: fd_t, path: [*:0]const u8, mode: u32) c_int;
114pub extern "c" fn symlink(existing: [*:0]const u8, new: [*:0]const u8) c_int;113pub extern "c" fn symlink(existing: [*:0]const u8, new: [*:0]const u8) c_int;
lib/std/c/dragonfly.zig+1
...@@ -13,6 +13,7 @@ pub fn _errno() *c_int {...@@ -13,6 +13,7 @@ pub fn _errno() *c_int {
13pub extern "c" fn getdents(fd: c_int, buf_ptr: [*]u8, nbytes: usize) usize;13pub extern "c" fn getdents(fd: c_int, buf_ptr: [*]u8, nbytes: usize) usize;
14pub extern "c" fn sigaltstack(ss: ?*stack_t, old_ss: ?*stack_t) c_int;14pub extern "c" fn sigaltstack(ss: ?*stack_t, old_ss: ?*stack_t) c_int;
15pub extern "c" fn getrandom(buf_ptr: [*]u8, buf_len: usize, flags: c_uint) isize;15pub extern "c" fn getrandom(buf_ptr: [*]u8, buf_len: usize, flags: c_uint) isize;
16pub extern "c" fn pipe2(fds: *[2]fd_t, flags: u32) c_int;
1617
17pub const dl_iterate_phdr_callback = fn (info: *dl_phdr_info, size: usize, data: ?*c_void) callconv(.C) c_int;18pub const dl_iterate_phdr_callback = fn (info: *dl_phdr_info, size: usize, data: ?*c_void) callconv(.C) c_int;
18pub extern "c" fn dl_iterate_phdr(callback: dl_iterate_phdr_callback, data: ?*c_void) c_int;19pub extern "c" fn dl_iterate_phdr(callback: dl_iterate_phdr_callback, data: ?*c_void) c_int;
lib/std/c/freebsd.zig+1
...@@ -14,6 +14,7 @@ pub extern "c" fn sigaltstack(ss: ?*stack_t, old_ss: ?*stack_t) c_int;...@@ -14,6 +14,7 @@ pub extern "c" fn sigaltstack(ss: ?*stack_t, old_ss: ?*stack_t) c_int;
14pub extern "c" fn getrandom(buf_ptr: [*]u8, buf_len: usize, flags: c_uint) isize;14pub extern "c" fn getrandom(buf_ptr: [*]u8, buf_len: usize, flags: c_uint) isize;
1515
16pub extern "c" fn pthread_getthreadid_np() c_int;16pub extern "c" fn pthread_getthreadid_np() c_int;
17pub extern "c" fn pipe2(fds: *[2]fd_t, flags: u32) c_int;
1718
18pub extern "c" fn posix_memalign(memptr: *?*c_void, alignment: usize, size: usize) c_int;19pub extern "c" fn posix_memalign(memptr: *?*c_void, alignment: usize, size: usize) c_int;
19pub extern "c" fn malloc_usable_size(?*const c_void) usize;20pub extern "c" fn malloc_usable_size(?*const c_void) usize;
lib/std/c/linux.zig+1
...@@ -86,6 +86,7 @@ pub extern "c" fn dl_iterate_phdr(callback: dl_iterate_phdr_callback, data: ?*c_...@@ -86,6 +86,7 @@ pub extern "c" fn dl_iterate_phdr(callback: dl_iterate_phdr_callback, data: ?*c_
86pub extern "c" fn sigaltstack(ss: ?*stack_t, old_ss: ?*stack_t) c_int;86pub extern "c" fn sigaltstack(ss: ?*stack_t, old_ss: ?*stack_t) c_int;
8787
88pub extern "c" fn memfd_create(name: [*:0]const u8, flags: c_uint) c_int;88pub extern "c" fn memfd_create(name: [*:0]const u8, flags: c_uint) c_int;
89pub extern "c" fn pipe2(fds: *[2]fd_t, flags: u32) c_int;
8990
90pub extern "c" fn fallocate(fd: fd_t, mode: c_int, offset: off_t, len: off_t) c_int;91pub extern "c" fn fallocate(fd: fd_t, mode: c_int, offset: off_t, len: off_t) c_int;
9192
lib/std/c/netbsd.zig+1
...@@ -16,6 +16,7 @@ pub extern "c" fn dl_iterate_phdr(callback: dl_iterate_phdr_callback, data: ?*c_...@@ -16,6 +16,7 @@ pub extern "c" fn dl_iterate_phdr(callback: dl_iterate_phdr_callback, data: ?*c_
1616
17pub extern "c" fn _lwp_self() lwpid_t;17pub extern "c" fn _lwp_self() lwpid_t;
1818
19pub extern "c" fn pipe2(fds: *[2]fd_t, flags: u32) c_int;
19pub extern "c" fn arc4random_buf(buf: [*]u8, len: usize) void;20pub extern "c" fn arc4random_buf(buf: [*]u8, len: usize) void;
20pub extern "c" fn __fstat50(fd: fd_t, buf: *Stat) c_int;21pub extern "c" fn __fstat50(fd: fd_t, buf: *Stat) c_int;
21pub extern "c" fn __stat50(path: [*:0]const u8, buf: *Stat) c_int;22pub extern "c" fn __stat50(path: [*:0]const u8, buf: *Stat) c_int;
lib/std/c/openbsd.zig+1
...@@ -17,6 +17,7 @@ pub extern "c" fn dl_iterate_phdr(callback: dl_iterate_phdr_callback, data: ?*c_...@@ -17,6 +17,7 @@ pub extern "c" fn dl_iterate_phdr(callback: dl_iterate_phdr_callback, data: ?*c_
17pub extern "c" fn arc4random_buf(buf: [*]u8, len: usize) void;17pub extern "c" fn arc4random_buf(buf: [*]u8, len: usize) void;
1818
19pub extern "c" fn getthrid() pid_t;19pub extern "c" fn getthrid() pid_t;
20pub extern "c" fn pipe2(fds: *[2]fd_t, flags: u32) c_int;
2021
21pub extern "c" fn getdents(fd: c_int, buf_ptr: [*]u8, nbytes: usize) usize;22pub extern "c" fn getdents(fd: c_int, buf_ptr: [*]u8, nbytes: usize) usize;
22pub extern "c" fn sigaltstack(ss: ?*stack_t, old_ss: ?*stack_t) c_int;23pub extern "c" fn sigaltstack(ss: ?*stack_t, old_ss: ?*stack_t) c_int;
lib/std/os.zig+42-19
...@@ -3755,31 +3755,54 @@ pub fn pipe() PipeError![2]fd_t {...@@ -3755,31 +3755,54 @@ pub fn pipe() PipeError![2]fd_t {
3755}3755}
37563756
3757pub fn pipe2(flags: u32) PipeError![2]fd_t {3757pub fn pipe2(flags: u32) PipeError![2]fd_t {
3758 if (comptime std.Target.current.isDarwin()) {3758 if (@hasDecl(system, "pipe2")) {
3759 var fds: [2]fd_t = try pipe();3759 var fds: [2]fd_t = undefined;
3760 if (flags == 0) return fds;3760 switch (errno(system.pipe2(&fds, flags))) {
3761 errdefer {3761 0 => return fds,
3762 close(fds[0]);
3763 close(fds[1]);
3764 }
3765 for (fds) |fd| switch (errno(system.fcntl(fd, F_SETFL, flags))) {
3766 0 => {},
3767 EINVAL => unreachable, // Invalid flags3762 EINVAL => unreachable, // Invalid flags
3768 EBADF => unreachable, // Always a race condition3763 EFAULT => unreachable, // Invalid fds pointer
3764 ENFILE => return error.SystemFdQuotaExceeded,
3765 EMFILE => return error.ProcessFdQuotaExceeded,
3769 else => |err| return unexpectedErrno(err),3766 else => |err| return unexpectedErrno(err),
3770 };3767 }
3768 }
3769
3770 var fds: [2]fd_t = try pipe();
3771 errdefer {
3772 close(fds[0]);
3773 close(fds[1]);
3774 }
3775
3776 if (flags == 0)
3771 return fds;3777 return fds;
3778
3779 // O_CLOEXEC is special, it's a file descriptor flag and must be set using
3780 // F_SETFD.
3781 if (flags & O_CLOEXEC != 0) {
3782 for (fds) |fd| {
3783 switch (errno(system.fcntl(fd, F_SETFD, @as(u32, FD_CLOEXEC)))) {
3784 0 => {},
3785 EINVAL => unreachable, // Invalid flags
3786 EBADF => unreachable, // Always a race condition
3787 else => |err| return unexpectedErrno(err),
3788 }
3789 }
3772 }3790 }
37733791
3774 var fds: [2]fd_t = undefined;3792 const new_flags = flags & ~@as(u32, O_CLOEXEC);
3775 switch (errno(system.pipe2(&fds, flags))) {3793 // Set every other flag affecting the file status using F_SETFL.
3776 0 => return fds,3794 if (new_flags != 0) {
3777 EINVAL => unreachable, // Invalid flags3795 for (fds) |fd| {
3778 EFAULT => unreachable, // Invalid fds pointer3796 switch (errno(system.fcntl(fd, F_SETFL, new_flags))) {
3779 ENFILE => return error.SystemFdQuotaExceeded,3797 0 => {},
3780 EMFILE => return error.ProcessFdQuotaExceeded,3798 EINVAL => unreachable, // Invalid flags
3781 else => |err| return unexpectedErrno(err),3799 EBADF => unreachable, // Always a race condition
3800 else => |err| return unexpectedErrno(err),
3801 }
3802 }
3782 }3803 }
3804
3805 return fds;
3783}3806}
37843807
3785pub const SysCtlError = error{3808pub const SysCtlError = error{