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