1 | #include <machine/rtems-bsd-user-space.h> |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright (c) 1997 |
---|
5 | * The Regents of the University of California. All rights reserved. |
---|
6 | * |
---|
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. |
---|
22 | */ |
---|
23 | |
---|
24 | #ifndef lint |
---|
25 | static const char rcsid[] _U_ = |
---|
26 | "@(#) $Header: /tcpdump/master/tcpdump/setsignal.c,v 1.11 2003-11-16 09:36:42 guy Exp $ (LBL)"; |
---|
27 | #endif |
---|
28 | |
---|
29 | #ifdef HAVE_CONFIG_H |
---|
30 | #include "config.h" |
---|
31 | #endif |
---|
32 | |
---|
33 | #include <tcpdump-stdinc.h> |
---|
34 | |
---|
35 | #include <signal.h> |
---|
36 | #ifdef HAVE_SIGACTION |
---|
37 | #include <string.h> |
---|
38 | #endif |
---|
39 | |
---|
40 | #ifdef HAVE_OS_PROTO_H |
---|
41 | #include "os-proto.h" |
---|
42 | #endif |
---|
43 | |
---|
44 | #include "setsignal.h" |
---|
45 | |
---|
46 | /* |
---|
47 | * An OS-independent signal() with, whenever possible, partial BSD |
---|
48 | * semantics, i.e. the signal handler is restored following service |
---|
49 | * of the signal, but system calls are *not* restarted, so that if |
---|
50 | * "pcap_breakloop()" is called in a signal handler in a live capture, |
---|
51 | * the read/recvfrom/whatever in the live capture doesn't get restarted, |
---|
52 | * it returns -1 and sets "errno" to EINTR, so we can break out of the |
---|
53 | * live capture loop. |
---|
54 | * |
---|
55 | * We use "sigaction()" if available. We don't specify that the signal |
---|
56 | * should restart system calls, so that should always do what we want. |
---|
57 | * |
---|
58 | * Otherwise, if "sigset()" is available, it probably has BSD semantics |
---|
59 | * while "signal()" has traditional semantics, so we use "sigset()"; it |
---|
60 | * might cause system calls to be restarted for the signal, however. |
---|
61 | * I don't know whether, in any systems where it did cause system calls to |
---|
62 | * be restarted, there was a way to ask it not to do so; there may no |
---|
63 | * longer be any interesting systems without "sigaction()", however, |
---|
64 | * and, if there are, they might have "sigvec()" with SV_INTERRUPT |
---|
65 | * (which I think first appeared in 4.3BSD). |
---|
66 | * |
---|
67 | * Otherwise, we use "signal()" - which means we might get traditional |
---|
68 | * semantics, wherein system calls don't get restarted *but* the |
---|
69 | * signal handler is reset to SIG_DFL and the signal is not blocked, |
---|
70 | * so that a subsequent signal would kill the process immediately. |
---|
71 | * |
---|
72 | * Did I mention that signals suck? At least in POSIX-compliant systems |
---|
73 | * they suck far less, as those systems have "sigaction()". |
---|
74 | */ |
---|
75 | RETSIGTYPE |
---|
76 | (*setsignal (int sig, RETSIGTYPE (*func)(int)))(int) |
---|
77 | { |
---|
78 | #ifdef HAVE_SIGACTION |
---|
79 | struct sigaction old, new; |
---|
80 | |
---|
81 | memset(&new, 0, sizeof(new)); |
---|
82 | new.sa_handler = func; |
---|
83 | if (sigaction(sig, &new, &old) < 0) |
---|
84 | return (SIG_ERR); |
---|
85 | return (old.sa_handler); |
---|
86 | |
---|
87 | #else |
---|
88 | #ifdef HAVE_SIGSET |
---|
89 | return (sigset(sig, func)); |
---|
90 | #else |
---|
91 | return (signal(sig, func)); |
---|
92 | #endif |
---|
93 | #endif |
---|
94 | } |
---|
95 | |
---|