X-Git-Url: https://git.tcpdump.org/tcpdump/blobdiff_plain/6e2695e9e0518960a90af1bc74bea4938259ac8c..refs/pull/433/head:/setsignal.c diff --git a/setsignal.c b/setsignal.c index 5269a7f1..1bcc032e 100644 --- a/setsignal.c +++ b/setsignal.c @@ -19,16 +19,11 @@ * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. */ -#ifndef lint -static const char rcsid[] = - "@(#) $Header: /tcpdump/master/tcpdump/setsignal.c,v 1.7 2000-07-11 00:49:03 assar Exp $ (LBL)"; -#endif - #ifdef HAVE_CONFIG_H #include "config.h" #endif -#include +#include #include #ifdef HAVE_SIGACTION @@ -42,15 +37,33 @@ static const char rcsid[] = #include "setsignal.h" /* - * An os independent signal() with BSD semantics, e.g. the signal - * catcher is restored following service of the signal. + * An OS-independent signal() with, whenever possible, partial BSD + * semantics, i.e. the signal handler is restored following service + * of the signal, but system calls are *not* restarted, so that if + * "pcap_breakloop()" is called in a signal handler in a live capture, + * the read/recvfrom/whatever in the live capture doesn't get restarted, + * it returns -1 and sets "errno" to EINTR, so we can break out of the + * live capture loop. + * + * We use "sigaction()" if available. We don't specify that the signal + * should restart system calls, so that should always do what we want. * - * When sigset() is available, signal() has SYSV semantics and sigset() - * has BSD semantics and call interface. Unfortunately, Linux does not - * have sigset() so we use the more complicated sigaction() interface - * there. + * Otherwise, if "sigset()" is available, it probably has BSD semantics + * while "signal()" has traditional semantics, so we use "sigset()"; it + * might cause system calls to be restarted for the signal, however. + * I don't know whether, in any systems where it did cause system calls to + * be restarted, there was a way to ask it not to do so; there may no + * longer be any interesting systems without "sigaction()", however, + * and, if there are, they might have "sigvec()" with SV_INTERRUPT + * (which I think first appeared in 4.3BSD). * - * Did I mention that signals suck? + * Otherwise, we use "signal()" - which means we might get traditional + * semantics, wherein system calls don't get restarted *but* the + * signal handler is reset to SIG_DFL and the signal is not blocked, + * so that a subsequent signal would kill the process immediately. + * + * Did I mention that signals suck? At least in POSIX-compliant systems + * they suck far less, as those systems have "sigaction()". */ RETSIGTYPE (*setsignal (int sig, RETSIGTYPE (*func)(int)))(int) @@ -60,9 +73,8 @@ RETSIGTYPE memset(&new, 0, sizeof(new)); new.sa_handler = func; -#ifdef SA_RESTART - new.sa_flags |= SA_RESTART; -#endif + if (sig == SIGCHLD) + new.sa_flags = SA_RESTART; if (sigaction(sig, &new, &old) < 0) return (SIG_ERR); return (old.sa_handler);