summaryrefslogtreecommitdiff
path: root/legacy/ecore/src/e_ev_signal.c
blob: 438d3969ade22f12eab18061bbad27f8ed02eb63 (plain)
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
#include "e_events.h"
#include "e_ev_signal.h"
#include "e_str.h"
#include "e_mem.h"
#include "e_util.h"
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
#include <errno.h>

static void         e_ev_signal_free(void *event);
static void         e_ev_signal_handle_sigchld(int num);
static void         e_ev_signal_handle_sigusr1(int num);
static void         e_ev_signal_handle_sigusr2(int num);
static void         e_ev_signal_handle_sighup(int num);
static void         e_ev_signal_handle_sigpipe(int num);
static void         e_ev_signal_handle_sigsegv(int num);
static void         e_ev_signal_handle_sigfpe(int num);
static void         e_ev_signal_handle_sigill(int num);
static void         e_ev_signal_handle_sigbus(int num);

#ifdef HAVE_SIGSTKFLT
static void         e_ev_signal_handle_sigstkflt(int num);
#endif
#ifdef HAVE_SIGPWR
static void         e_ev_signal_handle_sigpwr(int num);
#endif
static void         e_ev_signal_handle_sigchld(int num);
static void         e_ev_signal_handle_all(pid_t pid);

static int          signal_chld_count = 0;
static int          signal_usr1_count = 0;
static int          signal_usr2_count = 0;
static int          signal_hup_count = 0;

/* freeing stuff */
static void
e_ev_signal_free(void *event)
{
   FREE(event);
}

/* signal handlers we can return from and add to signal recieved counts */
static void
e_ev_signal_handle_sigchld(int num)
{
   signal_chld_count++;
   return;
   num = 0;
}

static void
e_ev_signal_handle_sigusr1(int num)
{
   signal_usr1_count++;
   return;
   num = 0;
}

static void
e_ev_signal_handle_sigusr2(int num)
{
   signal_usr2_count++;
   return;
   num = 0;
}

static void
e_ev_signal_handle_sighup(int num)
{
   signal_hup_count++;
   return;
   num = 0;
}

/* signals to ignore */
static void
e_ev_signal_handle_sigpipe(int num)
{
   return;
   num = 0;
}

/* signal handlers we cant return from - so handle here */
static void
e_ev_signal_handle_sigsegv(int num)
{
   for (;;)
     {
	fprintf(stderr, "EEEEEEEEK SEGV - waiting 10 seconds\n");
	sleep(10);
     }
   /* EEK - can't return - bad */
   abort();
   num = 0;
}

static void
e_ev_signal_handle_sigfpe(int num)
{
   /* EEK - can't return - bad */
   abort();
   num = 0;
}

static void
e_ev_signal_handle_sigill(int num)
{
   /* EEK - can't return - bad */
   abort();
   num = 0;
}

static void
e_ev_signal_handle_sigbus(int num)
{
   /* EEK - can't return - bad */
   abort();
   num = 0;
}

#ifdef HAVE_SIGSTKFLT
static void
e_ev_signal_handle_sigstkflt(int num)
{
   /* EEK - can't return - bad */
   abort();
   return;
   num = 0;
}
#endif

static void
e_ev_signal_handle_sigint(int num)
{
   exit(0);
   return;
   num = 0;
}

static void
e_ev_signal_handle_sigquit(int num)
{
   exit(0);
   return;
   num = 0;
}

static void
e_ev_signal_handle_sigabrt(int num)
{
   abort();
   return;
   num = 0;
}

static void
e_ev_signal_handle_sigalrm(int num)
{
   return;
   num = 0;
}

static void
e_ev_signal_handle_sigterm(int num)
{
   exit(0);
   return;
   num = 0;
}

#ifdef HAVE_SIGPWR
static void
e_ev_signal_handle_sigpwr(int num)
{
   exit(0);
   return;
   num = 0;
}
#endif

static void
e_ev_signal_handle_all(pid_t pid_pass)
{
   int                 status;
   pid_t               pid;

   if (signal_chld_count > 0)
     {
	while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
	  {
	     if (WIFEXITED(status))
	       {
		  Ev_Child           *e;
		  int                 code;

		  code = WEXITSTATUS(status);
		  e = NEW(Ev_Child, 1);
		  e->pid = pid;
		  e->exit_code = code;
		  e_add_event(EV_CHILD, e, e_ev_signal_free);
	       }
	  }
	signal_chld_count = 0;
     }
   while (signal_usr1_count > 0)
     {
	Ev_User            *e;

	e = NEW(Ev_User, 1);
	e->num = 0;
	e->hup = 0;
	e_add_event(EV_USER, e, e_ev_signal_free);
	signal_usr1_count--;
     }
   while (signal_hup_count > 0)
     {
	Ev_User            *e;

	e = NEW(Ev_User, 1);
	e->num = 0;
	e->hup = 1;
	e_add_event(EV_USER, e, e_ev_signal_free);
	signal_hup_count--;
     }
   return;
   pid_pass = 0;
}

