Use sigaction(), not signal(), to catch signals on UN*X.
We don't want to rely on signal() not resetting the signal handler. The
Single UNIX Specification says it's implementation-defined whether that
happens. Use sigaction(), instead, where, if you don't request that the
signal handler be reset, it's not reset.
diff --git a/rpcapd/rpcapd.c b/rpcapd/rpcapd.c
index 7217b79..75d5ad0 100755
--- a/rpcapd/rpcapd.c
+++ b/rpcapd/rpcapd.c
@@ -150,6 +150,9 @@
int isdaemon = 0; // Not null if the user wants to run this program as a daemon
int retval; // keeps the returning value from several functions
char errbuf[PCAP_ERRBUF_SIZE + 1]; // keeps the error string, prior to be printed
+#ifndef _WIN32
+ struct sigaction action;
+#endif
savefile[0] = 0;
loadfile[0] = 0;
@@ -293,9 +296,16 @@
exit(2);
}
#else
- // SIGTERM (i.e. kill -15) is not generated in Win32, although it is included for ANSI compatibility
- signal(SIGTERM, main_terminate);
- signal(SIGCHLD, main_reap_children);
+ memset(&action, 0, sizeof (action));
+ action.sa_handler = main_terminate;
+ action.sa_flags = 0;
+ sigemptyset(&action.sa_mask);
+ sigaction(SIGTERM, &action, NULL);
+ memset(&action, 0, sizeof (action));
+ action.sa_handler = main_reap_children;
+ action.sa_flags = 0;
+ sigemptyset(&action.sa_mask);
+ sigaction(SIGCHLD, &action, NULL);
// Ignore SIGPIPE - we'll get EPIPE when trying to write to a closed
// connection, we don't want to get killed by a signal in that case
signal(SIGPIPE, SIG_IGN);
@@ -316,7 +326,11 @@
setsid();
// generated under unix with 'kill -HUP', needed to reload the configuration
- signal(SIGHUP, fileconf_read);
+ memset(&action, 0, sizeof (action));
+ action.sa_handler = fileconf_read;
+ action.sa_flags = 0;
+ sigemptyset(&action.sa_mask);
+ sigaction(SIGHUP, &action, NULL);
if ((pid = fork()) != 0)
exit(0); // First child terminates
@@ -345,11 +359,19 @@
{
#ifndef _WIN32
// Enable the catching of Ctrl+C
- signal(SIGINT, main_terminate);
+ memset(&action, 0, sizeof (action));
+ action.sa_handler = main_terminate;
+ action.sa_flags = 0;
+ sigemptyset(&action.sa_mask);
+ sigaction(SIGINT, &action, NULL);
// generated under unix with 'kill -HUP', needed to reload the configuration
// We do not have this kind of signal in Win32
- signal(SIGHUP, fileconf_read);
+ memset(&action, 0, sizeof (action));
+ action.sa_handler = fileconf_read;
+ action.sa_flags = 0;
+ sigemptyset(&action.sa_mask);
+ sigaction(SIGHUP, &action, NULL);
#endif
printf("Press CTRL + C to stop the server...\n");