2 * ion/libmainloop/signal.c
4 * Copyright (c) Tuomo Valkonen 1999-2007.
6 * Ion is free software; you can redistribute it and/or modify it under
7 * the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 of the License, or
9 * (at your option) any later version.
13 #include <sys/types.h>
21 #include <libtu/objp.h>
22 #include <libtu/types.h>
23 #include <libtu/misc.h>
24 #include <libtu/locale.h>
25 #include <libtu/output.h>
30 static int kill_sig=0;
32 static int wait_sig=0;
35 static int usr2_sig=0;
36 static bool had_tmr=FALSE;
38 WHook *mainloop_sigchld_hook=NULL;
39 WHook *mainloop_sigusr2_hook=NULL;
45 static WTimer *queue=NULL;
48 #define TIMEVAL_LATER(a, b) \
49 ((a.tv_sec > b.tv_sec) || \
50 ((a.tv_sec == b.tv_sec) && \
51 (a.tv_usec > b.tv_usec)))
53 #define USECS_IN_SEC 1000000
56 static void do_timer_set()
58 struct itimerval val={{0, 0}, {0, 0}};
61 setitimer(ITIMER_REAL, &val, NULL);
65 /* Subtract queue time from current time, don't go below zero */
66 gettimeofday(&(val.it_value), NULL);
67 if(TIMEVAL_LATER((queue)->when, val.it_value)){
68 if(queue->when.tv_usec<val.it_value.tv_usec){
69 queue->when.tv_usec+=USECS_IN_SEC;
72 val.it_value.tv_usec=queue->when.tv_usec-val.it_value.tv_usec;
73 val.it_value.tv_sec=queue->when.tv_sec-val.it_value.tv_sec;
74 if(val.it_value.tv_usec<0)
75 val.it_value.tv_usec=0;
76 /* POSIX and some kernels have been designed by absolute morons and
77 * contain idiotic artificial restrictions on the value of tv_usec,
78 * that will only cause more code being run and clock cycles being
79 * spent to do the same thing, as the kernel will in any case convert
80 * the seconds to some other units.
82 val.it_value.tv_sec+=val.it_value.tv_usec/USECS_IN_SEC;
83 val.it_value.tv_usec%=USECS_IN_SEC;
89 val.it_interval.tv_usec=val.it_value.tv_usec;
90 val.it_interval.tv_sec=val.it_value.tv_sec;
92 if((setitimer(ITIMER_REAL, &val, NULL))){
104 static bool mrsh_chld(void (*fn)(pid_t, int), ChldParams *p)
111 static bool mrsh_chld_extl(ExtlFn fn, ChldParams *p)
113 ExtlTab t=extl_create_table();
116 extl_table_sets_i(t, "pid", (int)p->pid);
118 if(WIFEXITED(p->code)){
119 extl_table_sets_b(t, "exited", TRUE);
120 extl_table_sets_i(t, "exitstatus", WEXITSTATUS(p->code));
122 if(WIFSIGNALED(p->code)){
123 extl_table_sets_b(t, "signaled", TRUE);
124 extl_table_sets_i(t, "termsig", WTERMSIG(p->code));
126 extl_table_sets_i(t, "coredump", WCOREDUMP(p->code));
129 if(WIFSTOPPED(p->code)){
130 extl_table_sets_b(t, "stopped", TRUE);
131 extl_table_sets_i(t, "stopsig", WSTOPSIG(p->code));
133 /*if(WIFCONTINUED(p->code)){
134 extl_table_sets_b(t, "continued", TRUE);
137 ret=extl_call(fn, "t", NULL, t);
144 static bool mrsh_usr2(void (*fn)(void), void *p)
150 static bool mrsh_usr2_extl(ExtlFn fn, void *p)
153 ExtlTab t=extl_create_table();
154 ret=extl_call(fn, "t", NULL, t);
160 bool mainloop_check_signals()
162 struct timeval current_time;
168 if(mainloop_sigusr2_hook!=NULL){
169 hook_call(mainloop_sigusr2_hook, NULL,
170 (WHookMarshall*)mrsh_usr2,
171 (WHookMarshallExtl*)mrsh_usr2_extl);
179 while((p.pid=waitpid(-1, &p.code, WNOHANG|WUNTRACED))>0){
180 if(mainloop_sigchld_hook!=NULL &&
181 (WIFEXITED(p.code) || WIFSIGNALED(p.code))){
182 hook_call(mainloop_sigchld_hook, &p,
183 (WHookMarshall*)mrsh_chld,
184 (WHookMarshallExtl*)mrsh_chld_extl);
193 /* Check for timer events in the queue */
194 while(had_tmr && queue!=NULL){
196 gettimeofday(¤t_time, NULL);
198 if(TIMEVAL_LATER(current_time, queue->when)){
202 if(q->handler!=NULL){
203 WTimerHandler *handler=q->handler;
204 Obj *obj=q->objwatch.obj;
206 watch_reset(&(q->objwatch));
208 }else if(q->extl_handler!=extl_fn_none()){
209 ExtlFn fn=q->extl_handler;
210 Obj *obj=q->objwatch.obj;
211 watch_reset(&(q->objwatch));
212 q->extl_handler=extl_fn_none();
213 extl_call(fn, "o", NULL, obj);
227 static void add_to_current_time(struct timeval *when, uint msecs)
231 gettimeofday(when, NULL);
232 tmp_usec=when->tv_usec + (msecs * 1000);
233 when->tv_usec=tmp_usec % 1000000;
234 when->tv_sec+=tmp_usec / 1000000;
242 bool timer_is_set(WTimer *timer)
245 for(tmr=queue; tmr!=NULL; tmr=tmr->next){
253 void timer_do_set(WTimer *timer, uint msecs, WTimerHandler *handler,
261 /* Initialize the new queue timer event */
262 add_to_current_time(&(timer->when), msecs);
264 timer->handler=handler;
265 timer->extl_handler=fn;
267 watch_setup(&(timer->objwatch), obj, NULL);
269 watch_reset(&(timer->objwatch));
271 /* Add timerevent in place to queue */
276 if(TIMEVAL_LATER(q->when, timer->when))
289 void timer_set(WTimer *timer, uint msecs, WTimerHandler *handler,
292 timer_do_set(timer, msecs, handler, obj, extl_fn_none());
297 * Set \var{timer} to call \var{fn} in \var{msecs} milliseconds.
299 EXTL_EXPORT_AS(WTimer, set)
300 void timer_set_extl(WTimer *timer, uint msecs, ExtlFn fn)
302 timer_do_set(timer, msecs, NULL, NULL, extl_ref_fn(fn));
310 void timer_reset(WTimer *timer)
312 WTimer *q=queue, **qptr=&queue;
326 extl_unref_fn(timer->extl_handler);
327 timer->extl_handler=extl_fn_none();
328 watch_reset(&(timer->objwatch));
332 bool timer_init(WTimer *timer)
334 timer->when.tv_sec=0;
335 timer->when.tv_usec=0;
338 timer->extl_handler=extl_fn_none();
339 watch_init(&(timer->objwatch));
343 void timer_deinit(WTimer *timer)
349 WTimer *create_timer()
351 CREATEOBJ_IMPL(WTimer, timer, (p));
355 * Create a new timer.
357 EXTL_EXPORT_AS(mainloop, create_timer)
358 WTimer *create_timer_extl_owned()
360 WTimer *timer=create_timer();
362 ((Obj*)timer)->flags|=OBJ_EXTL_OWNED;
368 IMPLCLASS(WTimer, Obj, timer_deinit, NULL);
374 /*{{{ Signal handling */
377 static void fatal_signal_handler(int signal_num)
379 set_warn_handler(NULL);
380 warn(TR("Caught fatal signal %d. Dying without deinit."), signal_num);
381 signal(signal_num, SIG_DFL);
382 kill(getpid(), signal_num);
386 static void deadly_signal_handler(int signal_num)
388 set_warn_handler(NULL);
389 warn(TR("Caught signal %d. Dying."), signal_num);
390 signal(signal_num, SIG_DFL);
391 /*if(ioncore_g.opmode==IONCORE_OPMODE_INIT)
392 kill(getpid(), signal_num);
398 static void chld_handler(int signal_num)
403 while((pid=waitpid(-1, NULL, WNOHANG|WUNTRACED))>0){
411 static void usr2_handler(int signal_num)
417 static void exit_handler(int signal_num)
420 warn(TR("Got signal %d while %d is still to be handled."),
421 signal_num, kill_sig);
427 static void timer_handler(int signal_num)
433 static void ignore_handler(int signal_num)
440 /* glibc is broken (?) and does not define SA_RESTART with
441 * '-ansi -D_XOPEN_SOURCE -D_XOPEN_SOURCE_EXTENDED', so just try to live
447 #define IFTRAP(X) if(sigismember(which, X))
448 #define DEADLY(X) IFTRAP(X) signal(X, deadly_signal_handler);
449 #define FATAL(X) IFTRAP(X) signal(X, fatal_signal_handler);
450 #define IGNORE(X) IFTRAP(X) signal(X, SIG_IGN)
452 void mainloop_trap_signals(const sigset_t *which)
455 sigset_t set, oldset;
464 sigemptyset(&oldset);
465 sigprocmask(SIG_SETMASK, &set, &oldset);
478 /*IGNORE(SIGWINCH);*/
480 sigemptyset(&(sa.sa_mask));
483 sa.sa_handler=timer_handler;
484 sa.sa_flags=SA_RESTART;
485 sigaction(SIGALRM, &sa, NULL);
489 sa.sa_handler=chld_handler;
490 sa.sa_flags=SA_NOCLDSTOP|SA_RESTART;
491 sigaction(SIGCHLD, &sa, NULL);
495 sa.sa_handler=usr2_handler;
496 sa.sa_flags=SA_RESTART;
497 sigaction(SIGUSR2, &sa, NULL);
501 sa.sa_handler=exit_handler;
502 sa.sa_flags=SA_RESTART;
503 sigaction(SIGTERM, &sa, NULL);
507 sa.sa_handler=exit_handler;
508 sa.sa_flags=SA_RESTART;
509 sigaction(SIGUSR1, &sa, NULL);
512 /* SIG_IGN is preserved over execve and since the the default action
513 * for SIGPIPE is not to ignore it, some programs may get upset if
514 * the behaviour is not the default.
517 sa.sa_handler=ignore_handler;
518 sigaction(SIGPIPE, &sa, NULL);