summaryrefslogtreecommitdiff
path: root/test/unistd/tst-pselect.c
blob: cab945119ae28d408fbf32ad2d4d4ef8be74a3e0 (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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/select.h>

// our SIGALRM handler
void handler(int signum) {
	(void)signum;
	puts("got signal\n");
}

static int
do_test (void)
{
	int rc;
	sigset_t wait_mask, mask_sigchld;
	struct sigaction act;

	// block SIGALRM. We want to handle it only when we're ready
	sigemptyset(&mask_sigchld);
	sigaddset(&mask_sigchld, SIGALRM);
	sigprocmask(SIG_BLOCK,  &mask_sigchld, &wait_mask);
	sigdelset(&wait_mask, SIGALRM);

	// register a signal handler so we can see when the signal arrives
	memset(&act, 0, sizeof(act));
	sigemptyset(&act.sa_mask); // just in case an empty set isn't all 0's (total paranoia)
	act.sa_handler = handler;
	sigaction(SIGALRM, &act, NULL);

	// send ourselves a SIGARLM. It will pend until we unblock that signal in pselect()
	printf("sending ourselves a signal\n");
	kill(getpid(), SIGALRM);

	printf("signal is pending; calling pselect()\n");
	rc = pselect(0, NULL, NULL, NULL, NULL, &wait_mask);
	if (rc != -1 || errno != EINTR) {
		int e = errno;
		printf("pselect() returned %d, errno %d (%s)\n", rc, e, strerror(e));
		exit(1);
	}

	return 0;
}

#define TEST_FUNCTION do_test ()
#include <test-skeleton.c>