1#include <signal.h>
2#include <stdio.h>
3#include "posixtest.h"
4
5/*
6
7 * Copyright (c) 2002, Intel Corporation. All rights reserved.
8 * Created by:  rolla.n.selbak REMOVE-THIS AT intel DOT com
9 * This file is licensed under the GPL license.  For the full content
10 * of this license, see the COPYING file at the top level of this
11 * source tree.
12
13 *  Test that the sigwait() function.
14 *  If prior to the call to sigwait() there are multiple pending instances of
15 *  a single signal number (and it is implementation-defined that the signal
16 *  number DOES NOT support queued signals), then there should be no remaining
17 *  pending signals for that signal number.
18 *  Steps are:
19 *  1)  Block a signal that doesn't support queueing from delivery.
20 *  2)  Raise that signal 4 times.
21 *  3)  Call sigwait()
22 *  4)  Verify it cleared the signal from the pending signals and there
23 *      are no signals left in the pending list.
24 *
25 */
26
27int main(void)
28{
29	sigset_t newmask, pendingset;
30	int sig;
31
32	/* Empty set of blocked signals */
33	if ((sigemptyset(&newmask) == -1) || (sigemptyset(&pendingset) == -1)) {
34		printf("Error in sigemptyset()\n");
35		return PTS_UNRESOLVED;
36	}
37
38	/* Add SIGUSR2 to the set of blocked signals */
39	if (sigaddset(&newmask, SIGUSR2) == -1) {
40		perror("Error in sigaddset()\n");
41		return PTS_UNRESOLVED;
42	}
43
44	/* Block SIGUSR2 */
45	if (sigprocmask(SIG_SETMASK, &newmask, NULL) == -1) {
46		printf("Error in sigprocmask()\n");
47		return PTS_UNRESOLVED;
48	}
49
50	/* Send SIGUSR2 signal 4 times to this process.  Since it is blocked,
51	 * it should be pending. */
52	if (raise(SIGUSR2) != 0) {
53		printf("Could not raise SIGUSR2\n");
54		return PTS_UNRESOLVED;
55	}
56	if (raise(SIGUSR2) != 0) {
57		printf("Could not raise SIGUSR2\n");
58		return PTS_UNRESOLVED;
59	}
60	if (raise(SIGUSR2) != 0) {
61		printf("Could not raise SIGUSR2\n");
62		return PTS_UNRESOLVED;
63	}
64	if (raise(SIGUSR2) != 0) {
65		printf("Could not raise SIGUSR2\n");
66		return PTS_UNRESOLVED;
67	}
68
69	/* Obtain a set of pending signals */
70	if (sigpending(&pendingset) == -1) {
71		printf("Error calling sigpending()\n");
72		return PTS_UNRESOLVED;
73	}
74
75	/* Make sure SIGUSR2 is pending */
76	if (sigismember(&pendingset, SIGUSR2) == 0) {
77		printf("Error: signal SIGUSR2 not pending\n");
78		return PTS_UNRESOLVED;
79	}
80
81	/* Call sigwait */
82	if (sigwait(&newmask, &sig) != 0) {
83		printf("Error in sigwait\n");
84		return PTS_UNRESOLVED;
85	}
86
87	/* Make sure SIGUSR2 is not in the pending list anymore */
88	if (sigpending(&pendingset) == -1) {
89		printf("Error calling sigpending()\n");
90		return PTS_UNRESOLVED;
91	}
92
93	if (sigismember(&pendingset, SIGUSR2) == 1) {
94		printf("Test FAILED\n");
95		return PTS_FAIL;
96	}
97
98	printf("Test PASSED\n");
99	return PTS_PASS;
100
101}
102