const std = @import("std"); const posix = std.posix; const workflow_host = @import("../workflow/host.zig"); pub var stop_requested = std.atomic.Value(bool).init(false); pub var request_error_logged = std.atomic.Value(bool).init(false); pub const SignalGuard = struct { old_int: posix.Sigaction, old_term: posix.Sigaction, pub fn restore(self: SignalGuard) void { posix.sigaction(posix.SIG.INT, &self.old_int, null); posix.sigaction(posix.SIG.TERM, &self.old_term, null); } }; pub fn reset() void { stop_requested.store(false, .monotonic); request_error_logged.store(false, .monotonic); } pub fn loadStopRequested() bool { return stop_requested.load(.monotonic); } pub fn installSignalHandlers() !SignalGuard { const action: posix.Sigaction = .{ .handler = .{ .sigaction = handleSignal }, .mask = posix.sigemptyset(), .flags = posix.SA.SIGINFO | posix.SA.RESTART, }; var old_int: posix.Sigaction = undefined; var old_term: posix.Sigaction = undefined; posix.sigaction(posix.SIG.INT, &action, &old_int); posix.sigaction(posix.SIG.TERM, &action, &old_term); return .{ .old_int = old_int, .old_term = old_term, }; } fn handleSignal(sig: i32, info: *const posix.siginfo_t, ctx_ptr: ?*anyopaque) callconv(.c) void { _ = sig; _ = info; _ = ctx_ptr; stop_requested.store(true, .monotonic); } pub fn workflowHostCheckAbort(user_data: ?*anyopaque) callconv(.c) i32 { const host = workflow_host.hostStateFromUserData(user_data); return if (workflow_host.workflowHostShouldAbort(host, stop_requested.load(.monotonic))) 1 else 0; }