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>
19 #include <libtu/objp.h>
20 #include <libtu/types.h>
21 #include <libtu/misc.h>
22 #include <libtu/locale.h>
23 #include <libtu/output.h>
28 static int kill_sig=0;
30 static int wait_sig=0;
33 static int usr2_sig=0;
34 static bool had_tmr=FALSE;
36 WHook *mainloop_sigchld_hook=NULL;
37 WHook *mainloop_sigusr2_hook=NULL;
43 static WTimer *queue=NULL;
46 int mainloop_gettime(struct timeval *val)
48 #if defined(_POSIX_MONOTONIC_CLOCK) && (_POSIX_MONOTONIC_CLOCK>=0)
54 ret=clock_gettime(CLOCK_MONOTONIC, &spec);
56 if(ret==-1 && errno==EINVAL && checked==0){
61 val->tv_sec=spec.tv_sec;
62 val->tv_usec=spec.tv_nsec/1000;
68 #warning "Monotonic clock unavailable; please fix your operating system."
70 return gettimeofday(val, NULL);
74 #define TIMEVAL_LATER(a, b) \
75 ((a.tv_sec > b.tv_sec) || \
76 ((a.tv_sec == b.tv_sec) && \
77 (a.tv_usec > b.tv_usec)))
79 #define USECS_IN_SEC 1000000
82 static void do_timer_set()
84 struct itimerval val={{0, 0}, {0, 0}};
87 setitimer(ITIMER_REAL, &val, NULL);
91 /* Subtract queue time from current time, don't go below zero */
92 mainloop_gettime(&(val.it_value));
93 if(TIMEVAL_LATER((queue)->when, val.it_value)){
94 if(queue->when.tv_usec<val.it_value.tv_usec){
95 queue->when.tv_usec+=USECS_IN_SEC;
98 val.it_value.tv_usec=queue->when.tv_usec-val.it_value.tv_usec;
99 val.it_value.tv_sec=queue->when.tv_sec-val.it_value.tv_sec;
100 if(val.it_value.tv_usec<0)
101 val.it_value.tv_usec=0;
102 /* POSIX and some kernels have been designed by absolute morons and
103 * contain idiotic artificial restrictions on the value of tv_usec,
104 * that will only cause more code being run and clock cycles being
105 * spent to do the same thing, as the kernel will in any case convert
106 * the seconds to some other units.
108 val.it_value.tv_sec+=val.it_value.tv_usec/USECS_IN_SEC;
109 val.it_value.tv_usec%=USECS_IN_SEC;
115 val.it_interval.tv_usec=val.it_value.tv_usec;
116 val.it_interval.tv_sec=val.it_value.tv_sec;
118 if((setitimer(ITIMER_REAL, &val, NULL))){
130 static bool mrsh_chld(void (*fn)(pid_t, int), ChldParams *p)
137 static bool mrsh_chld_extl(ExtlFn fn, ChldParams *p)
139 ExtlTab t=extl_create_table();
142 extl_table_sets_i(t, "pid", (int)p->pid);
144 if(WIFEXITED(p->code)){
145 extl_table_sets_b(t, "exited", TRUE);
146 extl_table_sets_i(t, "exitstatus", WEXITSTATUS(p->code));
148 if(WIFSIGNALED(p->code)){
149 extl_table_sets_b(t, "signaled", TRUE);
150 extl_table_sets_i(t, "termsig", WTERMSIG(p->code));
152 extl_table_sets_i(t, "coredump", WCOREDUMP(p->code));
155 if(WIFSTOPPED(p->code)){
156 extl_table_sets_b(t, "stopped", TRUE);
157 extl_table_sets_i(t, "stopsig", WSTOPSIG(p->code));
159 /*if(WIFCONTINUED(p->code)){
160 extl_table_sets_b(t, "continued", TRUE);
163 ret=extl_call(fn, "t", NULL, t);
170 static bool mrsh_usr2(void (*fn)(void), void *p)
176 static bool mrsh_usr2_extl(ExtlFn fn, void *p)
179 ExtlTab t=extl_create_table();
180 ret=extl_call(fn, "t", NULL, t);
186 bool mainloop_check_signals()
188 struct timeval current_time;
194 if(mainloop_sigusr2_hook!=NULL){
195 hook_call(mainloop_sigusr2_hook, NULL,
196 (WHookMarshall*)mrsh_usr2,
197 (WHookMarshallExtl*)mrsh_usr2_extl);
205 while((p.pid=waitpid(-1, &p.code, WNOHANG|WUNTRACED))>0){
206 if(mainloop_sigchld_hook!=NULL &&
207 (WIFEXITED(p.code) || WIFSIGNALED(p.code))){
208 hook_call(mainloop_sigchld_hook, &p,
209 (WHookMarshall*)mrsh_chld,
210 (WHookMarshallExtl*)mrsh_chld_extl);
219 /* Check for timer events in the queue */
220 while(had_tmr && queue!=NULL){
222 mainloop_gettime(¤t_time);
224 if(TIMEVAL_LATER(current_time, queue->when)){
228 if(q->handler!=NULL){
229 WTimerHandler *handler=q->handler;
230 Obj *obj=q->objwatch.obj;
232 watch_reset(&(q->objwatch));
234 }else if(q->extl_handler!=extl_fn_none()){
235 ExtlFn fn=q->extl_handler;
236 Obj *obj=q->objwatch.obj;
237 watch_reset(&(q->objwatch));
238 q->extl_handler=extl_fn_none();
239 extl_call(fn, "o", NULL, obj);
253 static void add_to_current_time(struct timeval *when, uint msecs)
257 mainloop_gettime(when);
258 tmp_usec=when->tv_usec + (msecs * 1000);
259 when->tv_usec=tmp_usec % 1000000;
260 when->tv_sec+=tmp_usec / 1000000;
268 bool timer_is_set(WTimer *timer)
271 for(tmr=queue; tmr!=NULL; tmr=tmr->next){
279 void timer_do_set(WTimer *timer, uint msecs, WTimerHandler *handler,
287 /* Initialize the new queue timer event */
288 add_to_current_time(&(timer->when), msecs);
290 timer->handler=handler;
291 timer->extl_handler=fn;
293 watch_setup(&(timer->objwatch), obj, NULL);
295 watch_reset(&(timer->objwatch));
297 /* Add timerevent in place to queue */
302 if(TIMEVAL_LATER(q->when, timer->when))
315 void timer_set(WTimer *timer, uint msecs, WTimerHandler *handler,
318 timer_do_set(timer, msecs, handler, obj, extl_fn_none());
323 * Set \var{timer} to call \var{fn} in \var{msecs} milliseconds.
325 EXTL_EXPORT_AS(WTimer, set)
326 void timer_set_extl(WTimer *timer, uint msecs, ExtlFn fn)
328 timer_do_set(timer, msecs, NULL, NULL, extl_ref_fn(fn));
336 void timer_reset(WTimer *timer)
338 WTimer *q=queue, **qptr=&queue;
352 extl_unref_fn(timer->extl_handler);
353 timer->extl_handler=extl_fn_none();
354 watch_reset(&(timer->objwatch));
358 bool timer_init(WTimer *timer)
360 timer->when.tv_sec=0;
361 timer->when.tv_usec=0;
364 timer->extl_handler=extl_fn_none();
365 watch_init(&(timer->objwatch));
369 void timer_deinit(WTimer *timer)
375 WTimer *create_timer()
377 CREATEOBJ_IMPL(WTimer, timer, (p));
381 * Create a new timer.
383 EXTL_EXPORT_AS(mainloop, create_timer)
384 WTimer *create_timer_extl_owned()
386 WTimer *timer=create_timer();
388 ((Obj*)timer)->flags|=OBJ_EXTL_OWNED;
394 IMPLCLASS(WTimer, Obj, timer_deinit, NULL);
400 /*{{{ Signal handling */
403 static void fatal_signal_handler(int signal_num)
405 set_warn_handler(NULL);
406 warn(TR("Caught fatal signal %d. Dying without deinit."), signal_num);
407 signal(signal_num, SIG_DFL);
408 kill(getpid(), signal_num);
412 static void deadly_signal_handler(int signal_num)
414 set_warn_handler(NULL);
415 warn(TR("Caught signal %d. Dying."), signal_num);
416 signal(signal_num, SIG_DFL);
417 /*if(ioncore_g.opmode==IONCORE_OPMODE_INIT)
418 kill(getpid(), signal_num);
424 static void chld_handler(int signal_num)
429 while((pid=waitpid(-1, NULL, WNOHANG|WUNTRACED))>0){
437 static void usr2_handler(int signal_num)
443 static void exit_handler(int signal_num)
446 warn(TR("Got signal %d while %d is still to be handled."),
447 signal_num, kill_sig);
453 static void timer_handler(int signal_num)
459 static void ignore_handler(int signal_num)
466 /* glibc is broken (?) and does not define SA_RESTART with
467 * '-ansi -D_XOPEN_SOURCE -D_XOPEN_SOURCE_EXTENDED', so just try to live
473 #define IFTRAP(X) if(sigismember(which, X))
474 #define DEADLY(X) IFTRAP(X) signal(X, deadly_signal_handler);
475 #define FATAL(X) IFTRAP(X) signal(X, fatal_signal_handler);
476 #define IGNORE(X) IFTRAP(X) signal(X, SIG_IGN)
478 void mainloop_trap_signals(const sigset_t *which)
481 sigset_t set, oldset;
490 sigemptyset(&oldset);
491 sigprocmask(SIG_SETMASK, &set, &oldset);
504 /*IGNORE(SIGWINCH);*/
506 sigemptyset(&(sa.sa_mask));
509 sa.sa_handler=timer_handler;
510 sa.sa_flags=SA_RESTART;
511 sigaction(SIGALRM, &sa, NULL);
515 sa.sa_handler=chld_handler;
516 sa.sa_flags=SA_NOCLDSTOP|SA_RESTART;
517 sigaction(SIGCHLD, &sa, NULL);
521 sa.sa_handler=usr2_handler;
522 sa.sa_flags=SA_RESTART;
523 sigaction(SIGUSR2, &sa, NULL);
527 sa.sa_handler=exit_handler;
528 sa.sa_flags=SA_RESTART;
529 sigaction(SIGTERM, &sa, NULL);
533 sa.sa_handler=exit_handler;
534 sa.sa_flags=SA_RESTART;
535 sigaction(SIGUSR1, &sa, NULL);
538 /* SIG_IGN is preserved over execve and since the the default action
539 * for SIGPIPE is not to ignore it, some programs may get upset if
540 * the behaviour is not the default.
543 sa.sa_handler=ignore_handler;
544 sigaction(SIGPIPE, &sa, NULL);