forked from cotillion/cd-gamedriver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
signals.c
105 lines (86 loc) · 1.94 KB
/
signals.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <signal.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include "config.h"
#include "lint.h"
#include "interpret.h"
#include "backend.h"
void init_signals(void);
void deliver_signals(void);
static struct sig_disp
{
int sig;
char *name;
} disp[] = {
{SIGCHLD, "CHLD"},
{SIGHUP, "HUP"},
{SIGINT, "INT"},
{SIGQUIT, "QUIT"},
{SIGTERM, "TERM"},
{SIGUSR1, "USR1"},
{SIGUSR2, "USR2"},
{SIGTSTP, "TSTP"},
{SIGCONT, "CONT"},
{0, "UNKNOWN"},
};
#define MAX_SIGNALS (sizeof(disp)/sizeof(*disp))
static long long pending_signals = 0;
static sigset_t sigs_to_block;
static void
sig_handler(int sig)
{
int i;
/* When this happens our hname child has died
* and neads to be reaped */
if (sig == SIGCHLD)
{
waitpid(-1, NULL, WNOHANG);
}
for (i = 0; disp[i].sig && disp[i].sig != sig; i++)
;
pending_signals |= 1 << i;
}
void
init_signals()
{
int i;
struct sigaction act;
sigemptyset(&sigs_to_block);
for (i = 0; disp[i].sig; i++)
sigaddset(&sigs_to_block, disp[i].sig);
act.sa_handler = sig_handler;
act.sa_mask = sigs_to_block;
act.sa_flags = SA_RESTART;
for (i = 0; disp[i].sig; i++)
(void)sigaction(disp[i].sig, &act, NULL);
}
static struct task *signal_task;
static void
send_signals(void *x)
{
int i;
sigprocmask(SIG_BLOCK, &sigs_to_block, NULL);
for (i = 0; i < MAX_SIGNALS; i++)
if (pending_signals & (1 << i))
break;
pending_signals &= ~(1 << i);
sigprocmask(SIG_UNBLOCK, &sigs_to_block, NULL);
if (pending_signals)
reschedule_task(signal_task);
else
signal_task = 0;
if (i < MAX_SIGNALS)
{
push_string(disp[i].name, STRING_CSTRING);
apply_master_ob(M_EXTERNAL_SIGNAL, 1);
}
}
void
deliver_signals()
{
if (signal_task || !pending_signals)
return;
signal_task = create_task(send_signals, 0);
return;
}