source: signal.c @ 06e04a9

release-1.10release-1.9
Last change on this file since 06e04a9 was a7fac14, checked in by David Benjamin <davidben@mit.edu>, 13 years ago
Assign all watched signals to a dummy no-op disposition Otherwise some (namely SIGWINCH) may have SIG_DFL = SIG_IGN. This means that the signal is ignored if the signal thread is in the middle of processing another signal (a race condition), and on Solaris it's not delivered at all. Also more consistently consider errors in setup here fatal. And rename CHECK_RESULT to OR_DIE. Reported-by: Benjamin Kaduk <kaduk@mit.edu> Reviewed-by: Adam Glasgall <adam@crossproduct.net>
  • Property mode set to 100644
File size: 2.5 KB
RevLine 
[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]8static pthread_t signal_thread;
[3535a6e]9static sigset_t signal_set;
10
[1d21d9f]11static void (*signal_cb)(const siginfo_t*, void*);
[3535a6e]12static void *signal_cbdata;
13
[08e9842]14static void *signal_thread_func(void *data);
[3535a6e]15
[a7fac14]16static 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]36void 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]69static 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
87void owl_signal_shutdown(void) {
88  pthread_kill(signal_thread, SIGTERM);
89  pthread_join(signal_thread, NULL);
90}
Note: See TracBrowser for help on using the repository browser.