cloexec.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * Copyright (c) 2015 DeNA Co., Ltd.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to
  6. * deal in the Software without restriction, including without limitation the
  7. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. * sell copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. * IN THE SOFTWARE.
  21. */
  22. #include <fcntl.h>
  23. #include "cloexec.h"
  24. pthread_mutex_t cloexec_mutex = PTHREAD_MUTEX_INITIALIZER;
  25. static int set_cloexec(int fd)
  26. {
  27. return fcntl(fd, F_SETFD, FD_CLOEXEC) != -1 ? 0 : -1;
  28. }
  29. /*
  30. * note: the socket must be in non-blocking mode, or the call might block while the mutex is being locked
  31. */
  32. int cloexec_accept(int socket, struct sockaddr *addr, socklen_t *addrlen)
  33. {
  34. int fd;
  35. pthread_mutex_lock(&cloexec_mutex);
  36. if ((fd = accept(socket, addr, addrlen)) == -1)
  37. goto Exit;
  38. if (set_cloexec(fd) != 0) {
  39. close(fd);
  40. fd = -1;
  41. goto Exit;
  42. }
  43. Exit:
  44. pthread_mutex_unlock(&cloexec_mutex);
  45. return fd;
  46. }
  47. int cloexec_pipe(int fds[2])
  48. {
  49. #ifdef __linux__
  50. #ifndef _GNU_SOURCE
  51. extern int pipe2(int pipefd[2], int flags);
  52. #endif
  53. return pipe2(fds, O_CLOEXEC);
  54. #else
  55. int ret = -1;
  56. pthread_mutex_lock(&cloexec_mutex);
  57. if (pipe(fds) != 0)
  58. goto Exit;
  59. if (set_cloexec(fds[0]) != 0 || set_cloexec(fds[1]) != 0)
  60. goto Exit;
  61. ret = 0;
  62. Exit:
  63. pthread_mutex_unlock(&cloexec_mutex);
  64. return ret;
  65. #endif
  66. }
  67. int cloexec_socket(int domain, int type, int protocol)
  68. {
  69. #ifdef __linux__
  70. return socket(domain, type | SOCK_CLOEXEC, protocol);
  71. #else
  72. int fd = -1;
  73. pthread_mutex_lock(&cloexec_mutex);
  74. if ((fd = socket(domain, type, protocol)) == -1)
  75. goto Exit;
  76. if (set_cloexec(fd) != 0) {
  77. close(fd);
  78. fd = -1;
  79. goto Exit;
  80. }
  81. Exit:
  82. pthread_mutex_unlock(&cloexec_mutex);
  83. return fd;
  84. #endif
  85. }