Home | History | Annotate | Download | only in sigaction
      1 /*
      2 
      3  * Copyright (c) 2002-2003, Intel Corporation. All rights reserved.
      4  * Created by:  rusty.lynch REMOVE-THIS AT intel DOT com
      5  * This file is licensed under the GPL license.  For the full content
      6  * of this license, see the COPYING file at the top level of this
      7  * source tree.
      8 
      9   Test case for assertion #22 of the sigaction system call that verifies
     10   that if the SA_NODEFER flag is set for a given signal, then when the
     11   sa_sigaction signal-catching function is entered, then the signal that
     12   was caught is not added to the signal mask by raising that signal in the
     13   signal handler and verifying that the handler is reentered.
     14 
     15   Steps:
     16   1. Fork a new process
     17   2. (parent) wait for child
     18   3. (child) Setup a signal handler for SIGHUP with SA_NODEFER set
     19      in the sa_flags field
     20   4. (child) raise SIGHUP
     21   5. (child, signal handler) increment handler count
     22   6. (child, signal handler) if count is 1 then raise SIGHUP
     23   7. (child, signal handler) if count is 1 then set error variable
     24   8. (child) if error is set then return -1, else return 0
     25   6. (parent - returning from wait) If child returned 0 then exit 0,
     26      otherwise exit -1.
     27 */
     28 
     29 #define _XOPEN_SOURCE 600
     30 
     31 #include <signal.h>
     32 #include <stdio.h>
     33 #include <stdlib.h>
     34 #include <sys/wait.h>
     35 #include <unistd.h>
     36 #include "posixtest.h"
     37 
     38 int handler_count = 0;
     39 
     40 void handler(int signo)
     41 {
     42 	static int inside_handler = 0;
     43 
     44 	printf("SIGHUP caught\n");
     45 	if (inside_handler) {
     46 		printf("Signal caught while inside handler\n");
     47 		exit(0);
     48 	}
     49 
     50 	inside_handler++;
     51 	handler_count++;
     52 
     53 	if (handler_count == 1) {
     54 		printf("Raising SIGHUP\n");
     55 		raise(SIGHUP);
     56 		printf("Returning from raising SIGHUP\n");
     57 	}
     58 
     59 	inside_handler--;
     60 }
     61 
     62 int main(void)
     63 {
     64 	if (fork() == 0) {
     65 		/* child */
     66 
     67 		struct sigaction act;
     68 
     69 		act.sa_handler = handler;
     70 		act.sa_flags = SA_NODEFER;
     71 		sigemptyset(&act.sa_mask);
     72 		if (sigaction(SIGHUP, &act, 0) == -1) {
     73 			perror("Unexpected error while attempting to "
     74 			       "setup test pre-conditions");
     75 			return PTS_UNRESOLVED;
     76 		}
     77 
     78 		if (raise(SIGHUP) == -1) {
     79 			perror("Unexpected error while attempting to "
     80 			       "setup test pre-conditions");
     81 			return PTS_UNRESOLVED;
     82 		}
     83 
     84 		return PTS_FAIL;
     85 	} else {
     86 		int s;
     87 
     88 		/* parent */
     89 		if (wait(&s) == -1) {
     90 			perror("Unexpected error while setting up test "
     91 			       "pre-conditions");
     92 			return PTS_UNRESOLVED;
     93 		}
     94 
     95 		if (!WEXITSTATUS(s)) {
     96 			printf("Test PASSED\n");
     97 			return PTS_PASS;
     98 		}
     99 	}
    100 
    101 	printf("Test FAILED\n");
    102 	return PTS_FAIL;
    103 }
    104