* pthread_mutex_init.c (__pthread_mutex_init): Move some
[glibc.git] / misc / tst-pselect.c
blob123c31912e6edf8940c4508a26684fe6cfd03fb2
1 #include <errno.h>
2 #include <signal.h>
3 #include <stdio.h>
4 #include <unistd.h>
5 #include <sys/select.h>
6 #include <sys/wait.h>
9 static volatile int handler_called;
11 static void
12 handler (int sig)
14 handler_called = 1;
18 static int
19 do_test (void)
21 struct sigaction sa;
22 sa.sa_handler = handler;
23 sa.sa_flags = 0;
24 sigemptyset (&sa.sa_mask);
26 if (sigaction (SIGUSR1, &sa, NULL) != 0)
28 puts ("sigaction failed");
29 return 1;
32 if (sigblock (SIGUSR1) != 0)
34 puts ("sigblock failed");
35 return 1;
38 int fds[2][2];
40 if (pipe (fds[0]) != 0 || pipe (fds[1]) != 0)
42 puts ("pipe failed");
43 return 1;
46 fd_set rfds;
47 FD_ZERO (&rfds);
49 sigset_t ss;
50 sigprocmask (SIG_SETMASK, NULL, &ss);
51 sigdelset (&ss, SIGUSR1);
53 struct timespec to = { .tv_sec = 0, .tv_nsec = 500000000 };
55 pid_t p = fork ();
56 if (p == 0)
58 close (fds[0][1]);
59 close (fds[1][0]);
61 FD_SET (fds[0][0], &rfds);
63 int e;
66 errno = 0;
67 e = pselect (fds[0][0] + 1, &rfds, NULL, NULL, &to, &ss);
69 while (e == 0);
71 if (e != -1)
73 puts ("child: pselect did not fail");
74 return 0;
76 if (errno != EINTR)
78 puts ("child: pselect did not set errno to EINTR");
79 return 0;
82 TEMP_FAILURE_RETRY (write (fds[1][1], "foo", 3));
84 exit (0);
87 close (fds[0][0]);
88 close (fds[1][1]);
90 FD_SET (fds[1][0], &rfds);
92 kill (p, SIGUSR1);
94 int e = pselect (fds[1][0] + 1, &rfds, NULL, NULL, NULL, &ss);
95 if (e == -1)
97 puts ("parent: pselect failed");
98 return 1;
100 if (e != 1)
102 puts ("parent: pselect did not report readable fd");
103 return 1;
105 if (!FD_ISSET (fds[1][0], &rfds))
107 puts ("parent: pselect reports wrong fd");
108 return 1;
111 if (TEMP_FAILURE_RETRY (waitpid (p, NULL, 0)) != p)
113 puts ("waitpid failed");
114 return 1;
117 return 0;
120 #define TEST_FUNCTION do_test ()
121 #include "../test-skeleton.c"