Explicitly request literal mode after .Xr.
[netbsd-mini2440.git] / dist / tcpdump / setsignal.c
blob667ffa9d1114e0440e3641b46e8c4561607c766e
1 /* $NetBSD$ */
3 /*
4 * Copyright (c) 1997
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that: (1) source code distributions
9 * retain the above copyright notice and this paragraph in its entirety, (2)
10 * distributions including binary code include the above copyright notice and
11 * this paragraph in its entirety in the documentation or other materials
12 * provided with the distribution, and (3) all advertising materials mentioning
13 * features or use of this software display the following acknowledgement:
14 * ``This product includes software developed by the University of California,
15 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
16 * the University nor the names of its contributors may be used to endorse
17 * or promote products derived from this software without specific prior
18 * written permission.
19 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
20 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
24 #include <sys/cdefs.h>
25 #ifndef lint
26 #if 0
27 static const char rcsid[] _U_ =
28 "@(#) Header: /tcpdump/master/tcpdump/setsignal.c,v 1.11 2003/11/16 09:36:42 guy Exp (LBL)";
29 #else
30 __RCSID("$NetBSD: tcpdump2rcsid.ex,v 1.1 2001/06/25 20:09:58 itojun Exp $");
31 #endif
32 #endif
34 #ifdef HAVE_CONFIG_H
35 #include "config.h"
36 #endif
38 #include <tcpdump-stdinc.h>
40 #include <signal.h>
41 #ifdef HAVE_SIGACTION
42 #include <string.h>
43 #endif
45 #ifdef HAVE_OS_PROTO_H
46 #include "os-proto.h"
47 #endif
49 #include "setsignal.h"
52 * An OS-independent signal() with, whenever possible, partial BSD
53 * semantics, i.e. the signal handler is restored following service
54 * of the signal, but system calls are *not* restarted, so that if
55 * "pcap_breakloop()" is called in a signal handler in a live capture,
56 * the read/recvfrom/whatever in the live capture doesn't get restarted,
57 * it returns -1 and sets "errno" to EINTR, so we can break out of the
58 * live capture loop.
60 * We use "sigaction()" if available. We don't specify that the signal
61 * should restart system calls, so that should always do what we want.
63 * Otherwise, if "sigset()" is available, it probably has BSD semantics
64 * while "signal()" has traditional semantics, so we use "sigset()"; it
65 * might cause system calls to be restarted for the signal, however.
66 * I don't know whether, in any systems where it did cause system calls to
67 * be restarted, there was a way to ask it not to do so; there may no
68 * longer be any interesting systems without "sigaction()", however,
69 * and, if there are, they might have "sigvec()" with SV_INTERRUPT
70 * (which I think first appeared in 4.3BSD).
72 * Otherwise, we use "signal()" - which means we might get traditional
73 * semantics, wherein system calls don't get restarted *but* the
74 * signal handler is reset to SIG_DFL and the signal is not blocked,
75 * so that a subsequent signal would kill the process immediately.
77 * Did I mention that signals suck? At least in POSIX-compliant systems
78 * they suck far less, as those systems have "sigaction()".
80 RETSIGTYPE
81 (*setsignal (int sig, RETSIGTYPE (*func)(int)))(int)
83 #ifdef HAVE_SIGACTION
84 struct sigaction old, new;
86 memset(&new, 0, sizeof(new));
87 new.sa_handler = func;
88 if (sigaction(sig, &new, &old) < 0)
89 return (SIG_ERR);
90 return (old.sa_handler);
92 #else
93 #ifdef HAVE_SIGSET
94 return (sigset(sig, func));
95 #else
96 return (signal(sig, func));
97 #endif
98 #endif