aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorrsiddharth <s@ricketyspace.net>2020-04-19 16:16:57 -0400
committerrsiddharth <s@ricketyspace.net>2020-04-19 16:18:22 -0400
commit97501d0d3118c558703b78fd47ae571beecbcdb3 (patch)
tree6830d454d33f98d759ca733cdd436d9fa35a3ddf
parenta5a140ae81567afd2b8fb06a2f92178826bda840 (diff)
downloadnserver-97501d0d3118c558703b78fd47ae571beecbcdb3.tar.gz
nserver-97501d0d3118c558703b78fd47ae571beecbcdb3.tar.bz2
nserver-97501d0d3118c558703b78fd47ae571beecbcdb3.tar.xz
nserver-97501d0d3118c558703b78fd47ae571beecbcdb3.zip
nserver.c: Update main.
* bin/nserver.c (main): Setup sigaction. (setup_sigaction): Helper fuunction to setup sigaction. (handler_sigchild): Add sigaction handler.
-rw-r--r--bin/nserver.c30
1 files changed, 30 insertions, 0 deletions
diff --git a/bin/nserver.c b/bin/nserver.c
index 8c05e7d..d2452be 100644
--- 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);
}