1 /* Substitute for <sys/select.h>.
2    Copyright (C) 2007-2021 Free Software Foundation, Inc.
3 
4    This file is free software: you can redistribute it and/or modify
5    it under the terms of the GNU Lesser General Public License as
6    published by the Free Software Foundation; either version 2.1 of the
7    License, or (at your option) any later version.
8 
9    This file 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
12    GNU Lesser General Public License for more details.
13 
14    You should have received a copy of the GNU Lesser General Public License
15    along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
16 
17 # if __GNUC__ >= 3
18 @PRAGMA_SYSTEM_HEADER@
19 # endif
20 @PRAGMA_COLUMNS@
21 
22 /* On OSF/1 and Solaris 2.6, <sys/types.h> and <sys/time.h>
23    both include <sys/select.h>.
24    On Cygwin and OpenBSD, <sys/time.h> includes <sys/select.h>.
25    Simply delegate to the system's header in this case.  */
26 #if (@HAVE_SYS_SELECT_H@                                                \
27      && !defined _GL_SYS_SELECT_H_REDIRECT_FROM_SYS_TYPES_H             \
28      && ((defined __osf__ && defined _SYS_TYPES_H_                      \
29           && defined _OSF_SOURCE)                                       \
30          || (defined __sun && defined _SYS_TYPES_H                      \
31              && (! (defined _XOPEN_SOURCE || defined _POSIX_C_SOURCE)   \
32                  || defined __EXTENSIONS__))))
33 
34 # define _GL_SYS_SELECT_H_REDIRECT_FROM_SYS_TYPES_H
35 # @INCLUDE_NEXT@ @NEXT_SYS_SELECT_H@
36 
37 #elif (@HAVE_SYS_SELECT_H@                                              \
38        && (defined _CYGWIN_SYS_TIME_H                                   \
39            || (!defined _GL_SYS_SELECT_H_REDIRECT_FROM_SYS_TIME_H       \
40                && ((defined __osf__ && defined _SYS_TIME_H_             \
41                     && defined _OSF_SOURCE)                             \
42                    || (defined __OpenBSD__ && defined _SYS_TIME_H_)     \
43                    || (defined __sun && defined _SYS_TIME_H             \
44                        && (! (defined _XOPEN_SOURCE                     \
45                               || defined _POSIX_C_SOURCE)               \
46                            || defined __EXTENSIONS__))))))
47 
48 # define _GL_SYS_SELECT_H_REDIRECT_FROM_SYS_TIME_H
49 # @INCLUDE_NEXT@ @NEXT_SYS_SELECT_H@
50 
51 /* On IRIX 6.5, <sys/timespec.h> includes <sys/types.h>, which includes
52    <sys/bsd_types.h>, which includes <sys/select.h>.  At this point we cannot
53    include <signal.h>, because that includes <internal/signal_core.h>, which
54    gives a syntax error because <sys/timespec.h> has not been completely
55    processed.  Simply delegate to the system's header in this case.  */
56 #elif @HAVE_SYS_SELECT_H@ && defined __sgi && (defined _SYS_BSD_TYPES_H && !defined _GL_SYS_SELECT_H_REDIRECT_FROM_SYS_BSD_TYPES_H)
57 
58 # define _GL_SYS_SELECT_H_REDIRECT_FROM_SYS_BSD_TYPES_H
59 # @INCLUDE_NEXT@ @NEXT_SYS_SELECT_H@
60 
61 /* On OpenBSD 5.0, <pthread.h> includes <sys/types.h>, which includes
62    <sys/select.h>.  At this point we cannot include <signal.h>, because that
63    includes gnulib's pthread.h override, which gives a syntax error because
64    /usr/include/pthread.h has not been completely processed.  Simply delegate
65    to the system's header in this case.  */
66 #elif @HAVE_SYS_SELECT_H@ && defined __OpenBSD__ && (defined _PTHREAD_H_ && !defined PTHREAD_MUTEX_INITIALIZER)
67 
68 # @INCLUDE_NEXT@ @NEXT_SYS_SELECT_H@
69 
70 #else
71 
72 #ifndef _@GUARD_PREFIX@_SYS_SELECT_H
73 
74 /* On many platforms, <sys/select.h> assumes prior inclusion of
75    <sys/types.h>.  Also, mingw defines sigset_t there, instead of
76    in <signal.h> where it belongs.  */
77 #include <sys/types.h>
78 
79 #if @HAVE_SYS_SELECT_H@
80 
81 /* On OSF/1 4.0, <sys/select.h> provides only a forward declaration
82    of 'struct timeval', and no definition of this type.
83    Also, Mac OS X, AIX, HP-UX, IRIX, Solaris, Interix declare select()
84    in <sys/time.h>.
85    But avoid namespace pollution on glibc systems and "unknown type
86    name" problems on Cygwin.  */
87 # if !(defined __GLIBC__ || defined __CYGWIN__)
88 #  include <sys/time.h>
89 # endif
90 
91 /* On AIX 7 and Solaris 10, <sys/select.h> provides an FD_ZERO implementation
92    that relies on memset(), but without including <string.h>.
93    But in any case avoid namespace pollution on glibc systems.  */
94 # if (defined __OpenBSD__ || defined _AIX || defined __sun || defined __osf__ || defined __BEOS__) \
95      && ! defined __GLIBC__
96 #  include <string.h>
97 # endif
98 
99 /* The include_next requires a split double-inclusion guard.  */
100 # @INCLUDE_NEXT@ @NEXT_SYS_SELECT_H@
101 
102 #endif
103 
104 /* Get definition of 'sigset_t'.
105    But avoid namespace pollution on glibc systems and "unknown type
106    name" problems on Cygwin.
107    On OS/2 kLIBC, sigset_t is defined in <sys/select.h>, too. In addition,
108    if <sys/param.h> is included, <types.h> -> <sys/types.h> -> <sys/select.h>
109    are included. Then <signal.h> -> <pthread.h> are included by GNULIB. By the
110    way, <pthread.h> requires PAGE_SIZE defined in <sys/param.h>. However,
111    <sys/param.h> has not been processed, yet. As a result, 'PAGE_SIZE'
112    undeclared error occurs in <pthread.h>.
113    Do this after the include_next (for the sake of OpenBSD 5.0) but before
114    the split double-inclusion guard (for the sake of Solaris).  */
115 #if !((defined __GLIBC__ || defined __CYGWIN__ || defined __KLIBC__) \
116       && !defined __UCLIBC__)
117 # include <signal.h>
118 #endif
119 
120 #ifndef _@GUARD_PREFIX@_SYS_SELECT_H
121 #define _@GUARD_PREFIX@_SYS_SELECT_H
122 
123 #if !@HAVE_SYS_SELECT_H@
124 /* A platform that lacks <sys/select.h>.  */
125 /* Get the 'struct timeval' and 'fd_set' types and the FD_* macros
126    on most platforms.  */
127 # include <sys/time.h>
128 /* On HP-UX 11, <sys/time.h> provides an FD_ZERO implementation
129    that relies on memset(), but without including <string.h>.  */
130 # if defined __hpux
131 #  include <string.h>
132 # endif
133 /* On native Windows platforms:
134    Get the 'fd_set' type.
135    Get the close() declaration before we override it.  */
136 # if @HAVE_WINSOCK2_H@
137 #  if !defined _GL_INCLUDING_WINSOCK2_H
138 #   define _GL_INCLUDING_WINSOCK2_H
139 #   include <winsock2.h>
140 #   undef _GL_INCLUDING_WINSOCK2_H
141 #  endif
142 #  include <io.h>
143 # endif
144 #endif
145 
146 /* The definitions of _GL_FUNCDECL_RPL etc. are copied here.  */
147 
148 /* The definition of _GL_WARN_ON_USE is copied here.  */
149 
150 
151 /* Fix some definitions from <winsock2.h>.  */
152 
153 #if @HAVE_WINSOCK2_H@
154 
155 # if !GNULIB_defined_rpl_fd_isset
156 
157 /* Re-define FD_ISSET to avoid a WSA call while we are not using
158    network sockets.  */
159 static int
160 rpl_fd_isset (SOCKET fd, fd_set * set)
161 {
162   u_int i;
163   if (set == NULL)
164     return 0;
165 
166   for (i = 0; i < set->fd_count; i++)
167     if (set->fd_array[i] == fd)
168       return 1;
169 
170   return 0;
171 }
172 
173 #  define GNULIB_defined_rpl_fd_isset 1
174 # endif
175 
176 # undef FD_ISSET
177 # define FD_ISSET(fd, set) rpl_fd_isset(fd, set)
178 
179 #endif
180 
181 /* Hide some function declarations from <winsock2.h>.  */
182 
183 #if @HAVE_WINSOCK2_H@
184 # if !defined _@GUARD_PREFIX@_UNISTD_H
185 #  if !(defined __cplusplus && defined GNULIB_NAMESPACE)
186 #   undef close
187 #   define close close_used_without_including_unistd_h
188 #  elif !defined __clang__
189     _GL_WARN_ON_USE (close,
190                      "close() used without including <unistd.h>");
191 #  endif
192 #  if !(defined __cplusplus && defined GNULIB_NAMESPACE)
193 #   undef gethostname
194 #   define gethostname gethostname_used_without_including_unistd_h
195 #  elif !defined __clang__
196     _GL_WARN_ON_USE (gethostname,
197                      "gethostname() used without including <unistd.h>");
198 #  endif
199 # endif
200 # if !defined _@GUARD_PREFIX@_SYS_SOCKET_H
201 #  if !(defined __cplusplus && defined GNULIB_NAMESPACE)
202 #   undef socket
203 #   define socket              socket_used_without_including_sys_socket_h
204 #   undef connect
205 #   define connect             connect_used_without_including_sys_socket_h
206 #   undef accept
207 #   define accept              accept_used_without_including_sys_socket_h
208 #   undef bind
209 #   define bind                bind_used_without_including_sys_socket_h
210 #   undef getpeername
211 #   define getpeername         getpeername_used_without_including_sys_socket_h
212 #   undef getsockname
213 #   define getsockname         getsockname_used_without_including_sys_socket_h
214 #   undef getsockopt
215 #   define getsockopt          getsockopt_used_without_including_sys_socket_h
216 #   undef listen
217 #   define listen              listen_used_without_including_sys_socket_h
218 #   undef recv
219 #   define recv                recv_used_without_including_sys_socket_h
220 #   undef send
221 #   define send                send_used_without_including_sys_socket_h
222 #   undef recvfrom
223 #   define recvfrom            recvfrom_used_without_including_sys_socket_h
224 #   undef sendto
225 #   define sendto              sendto_used_without_including_sys_socket_h
226 #   undef setsockopt
227 #   define setsockopt          setsockopt_used_without_including_sys_socket_h
228 #   undef shutdown
229 #   define shutdown            shutdown_used_without_including_sys_socket_h
230 #  elif !defined __clang__
231     _GL_WARN_ON_USE (socket,
232                      "socket() used without including <sys/socket.h>");
233     _GL_WARN_ON_USE (connect,
234                      "connect() used without including <sys/socket.h>");
235     _GL_WARN_ON_USE (accept,
236                      "accept() used without including <sys/socket.h>");
237     _GL_WARN_ON_USE (bind,
238                      "bind() used without including <sys/socket.h>");
239     _GL_WARN_ON_USE (getpeername,
240                      "getpeername() used without including <sys/socket.h>");
241     _GL_WARN_ON_USE (getsockname,
242                      "getsockname() used without including <sys/socket.h>");
243     _GL_WARN_ON_USE (getsockopt,
244                      "getsockopt() used without including <sys/socket.h>");
245     _GL_WARN_ON_USE (listen,
246                      "listen() used without including <sys/socket.h>");
247     _GL_WARN_ON_USE (recv,
248                      "recv() used without including <sys/socket.h>");
249     _GL_WARN_ON_USE (send,
250                      "send() used without including <sys/socket.h>");
251     _GL_WARN_ON_USE (recvfrom,
252                      "recvfrom() used without including <sys/socket.h>");
253     _GL_WARN_ON_USE (sendto,
254                      "sendto() used without including <sys/socket.h>");
255     _GL_WARN_ON_USE (setsockopt,
256                      "setsockopt() used without including <sys/socket.h>");
257     _GL_WARN_ON_USE (shutdown,
258                      "shutdown() used without including <sys/socket.h>");
259 #  endif
260 # endif
261 #endif
262 
263 
264 #if @GNULIB_PSELECT@
265 # if @REPLACE_PSELECT@
266 #  if !(defined __cplusplus && defined GNULIB_NAMESPACE)
267 #   undef pselect
268 #   define pselect rpl_pselect
269 #  endif
270 _GL_FUNCDECL_RPL (pselect, int,
271                   (int, fd_set *restrict, fd_set *restrict, fd_set *restrict,
272                    struct timespec const *restrict, const sigset_t *restrict));
273 _GL_CXXALIAS_RPL (pselect, int,
274                   (int, fd_set *restrict, fd_set *restrict, fd_set *restrict,
275                    struct timespec const *restrict, const sigset_t *restrict));
276 # else
277 #  if !@HAVE_PSELECT@
278 _GL_FUNCDECL_SYS (pselect, int,
279                   (int, fd_set *restrict, fd_set *restrict, fd_set *restrict,
280                    struct timespec const *restrict, const sigset_t *restrict));
281 #  endif
282 _GL_CXXALIAS_SYS (pselect, int,
283                   (int, fd_set *restrict, fd_set *restrict, fd_set *restrict,
284                    struct timespec const *restrict, const sigset_t *restrict));
285 # endif
286 _GL_CXXALIASWARN (pselect);
287 #elif defined GNULIB_POSIXCHECK
288 # undef pselect
289 # if HAVE_RAW_DECL_PSELECT
290 _GL_WARN_ON_USE (pselect, "pselect is not portable - "
291                  "use gnulib module pselect for portability");
292 # endif
293 #endif
294 
295 #if @GNULIB_SELECT@
296 # if @REPLACE_SELECT@
297 #  if !(defined __cplusplus && defined GNULIB_NAMESPACE)
298 #   undef select
299 #   define select rpl_select
300 #  endif
301 _GL_FUNCDECL_RPL (select, int,
302                   (int, fd_set *restrict, fd_set *restrict, fd_set *restrict,
303                    struct timeval *restrict));
304 _GL_CXXALIAS_RPL (select, int,
305                   (int, fd_set *restrict, fd_set *restrict, fd_set *restrict,
306                    timeval *restrict));
307 # else
308 _GL_CXXALIAS_SYS (select, int,
309                   (int, fd_set *restrict, fd_set *restrict, fd_set *restrict,
310                    timeval *restrict));
311 # endif
312 _GL_CXXALIASWARN (select);
313 #elif @HAVE_WINSOCK2_H@
314 # undef select
315 # define select select_used_without_requesting_gnulib_module_select
316 #elif defined GNULIB_POSIXCHECK
317 # undef select
318 # if HAVE_RAW_DECL_SELECT
319 _GL_WARN_ON_USE (select, "select is not always POSIX compliant - "
320                  "use gnulib module select for portability");
321 # endif
322 #endif
323 
324 
325 #endif /* _@GUARD_PREFIX@_SYS_SELECT_H */
326 #endif /* _@GUARD_PREFIX@_SYS_SELECT_H */
327 #endif /* OSF/1 */
328