aboutsummaryrefslogtreecommitdiff
path: root/testcases/open_posix_testsuite/conformance/interfaces/sigaction/23-4.c
blob: 43de3e27cb866c26b115d5973df918698302b49d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
* Copyright (c) 2005, Bull S.A..  All rights reserved.
* Created by: Sebastien Decugis

* This program is free software; you can redistribute it and/or modify it
* under the terms of version 2 of the GNU General Public License as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it would be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

* This sample test aims to check the following assertions:
*
* If SA_NODEFER is not set in sa_flags, the caught signal is added to the
* thread's signal mask during the handler execution.

* The steps are:
* -> register a signal handler for SIGCHLD
* -> raise SIGCHLD
* -> In handler, check for reentrance then raise SIGCHLD again.

* The test fails if signal handler if reentered or signal is not pending when raised again.
*/


/******************************************************************************/
/*************************** standard includes ********************************/
/******************************************************************************/
#include <pthread.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <signal.h>
#include <errno.h>

/******************************************************************************/
/***************************   Test framework   *******************************/
/******************************************************************************/
#include "../testfrmw/testfrmw.h"
#include "../testfrmw/testfrmw.c"
/* This header is responsible for defining the following macros:
 * UNRESOLVED(ret, descr);
 *    where descr is a description of the error and ret is an int
 *   (error code for example)
 * FAILED(descr);
 *    where descr is a short text saying why the test has failed.
 * PASSED();
 *    No parameter.
 *
 * Both three macros shall terminate the calling process.
 * The testcase shall not terminate in any other maneer.
 *
 * The other file defines the functions
 * void output_init()
 * void output(char * string, ...)
 *
 * Those may be used to output information.
 */

/******************************************************************************/
/**************************** Configuration ***********************************/
/******************************************************************************/
#ifndef VERBOSE
#define VERBOSE 1
#endif

#define SIGNAL SIGCHLD

/******************************************************************************/
/***************************    Test case   ***********************************/
/******************************************************************************/

int called = 0;

void handler(int sig LTP_ATTRIBUTE_UNUSED)
{
	int ret;
	sigset_t pending;
	called++;

	if (called == 2) {
		FAILED("Signal was not masked in signal handler");
	}

	if (called == 1) {

		/* Raise the signal again. It should be masked */
		ret = raise(SIGNAL);

		if (ret != 0) {
			UNRESOLVED(ret, "Failed to raise SIGCHLD again");
		}

		/* check the signal is pending */
		ret = sigpending(&pending);

		if (ret != 0) {
			UNRESOLVED(ret, "Failed to get pending signal set");
		}

		ret = sigismember(&pending, SIGNAL);

		if (ret != 1) {
			FAILED("signal is not pending");
		}
	}

	called++;
}

/* main function */
int main(void)
{
	int ret;

	struct sigaction sa;

	/* Initialize output */
	output_init();

	/* Set the signal handler */
	sa.sa_flags = 0;

	sa.sa_handler = handler;

	ret = sigemptyset(&sa.sa_mask);

	if (ret != 0) {
		UNRESOLVED(ret, "Failed to empty signal set");
	}

	/* Install the signal handler for SIGCHLD */
	ret = sigaction(SIGNAL, &sa, 0);

	if (ret != 0) {
		UNRESOLVED(ret, "Failed to set signal handler");
	}

	ret = raise(SIGNAL);

	if (ret != 0) {
		UNRESOLVED(ret, "Failed to raise SIGCHLD");
	}

	while (called != 4)
		sched_yield();

	/* Test passed */
#if VERBOSE > 0

	output("Test passed\n");

#endif

	PASSED;
}