Obsolete
/
libipc-old
Archived
3
0
Fork 0

ipcd: handling SIGHUP

master
Philippe Pittoli 2022-12-29 12:16:12 +01:00
parent 05a47b8473
commit c77ae35751
1 changed files with 35 additions and 18 deletions

View File

@ -2,9 +2,12 @@ const std = @import("std");
const hexdump = @import("./hexdump.zig"); const hexdump = @import("./hexdump.zig");
const net = std.net; const net = std.net;
const fmt = std.fmt; const fmt = std.fmt;
const os = std.os;
const ipc = @import("./main.zig"); const ipc = @import("./main.zig");
const builtin = @import("builtin");
const native_os = builtin.target.os.tag;
const print = std.debug.print; const print = std.debug.print;
const testing = std.testing; const testing = std.testing;
const print_eq = @import("./util.zig").print_eq; const print_eq = @import("./util.zig").print_eq;
@ -20,8 +23,6 @@ const print_eq = @import("./util.zig").print_eq;
// TODO: API should completely obfuscate the inner structures. // TODO: API should completely obfuscate the inner structures.
// Only structures in this file should be necessary. // Only structures in this file should be necessary.
var should_quit: bool = false;
fn create_service() !void { fn create_service() !void {
const config = .{.safety = true}; const config = .{.safety = true};
var gpa = std.heap.GeneralPurposeAllocator(config){}; var gpa = std.heap.GeneralPurposeAllocator(config){};
@ -36,11 +37,39 @@ fn create_service() !void {
// SERVER SIDE: creating a service. // SERVER SIDE: creating a service.
_ = try ctx.server_init(path); _ = try ctx.server_init(path);
// TODO: signal handler, to quit when asked // signal handler, to quit when asked
const S = struct {
var should_quit: bool = false;
fn handler(sig: i32, info: *const os.siginfo_t, _: ?*const anyopaque) callconv(.C) void {
print ("A signal has been received: {}\n", .{sig});
// Check that we received the correct signal.
switch (native_os) {
.netbsd => {
if (sig != os.SIG.HUP or sig != info.info.signo)
return;
},
else => {
if (sig != os.SIG.HUP and sig != info.signo)
return;
},
}
should_quit = true;
}
};
var sa = os.Sigaction{
.handler = .{ .sigaction = &S.handler },
.mask = os.empty_sigset, // Do not mask any signal.
.flags = os.SA.SIGINFO,
};
// Quit on SIGHUP (kill -1).
try os.sigaction(os.SIG.HUP, &sa, null);
var some_event: ipc.Event = undefined; var some_event: ipc.Event = undefined;
ctx.timer = 2000; // 2 seconds ctx.timer = 10000; // 10 seconds
while(true) { while(! S.should_quit) {
some_event = try ctx.wait_event(); some_event = try ctx.wait_event();
switch (some_event.t) { switch (some_event.t) {
.TIMER => { .TIMER => {
@ -100,21 +129,9 @@ fn create_service() !void {
break; break;
}, },
} }
if (should_quit) {
break;
}
} }
print("Goodbye\n", .{});
// Server.accept returns a net.Connection (handle = fd, addr = net.Address).
// var client = try server.accept();
// var buf: [4096]u8 = undefined;
// const n = try ctx.read_ (client, &buf);
// print("new client: {}\n", .{client});
// print("{} bytes: {s}\n", .{n, buf});
print("End the create_service function\n", .{});
} }
pub fn main() !u8 { pub fn main() !u8 {