Debian Codegraph @

lib/pselect.c

raw
1 /* pselect - synchronous I/O multiplexing
2
3 Copyright 2011-2026 Free Software Foundation, Inc.
4
5 This file is part of gnulib.
6
7 This file is free software: you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as
9 published by the Free Software Foundation; either version 2.1 of the
10 License, or (at your option) any later version.
11
12 This file is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with this program. If not, see <https://www.gnu.org/licenses/>. */
19
20 /* written by Paul Eggert */
21
22 #include <config.h>
23
24 #include <sys/select.h>
25
26 #include <errno.h>
27 #include <signal.h>
28
29 /* Examine the size-NFDS file descriptor sets in RFDS, WFDS, and XFDS
30 to see whether some of their descriptors are ready for reading,
31 ready for writing, or have exceptions pending. Wait for at most
32 TIMEOUT seconds, and use signal mask SIGMASK while waiting. A null
33 pointer parameter stands for no descriptors, an infinite timeout,
34 or an unaffected signal mask. */
35
36 #if !HAVE_PSELECT
37
38 int
39 pselect (int nfds, fd_set *restrict rfds,
40 fd_set *restrict wfds, fd_set *restrict xfds,
41 struct timespec const *restrict timeout,
42 sigset_t const *restrict sigmask)
43 {
44 if (nfds < 0 || nfds > FD_SETSIZE)
45 {
46 errno = EINVAL;
47 return -1;
48 }
49
50 struct timeval tv;
51 struct timeval *tvp;
52 if (timeout)
53 {
54 if (! (0 <= timeout->tv_nsec && timeout->tv_nsec < 1000000000))
55 {
56 errno = EINVAL;
57 return -1;
58 }
59
60 tv = (struct timeval) {
61 .tv_sec = timeout->tv_sec,
62 .tv_usec = (timeout->tv_nsec + 999) / 1000
63 };
64 tvp = &tv;
65 }
66 else
67 tvp = NULL;
68
69 sigset_t origmask;
70 /* Signal mask munging should be atomic, but this is the best we can
71 do in this emulation. */
72 if (sigmask)
73 pthread_sigmask (SIG_SETMASK, sigmask, &origmask);
74
75 int select_result = select (nfds, rfds, wfds, xfds, tvp);
76
77 if (sigmask)
78 {
79 int select_errno = errno;
80 pthread_sigmask (SIG_SETMASK, &origmask, NULL);
81 errno = select_errno;
82 }
83
84 return select_result;
85 }
86
87 #else /* HAVE_PSELECT */
88 # include <unistd.h>
89 # undef pselect
90
91 int
92 rpl_pselect (int nfds, fd_set *restrict rfds,
93 fd_set *restrict wfds, fd_set *restrict xfds,
94 struct timespec const *restrict timeout,
95 sigset_t const *restrict sigmask)
96 {
97 /* FreeBSD 8.2 has a bug: it does not always detect invalid fds. */
98 if (nfds < 0 || nfds > FD_SETSIZE)
99 {
100 errno = EINVAL;
101 return -1;
102 }
103 for (int i = 0; i < nfds; i++)
104 {
105 if (((rfds && FD_ISSET (i, rfds))
106 || (wfds && FD_ISSET (i, wfds))
107 || (xfds && FD_ISSET (i, xfds)))
108 && dup2 (i, i) != i)
109 return -1;
110 }
111
112 return pselect (nfds, rfds, wfds, xfds, timeout, sigmask);
113 }
114
115 #endif