* time/time.h [__need_timespec et al]: Include <bits/types.h> here too.
[glibc.git] / misc / tst-pselect.c
blob55253ded84668184715471bf983e02accced679c
1 #include <errno.h>
2 #include <signal.h>
3 #include <stdio.h>
4 #include <sys/select.h>
5 #include <sys/wait.h>
8 static volatile int handler_called;
10 static void
11 handler (int sig)
13 handler_called = 1;
17 static int
18 do_test (void)
20 struct sigaction sa;
21 sa.sa_handler = handler;
22 sa.sa_flags = 0;
23 sigemptyset (&sa.sa_mask);
25 if (sigaction (SIGUSR1, &sa, NULL) != 0)
27 puts ("sigaction failed");
28 return 1;
31 if (sigblock (SIGUSR1) != 0)
33 puts ("sigblock failed");
34 return 1;
37 int fds[2][2];
39 if (pipe (fds[0]) != 0 || pipe (fds[1]) != 0)
41 puts ("pipe failed");
42 return 1;
45 fd_set rfds;
46 FD_ZERO (&rfds);
48 sigset_t ss;
49 sigprocmask (SIG_SETMASK, NULL, &ss);
50 sigdelset (&ss, SIGUSR1);
52 struct timespec to = { .tv_sec = 0, .tv_nsec = 500000000 };
54 pid_t p = fork ();
55 if (p == 0)
57 close (fds[0][1]);
58 close (fds[1][0]);
60 FD_SET (fds[0][0], &rfds);
62 int e;
65 errno = 0;
66 e = pselect (fds[0][0] + 1, &rfds, NULL, NULL, &to, &ss);
68 while (e == 0);
70 if (e != -1)
72 puts ("child: pselect did not fail");
73 return 0;
75 if (errno != EINTR)
77 puts ("child: pselect did not set errno to EINTR");
78 return 0;
81 TEMP_FAILURE_RETRY (write (fds[1][1], "foo", 3));
83 exit (0);
86 close (fds[0][0]);
87 close (fds[1][1]);
89 FD_SET (fds[1][0], &rfds);
91 kill (p, SIGUSR1);
93 int e = pselect (fds[1][0] + 1, &rfds, NULL, NULL, NULL, &ss);
94 if (e == -1)
96 puts ("parent: pselect failed");
97 return 1;
99 if (e != 1)
101 puts ("parent: pselect did not report readable fd");
102 return 1;
104 if (!FD_ISSET (fds[1][0], &rfds))
106 puts ("parent: pselect reports wrong fd");
107 return 1;
110 if (TEMP_FAILURE_RETRY (waitpid (p, NULL, 0)) != p)
112 puts ("waitpid failed");
113 return 1;
116 return 0;
119 #define TEST_FUNCTION do_test ()
120 #include "../test-skeleton.c"