From 9781e02eea5171defda8042ae0d79335222206c8 Mon Sep 17 00:00:00 2001 From: psznm Date: Sat, 22 Nov 2025 19:30:21 +0100 Subject: [PATCH 1/5] Add 0 to SIG enum --- lib/std/c.zig | 8 ++++++++ lib/std/os/linux.zig | 6 ++++++ 2 files changed, 14 insertions(+) diff --git a/lib/std/c.zig b/lib/std/c.zig index f22112a086..196460d54e 100644 --- a/lib/std/c.zig +++ b/lib/std/c.zig @@ -2662,6 +2662,8 @@ pub const SIG = switch (native_os) { pub const IOT: SIG = .ABRT; pub const POLL: SIG = .EMT; + /// Invalid signal. Used in kill to perform checking without sending signal. + INVAL = 0, /// hangup HUP = 1, /// interrupt @@ -2756,6 +2758,7 @@ pub const SIG = switch (native_os) { pub const RTMIN = 65; pub const RTMAX = 126; + INVAL = 0, HUP = 1, INT = 2, QUIT = 3, @@ -2821,6 +2824,7 @@ pub const SIG = switch (native_os) { pub const POLL: SIG = .IO; + INVAL = 0, HUP = 1, INT = 2, QUIT = 3, @@ -2895,6 +2899,7 @@ pub const SIG = switch (native_os) { pub const IOT: SIG = .ABRT; + INVAL = 0, HUP = 1, INT = 2, QUIT = 3, @@ -2941,6 +2946,7 @@ pub const SIG = switch (native_os) { pub const IOT: SIG = .ABRT; + INVAL = 0, HUP = 1, INT = 2, QUIT = 3, @@ -2989,6 +2995,7 @@ pub const SIG = switch (native_os) { pub const IOT: SIG = .ABRT; + INVAL = 0, HUP = 1, INT = 2, QUIT = 3, @@ -3035,6 +3042,7 @@ pub const SIG = switch (native_os) { pub const IOT: SIG = .ABRT; + INVAL = 0, HUP = 1, INT = 2, QUIT = 3, diff --git a/lib/std/os/linux.zig b/lib/std/os/linux.zig index 2029356a66..4bffaded77 100644 --- a/lib/std/os/linux.zig +++ b/lib/std/os/linux.zig @@ -3741,6 +3741,8 @@ pub const SIG = if (is_mips) enum(u32) { pub const IOT: SIG = .ABRT; pub const POLL: SIG = .IO; + INVAL = 0, + // /arch/mips/include/uapi/asm/signal.h#L25 HUP = 1, INT = 2, @@ -3787,6 +3789,8 @@ pub const SIG = if (is_mips) enum(u32) { pub const PWR: SIG = .LOST; pub const POLL: SIG = .IO; + /// Perform error checking without sending signal. + INVAL = 0, HUP = 1, INT = 2, QUIT = 3, @@ -3830,6 +3834,8 @@ pub const SIG = if (is_mips) enum(u32) { pub const POLL: SIG = .IO; pub const IOT: SIG = .ABRT; + /// Perform error checking without sending signal. + INVAL = 0, HUP = 1, INT = 2, QUIT = 3, From 5aff6cd7b607b93dd37f4af640cc203a1088fbe4 Mon Sep 17 00:00:00 2001 From: psznm Date: Sun, 23 Nov 2025 11:08:56 +0100 Subject: [PATCH 2/5] Add test for kill 0 --- lib/std/posix/test.zig | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lib/std/posix/test.zig b/lib/std/posix/test.zig index aaaaa1d948..4e06382536 100644 --- a/lib/std/posix/test.zig +++ b/lib/std/posix/test.zig @@ -980,3 +980,12 @@ const CommonOpenFlags = packed struct { return result; } }; + +test "kill 0 can be used to perform checks of sending signal" { + if (native_os == .wasi) return error.SkipZigTest; + if (native_os == .windows) return error.SkipZigTest; + posix.kill(posix.getpid(), .INVAL) catch |err| switch (err) { + posix.KillError.PermissionDenied => return, + else => return err, + }; +} From 2f738eb8770eb30c74442dd359d3a3a63370734e Mon Sep 17 00:00:00 2001 From: psznm Date: Mon, 24 Nov 2025 18:19:56 +0100 Subject: [PATCH 3/5] Standalone kill test --- lib/std/os/plan9.zig | 2 ++ lib/std/posix/test.zig | 9 --------- test/standalone/posix/build.zig | 3 +++ test/standalone/posix/kill.zig | 20 ++++++++++++++++++++ 4 files changed, 25 insertions(+), 9 deletions(-) create mode 100644 test/standalone/posix/kill.zig diff --git a/lib/std/os/plan9.zig b/lib/std/os/plan9.zig index 36bf83b21c..bd476c73ff 100644 --- a/lib/std/os/plan9.zig +++ b/lib/std/os/plan9.zig @@ -141,6 +141,8 @@ pub fn getpid() u32 { return tos.pid; } pub const SIG = struct { + /// Invalid signal. Used in kill to perform checking without sending signal. + pub const INVAL = 0; /// hangup pub const HUP = 1; /// interrupt diff --git a/lib/std/posix/test.zig b/lib/std/posix/test.zig index 4e06382536..aaaaa1d948 100644 --- a/lib/std/posix/test.zig +++ b/lib/std/posix/test.zig @@ -980,12 +980,3 @@ const CommonOpenFlags = packed struct { return result; } }; - -test "kill 0 can be used to perform checks of sending signal" { - if (native_os == .wasi) return error.SkipZigTest; - if (native_os == .windows) return error.SkipZigTest; - posix.kill(posix.getpid(), .INVAL) catch |err| switch (err) { - posix.KillError.PermissionDenied => return, - else => return err, - }; -} diff --git a/test/standalone/posix/build.zig b/test/standalone/posix/build.zig index 52ec99628d..bdfd793975 100644 --- a/test/standalone/posix/build.zig +++ b/test/standalone/posix/build.zig @@ -20,6 +20,9 @@ const cases = [_]Case{ .{ .src_path = "relpaths.zig", }, + .{ + .src_path = "kill.zig", + }, }; pub fn build(b: *std.Build) void { diff --git a/test/standalone/posix/kill.zig b/test/standalone/posix/kill.zig new file mode 100644 index 0000000000..14853cac7d --- /dev/null +++ b/test/standalone/posix/kill.zig @@ -0,0 +1,20 @@ +const std = @import("std"); +const posix = std.posix; + +pub fn main() !void { + try test_kill_zero_self_should_succeed(); + try test_kill_nonexistent(); +} + +fn test_kill_nonexistent() !void { + const impossible_pid: posix.pid_t = 1_999_999_999; + posix.kill(impossible_pid, .INVAL) catch |err| switch (err) { + posix.KillError.ProcessNotFound => return, + else => return err, + }; + return error.ProcessShouldHaveNotBeenFound; +} + +fn test_kill_zero_self_should_succeed() !void { + try posix.kill(posix.getpid(), .INVAL); +} From cdb85b2c87f0175491886e84c1fa06f449860a54 Mon Sep 17 00:00:00 2001 From: psznm Date: Mon, 24 Nov 2025 20:26:59 +0100 Subject: [PATCH 4/5] Limit test to OS with known maximum PID. Omit windows from kill 0 test --- test/standalone/posix/kill.zig | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test/standalone/posix/kill.zig b/test/standalone/posix/kill.zig index 14853cac7d..87cb864875 100644 --- a/test/standalone/posix/kill.zig +++ b/test/standalone/posix/kill.zig @@ -1,5 +1,7 @@ const std = @import("std"); const posix = std.posix; +const builtin = @import("builtin"); +const native_os = builtin.target.os.tag; pub fn main() !void { try test_kill_zero_self_should_succeed(); @@ -7,6 +9,10 @@ pub fn main() !void { } fn test_kill_nonexistent() !void { + if ((native_os != .linux) and (native_os != .macos)) return; + // Linux is limited by PID_MAX_LIMIT constant which is around 4 million + // MacOS maximum pid appears to be 99999 and not configurable. + // Others are unknown thus not tested. const impossible_pid: posix.pid_t = 1_999_999_999; posix.kill(impossible_pid, .INVAL) catch |err| switch (err) { posix.KillError.ProcessNotFound => return, @@ -16,5 +22,7 @@ fn test_kill_nonexistent() !void { } fn test_kill_zero_self_should_succeed() !void { + // Windows does not have kill -0 equivalent + if (native_os == .windows) return; try posix.kill(posix.getpid(), .INVAL); } From a79ccf9679fc166fd4582c248190f3a2a16eb418 Mon Sep 17 00:00:00 2001 From: psznm Date: Mon, 24 Nov 2025 21:00:50 +0100 Subject: [PATCH 5/5] use testing.expectError --- test/standalone/posix/kill.zig | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/test/standalone/posix/kill.zig b/test/standalone/posix/kill.zig index 87cb864875..3dd3bbc340 100644 --- a/test/standalone/posix/kill.zig +++ b/test/standalone/posix/kill.zig @@ -14,11 +14,7 @@ fn test_kill_nonexistent() !void { // MacOS maximum pid appears to be 99999 and not configurable. // Others are unknown thus not tested. const impossible_pid: posix.pid_t = 1_999_999_999; - posix.kill(impossible_pid, .INVAL) catch |err| switch (err) { - posix.KillError.ProcessNotFound => return, - else => return err, - }; - return error.ProcessShouldHaveNotBeenFound; + try std.testing.expectError(posix.KillError.ProcessNotFound, posix.kill(impossible_pid, .INVAL)); } fn test_kill_zero_self_should_succeed() !void {