pipe2.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /* Create a pipe, with specific opening flags.
  2. Copyright (C) 2009-2011 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, write to the Free Software Foundation,
  13. Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
  14. #include <config.h>
  15. /* Specification. */
  16. #include <unistd.h>
  17. #include <errno.h>
  18. #include <fcntl.h>
  19. #include "binary-io.h"
  20. #include "verify.h"
  21. #if GNULIB_defined_O_NONBLOCK
  22. # include "nonblocking.h"
  23. #endif
  24. #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
  25. /* Native Woe32 API. */
  26. # include <io.h>
  27. #endif
  28. int
  29. pipe2 (int fd[2], int flags)
  30. {
  31. /* Mingw _pipe() corrupts fd on failure; also, if we succeed at
  32. creating the pipe but later fail at changing fcntl, we want
  33. to leave fd unchanged: http://austingroupbugs.net/view.php?id=467 */
  34. int tmp[2];
  35. tmp[0] = fd[0];
  36. tmp[1] = fd[1];
  37. #if HAVE_PIPE2
  38. # undef pipe2
  39. /* Try the system call first, if it exists. (We may be running with a glibc
  40. that has the function but with an older kernel that lacks it.) */
  41. {
  42. /* Cache the information whether the system call really exists. */
  43. static int have_pipe2_really; /* 0 = unknown, 1 = yes, -1 = no */
  44. if (have_pipe2_really >= 0)
  45. {
  46. int result = pipe2 (fd, flags);
  47. if (!(result < 0 && errno == ENOSYS))
  48. {
  49. have_pipe2_really = 1;
  50. return result;
  51. }
  52. have_pipe2_really = -1;
  53. }
  54. }
  55. #endif
  56. /* Check the supported flags. */
  57. if ((flags & ~(O_CLOEXEC | O_NONBLOCK | O_BINARY | O_TEXT)) != 0)
  58. {
  59. errno = EINVAL;
  60. return -1;
  61. }
  62. #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
  63. /* Native Woe32 API. */
  64. if (_pipe (fd, 4096, flags & ~O_NONBLOCK) < 0)
  65. {
  66. fd[0] = tmp[0];
  67. fd[1] = tmp[1];
  68. return -1;
  69. }
  70. /* O_NONBLOCK handling.
  71. On native Windows platforms, O_NONBLOCK is defined by gnulib. Use the
  72. functions defined by the gnulib module 'nonblocking'. */
  73. # if GNULIB_defined_O_NONBLOCK
  74. if (flags & O_NONBLOCK)
  75. {
  76. if (set_nonblocking_flag (fd[0], true) != 0
  77. || set_nonblocking_flag (fd[1], true) != 0)
  78. goto fail;
  79. }
  80. # else
  81. {
  82. verify (O_NONBLOCK == 0);
  83. }
  84. # endif
  85. return 0;
  86. #else
  87. /* Unix API. */
  88. if (pipe (fd) < 0)
  89. return -1;
  90. /* POSIX <http://www.opengroup.org/onlinepubs/9699919799/functions/pipe.html>
  91. says that initially, the O_NONBLOCK and FD_CLOEXEC flags are cleared on
  92. both fd[0] and fd[1]. */
  93. /* O_NONBLOCK handling.
  94. On Unix platforms, O_NONBLOCK is defined by the system. Use fcntl(). */
  95. if (flags & O_NONBLOCK)
  96. {
  97. int fcntl_flags;
  98. if ((fcntl_flags = fcntl (fd[1], F_GETFL, 0)) < 0
  99. || fcntl (fd[1], F_SETFL, fcntl_flags | O_NONBLOCK) == -1
  100. || (fcntl_flags = fcntl (fd[0], F_GETFL, 0)) < 0
  101. || fcntl (fd[0], F_SETFL, fcntl_flags | O_NONBLOCK) == -1)
  102. goto fail;
  103. }
  104. if (flags & O_CLOEXEC)
  105. {
  106. int fcntl_flags;
  107. if ((fcntl_flags = fcntl (fd[1], F_GETFD, 0)) < 0
  108. || fcntl (fd[1], F_SETFD, fcntl_flags | FD_CLOEXEC) == -1
  109. || (fcntl_flags = fcntl (fd[0], F_GETFD, 0)) < 0
  110. || fcntl (fd[0], F_SETFD, fcntl_flags | FD_CLOEXEC) == -1)
  111. goto fail;
  112. }
  113. # if O_BINARY
  114. if (flags & O_BINARY)
  115. {
  116. setmode (fd[1], O_BINARY);
  117. setmode (fd[0], O_BINARY);
  118. }
  119. else if (flags & O_TEXT)
  120. {
  121. setmode (fd[1], O_TEXT);
  122. setmode (fd[0], O_TEXT);
  123. }
  124. # endif
  125. return 0;
  126. #endif
  127. #if GNULIB_defined_O_NONBLOCK || \
  128. !((defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__)
  129. fail:
  130. {
  131. int saved_errno = errno;
  132. close (fd[0]);
  133. close (fd[1]);
  134. fd[0] = tmp[0];
  135. fd[1] = tmp[1];
  136. errno = saved_errno;
  137. return -1;
  138. }
  139. #endif
  140. }