Changeset 3535a6e for owl.c


Ignore:
Timestamp:
May 23, 2011, 8:57:46 PM (13 years ago)
Author:
David Benjamin <davidben@mit.edu>
Branches:
master, release-1.10, release-1.8, release-1.9
Children:
257b9c4
Parents:
959cb85
git-author:
David Benjamin <davidben@mit.edu> (02/26/11 00:15:35)
git-committer:
David Benjamin <davidben@mit.edu> (05/23/11 20:57:46)
Message:
First go at sigwait-based signal handling

Instead of relying on pselect and signal masking to listen for signals,
which glib doesn't support, we spawn a dedicated signal thread that
loops in sigwait. These signals are posted back to the main message loop
which may handle them at will. This avoids the need for complex
reentrant code and sig_atomic_t.

This removes the final pre-select action.

SIGINT doesn't quite work right yet because we can no longer take it in
the middle of an event loop iteration.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • owl.c

    r6b4033f r3535a6e  
    354354}
    355355
    356 void sig_handler(int sig, siginfo_t *si, void *data)
    357 {
    358   if (sig==SIGWINCH) {
     356static void sig_handler(int sig, void *data) {
     357  owl_function_debugmsg("Got signal %d", sig);
     358  /* TODO: These don't need to be re-entrant anymore! */
     359  if (sig == SIGWINCH) {
    359360    /* we can't inturrupt a malloc here, so it just sets a flag
    360361     * schedulding a resize for later
    361362     */
    362363    owl_function_resize();
    363   } else if (sig==SIGPIPE || sig==SIGCHLD) {
    364     /* Set a flag and some info that we got the sigpipe
    365      * so we can record that we got it and why... */
    366     owl_global_set_errsignal(&g, sig, si);
    367   } else if (sig==SIGTERM || sig==SIGHUP) {
     364  } else if (sig == SIGPIPE || sig == SIGCHLD) {
     365    owl_function_error("Got unexpected signal: %d %s",
     366                       sig, (sig == SIGPIPE) ? "SIGPIPE" : "SIGCHLD");
     367  } else if (sig == SIGTERM || sig == SIGHUP) {
    368368    owl_function_quit();
    369   }
    370 }
    371 
    372 void sigint_handler(int sig, siginfo_t *si, void *data)
    373 {
    374   owl_global_set_interrupted(&g);
    375 }
    376 
    377 static int owl_errsignal_pre_select_action(owl_ps_action *a, void *data)
    378 {
    379   siginfo_t si;
    380   int signum;
    381   if ((signum = owl_global_get_errsignal_and_clear(&g, &si)) > 0) {
    382     owl_function_error("Got unexpected signal: %d %s  (code: %d band: %ld  errno: %d)",
    383         signum, signum==SIGPIPE?"SIGPIPE":"SIG????",
    384         si.si_code, si.si_band, si.si_errno);
    385   }
    386   return 0;
     369  } else if (sig == SIGINT) {
     370    owl_input in;
     371    in.ch = in.uch = owl_global_get_startup_tio(&g)->c_cc[VINTR];
     372    owl_process_input_char(in);
     373  }
    387374}
    388375
    389376void owl_register_signal_handlers(void) {
    390   struct sigaction sigact;
    391 
    392   /* signal handler */
    393   /*sigact.sa_handler=sig_handler;*/
    394   sigact.sa_sigaction=sig_handler;
    395   sigemptyset(&sigact.sa_mask);
    396   sigact.sa_flags=SA_SIGINFO;
    397   sigaction(SIGWINCH, &sigact, NULL);
    398   sigaction(SIGALRM, &sigact, NULL);
    399   sigaction(SIGPIPE, &sigact, NULL);
    400   sigaction(SIGTERM, &sigact, NULL);
    401   sigaction(SIGHUP, &sigact, NULL);
    402 
    403   sigact.sa_sigaction=sigint_handler;
    404   sigaction(SIGINT, &sigact, NULL);
     377  sigset_t sigset;
     378
     379  sigemptyset(&sigset);
     380  sigaddset(&sigset, SIGWINCH);
     381  sigaddset(&sigset, SIGALRM);
     382  sigaddset(&sigset, SIGPIPE);
     383  sigaddset(&sigset, SIGTERM);
     384  sigaddset(&sigset, SIGHUP);
     385  sigaddset(&sigset, SIGINT);
     386
     387  owl_signal_init(&sigset, sig_handler, NULL);
    405388}
    406389
     
    479462  g.load_initial_subs = opts.load_initial_subs;
    480463
    481   owl_register_signal_handlers();
    482464  owl_start_curses();
    483465
     
    491473  g_strfreev(argv_copy);
    492474  owl_global_set_haveaim(&g);
     475
     476  owl_register_signal_handlers();
    493477
    494478  /* register STDIN dispatch; throw away return, we won't need it */
     
    591575  g_source_unref(source);
    592576
    593   owl_select_add_pre_select_action(owl_errsignal_pre_select_action, NULL, NULL);
    594 
    595577  owl_function_debugmsg("startup: entering main loop");
    596578  owl_select_run_loop();
Note: See TracChangeset for help on using the changeset viewer.