2 * ion/libmainloop/signal.c
4 * Copyright (c) Tuomo Valkonen 1999-2007.
6 * See the included file LICENSE for details.
10 #include <sys/types.h>
18 #include <libtu/objp.h>
19 #include <libtu/types.h>
20 #include <libtu/misc.h>
21 #include <libtu/locale.h>
22 #include <libtu/output.h>
27 static int kill_sig=0;
29 static int wait_sig=0;
32 static int usr2_sig=0;
33 static bool had_tmr=FALSE;
35 WHook *mainloop_sigchld_hook=NULL;
36 WHook *mainloop_sigusr2_hook=NULL;
42 static WTimer *queue=NULL;
45 #define TIMEVAL_LATER(a, b) \
46 ((a.tv_sec > b.tv_sec) || \
47 ((a.tv_sec == b.tv_sec) && \
48 (a.tv_usec > b.tv_usec)))
50 #define USECS_IN_SEC 1000000
53 static void do_timer_set()
55 struct itimerval val={{0, 0}, {0, 0}};
58 setitimer(ITIMER_REAL, &val, NULL);
62 /* Subtract queue time from current time, don't go below zero */
63 gettimeofday(&(val.it_value), NULL);
64 if(TIMEVAL_LATER((queue)->when, val.it_value)){
65 if(queue->when.tv_usec<val.it_value.tv_usec){
66 queue->when.tv_usec+=USECS_IN_SEC;
69 val.it_value.tv_usec=queue->when.tv_usec-val.it_value.tv_usec;
70 val.it_value.tv_sec=queue->when.tv_sec-val.it_value.tv_sec;
71 if(val.it_value.tv_usec<0)
72 val.it_value.tv_usec=0;
73 /* POSIX and some kernels have been designed by absolute morons and
74 * contain idiotic artificial restrictions on the value of tv_usec,
75 * that will only cause more code being run and clock cycles being
76 * spent to do the same thing, as the kernel will in any case convert
77 * the seconds to some other units.
79 val.it_value.tv_sec+=val.it_value.tv_usec/USECS_IN_SEC;
80 val.it_value.tv_usec%=USECS_IN_SEC;
86 val.it_interval.tv_usec=val.it_value.tv_usec;
87 val.it_interval.tv_sec=val.it_value.tv_sec;
89 if((setitimer(ITIMER_REAL, &val, NULL))){
101 static bool mrsh_chld(void (*fn)(pid_t, int), ChldParams *p)
108 static bool mrsh_chld_extl(ExtlFn fn, ChldParams *p)
110 ExtlTab t=extl_create_table();
113 extl_table_sets_i(t, "pid", (int)p->pid);
115 if(WIFEXITED(p->code)){
116 extl_table_sets_b(t, "exited", TRUE);
117 extl_table_sets_i(t, "exitstatus", WEXITSTATUS(p->code));
119 if(WIFSIGNALED(p->code)){
120 extl_table_sets_b(t, "signaled", TRUE);
121 extl_table_sets_i(t, "termsig", WTERMSIG(p->code));
123 extl_table_sets_i(t, "coredump", WCOREDUMP(p->code));
126 if(WIFSTOPPED(p->code)){
127 extl_table_sets_b(t, "stopped", TRUE);
128 extl_table_sets_i(t, "stopsig", WSTOPSIG(p->code));
130 /*if(WIFCONTINUED(p->code)){
131 extl_table_sets_b(t, "continued", TRUE);
134 ret=extl_call(fn, "t", NULL, t);
141 static bool mrsh_usr2(void (*fn)(void), void *p)
147 static bool mrsh_usr2_extl(ExtlFn fn, void *p)
150 ExtlTab t=extl_create_table();
151 ret=extl_call(fn, "t", NULL, t);
157 bool mainloop_check_signals()
159 struct timeval current_time;
165 if(mainloop_sigusr2_hook!=NULL){
166 hook_call(mainloop_sigusr2_hook, NULL,
167 (WHookMarshall*)mrsh_usr2,
168 (WHookMarshallExtl*)mrsh_usr2_extl);
176 while((p.pid=waitpid(-1, &p.code, WNOHANG|WUNTRACED))>0){
177 if(mainloop_sigchld_hook!=NULL &&
178 (WIFEXITED(p.code) || WIFSIGNALED(p.code))){
179 hook_call(mainloop_sigchld_hook, &p,
180 (WHookMarshall*)mrsh_chld,
181 (WHookMarshallExtl*)mrsh_chld_extl);
190 /* Check for timer events in the queue */
191 while(had_tmr && queue!=NULL){
193 gettimeofday(¤t_time, NULL);
195 if(TIMEVAL_LATER(current_time, queue->when)){
199 if(q->handler!=NULL){
200 WTimerHandler *handler=q->handler;
201 Obj *obj=q->objwatch.obj;
203 watch_reset(&(q->objwatch));
205 }else if(q->extl_handler!=extl_fn_none()){
206 ExtlFn fn=q->extl_handler;
207 Obj *obj=q->objwatch.obj;
208 watch_reset(&(q->objwatch));
209 q->extl_handler=extl_fn_none();
210 extl_call(fn, "o", NULL, obj);
224 static void add_to_current_time(struct timeval *when, uint msecs)
228 gettimeofday(when, NULL);
229 tmp_usec=when->tv_usec + (msecs * 1000);
230 when->tv_usec=tmp_usec % 1000000;
231 when->tv_sec+=tmp_usec / 1000000;
239 bool timer_is_set(WTimer *timer)
242 for(tmr=queue; tmr!=NULL; tmr=tmr->next){
250 void timer_do_set(WTimer *timer, uint msecs, WTimerHandler *handler,
258 /* Initialize the new queue timer event */
259 add_to_current_time(&(timer->when), msecs);
261 timer->handler=handler;
262 timer->extl_handler=fn;
264 watch_setup(&(timer->objwatch), obj, NULL);
266 watch_reset(&(timer->objwatch));
268 /* Add timerevent in place to queue */
273 if(TIMEVAL_LATER(q->when, timer->when))
286 void timer_set(WTimer *timer, uint msecs, WTimerHandler *handler,
289 timer_do_set(timer, msecs, handler, obj, extl_fn_none());
294 * Set \var{timer} to call \var{fn} in \var{msecs} milliseconds.
296 EXTL_EXPORT_AS(WTimer, set)
297 void timer_set_extl(WTimer *timer, uint msecs, ExtlFn fn)
299 timer_do_set(timer, msecs, NULL, NULL, extl_ref_fn(fn));
307 void timer_reset(WTimer *timer)
309 WTimer *q=queue, **qptr=&queue;
323 extl_unref_fn(timer->extl_handler);
324 timer->extl_handler=extl_fn_none();
325 watch_reset(&(timer->objwatch));
329 bool timer_init(WTimer *timer)
331 timer->when.tv_sec=0;
332 timer->when.tv_usec=0;
335 timer->extl_handler=extl_fn_none();
336 watch_init(&(timer->objwatch));
340 void timer_deinit(WTimer *timer)
346 WTimer *create_timer()
348 CREATEOBJ_IMPL(WTimer, timer, (p));
352 * Create a new timer.
354 EXTL_EXPORT_AS(mainloop, create_timer)
355 WTimer *create_timer_extl_owned()
357 WTimer *timer=create_timer();
359 ((Obj*)timer)->flags|=OBJ_EXTL_OWNED;
365 IMPLCLASS(WTimer, Obj, timer_deinit, NULL);
371 /*{{{ Signal handling */
374 static void fatal_signal_handler(int signal_num)
376 set_warn_handler(NULL);
377 warn(TR("Caught fatal signal %d. Dying without deinit."), signal_num);
378 signal(signal_num, SIG_DFL);
379 kill(getpid(), signal_num);
383 static void deadly_signal_handler(int signal_num)
385 set_warn_handler(NULL);
386 warn(TR("Caught signal %d. Dying."), signal_num);
387 signal(signal_num, SIG_DFL);
388 /*if(ioncore_g.opmode==IONCORE_OPMODE_INIT)
389 kill(getpid(), signal_num);
395 static void chld_handler(int signal_num)
400 while((pid=waitpid(-1, NULL, WNOHANG|WUNTRACED))>0){
408 static void usr2_handler(int signal_num)
414 static void exit_handler(int signal_num)
417 warn(TR("Got signal %d while %d is still to be handled."),
418 signal_num, kill_sig);
424 static void timer_handler(int signal_num)
430 static void ignore_handler(int signal_num)
437 /* glibc is broken (?) and does not define SA_RESTART with
438 * '-ansi -D_XOPEN_SOURCE -D_XOPEN_SOURCE_EXTENDED', so just try to live
444 #define IFTRAP(X) if(sigismember(which, X))
445 #define DEADLY(X) IFTRAP(X) signal(X, deadly_signal_handler);
446 #define FATAL(X) IFTRAP(X) signal(X, fatal_signal_handler);
447 #define IGNORE(X) IFTRAP(X) signal(X, SIG_IGN)
449 void mainloop_trap_signals(const sigset_t *which)
452 sigset_t set, oldset;
461 sigemptyset(&oldset);
462 sigprocmask(SIG_SETMASK, &set, &oldset);
475 /*IGNORE(SIGWINCH);*/
477 sigemptyset(&(sa.sa_mask));
480 sa.sa_handler=timer_handler;
481 sa.sa_flags=SA_RESTART;
482 sigaction(SIGALRM, &sa, NULL);
486 sa.sa_handler=chld_handler;
487 sa.sa_flags=SA_NOCLDSTOP|SA_RESTART;
488 sigaction(SIGCHLD, &sa, NULL);
492 sa.sa_handler=usr2_handler;
493 sa.sa_flags=SA_RESTART;
494 sigaction(SIGUSR2, &sa, NULL);
498 sa.sa_handler=exit_handler;
499 sa.sa_flags=SA_RESTART;
500 sigaction(SIGTERM, &sa, NULL);
504 sa.sa_handler=exit_handler;
505 sa.sa_flags=SA_RESTART;
506 sigaction(SIGUSR1, &sa, NULL);
509 /* SIG_IGN is preserved over execve and since the the default action
510 * for SIGPIPE is not to ignore it, some programs may get upset if
511 * the behaviour is not the default.
514 sa.sa_handler=ignore_handler;
515 sigaction(SIGPIPE, &sa, NULL);