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