authorgravatar for bblack@wikimedia.orgBrandon Black <bblack@wikimedia.org> 2025-09-12 07:20:08-05:00
committergravatar for bblack@wikimedia.orgBrandon Black <bblack@wikimedia.org> 2025-09-12 07:20:08-05:00
log6dbfba526f7b7cf1256c133d025b11409c97dda7
treed2b58d556ea8f61a38f87f6e98ce6556fa30fc93
parent04071d64bb83c0600d445a2f4b2541e68a6021bf

linux: Doc and check retval for no-fail pid calls

The switch from @bitCast() to @intCast() here safety-checks Linux's assertion that these 3 calls never return errors (negative values as pid_t). getppid() can legally return 0 if the parent is in a different pid namespace, but this is not an error.

1 files changed, 6 insertions(+), 3 deletions(-)

lib/std/os/linux.zig+6-3
...@@ -1839,15 +1839,18 @@ pub fn setsid() usize {...@@ -1839,15 +1839,18 @@ pub fn setsid() usize {
1839}1839}
18401840
1841pub fn getpid() pid_t {1841pub fn getpid() pid_t {
1842 return @bitCast(@as(u32, @truncate(syscall0(.getpid))));1842 // Casts result to a pid_t, safety-checking >= 0, because getpid() cannot fail
1843 return @intCast(@as(u32, @truncate(syscall0(.getpid))));
1843}1844}
18441845
1845pub fn getppid() pid_t {1846pub fn getppid() pid_t {
1846 return @bitCast(@as(u32, @truncate(syscall0(.getppid))));1847 // Casts result to a pid_t, safety-checking >= 0, because getppid() cannot fail
1848 return @intCast(@as(u32, @truncate(syscall0(.getppid))));
1847}1849}
18481850
1849pub fn gettid() pid_t {1851pub fn gettid() pid_t {
1850 return @bitCast(@as(u32, @truncate(syscall0(.gettid))));1852 // Casts result to a pid_t, safety-checking >= 0, because gettid() cannot fail
1853 return @intCast(@as(u32, @truncate(syscall0(.gettid))));
1851}1854}
18521855
1853pub fn sigprocmask(flags: u32, noalias set: ?*const sigset_t, noalias oldset: ?*sigset_t) usize {1856pub fn sigprocmask(flags: u32, noalias set: ?*const sigset_t, noalias oldset: ?*sigset_t) usize {