1*db3296cfSderaadt /* $OpenBSD: signodefer.c,v 1.3 2003/07/31 21:48:06 deraadt Exp $ */
28bf7caf4Smarc /* PUBLIC DOMAIN Oct 2002 <marc@snafu.org> */
38bf7caf4Smarc
48bf7caf4Smarc /*
58bf7caf4Smarc * test signal delivery of active signals (SA_NODEFER)
68bf7caf4Smarc */
78bf7caf4Smarc
88bf7caf4Smarc #include <signal.h>
98bf7caf4Smarc #include <stdio.h>
108bf7caf4Smarc #include <unistd.h>
118bf7caf4Smarc
128bf7caf4Smarc #include "test.h"
138bf7caf4Smarc
148bf7caf4Smarc volatile sig_atomic_t sigactive;
158bf7caf4Smarc volatile sig_atomic_t sigcount;
168bf7caf4Smarc volatile sig_atomic_t was_active;
178bf7caf4Smarc
18*db3296cfSderaadt static void
act_handler(int signal,siginfo_t * siginfo,void * context)198bf7caf4Smarc act_handler(int signal, siginfo_t *siginfo, void *context)
208bf7caf4Smarc {
218bf7caf4Smarc char *str;
228bf7caf4Smarc
238bf7caf4Smarc /* how many times has the handler been called */
248bf7caf4Smarc was_active += sigactive++;
258bf7caf4Smarc sigcount += 1;
268bf7caf4Smarc
278bf7caf4Smarc /* verify siginfo since we asked for it. */
288bf7caf4Smarc ASSERT(siginfo != NULL);
298bf7caf4Smarc
308bf7caf4Smarc asprintf(&str,
31f9fcec45Smarc "%sact_handler/%d, signal %d, siginfo %p, context %p\n",
328bf7caf4Smarc was_active ? "[recurse] " : "",
338bf7caf4Smarc sigcount, signal, siginfo, context);
348bf7caf4Smarc CHECKe(write(STDOUT_FILENO, str, strlen(str)));
358bf7caf4Smarc /* Odd times entered send ourself the same signal */
368bf7caf4Smarc if (sigcount & 1)
378bf7caf4Smarc CHECKe(kill(getpid(), SIGUSR1));
388bf7caf4Smarc
398bf7caf4Smarc sigactive = 0;
408bf7caf4Smarc }
418bf7caf4Smarc
428bf7caf4Smarc int
main(int argc,char ** argv)438bf7caf4Smarc main(int argc, char **argv)
448bf7caf4Smarc {
458bf7caf4Smarc struct sigaction act;
468bf7caf4Smarc
478bf7caf4Smarc act.sa_sigaction = act_handler;
488bf7caf4Smarc sigemptyset(&act.sa_mask);
498bf7caf4Smarc act.sa_flags = SA_SIGINFO;
508bf7caf4Smarc ASSERT(sigaction(SIGUSR1, &act, NULL) == 0);
518bf7caf4Smarc
528bf7caf4Smarc /* see if the signal handler recurses */
538bf7caf4Smarc CHECKe(kill(getpid(), SIGUSR1));
548bf7caf4Smarc sleep(1);
558bf7caf4Smarc ASSERT(was_active == 0);
568bf7caf4Smarc
578bf7caf4Smarc /* allow recursive handlers, see that it is handled right */
588bf7caf4Smarc act.sa_flags |= SA_NODEFER;
598bf7caf4Smarc ASSERT(sigaction(SIGUSR1, &act, NULL) == 0);
608bf7caf4Smarc
618bf7caf4Smarc /* see if the signal handler recurses */
628bf7caf4Smarc CHECKe(kill(getpid(), SIGUSR1));
638bf7caf4Smarc sleep(1);
648bf7caf4Smarc ASSERT(was_active == 1);
658bf7caf4Smarc
668bf7caf4Smarc SUCCEED;
678bf7caf4Smarc }
68