| ... | @@ -1,10 +1,8 @@ | ... | @@ -1,10 +1,8 @@ |
| 1 | //! This file provides the system interface functions for Linux matching those | 1 | //! This file provides the system interface functions for Linux matching those |
| 2 | //! that are provided by libc, whether or not libc is linked. The following | 2 | //! that are provided by libc, whether or not libc is linked. The following |
| 3 | //! abstractions are made: | 3 | //! abstractions are made: |
| 4 | //! * Work around kernel bugs and limitations. For example, see sendmmsg. | | |
| 5 | //! * Implement all the syscalls in the same way that libc functions will | 4 | //! * Implement all the syscalls in the same way that libc functions will |
| 6 | //! provide `rename` when only the `renameat` syscall exists. | 5 | //! provide `rename` when only the `renameat` syscall exists. |
| 7 | //! * Does not support POSIX thread cancellation. | | |
| 8 | const std = @import("../std.zig"); | 6 | const std = @import("../std.zig"); |
| 9 | const builtin = @import("builtin"); | 7 | const builtin = @import("builtin"); |
| 10 | const assert = std.debug.assert; | 8 | const assert = std.debug.assert; |
| ... | @@ -1768,7 +1766,7 @@ pub fn seteuid(euid: uid_t) usize { | ... | @@ -1768,7 +1766,7 @@ pub fn seteuid(euid: uid_t) usize { |
| 1768 | // id will not be changed. Since uid_t is unsigned, this wraps around to the | 1766 | // id will not be changed. Since uid_t is unsigned, this wraps around to the |
| 1769 | // max value in C. | 1767 | // max value in C. |
| 1770 | comptime assert(@typeInfo(uid_t) == .int and @typeInfo(uid_t).int.signedness == .unsigned); | 1768 | comptime assert(@typeInfo(uid_t) == .int and @typeInfo(uid_t).int.signedness == .unsigned); |
| 1771 | return setresuid(std.math.maxInt(uid_t), euid, std.math.maxInt(uid_t)); | 1769 | return setresuid(maxInt(uid_t), euid, maxInt(uid_t)); |
| 1772 | } | 1770 | } |
| 1773 | | 1771 | |
| 1774 | pub fn setegid(egid: gid_t) usize { | 1772 | pub fn setegid(egid: gid_t) usize { |
| ... | @@ -1779,7 +1777,7 @@ pub fn setegid(egid: gid_t) usize { | ... | @@ -1779,7 +1777,7 @@ pub fn setegid(egid: gid_t) usize { |
| 1779 | // id will not be changed. Since gid_t is unsigned, this wraps around to the | 1777 | // id will not be changed. Since gid_t is unsigned, this wraps around to the |
| 1780 | // max value in C. | 1778 | // max value in C. |
| 1781 | comptime assert(@typeInfo(uid_t) == .int and @typeInfo(uid_t).int.signedness == .unsigned); | 1779 | comptime assert(@typeInfo(uid_t) == .int and @typeInfo(uid_t).int.signedness == .unsigned); |
| 1782 | return setresgid(std.math.maxInt(gid_t), egid, std.math.maxInt(gid_t)); | 1780 | return setresgid(maxInt(gid_t), egid, maxInt(gid_t)); |
| 1783 | } | 1781 | } |
| 1784 | | 1782 | |
| 1785 | pub fn getresuid(ruid: *uid_t, euid: *uid_t, suid: *uid_t) usize { | 1783 | pub fn getresuid(ruid: *uid_t, euid: *uid_t, suid: *uid_t) usize { |
| ... | @@ -2013,44 +2011,13 @@ pub fn sendmsg(fd: i32, msg: *const msghdr_const, flags: u32) usize { | ... | @@ -2013,44 +2011,13 @@ pub fn sendmsg(fd: i32, msg: *const msghdr_const, flags: u32) usize { |
| 2013 | } | 2011 | } |
| 2014 | } | 2012 | } |
| 2015 | | 2013 | |
| | 2014 | /// Warning: libc is defined to have incompatible integer types with the |
| | 2015 | /// corresponding kernel data structures for this syscall. |
| | 2016 | /// |
| | 2017 | /// Warning: on 64-bit systems, if any message length would exceed `maxInt(i32)`, |
| | 2018 | /// number of bytes sent cannot be determined, because the kernel uses `ssize_t` |
| | 2019 | /// for `sendmsg` return value but `int` for the corresponding values here. |
| 2016 | pub fn sendmmsg(fd: i32, msgvec: [*]mmsghdr_const, vlen: u32, flags: u32) usize { | 2020 | pub fn sendmmsg(fd: i32, msgvec: [*]mmsghdr_const, vlen: u32, flags: u32) usize { |
| 2017 | if (@typeInfo(usize).int.bits > @typeInfo(@typeInfo(mmsghdr).@"struct".fields[1].type).int.bits) { | | |
| 2018 | // workaround kernel brokenness: | | |
| 2019 | // if adding up all iov_len overflows a i32 then split into multiple calls | | |
| 2020 | // see https://www.openwall.com/lists/musl/2014/06/07/5 | | |
| 2021 | const kvlen = if (vlen > IOV_MAX) IOV_MAX else vlen; // matches kernel | | |
| 2022 | var next_unsent: usize = 0; | | |
| 2023 | for (msgvec[0..kvlen], 0..) |*msg, i| { | | |
| 2024 | var size: i32 = 0; | | |
| 2025 | const msg_iovlen = @as(usize, @intCast(msg.hdr.iovlen)); // kernel side this is treated as unsigned | | |
| 2026 | for (msg.hdr.iov[0..msg_iovlen]) |iov| { | | |
| 2027 | if (iov.len > std.math.maxInt(i32) or @addWithOverflow(size, @as(i32, @intCast(iov.len)))[1] != 0) { | | |
| 2028 | // batch-send all messages up to the current message | | |
| 2029 | if (next_unsent < i) { | | |
| 2030 | const batch_size = i - next_unsent; | | |
| 2031 | const r = syscall4(.sendmmsg, @as(usize, @bitCast(@as(isize, fd))), @intFromPtr(&msgvec[next_unsent]), batch_size, flags); | | |
| 2032 | if (E.init(r) != .SUCCESS) return next_unsent; | | |
| 2033 | if (r < batch_size) return next_unsent + r; | | |
| 2034 | } | | |
| 2035 | // send current message as own packet | | |
| 2036 | const r = sendmsg(fd, &msg.hdr, flags); | | |
| 2037 | if (E.init(r) != .SUCCESS) return r; | | |
| 2038 | // Linux limits the total bytes sent by sendmsg to INT_MAX, so this cast is safe. | | |
| 2039 | msg.len = @as(u32, @intCast(r)); | | |
| 2040 | next_unsent = i + 1; | | |
| 2041 | break; | | |
| 2042 | } | | |
| 2043 | size += @intCast(iov.len); | | |
| 2044 | } | | |
| 2045 | } | | |
| 2046 | if (next_unsent < kvlen or next_unsent == 0) { // want to make sure at least one syscall occurs (e.g. to trigger MSG.EOR) | | |
| 2047 | const batch_size = kvlen - next_unsent; | | |
| 2048 | const r = syscall4(.sendmmsg, @as(usize, @bitCast(@as(isize, fd))), @intFromPtr(&msgvec[next_unsent]), batch_size, flags); | | |
| 2049 | if (E.init(r) != .SUCCESS) return r; | | |
| 2050 | return next_unsent + r; | | |
| 2051 | } | | |
| 2052 | return kvlen; | | |
| 2053 | } | | |
| 2054 | return syscall4(.sendmmsg, @as(usize, @bitCast(@as(isize, fd))), @intFromPtr(msgvec), vlen, flags); | 2021 | return syscall4(.sendmmsg, @as(usize, @bitCast(@as(isize, fd))), @intFromPtr(msgvec), vlen, flags); |
| 2055 | } | 2022 | } |
| 2056 | | 2023 | |
| ... | @@ -8613,7 +8580,7 @@ pub const PR = enum(i32) { | ... | @@ -8613,7 +8580,7 @@ pub const PR = enum(i32) { |
| 8613 | pub const SET_MM_MAP = 14; | 8580 | pub const SET_MM_MAP = 14; |
| 8614 | pub const SET_MM_MAP_SIZE = 15; | 8581 | pub const SET_MM_MAP_SIZE = 15; |
| 8615 | | 8582 | |
| 8616 | pub const SET_PTRACER_ANY = std.math.maxInt(c_ulong); | 8583 | pub const SET_PTRACER_ANY = maxInt(c_ulong); |
| 8617 | | 8584 | |
| 8618 | pub const FP_MODE_FR = 1 << 0; | 8585 | pub const FP_MODE_FR = 1 << 0; |
| 8619 | pub const FP_MODE_FRE = 1 << 1; | 8586 | pub const FP_MODE_FRE = 1 << 1; |