12c28215423293e443469a07ae7011135d058b671Garrett Cooper/*
20dc076565f772bb1953209fb69ea150b494aaa40robbiew * Copyright (c) 2002-2003, Intel Corporation. All rights reserved.
30dc076565f772bb1953209fb69ea150b494aaa40robbiew * Created by:  salwan.searty REMOVE-THIS AT intel DOT com
40dc076565f772bb1953209fb69ea150b494aaa40robbiew * This file is licensed under the GPL license.  For the full content
52c28215423293e443469a07ae7011135d058b671Garrett Cooper * of this license, see the COPYING file at the top level of this
60dc076565f772bb1953209fb69ea150b494aaa40robbiew * source tree.
70dc076565f772bb1953209fb69ea150b494aaa40robbiew
82c28215423293e443469a07ae7011135d058b671Garrett Cooper This program tests the assertion that the lowest pending signal will be
90dc076565f772bb1953209fb69ea150b494aaa40robbiew selected for delivery if there are any multiple pending signals in the
100dc076565f772bb1953209fb69ea150b494aaa40robbiew range SIGRTMIN to SIGRTMAX.
110dc076565f772bb1953209fb69ea150b494aaa40robbiew
120dc076565f772bb1953209fb69ea150b494aaa40robbiew Steps:
130dc076565f772bb1953209fb69ea150b494aaa40robbiew - Register for myhandler to be called when any signal between SIGRTMIN
140dc076565f772bb1953209fb69ea150b494aaa40robbiew   and SIGRTMAX is generated, and make
150dc076565f772bb1953209fb69ea150b494aaa40robbiew   sure SA_SIGINFO is set.
160dc076565f772bb1953209fb69ea150b494aaa40robbiew - Also, make sure that all of these signals are added to the handler's
170dc076565f772bb1953209fb69ea150b494aaa40robbiew   signal mask.
180dc076565f772bb1953209fb69ea150b494aaa40robbiew - Initially block all of these signals from the process.
190dc076565f772bb1953209fb69ea150b494aaa40robbiew - Raise all of these signals using sigqueue.
200dc076565f772bb1953209fb69ea150b494aaa40robbiew - Unblock all of these queued signals simultaneously using sigprocmask.
210dc076565f772bb1953209fb69ea150b494aaa40robbiew - Verify that the signals are delivered in order from smallest to
220dc076565f772bb1953209fb69ea150b494aaa40robbiew   biggest.
230dc076565f772bb1953209fb69ea150b494aaa40robbiew */
240dc076565f772bb1953209fb69ea150b494aaa40robbiew
250dc076565f772bb1953209fb69ea150b494aaa40robbiew#define _XOPEN_SOURCE 600
260dc076565f772bb1953209fb69ea150b494aaa40robbiew#define _XOPEN_REALTIME 1
270dc076565f772bb1953209fb69ea150b494aaa40robbiew
280dc076565f772bb1953209fb69ea150b494aaa40robbiew#include <signal.h>
290dc076565f772bb1953209fb69ea150b494aaa40robbiew#include <stdio.h>
300dc076565f772bb1953209fb69ea150b494aaa40robbiew#include <unistd.h>
310dc076565f772bb1953209fb69ea150b494aaa40robbiew#include <stdlib.h>
320dc076565f772bb1953209fb69ea150b494aaa40robbiew#include <errno.h>
330dc076565f772bb1953209fb69ea150b494aaa40robbiew#include "posixtest.h"
340dc076565f772bb1953209fb69ea150b494aaa40robbiew
350dc076565f772bb1953209fb69ea150b494aaa40robbiewint last_signal = 0;
360dc076565f772bb1953209fb69ea150b494aaa40robbiewint test_failed = 0;
370dc076565f772bb1953209fb69ea150b494aaa40robbiew
38354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gaovoid myhandler(int signo, siginfo_t * info, void *context)
39354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao{
40354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	printf("%d, ", signo);
410dc076565f772bb1953209fb69ea150b494aaa40robbiew	if (last_signal >= signo) {
420dc076565f772bb1953209fb69ea150b494aaa40robbiew		test_failed = 1;
430dc076565f772bb1953209fb69ea150b494aaa40robbiew	}
440dc076565f772bb1953209fb69ea150b494aaa40robbiew}
450dc076565f772bb1953209fb69ea150b494aaa40robbiew
464ca2bbdcd3003f3c8df4e6129e9c7b2bd1514f87Cyril Hrubisint main(void)
470dc076565f772bb1953209fb69ea150b494aaa40robbiew{
480dc076565f772bb1953209fb69ea150b494aaa40robbiew	int pid, rtsig;
490dc076565f772bb1953209fb69ea150b494aaa40robbiew	union sigval value;
500dc076565f772bb1953209fb69ea150b494aaa40robbiew	struct sigaction act;
510dc076565f772bb1953209fb69ea150b494aaa40robbiew	sigset_t mask;
520dc076565f772bb1953209fb69ea150b494aaa40robbiew
530dc076565f772bb1953209fb69ea150b494aaa40robbiew	act.sa_flags = SA_SIGINFO;
540dc076565f772bb1953209fb69ea150b494aaa40robbiew	act.sa_sigaction = myhandler;
550dc076565f772bb1953209fb69ea150b494aaa40robbiew	sigemptyset(&act.sa_mask);
560dc076565f772bb1953209fb69ea150b494aaa40robbiew
570dc076565f772bb1953209fb69ea150b494aaa40robbiew	sigemptyset(&mask);
580dc076565f772bb1953209fb69ea150b494aaa40robbiew
59354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	for (rtsig = SIGRTMAX; rtsig >= SIGRTMIN; rtsig--) {
600dc076565f772bb1953209fb69ea150b494aaa40robbiew		sigaddset(&act.sa_mask, rtsig);
610dc076565f772bb1953209fb69ea150b494aaa40robbiew		sighold(rtsig);
620dc076565f772bb1953209fb69ea150b494aaa40robbiew		sigaddset(&mask, rtsig);
630dc076565f772bb1953209fb69ea150b494aaa40robbiew	}
640dc076565f772bb1953209fb69ea150b494aaa40robbiew
650dc076565f772bb1953209fb69ea150b494aaa40robbiew	pid = getpid();
660dc076565f772bb1953209fb69ea150b494aaa40robbiew	value.sival_int = 5;	/* 5 is just an arbitrary value */
670dc076565f772bb1953209fb69ea150b494aaa40robbiew
68354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	for (rtsig = SIGRTMAX; rtsig >= SIGRTMIN; rtsig--) {
690dc076565f772bb1953209fb69ea150b494aaa40robbiew		sigaction(rtsig, &act, 0);
700dc076565f772bb1953209fb69ea150b494aaa40robbiew		if (sigqueue(pid, rtsig, value) != 0) {
71354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			printf
72354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			    ("Test UNRESOLVED: call to sigqueue did not return success\n");
730dc076565f772bb1953209fb69ea150b494aaa40robbiew			return PTS_UNRESOLVED;
740dc076565f772bb1953209fb69ea150b494aaa40robbiew		}
750dc076565f772bb1953209fb69ea150b494aaa40robbiew	}
760dc076565f772bb1953209fb69ea150b494aaa40robbiew
770dc076565f772bb1953209fb69ea150b494aaa40robbiew	sigprocmask(SIG_UNBLOCK, &mask, NULL);
780dc076565f772bb1953209fb69ea150b494aaa40robbiew	printf("\n");
790dc076565f772bb1953209fb69ea150b494aaa40robbiew
800dc076565f772bb1953209fb69ea150b494aaa40robbiew	if (test_failed == 1) {
81354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		printf
82354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		    ("Test FAILED: A pending signal was delivered even though a smaller one is still pending.\n");
830dc076565f772bb1953209fb69ea150b494aaa40robbiew		return PTS_FAIL;
840dc076565f772bb1953209fb69ea150b494aaa40robbiew	}
850dc076565f772bb1953209fb69ea150b494aaa40robbiew
860dc076565f772bb1953209fb69ea150b494aaa40robbiew	return PTS_PASS;
87ec6edca7aa42b6affd989ef91b5897f96795e40fChris Dearman}
88