7-1.c revision c911fa64e3ec4bb25012a36ebdd2ad8f747d9ea8
1/*
2 * Copyright (c) 2002-2003, Intel Corporation. All rights reserved.
3 * Created by:  salwan.searty 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 This program tests the assertion that if SA_SIGINFO is set, then the
9 signal shall be queued to the receiving process and that sigqueue returns 0.
10
11 Steps:
12 - Register for myhandler to be called when SIGTOTEST is called, and make
13   sure SA_SIGINFO is set.
14 - Block signal SIGTOTEST from the process.
15 - Using sigqueue(), send NUMCALLS instances of SIGTOTEST to the current
16   process.
17 - Call sigwaitinfo() NUMCALLS times, and verify that the queued signals are
18   selected in FIFO order.
19
20 */
21
22#define _XOPEN_SOURCE 600
23#define _XOPEN_REALTIME 1
24#define SIGTOTEST SIGRTMIN
25#define NUMCALLS 5
26
27#include <signal.h>
28#include <stdio.h>
29#include <unistd.h>
30#include <stdlib.h>
31#include <errno.h>
32#include "posixtest.h"
33
34int counter = 0;
35
36void myhandler(int signo, siginfo_t *info, void *context) {
37	counter++;
38}
39
40int main()
41{
42	int pid, i;
43	union sigval value;
44	struct sigaction act;
45	sigset_t selectset;
46	siginfo_t info;
47
48	act.sa_flags = SA_SIGINFO;
49	act.sa_sigaction = myhandler;
50	sigemptyset(&act.sa_mask);
51	sigaction(SIGTOTEST, &act, 0);
52
53	pid = getpid();
54
55	sighold(SIGTOTEST);
56
57	for (i=NUMCALLS; i>0; i--) {
58		value.sival_int = i;
59		if (sigqueue(pid, SIGTOTEST, value) != 0) {
60			printf("Test FAILED: call to sigqueue did not return success\n");
61			return PTS_FAIL;
62		}
63	}
64
65        sigemptyset(&selectset);
66        sigaddset(&selectset, SIGTOTEST);
67
68	for (i=NUMCALLS; i>0; i--) {
69	        if (sigwaitinfo(&selectset, &info) != SIGTOTEST) {
70	                perror("sigwaitinfo() returned signal other than SIGTOTEST\n");
71	                return PTS_UNRESOLVED;
72	        }
73	        if (info.si_value.sival_int != i) {
74	                printf("Test FAILED: The queued value %d was dequeued before "
75                        "the queued value %d even though %d was queued first.\n",
76			info.si_value.sival_int, i, i);
77	                return PTS_FAIL;
78	        }
79	}
80
81	return PTS_PASS;
82}
83