int
e_ev_signal_events_pending(void)
{
   return (signal_chld_count + signal_usr1_count + signal_hup_count);
}

void
e_ev_signal_init(void)
{
   struct sigaction    sa;

   e_add_event_pid(0, e_ev_signal_handle_all);

   sa.sa_handler = e_ev_signal_handle_sigchld;
   sa.sa_flags = SA_RESTART;
   sigemptyset(&sa.sa_mask);
   sigaction(SIGCHLD, &sa, (struct sigaction *)0);

   sa.sa_handler = e_ev_signal_handle_sigusr1;
   sa.sa_flags = SA_RESTART;
   sigemptyset(&sa.sa_mask);
   sigaction(SIGUSR1, &sa, (struct sigaction *)0);

   sa.sa_handler = e_ev_signal_handle_sigusr2;
   sa.sa_flags = SA_RESTART;
   sigemptyset(&sa.sa_mask);
   sigaction(SIGUSR2, &sa, (struct sigaction *)0);

   sa.sa_handler = e_ev_signal_handle_sighup;
   sa.sa_flags = SA_RESTART;
   sigemptyset(&sa.sa_mask);
   sigaction(SIGHUP, &sa, (struct sigaction *)0);

   sa.sa_handler = e_ev_signal_handle_sigpipe;
   sa.sa_flags = SA_RESTART;
   sigemptyset(&sa.sa_mask);
   sigaction(SIGPIPE, &sa, (struct sigaction *)0);

   sa.sa_handler = e_ev_signal_handle_sigsegv;
   sa.sa_flags = SA_RESTART;
   sigemptyset(&sa.sa_mask);
   sigaction(SIGSEGV, &sa, (struct sigaction *)0);

   sa.sa_handler = e_ev_signal_handle_sigfpe;
   sa.sa_flags = SA_RESTART;
   sigemptyset(&sa.sa_mask);
   sigaction(SIGFPE, &sa, (struct sigaction *)0);

   sa.sa_handler = e_ev_signal_handle_sigill;
   sa.sa_flags = SA_RESTART;
   sigemptyset(&sa.sa_mask);
   sigaction(SIGILL, &sa, (struct sigaction *)0);

   sa.sa_handler = e_ev_signal_handle_sigbus;
   sa.sa_flags = SA_RESTART;
   sigemptyset(&sa.sa_mask);
   sigaction(SIGBUS, &sa, (struct sigaction *)0);
#ifdef HAVE_SIGSTKFLT
   sa.sa_handler = e_ev_signal_handle_sigstkflt;
   sa.sa_flags = SA_RESTART;
   sigemptyset(&sa.sa_mask);
   sigaction(SIGSTKFLT, &sa, (struct sigaction *)0);
#endif
   sa.sa_handler = e_ev_signal_handle_sigint;
   sa.sa_flags = SA_RESTART;
   sigemptyset(&sa.sa_mask);
   sigaction(SIGINT, &sa, (struct sigaction *)0);

   sa.sa_handler = e_ev_signal_handle_sigquit;
   sa.sa_flags = SA_RESTART;
   sigemptyset(&sa.sa_mask);
   sigaction(SIGQUIT, &sa, (struct sigaction *)0);

   sa.sa_handler = e_ev_signal_handle_sigabrt;
   sa.sa_flags = SA_RESTART;
   sigemptyset(&sa.sa_mask);
   sigaction(SIGABRT, &sa, (struct sigaction *)0);

   sa.sa_handler = e_ev_signal_handle_sigalrm;
   sa.sa_flags = SA_RESTART;
   sigemptyset(&sa.sa_mask);
   sigaction(SIGALRM, &sa, (struct sigaction *)0);

   sa.sa_handler = e_ev_signal_handle_sigterm;
   sa.sa_flags = SA_RESTART;
   sigemptyset(&sa.sa_mask);
   sigaction(SIGTERM, &sa, (struct sigaction *)0);
#ifdef HAVE_SIGPWR
   sa.sa_handler = e_ev_signal_handle_sigpwr;
   sa.sa_flags = SA_RESTART;
   sigemptyset(&sa.sa_mask);
   sigaction(SIGPWR, &sa, (struct sigaction *)0);
#endif
}