sys_select.in.h 13 KB

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