accept4.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /* Accept a connection on a socket, with specific opening flags.
  2. Copyright (C) 2009-2017 Free Software Foundation, Inc.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public License as published by
  5. the Free Software Foundation; either version 2, or (at your option)
  6. any later version.
  7. This program 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 along
  12. with this program; if not, see <http://www.gnu.org/licenses/>. */
  13. #include <config.h>
  14. /* Specification. */
  15. #include <sys/socket.h>
  16. #include <errno.h>
  17. #include <fcntl.h>
  18. #include "binary-io.h"
  19. #include "msvc-nothrow.h"
  20. #ifndef SOCK_CLOEXEC
  21. # define SOCK_CLOEXEC 0
  22. #endif
  23. int
  24. accept4 (int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags)
  25. {
  26. int fd;
  27. #if HAVE_ACCEPT4
  28. # undef accept4
  29. /* Try the system call first, if it exists. (We may be running with a glibc
  30. that has the function but with an older kernel that lacks it.) */
  31. {
  32. /* Cache the information whether the system call really exists. */
  33. static int have_accept4_really; /* 0 = unknown, 1 = yes, -1 = no */
  34. if (have_accept4_really >= 0)
  35. {
  36. int result = accept4 (sockfd, addr, addrlen, flags);
  37. if (!(result < 0 && errno == ENOSYS))
  38. {
  39. have_accept4_really = 1;
  40. return result;
  41. }
  42. have_accept4_really = -1;
  43. }
  44. }
  45. #endif
  46. /* Check the supported flags. */
  47. if ((flags & ~(SOCK_CLOEXEC | O_TEXT | O_BINARY)) != 0)
  48. {
  49. errno = EINVAL;
  50. return -1;
  51. }
  52. fd = accept (sockfd, addr, addrlen);
  53. if (fd < 0)
  54. return -1;
  55. #if SOCK_CLOEXEC
  56. # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
  57. /* Native Windows API. */
  58. if (flags & SOCK_CLOEXEC)
  59. {
  60. HANDLE curr_process = GetCurrentProcess ();
  61. HANDLE old_handle = (HANDLE) _get_osfhandle (fd);
  62. HANDLE new_handle;
  63. int nfd;
  64. if (!DuplicateHandle (curr_process, /* SourceProcessHandle */
  65. old_handle, /* SourceHandle */
  66. curr_process, /* TargetProcessHandle */
  67. (PHANDLE) &new_handle, /* TargetHandle */
  68. (DWORD) 0, /* DesiredAccess */
  69. FALSE, /* InheritHandle */
  70. DUPLICATE_SAME_ACCESS)) /* Options */
  71. {
  72. close (fd);
  73. errno = EBADF; /* arbitrary */
  74. return -1;
  75. }
  76. /* Closing fd before allocating the new fd ensures that the new fd will
  77. have the minimum possible value. */
  78. close (fd);
  79. nfd = _open_osfhandle ((intptr_t) new_handle,
  80. O_NOINHERIT | (flags & (O_TEXT | O_BINARY)));
  81. if (nfd < 0)
  82. {
  83. CloseHandle (new_handle);
  84. return -1;
  85. }
  86. return nfd;
  87. }
  88. # else
  89. /* Unix API. */
  90. if (flags & SOCK_CLOEXEC)
  91. {
  92. int fcntl_flags;
  93. if ((fcntl_flags = fcntl (fd, F_GETFD, 0)) < 0
  94. || fcntl (fd, F_SETFD, fcntl_flags | FD_CLOEXEC) == -1)
  95. {
  96. int saved_errno = errno;
  97. close (fd);
  98. errno = saved_errno;
  99. return -1;
  100. }
  101. }
  102. # endif
  103. #endif
  104. #if O_BINARY
  105. if (flags & O_BINARY)
  106. set_binary_mode (fd, O_BINARY);
  107. else if (flags & O_TEXT)
  108. set_binary_mode (fd, O_TEXT);
  109. #endif
  110. return fd;
  111. }