]> git.ipfire.org Git - thirdparty/glibc.git/blob - misc/tst-pselect.c
aarch64: add STO_AARCH64_VARIANT_PCS and DT_AARCH64_VARIANT_PCS
[thirdparty/glibc.git] / misc / tst-pselect.c
1 /* Copyright (C) 2006-2018 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <http://www.gnu.org/licenses/>. */
17
18 #include <errno.h>
19 #include <signal.h>
20 #include <stdio.h>
21 #include <unistd.h>
22 #include <sys/select.h>
23 #include <sys/wait.h>
24 #include <stdlib.h>
25
26
27 static volatile int handler_called;
28
29 static void
30 handler (int sig)
31 {
32 handler_called = 1;
33 }
34
35
36 static int
37 do_test (void)
38 {
39 struct sigaction sa;
40 sa.sa_handler = handler;
41 sa.sa_flags = 0;
42 sigemptyset (&sa.sa_mask);
43
44 if (sigaction (SIGUSR1, &sa, NULL) != 0)
45 {
46 puts ("sigaction failed");
47 return 1;
48 }
49
50 sa.sa_handler = SIG_IGN;
51 if (sigaction (SIGCHLD, &sa, NULL) != 0)
52 {
53 puts ("2nd sigaction failed");
54 return 1;
55 }
56
57 sigset_t ss_usr1;
58 sigemptyset (&ss_usr1);
59 sigaddset (&ss_usr1, SIGUSR1);
60 if (sigprocmask (SIG_BLOCK, &ss_usr1, NULL) != 0)
61 {
62 puts ("sigprocmask failed");
63 return 1;
64 }
65
66 int fds[2][2];
67
68 if (pipe (fds[0]) != 0 || pipe (fds[1]) != 0)
69 {
70 puts ("pipe failed");
71 return 1;
72 }
73
74 fd_set rfds;
75 FD_ZERO (&rfds);
76
77 sigset_t ss;
78 sigprocmask (SIG_SETMASK, NULL, &ss);
79 sigdelset (&ss, SIGUSR1);
80
81 struct timespec to = { .tv_sec = 0, .tv_nsec = 500000000 };
82
83 pid_t parent = getpid ();
84 pid_t p = fork ();
85 if (p == 0)
86 {
87 close (fds[0][1]);
88 close (fds[1][0]);
89
90 FD_SET (fds[0][0], &rfds);
91
92 int e;
93 do
94 {
95 if (getppid () != parent)
96 exit (2);
97
98 errno = 0;
99 e = pselect (fds[0][0] + 1, &rfds, NULL, NULL, &to, &ss);
100 }
101 while (e == 0);
102
103 if (e != -1)
104 {
105 puts ("child: pselect did not fail");
106 return 0;
107 }
108 if (errno != EINTR)
109 {
110 puts ("child: pselect did not set errno to EINTR");
111 return 0;
112 }
113
114 TEMP_FAILURE_RETRY (write (fds[1][1], "foo", 3));
115
116 exit (0);
117 }
118
119 close (fds[0][0]);
120 close (fds[1][1]);
121
122 FD_SET (fds[1][0], &rfds);
123
124 kill (p, SIGUSR1);
125
126 int e = pselect (fds[1][0] + 1, &rfds, NULL, NULL, NULL, &ss);
127 if (e == -1)
128 {
129 puts ("parent: pselect failed");
130 return 1;
131 }
132 if (e != 1)
133 {
134 puts ("parent: pselect did not report readable fd");
135 return 1;
136 }
137 if (!FD_ISSET (fds[1][0], &rfds))
138 {
139 puts ("parent: pselect reports wrong fd");
140 return 1;
141 }
142
143 return 0;
144 }
145
146 #define TEST_FUNCTION do_test ()
147 #include "../test-skeleton.c"