-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsetsignal.c
52 lines (45 loc) · 1.1 KB
/
setsignal.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/*
* See the file "COPYING" in the main distribution directory for copyright.
*/
#include "zeek/zeek-config.h" /* must appear before first ifdef */
#include <sys/types.h>
#ifdef HAVE_MEMORY_H
#include <memory.h>
#endif
#include <signal.h>
#ifdef HAVE_SIGACTION
#include <string.h>
#endif
#include "setsignal.h"
/*
* An os independent signal() with BSD semantics, e.g. the signal
* catcher is restored following service of the signal.
*
* 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.
*
* Did I mention that signals suck?
*/
RETSIGTYPE
(*setsignal (int sig, RETSIGTYPE (*func)(int)))(int)
{
#ifdef HAVE_SIGACTION
struct sigaction old, new;
memset(&new, 0, sizeof(new));
new.sa_handler = func;
#ifdef SA_RESTART
new.sa_flags |= SA_RESTART;
#endif
if (sigaction(sig, &new, &old) < 0)
return (SIG_ERR);
return (old.sa_handler);
#else
#ifdef HAVE_SIGSET
return (sigset(sig, func));
#else
return (signal(sig, func));
#endif
#endif
}