nserver

nihilistic stats server
Log | Files | Refs | README | LICENSE

commit 97501d0d3118c558703b78fd47ae571beecbcdb3
parent a5a140ae81567afd2b8fb06a2f92178826bda840
Author: rsiddharth <s@ricketyspace.net>
Date:   Sun, 19 Apr 2020 16:16:57 -0400

nserver.c: Update main.

* bin/nserver.c (main): Setup sigaction.
(setup_sigaction): Helper fuunction to setup sigaction.
(handler_sigchild): Add sigaction handler.

Diffstat:
bin/nserver.c | 30++++++++++++++++++++++++++++++
1 file changed, 30 insertions(+), 0 deletions(-)

diff --git a/bin/nserver.c b/bin/nserver.c @@ -1,13 +1,38 @@ /* SPDX-License-Identifier: BSD-3-Clause */ /* + * Copyright © 2010, Zed A. Shaw. * Copyright © 2020 rsiddharth <s@ricketyspace.net> */ #include <nserve.h> #include <nsocket.h> +#include <signal.h> +#include <sys/wait.h> #define BACKLOG 10 +void handle_sigchild(int sig) +{ + sig = 0; // ignore it + while(waitpid(-1, NULL, WNOHANG) > 0) { + } +} + +int setup_sigaction() +{ + struct sigaction sa = { + .sa_handler = handle_sigchild, + .sa_flags = SA_RESTART | SA_NOCLDSTOP + }; + sigemptyset(&sa.sa_mask); + int rc = sigaction(SIGCHLD, &sa, 0); + check(rc != -1, "signal handler setup failed."); + + return 0; + error: + return -1; +} + int main(void) { int rc = 0; @@ -16,6 +41,10 @@ int main(void) socklen_t sockaddr_c_len; pid_t pidc; + // create a sigaction that handles SIGCHLD + rc = setup_sigaction(); + check(rc != -1, "sigaction setup failed"); + sockfd_s = get_socket(); check(sockfd_s > 0, "nserver: unable to get socket"); @@ -30,6 +59,7 @@ int main(void) check(pidc != -1, "nserver: fork failed"); if (pidc == 0) { + close(sockfd_s); nserve(sockfd_c); }