summaryrefslogtreecommitdiff
path: root/src/lib/ecore/ecore_signal.c
blob: 98934e63b33faf1613fcaca23189976d2d735959 (plain) (blame)
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <inttypes.h>
#include <stdint.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
#include <unistd.h>
#include <assert.h>
#include <fcntl.h>
#include <errno.h>
#include <pthread.h>

#include "Ecore.h"
#include "ecore_private.h"

/* make mono happy - this is evil though... */
#undef SIGPWR

typedef struct _Pid_Info Pid_Info;

struct _Pid_Info
{
   pid_t pid;
   int fd;
};

static void _ecore_signal_exe_exit_delay(void *data, const Efl_Event *event);
static void _ecore_signal_waitpid(Eina_Bool once, siginfo_t info);
static void _ecore_signal_generic_free(void *data, void *event);

typedef void (*Signal_Handler)(int sig, siginfo_t *si, void *foo);

static int sig_pipe[2] = { -1, -1 }; // [0] == read, [1] == write
static Eo *sig_pipe_handler = NULL;
static Eina_Spinlock sig_pid_lock;
static Eina_List *sig_pid_info_list = NULL;

typedef struct _Signal_Data
{
   int sig;
   siginfo_t info;
} Signal_Data;

static Eina_Bool
_ecore_signal_pipe_read(Eo *obj)
{
   Signal_Data sdata;
   int ret;

   ret = read(sig_pipe[0], &sdata, sizeof(sdata));
   if (ret != sizeof(sdata)) return EINA_FALSE;
   switch (sdata.sig)
     {
      case SIGPIPE:
        break;
      case SIGALRM:
        break;
      case SIGCHLD:
        _ecore_signal_waitpid(EINA_FALSE, sdata.info);
        break;
      case SIGUSR1:
      case SIGUSR2:
          {
             Ecore_Event_Signal_User *e = _ecore_event_signal_user_new();
             if (e)
               {
                  if (sdata.sig == SIGUSR1) e->number = 1;
                  else e->number = 2;
                  e->data = sdata.info;
                  ecore_event_add(ECORE_EVENT_SIGNAL_USER, e,
                                  _ecore_signal_generic_free, NULL);
               }
             Eo *loop = efl_provider_find(obj, EFL_LOOP_CLASS);
             if (loop)
               {
                  if (sdata.sig == SIGUSR1)
                    efl_event_callback_call(loop, EFL_LOOP_EVENT_SIGNAL_USR1, NULL);
                  else
                    efl_event_callback_call(loop, EFL_LOOP_EVENT_SIGNAL_USR2, NULL);
               }
          }
        break;
      case SIGHUP:
          {
             Ecore_Event_Signal_Hup *e = _ecore_event_signal_hup_new();
             if (e)
               {
                  e->data = sdata.info;
                  ecore_event_add(ECORE_EVENT_SIGNAL_HUP, e,
                                  _ecore_signal_generic_free, NULL);
               }
             Eo *loop = efl_provider_find(obj, EFL_LOOP_CLASS);
             if (loop)
               efl_event_callback_call(loop, EFL_LOOP_EVENT_SIGNAL_HUP, NULL);
          }
        break;
      case SIGQUIT:
      case SIGINT:
      case SIGTERM:
          {
             Ecore_Event_Signal_Exit *e = _ecore_event_signal_exit_new();
             if (e)
               {
                  if (sdata.sig == SIGQUIT) e->quit = 1;
                  else if (sdata.sig == SIGINT) e->interrupt = 1;
                  else e->terminate = 1;
                  e->data = sdata.info;
                  ecore_event_add(ECORE_EVENT_SIGNAL_EXIT, e,
                                  _ecore_signal_generic_free, NULL);
               }
          }
        break;
#ifdef SIGPWR
      case SIGPWR:
          {
             Ecore_Event_Signal_Power *e = _ecore_event_signal_power_new();
             if (e)
               {
                  e->data = sdata.info;
                  ecore_event_add(ECORE_EVENT_SIGNAL_POWER, e,
                                  _ecore_signal_generic_free, NULL);
               }
          }
        break;
#endif
      default:
        break;
     }
   return EINA_TRUE;
}

