[6bd485e] | 1 | #include <errno.h> |
---|
[a7fac14] | 2 | #include <glib.h> |
---|
[3535a6e] | 3 | #include <pthread.h> |
---|
| 4 | #include <signal.h> |
---|
| 5 | #include <stdio.h> |
---|
| 6 | #include <stdlib.h> |
---|
| 7 | |
---|
[08e9842] | 8 | static pthread_t signal_thread; |
---|
[3535a6e] | 9 | static sigset_t signal_set; |
---|
| 10 | |
---|
[1d21d9f] | 11 | static void (*signal_cb)(const siginfo_t*, void*); |
---|
[3535a6e] | 12 | static void *signal_cbdata; |
---|
| 13 | |
---|
[08e9842] | 14 | static void *signal_thread_func(void *data); |
---|
[3535a6e] | 15 | |
---|
[a7fac14] | 16 | static void dummy_handler(int signum) |
---|
| 17 | { |
---|
| 18 | /* Do nothing. This should never get called. It'd be nice to report the error |
---|
| 19 | * or something, but you can't have nice things in a signal handler. */ |
---|
| 20 | } |
---|
| 21 | |
---|
| 22 | #define OR_DIE(s, syscall) \ |
---|
| 23 | G_STMT_START { \ |
---|
| 24 | if ((syscall) == -1) { \ |
---|
| 25 | perror((s)); \ |
---|
| 26 | exit(1); \ |
---|
| 27 | } \ |
---|
| 28 | } G_STMT_END |
---|
| 29 | |
---|
| 30 | /* Initializes the signal thread to listen for 'signals' on a dedicated |
---|
[81db142] | 31 | * thread. 'callback' is called *on the signal thread* when a signal |
---|
| 32 | * is received. |
---|
| 33 | * |
---|
| 34 | * This function /must/ be called before any other threads are |
---|
| 35 | * created. (Otherwise the signals will not get blocked correctly.) */ |
---|
[a7fac14] | 36 | void owl_signal_init(const int *signals, int num_signals, void (*callback)(const siginfo_t*, void*), void *data) { |
---|
| 37 | struct sigaction sig_dummy = { .sa_handler = dummy_handler }; |
---|
[6bd485e] | 38 | int ret; |
---|
[a7fac14] | 39 | int i; |
---|
[3535a6e] | 40 | |
---|
| 41 | signal_cb = callback; |
---|
| 42 | signal_cbdata = data; |
---|
[a7fac14] | 43 | |
---|
| 44 | /* Stuff the signals into our sigset_t. Also assign all of them to a dummy |
---|
| 45 | * handler. Otherwise, if their default is SIG_IGN, they will get dropped if |
---|
| 46 | * delivered while processing. On Solaris, they will not get delivered at |
---|
| 47 | * all. */ |
---|
| 48 | OR_DIE("sigemptyset", sigemptyset(&signal_set)); |
---|
| 49 | for (i = 0; i < num_signals; i++) { |
---|
| 50 | OR_DIE("sigaddset", sigaddset(&signal_set, signals[i])); |
---|
| 51 | OR_DIE("sigaction", sigaction(signals[i], &sig_dummy, NULL)); |
---|
| 52 | } |
---|
| 53 | |
---|
[3535a6e] | 54 | /* Block these signals in all threads, so we can get them. */ |
---|
[a7fac14] | 55 | if ((ret = pthread_sigmask(SIG_BLOCK, &signal_set, NULL)) != 0) { |
---|
[6bd485e] | 56 | errno = ret; |
---|
| 57 | perror("pthread_sigmask"); |
---|
[a7fac14] | 58 | exit(1); |
---|
[6bd485e] | 59 | } |
---|
[3535a6e] | 60 | /* Spawn a dedicated thread to sigwait. */ |
---|
[08e9842] | 61 | if ((ret = pthread_create(&signal_thread, NULL, |
---|
| 62 | signal_thread_func, NULL)) != 0) { |
---|
| 63 | errno = ret; |
---|
| 64 | perror("pthread_create"); |
---|
[3535a6e] | 65 | exit(1); |
---|
| 66 | } |
---|
| 67 | } |
---|
| 68 | |
---|
[08e9842] | 69 | static void *signal_thread_func(void *data) { |
---|
[3535a6e] | 70 | while (1) { |
---|
[1d21d9f] | 71 | siginfo_t siginfo; |
---|
[3535a6e] | 72 | int ret; |
---|
| 73 | |
---|
[1d21d9f] | 74 | ret = sigwaitinfo(&signal_set, &siginfo); |
---|
| 75 | /* TODO: Print an error? */ |
---|
| 76 | if (ret < 0) |
---|
[3535a6e] | 77 | continue; |
---|
| 78 | |
---|
[1d21d9f] | 79 | signal_cb(&siginfo, signal_cbdata); |
---|
[08e9842] | 80 | /* Die on SIGTERM. */ |
---|
[1d21d9f] | 81 | if (siginfo.si_signo == SIGTERM) |
---|
[08e9842] | 82 | break; |
---|
[3535a6e] | 83 | } |
---|
| 84 | return NULL; |
---|
| 85 | } |
---|
[08e9842] | 86 | |
---|
| 87 | void owl_signal_shutdown(void) { |
---|
| 88 | pthread_kill(signal_thread, SIGTERM); |
---|
| 89 | pthread_join(signal_thread, NULL); |
---|
| 90 | } |
---|