Fix memory handling in strxfrm_l [BZ #16009]
[glibc.git] / misc / tst-pselect.c
blob095d794cb2735e18c233d8c6c550f06580516689
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>
7 #include <stdlib.h>
10 static volatile int handler_called;
12 static void
13 handler (int sig)
15 handler_called = 1;
19 static int
20 do_test (void)
22 struct sigaction sa;
23 sa.sa_handler = handler;
24 sa.sa_flags = 0;
25 sigemptyset (&sa.sa_mask);
27 if (sigaction (SIGUSR1, &sa, NULL) != 0)
29 puts ("sigaction failed");
30 return 1;
33 sa.sa_handler = SIG_IGN;
34 sa.sa_flags = SA_NOCLDWAIT;
36 if (sigaction (SIGCHLD, &sa, NULL) != 0)
38 puts ("2nd sigaction failed");
39 return 1;
42 sigset_t ss_usr1;
43 sigemptyset (&ss_usr1);
44 sigaddset (&ss_usr1, SIGUSR1);
45 if (sigprocmask (SIG_BLOCK, &ss_usr1, NULL) != 0)
47 puts ("sigprocmask failed");
48 return 1;
51 int fds[2][2];
53 if (pipe (fds[0]) != 0 || pipe (fds[1]) != 0)
55 puts ("pipe failed");
56 return 1;
59 fd_set rfds;
60 FD_ZERO (&rfds);
62 sigset_t ss;
63 sigprocmask (SIG_SETMASK, NULL, &ss);
64 sigdelset (&ss, SIGUSR1);
66 struct timespec to = { .tv_sec = 0, .tv_nsec = 500000000 };
68 pid_t parent = getpid ();
69 pid_t p = fork ();
70 if (p == 0)
72 close (fds[0][1]);
73 close (fds[1][0]);
75 FD_SET (fds[0][0], &rfds);
77 int e;
80 if (getppid () != parent)
81 exit (2);
83 errno = 0;
84 e = pselect (fds[0][0] + 1, &rfds, NULL, NULL, &to, &ss);
86 while (e == 0);
88 if (e != -1)
90 puts ("child: pselect did not fail");
91 return 0;
93 if (errno != EINTR)
95 puts ("child: pselect did not set errno to EINTR");
96 return 0;
99 TEMP_FAILURE_RETRY (write (fds[1][1], "foo", 3));
101 exit (0);
104 close (fds[0][0]);
105 close (fds[1][1]);
107 FD_SET (fds[1][0], &rfds);
109 kill (p, SIGUSR1);
111 int e = pselect (fds[1][0] + 1, &rfds, NULL, NULL, NULL, &ss);
112 if (e == -1)
114 puts ("parent: pselect failed");
115 return 1;
117 if (e != 1)
119 puts ("parent: pselect did not report readable fd");
120 return 1;
122 if (!FD_ISSET (fds[1][0], &rfds))
124 puts ("parent: pselect reports wrong fd");
125 return 1;
128 return 0;
131 #define TEST_FUNCTION do_test ()
132 #include "../test-skeleton.c"