static void
_ecore_signal_cb_read(void *data EINA_UNUSED, const Efl_Event *event EINA_UNUSED)
{
   while (_ecore_signal_pipe_read(event->object));
}

static void
_ecore_signal_cb_del(void *data EINA_UNUSED, const Efl_Event *event)
{
   if (event->object == sig_pipe_handler) sig_pipe_handler = NULL;
}

EFL_CALLBACKS_ARRAY_DEFINE(_event_watch,
                             { EFL_LOOP_HANDLER_EVENT_READ, _ecore_signal_cb_read },
                             { EFL_EVENT_DEL, _ecore_signal_cb_del });

static void
_ecore_signal_callback(int sig, siginfo_t *si, void *foo EINA_UNUSED)
{
   Signal_Data sdata;

   memset(&sdata, 0, sizeof(Signal_Data));
   sdata.sig = sig;
   sdata.info = *si;
   if (sdata.sig >= 0)
     {
        int err = errno;
        const ssize_t bytes = write(sig_pipe[1], &sdata, sizeof(sdata));
        if (EINA_UNLIKELY(bytes != sizeof(sdata)))
          {
             err = errno;
             ERR("write() failed: %s", strerror(err));
          }
        errno = err;
     }
}

static void
_ecore_signal_callback_set(int sig, Signal_Handler func)
{
   struct sigaction sa;

   sa.sa_sigaction = func;
   sa.sa_flags = SA_RESTART | SA_SIGINFO;
   sigemptyset(&sa.sa_mask);
   sigaction(sig, &sa, NULL);
}

static void
_signalhandler_setup(void)
{
   sigset_t newset;

   _ecore_signal_callback_set(SIGPIPE, _ecore_signal_callback);
   _ecore_signal_callback_set(SIGALRM, _ecore_signal_callback);
   _ecore_signal_callback_set(SIGCHLD, _ecore_signal_callback);
   _ecore_signal_callback_set(SIGUSR1, _ecore_signal_callback);
   _ecore_signal_callback_set(SIGUSR2, _ecore_signal_callback);
   _ecore_signal_callback_set(SIGHUP,  _ecore_signal_callback);
   _ecore_signal_callback_set(SIGQUIT, _ecore_signal_callback);
   _ecore_signal_callback_set(SIGINT,  _ecore_signal_callback);
   _ecore_signal_callback_set(SIGTERM, _ecore_signal_callback);
#ifdef SIGPWR
   _ecore_signal_callback_set(SIGPWR,  _ecore_signal_callback);
#endif

#ifndef _WIN32
   sigemptyset(&newset);
   sigaddset(&newset, SIGPIPE);
   sigaddset(&newset, SIGALRM);
   sigaddset(&newset, SIGCHLD);
   sigaddset(&newset, SIGUSR1);
   sigaddset(&newset, SIGUSR2);
   sigaddset(&newset, SIGHUP);
   sigaddset(&newset, SIGQUIT);
   sigaddset(&newset, SIGINT);
   sigaddset(&newset, SIGTERM);
# ifdef SIGPWR
   sigaddset(&newset, SIGPWR);
# endif
   pthread_sigmask(SIG_UNBLOCK, &newset, NULL);
#endif
}

