add signal handling to make core more robust

This commit is contained in:
Emil Lerch 2023-05-12 17:11:02 -07:00
parent 5be44c911f
commit 07c7ba06ad
Signed by: lobo
GPG Key ID: A7B62D657EF764F8

View File

@ -168,24 +168,39 @@ fn exitApplication(
_: *const std.os.siginfo_t, _: *const std.os.siginfo_t,
_: ?*const anyopaque, _: ?*const anyopaque,
) callconv(.C) noreturn { ) callconv(.C) noreturn {
exitApp(); exitApp(0);
std.os.exit(0); std.os.exit(0);
} }
fn exitApp() void { fn exitApp(exitcode: u8) void {
std.io.getStdOut().writer().print("termination request: stopping watch\n", .{}) catch {}; if (exitcode == 0)
std.io.getStdOut().writer().print("termination request: stopping watch\n", .{}) catch {}
else
std.io.getStdErr().writer().print("abnormal termination: stopping watch\n", .{}) catch {};
watcher.stopWatch() catch @panic("could not stop watcher"); watcher.stopWatch() catch @panic("could not stop watcher");
std.io.getStdOut().writer().print("exiting application\n", .{}) catch {}; std.io.getStdOut().writer().print("exiting application\n", .{}) catch {};
watcher.deinit(); watcher.deinit();
std.os.exit(exitcode);
// joining threads will hang...we're ultimately in a signal handler. // joining threads will hang...we're ultimately in a signal handler.
// But everything is shut down cleanly now, so I don't think it hurts to // But everything is shut down cleanly now, so I don't think it hurts to
// just kill it all // just kill it all
// if (watcher_thread) |t| // if (watcher_thread) |t|
// t.join(); // t.join();
} }
fn installSignalHandler() !void {
var act = std.os.Sigaction{
.handler = .{ .sigaction = exitApplication },
.mask = std.os.empty_sigset,
.flags = (std.os.SA.SIGINFO | std.os.SA.RESTART | std.os.SA.RESETHAND),
};
try std.os.sigaction(std.os.SIG.INT, &act, null);
try std.os.sigaction(std.os.SIG.TERM, &act, null);
}
pub fn main() !void { pub fn main() !void {
defer exitApp(); defer exitApp(1);
// stdout is for the actual output of your application, for example if you // stdout is for the actual output of your application, for example if you
// are implementing gzip, then only the compressed bytes should be sent to // are implementing gzip, then only the compressed bytes should be sent to