diff --git a/src/root.zig b/src/root.zig index eb9a515..af16576 100644 --- a/src/root.zig +++ b/src/root.zig @@ -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; } diff --git a/src/signals/models.zig b/src/signals/models.zig new file mode 100644 index 0000000..67140cb --- /dev/null +++ b/src/signals/models.zig @@ -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; + } +};