static void
_ecore_signal_pipe_init(void)
{
   eina_spinlock_new(&sig_pid_lock);
   if (sig_pipe[0] == -1)
     {
        if (pipe(sig_pipe) != 0)
          {
             sig_pipe[0] = -1;
             return;
          }
        eina_file_close_on_exec(sig_pipe[0], EINA_TRUE);
        eina_file_close_on_exec(sig_pipe[1], EINA_TRUE);
        fcntl(sig_pipe[0], F_SETFL, O_NONBLOCK);
     }
   _signalhandler_setup();
   if (!sig_pipe_handler)
     sig_pipe_handler =
       efl_add(EFL_LOOP_HANDLER_CLASS, ML_OBJ,
               efl_loop_handler_fd_set(efl_added, sig_pipe[0]),
               efl_loop_handler_active_set(efl_added, EFL_LOOP_HANDLER_FLAGS_READ),
               efl_event_callback_array_add(efl_added, _event_watch(), NULL));
}

static void
_ecore_signal_pipe_shutdown(void)
{
   if (sig_pipe[0] != -1)
     {
        close(sig_pipe[0]);
        close(sig_pipe[1]);
        sig_pipe[0] = -1;
        sig_pipe[1] = -1;
     }
   if (sig_pipe_handler)
     {
        efl_del(sig_pipe_handler);
        sig_pipe_handler = NULL;
     }
   eina_spinlock_free(&sig_pid_lock);
}

static void
_ecore_signal_cb_fork(void *data EINA_UNUSED)
{
   _ecore_signal_pipe_shutdown();
   _ecore_signal_pipe_init();
}

void
_ecore_signal_init(void)
{
   _ecore_signal_pipe_init();
   ecore_fork_reset_callback_add(_ecore_signal_cb_fork, NULL);
}

void
_ecore_signal_shutdown(void)
{
   ecore_fork_reset_callback_del(_ecore_signal_cb_fork, NULL);
   _ecore_signal_pipe_shutdown();
   // we probably should restore.. but not a good idea
   // pthread_sigmask(SIG_SETMASK, &sig_oldset, NULL);
}

void
_ecore_signal_received_process(Eo *obj EINA_UNUSED, Efl_Loop_Data *pd EINA_UNUSED)
{
   // do nothing - the efl loop handler read event will handle it
}

int
_ecore_signal_count_get(Eo *obj EINA_UNUSED, Efl_Loop_Data *pd EINA_UNUSED)
{
   // we will always have 0 signals be3cause they will instead be read from
   // a pipe fd and placed in a queue/list that
   // _ecore_signal_received_process() will then walk and process/do
   return 0;
}

void
_ecore_signal_pid_lock(void)
{
   eina_spinlock_take(&sig_pid_lock);
}

void
_ecore_signal_pid_unlock(void)
{
   eina_spinlock_release(&sig_pid_lock);
}

void
_ecore_signal_pid_register(pid_t pid, int fd)
{
   Pid_Info *pi = calloc(1, sizeof(Pid_Info));
   if (!pi) return;
   pi->pid = pid;
   pi->fd = fd;
   sig_pid_info_list = eina_list_append(sig_pid_info_list, pi);
}

void
_ecore_signal_pid_unregister(pid_t pid, int fd)
{
   Eina_List *l;
   Pid_Info *pi;

   EINA_LIST_FOREACH(sig_pid_info_list, l, pi)
     {
        if ((pi->pid == pid) && (pi->fd == fd))
          {
             sig_pid_info_list = eina_list_remove_list(sig_pid_info_list, l);
             free(pi);
             return;
          }
     }
}

static void
_ecore_signal_exe_exit_delay(void *data, const Efl_Event *event)
{
   Ecore_Exe_Event_Del *e = data;

   if (!e) return;
   _ecore_exe_doomsday_clock_set(e->exe, NULL);
   ecore_event_add(ECORE_EXE_EVENT_DEL, e,
                   _ecore_exe_event_del_free, NULL);
   efl_del(event->object);
}

