Use sigaction instead of signal.

SIGCHLD and SIGALRM are now exclusives (one mask other)
This commit is contained in:
Philippe Pepiot 2011-01-04 03:02:29 +01:00
parent c817b7277b
commit 0a2c1f9d13

View File

@ -146,6 +146,7 @@ quit(void)
void
status_timer(void)
{
struct sigaction sa;
struct itimerval timer = {
.it_interval = {
.tv_usec = 0,
@ -157,7 +158,11 @@ status_timer(void)
},
};
signal(SIGALRM, &signal_handle);
memset(&sa, 0, sizeof(sa));
sa.sa_handler = signal_handle;
sigemptyset(&sa.sa_mask);
sigaddset(&sa.sa_mask, SIGCHLD);
sigaction(SIGALRM, &sa, NULL);
setitimer(ITIMER_REAL, &timer, NULL);
}
@ -405,17 +410,12 @@ signal_handle(int sig)
exit(EXIT_SUCCESS);
break;
case SIGCHLD:
/* re-set signal handler and wait childs */
if (signal(SIGCHLD, &signal_handle) == SIG_ERR)
warn("signal(%d)", SIGCHLD);
/* wait childs */
while ((pid = waitpid(-1, NULL, WNOHANG)) > 0)
if (pid == conf.status_pid)
conf.status_pid = -1;
break;
case SIGALRM:
/* re-set signal handler */
if (signal(SIGALRM, &signal_handle) == SIG_ERR)
warn("signal(%d)", SIGALRM);
/* exec status script (only if still not running) */
if (conf.status_pid == (pid_t)-1)
conf.status_pid = spawn(conf.status_path);
@ -437,7 +437,7 @@ main(int argc, char **argv)
char *ol = "csgVS";
extern char *optarg;
extern int optind;
int sigs[] = { SIGTERM, SIGQUIT, SIGCHLD };
struct sigaction sa;
argv_global = xstrdup(argv[0]);
sprintf(conf.confpath, "%s/"DEF_CONF, getenv("HOME"));
@ -518,17 +518,22 @@ main(int argc, char **argv)
if(!(dpy = XOpenDisplay(NULL)))
errx(EXIT_FAILURE, "cannot open X server.");
/* Set signal handler */
for (i = 0; i < (int)LEN(sigs); i++)
if (signal(sigs[i], &signal_handle) == SIG_ERR)
warn("signal(%d)", sigs[i]);
/* Check if an other WM is already running; set the error handler */
XSetErrorHandler(errorhandler);
/* Let's Go ! */
init();
scan();
/* set signal handler */
memset(&sa, 0, sizeof(sa));
sa.sa_handler = signal_handle;
sigemptyset(&sa.sa_mask);
sigaction(SIGQUIT, &sa, NULL);
sigaction(SIGTERM, &sa, NULL);
sigaddset(&sa.sa_mask, SIGALRM);
sigaction(SIGCHLD, &sa, NULL);
if (estatus)
status_timer();
mainloop();