]> git.ipfire.org Git - thirdparty/glibc.git/blob - misc/tst-pselect.c
Avoid deprecated sigblock in misc/tst-pselect.c.
[thirdparty/glibc.git] / misc / tst-pselect.c
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>
8
9
10 static volatile int handler_called;
11
12 static void
13 handler (int sig)
14 {
15 handler_called = 1;
16 }
17
18
19 static int
20 do_test (void)
21 {
22 struct sigaction sa;
23 sa.sa_handler = handler;
24 sa.sa_flags = 0;
25 sigemptyset (&sa.sa_mask);
26
27 if (sigaction (SIGUSR1, &sa, NULL) != 0)
28 {
29 puts ("sigaction failed");
30 return 1;
31 }
32
33 sa.sa_handler = SIG_IGN;
34 sa.sa_flags = SA_NOCLDWAIT;
35
36 if (sigaction (SIGCHLD, &sa, NULL) != 0)
37 {
38 puts ("2nd sigaction failed");
39 return 1;
40 }
41
42 sigset_t ss_usr1;
43 sigemptyset (&ss_usr1);
44 sigaddset (&ss_usr1, SIGUSR1);
45 if (sigprocmask (SIG_BLOCK, &ss_usr1, NULL) != 0)
46 {
47 puts ("sigprocmask failed");
48 return 1;
49 }
50
51 int fds[2][2];
52
53 if (pipe (fds[0]) != 0 || pipe (fds[1]) != 0)
54 {
55 puts ("pipe failed");
56 return 1;
57 }
58
59 fd_set rfds;
60 FD_ZERO (&rfds);
61
62 sigset_t ss;
63 sigprocmask (SIG_SETMASK, NULL, &ss);
64 sigdelset (&ss, SIGUSR1);
65
66 struct timespec to = { .tv_sec = 0, .tv_nsec = 500000000 };
67
68 pid_t parent = getpid ();
69 pid_t p = fork ();
70 if (p == 0)
71 {
72 close (fds[0][1]);
73 close (fds[1][0]);
74
75 FD_SET (fds[0][0], &rfds);
76
77 int e;
78 do
79 {
80 if (getppid () != parent)
81 exit (2);
82
83 errno = 0;
84 e = pselect (fds[0][0] + 1, &rfds, NULL, NULL, &to, &ss);
85 }
86 while (e == 0);
87
88 if (e != -1)
89 {
90 puts ("child: pselect did not fail");
91 return 0;
92 }
93 if (errno != EINTR)
94 {
95 puts ("child: pselect did not set errno to EINTR");
96 return 0;
97 }
98
99 TEMP_FAILURE_RETRY (write (fds[1][1], "foo", 3));
100
101 exit (0);
102 }
103
104 close (fds[0][0]);
105 close (fds[1][1]);
106
107 FD_SET (fds[1][0], &rfds);
108
109 kill (p, SIGUSR1);
110
111 int e = pselect (fds[1][0] + 1, &rfds, NULL, NULL, NULL, &ss);
112 if (e == -1)
113 {
114 puts ("parent: pselect failed");
115 return 1;
116 }
117 if (e != 1)
118 {
119 puts ("parent: pselect did not report readable fd");
120 return 1;
121 }
122 if (!FD_ISSET (fds[1][0], &rfds))
123 {
124 puts ("parent: pselect reports wrong fd");
125 return 1;
126 }
127
128 return 0;
129 }
130
131 #define TEST_FUNCTION do_test ()
132 #include "../test-skeleton.c"