static void
_ecore_signal_waitpid(Eina_Bool once, siginfo_t info)
{
   pid_t pid;
   int status;

   while ((pid = waitpid(-1, &status, WNOHANG)) > 0)
     {
        Ecore_Exe_Event_Del *e = _ecore_exe_event_del_new();

        //FIXME: If this process is set respawn, respawn with a suitable backoff
        // period for those that need too much respawning.
        if (e)
          {
             if (WIFEXITED(status))
               {
                  e->exit_code = WEXITSTATUS(status);
                  e->exited = 1;
               }
             else if (WIFSIGNALED(status))
               {
                  e->exit_signal = WTERMSIG(status);
                  e->signalled = 1;
               }
             e->pid = pid;
             e->exe = _ecore_exe_find(pid);
             e->data = info;  // No need to clone this.
             if ((e->exe) &&
                 (ecore_exe_flags_get(e->exe) &
                  (ECORE_EXE_PIPE_READ | ECORE_EXE_PIPE_ERROR)))
               {
                  /* We want to report the Last Words of the exe, so delay this event.
                   * This is twice as relevant for stderr.
                   * There are three possibilities here -
                   *  1 There are no Last Words.
                   *  2 There are Last Words, they are not ready to be read.
                   *  3 There are Last Words, they are ready to be read.
                   *
                   * For 1 we don't want to delay, for 3 we want to delay.
                   * 2 is the problem.  If we check for data now and there
                   * is none, then there is no way to differentiate 1 and 2.
                   * If we don't delay, we may loose data, but if we do delay,
                   * there may not be data and the exit event never gets sent.
                   *
                   * Any way you look at it, there has to be some time passed
                   * before the exit event gets sent.  So the strategy here is
                   * to setup a timer event that will send the exit event after
                   * an arbitrary, but brief, time.
                   *
                   * This is probably paranoid, for the less paraniod, we could
                   * check to see for Last Words, and only delay if there are any.
                   * This has it's own set of problems. */
                  efl_del(_ecore_exe_doomsday_clock_get(e->exe));

                  Efl_Loop_Timer *doomsday_clock =
                    efl_add(EFL_LOOP_TIMER_CLASS, ML_OBJ,
                            efl_loop_timer_interval_set(efl_added, 0.1),
                            efl_event_callback_add
                            (efl_added, EFL_LOOP_TIMER_EVENT_TICK,
                             _ecore_signal_exe_exit_delay, e));
                  _ecore_exe_doomsday_clock_set(e->exe, doomsday_clock);
               }
             else ecore_event_add(ECORE_EXE_EVENT_DEL, e,
                                  _ecore_exe_event_del_free, NULL);
          }

        // XXX: this is not brilliant. this ends up running from the main loop
        // reading the signal pipe to handle signals. that means handling
        // exe exits from children will be bottlenecked by how often
        // the main loop can wake up (or well latency may not be great).
        // this should probably have a dedicated thread ythat does a waitpid()
        // and blocks and waits sending results to the resulting pipe
        Eina_List *l, *ll;
        Pid_Info *pi;

        EINA_LIST_FOREACH_SAFE(sig_pid_info_list, l, ll, pi)
          {
             if (pi->pid == pid)
               {
                  Ecore_Signal_Pid_Info pinfo;

                  sig_pid_info_list = eina_list_remove_list
                    (sig_pid_info_list, ll);
                  pinfo.pid = pid;
                  pinfo.info = info;
                  if (WIFEXITED(status))
                    {
                       pinfo.exit_code = WEXITSTATUS(status);
                       pinfo.exit_signal = -1;
                    }
                  else if (WIFSIGNALED(status))
                    {
                       pinfo.exit_code = -1;
                       pinfo.exit_signal = WTERMSIG(status);
                    }
                  if (write(pi->fd, &pinfo, sizeof(Ecore_Signal_Pid_Info))
                      != sizeof(Ecore_Signal_Pid_Info))
                    {
                       ERR("Can't write to custom exe exit info pipe");
                    }
                  free(pi);
                  break;
               }
          }
        if (once) break;
     }
}

static void
_ecore_signal_generic_free(void *data EINA_UNUSED, void *event)
{
   free(event);
}