From 9dd18c6373cd68a978a4994a9cb7219d4d748471 Mon Sep 17 00:00:00 2001 From: Isaac Freund Date: Sun, 5 Jul 2020 14:49:34 +0200 Subject: [PATCH] server: exit cleanly on SIGINT and SIGTERM --- river/main.zig | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/river/main.zig b/river/main.zig index 6bf769f..d7875da 100644 --- a/river/main.zig +++ b/river/main.zig @@ -32,6 +32,8 @@ const usage: []const u8 = \\ ; +var server: Server = undefined; + pub fn main() anyerror!void { var startup_command: ?[]const u8 = null; { @@ -69,7 +71,15 @@ pub fn main() anyerror!void { log.info(.server, "initializing", .{}); - var server: Server = undefined; + // Setup a handler for SIGINT and SIGTERM so we can clean up properly + var act = std.os.Sigaction{ + .sigaction = handleSignal, + .mask = std.os.empty_sigset, + .flags = 0, + }; + std.os.sigaction(std.os.SIGINT, &act, null); + std.os.sigaction(std.os.SIGTERM, &act, null); + try server.init(); defer server.deinit(); @@ -94,3 +104,10 @@ fn printErrorExit(comptime format: []const u8, args: var) noreturn { stderr.print(format ++ "\n", args) catch std.os.exit(1); std.os.exit(1); } + +fn handleSignal(sig: i32, info: *const std.os.siginfo_t, ctx_ptr: ?*const c_void) callconv(.C) void { + switch (sig) { + std.os.SIGINT, std.os.SIGTERM => c.wl_display_terminate(server.wl_display), + else => unreachable, + } +}