blob: a0445ca0bb7f0e89bcc27d31ced2ebd42010e398 [file] [log] [blame]
robbiew0dc07652005-06-03 16:29:48 +00001/*
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
Garrett Cooper2c282152010-12-16 00:55:50 -08006 * of this license, see the COPYING file at the top level of this
robbiew0dc07652005-06-03 16:29:48 +00007 * source tree.
8
Garrett Cooper2c282152010-12-16 00:55:50 -08009 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
robbiew0dc07652005-06-03 16:29:48 +000013 signal handler and verifying that the handler is reentered.
Garrett Cooper2c282152010-12-16 00:55:50 -080014
robbiew0dc07652005-06-03 16:29:48 +000015 Steps:
16 1. Fork a new process
17 2. (parent) wait for child
18 3. (child) Setup a signal handler for SIGINT with SA_NODEFER set
19 in the sa_flags field
20 4. (child) raise SIGINT
21 5. (child, signal handler) increment handler count
22 6. (child, signal handler) if count is 1 then raise SIGINT
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
38int handler_count = 0;
39
40void handler(int signo)
41{
42 static int inside_handler = 0;
43
44 printf("SIGINT 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 SIGINT\n");
55 raise(SIGINT);
56 printf("Returning from raising SIGINT\n");
57 }
58
59 inside_handler--;
60}
61
62int main()
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(SIGINT, &act, 0) == -1) {
73 perror("Unexpected error while attempting to "
74 "setup test pre-conditions");
75 return PTS_UNRESOLVED;
76 }
Garrett Cooper2c282152010-12-16 00:55:50 -080077
robbiew0dc07652005-06-03 16:29:48 +000078 if (raise(SIGINT) == -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 {
Garrett Cooper2c282152010-12-16 00:55:50 -080086 int s;
robbiew0dc07652005-06-03 16:29:48 +000087
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");
Garrett Cooper2c282152010-12-16 00:55:50 -0800102 return PTS_FAIL;
Chris Dearmanec6edca2012-10-17 19:54:01 -0700103}