Introduce signals module for signal handling

This commit is contained in:
2026-08-03 22:36:13 +02:00
parent 2688cacfa7
commit a8ff9d95bb
2 changed files with 57 additions and 2 deletions

View File

@@ -3,10 +3,12 @@ pub const deps = struct {
};
pub const config = @import("config/config.zig");
pub const memory = @import("memory/util.zig");
pub const config = @import("config/config.zig");
pub const memory = @import("memory/util.zig");
pub const signals = @import("signals/models.zig");
test {
_ = config;
_ = memory;
_ = signals;
}

53
src/signals/models.zig Normal file
View File

@@ -0,0 +1,53 @@
const std = @import("std");
const posix = std.posix;
const linux = std.os.linux;
pub const Event = enum {
none, // Sentinel
shutdown, // SIGTERM, SIGINT
reload, // SIGHUP
pub fn fromSignal(sig: i32) ?Event
{
const signal: posix.SIG = @enumFromInt(@as(u32, @intCast(sig)));
return switch (signal) {
.TERM,
.INT => .shutdown,
.HUP => .reload,
else => null,
};
}
};
pub const SignalFd = struct {
fd: posix.fd_t,
pub fn init() !SignalFd
{
var mask = linux.sigemptyset();
linux.sigaddset(&mask, posix.SIG.TERM);
linux.sigaddset(&mask, posix.SIG.INT);
linux.sigaddset(&mask, posix.SIG.HUP);
posix.sigprocmask(posix.SIG.BLOCK, &mask, null);
const fd = try posix.signalfd(-1, &mask, linux.SFD.CLOEXEC);
return .{ .fd = fd };
}
pub fn deinit(self: SignalFd) void { _ = std.os.linux.close(self.fd); }
//
pub fn read(self: SignalFd) !Event
{
var info: linux.signalfd_siginfo = undefined;
_ = try posix.read(self.fd, std.mem.asBytes(&info));
return Event.fromSignal(@intCast(info.signo)) orelse .none;
}
};