139297Sfenner/* 239297Sfenner * Copyright (c) 1997 339297Sfenner * The Regents of the University of California. All rights reserved. 439297Sfenner * 539297Sfenner * Redistribution and use in source and binary forms, with or without 639297Sfenner * modification, are permitted provided that: (1) source code distributions 739297Sfenner * retain the above copyright notice and this paragraph in its entirety, (2) 839297Sfenner * distributions including binary code include the above copyright notice and 939297Sfenner * this paragraph in its entirety in the documentation or other materials 1039297Sfenner * provided with the distribution, and (3) all advertising materials mentioning 1139297Sfenner * features or use of this software display the following acknowledgement: 1239297Sfenner * ``This product includes software developed by the University of California, 1339297Sfenner * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of 1439297Sfenner * the University nor the names of its contributors may be used to endorse 1539297Sfenner * or promote products derived from this software without specific prior 1639297Sfenner * written permission. 1739297Sfenner * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED 1839297Sfenner * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF 1939297Sfenner * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 2039297Sfenner */ 2139297Sfenner 2239297Sfenner#ifndef lint 23127668Sbmsstatic const char rcsid[] _U_ = 24190207Srpaulo "@(#) $Header: /tcpdump/master/tcpdump/setsignal.c,v 1.11 2003-11-16 09:36:42 guy Exp $ (LBL)"; 2539297Sfenner#endif 2639297Sfenner 2756893Sfenner#ifdef HAVE_CONFIG_H 2856893Sfenner#include "config.h" 2956893Sfenner#endif 3056893Sfenner 31127668Sbms#include <tcpdump-stdinc.h> 3239297Sfenner 3339297Sfenner#include <signal.h> 3439297Sfenner#ifdef HAVE_SIGACTION 3539297Sfenner#include <string.h> 3639297Sfenner#endif 3739297Sfenner 3839297Sfenner#ifdef HAVE_OS_PROTO_H 3939297Sfenner#include "os-proto.h" 4039297Sfenner#endif 4139297Sfenner 4239297Sfenner#include "setsignal.h" 4339297Sfenner 4439297Sfenner/* 45127668Sbms * An OS-independent signal() with, whenever possible, partial BSD 46127668Sbms * semantics, i.e. the signal handler is restored following service 47127668Sbms * of the signal, but system calls are *not* restarted, so that if 48127668Sbms * "pcap_breakloop()" is called in a signal handler in a live capture, 49127668Sbms * the read/recvfrom/whatever in the live capture doesn't get restarted, 50127668Sbms * it returns -1 and sets "errno" to EINTR, so we can break out of the 51127668Sbms * live capture loop. 5239297Sfenner * 53127668Sbms * We use "sigaction()" if available. We don't specify that the signal 54127668Sbms * should restart system calls, so that should always do what we want. 5539297Sfenner * 56127668Sbms * Otherwise, if "sigset()" is available, it probably has BSD semantics 57127668Sbms * while "signal()" has traditional semantics, so we use "sigset()"; it 58127668Sbms * might cause system calls to be restarted for the signal, however. 59127668Sbms * I don't know whether, in any systems where it did cause system calls to 60127668Sbms * be restarted, there was a way to ask it not to do so; there may no 61127668Sbms * longer be any interesting systems without "sigaction()", however, 62127668Sbms * and, if there are, they might have "sigvec()" with SV_INTERRUPT 63127668Sbms * (which I think first appeared in 4.3BSD). 64127668Sbms * 65127668Sbms * Otherwise, we use "signal()" - which means we might get traditional 66127668Sbms * semantics, wherein system calls don't get restarted *but* the 67127668Sbms * signal handler is reset to SIG_DFL and the signal is not blocked, 68127668Sbms * so that a subsequent signal would kill the process immediately. 69127668Sbms * 70127668Sbms * Did I mention that signals suck? At least in POSIX-compliant systems 71127668Sbms * they suck far less, as those systems have "sigaction()". 7239297Sfenner */ 7339297SfennerRETSIGTYPE 7439297Sfenner(*setsignal (int sig, RETSIGTYPE (*func)(int)))(int) 7539297Sfenner{ 7639297Sfenner#ifdef HAVE_SIGACTION 7739297Sfenner struct sigaction old, new; 7839297Sfenner 7939297Sfenner memset(&new, 0, sizeof(new)); 8039297Sfenner new.sa_handler = func; 8139297Sfenner if (sigaction(sig, &new, &old) < 0) 8239297Sfenner return (SIG_ERR); 8339297Sfenner return (old.sa_handler); 8439297Sfenner 8539297Sfenner#else 8639297Sfenner#ifdef HAVE_SIGSET 8739297Sfenner return (sigset(sig, func)); 8839297Sfenner#else 8939297Sfenner return (signal(sig, func)); 9039297Sfenner#endif 9139297Sfenner#endif 9239297Sfenner} 9339297Sfenner 94