open.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /* Open a descriptor to a file.
  2. Copyright (C) 2007-2020 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 General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) 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 General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <https://www.gnu.org/licenses/>. */
  13. /* Written by Bruno Haible <bruno@clisp.org>, 2007. */
  14. /* If the user's config.h happens to include <fcntl.h>, let it include only
  15. the system's <fcntl.h> here, so that orig_open doesn't recurse to
  16. rpl_open. */
  17. #define __need_system_fcntl_h
  18. #include <config.h>
  19. /* Get the original definition of open. It might be defined as a macro. */
  20. #include <fcntl.h>
  21. #include <sys/types.h>
  22. #undef __need_system_fcntl_h
  23. static int
  24. orig_open (const char *filename, int flags, mode_t mode)
  25. {
  26. #if defined _WIN32 && !defined __CYGWIN__
  27. return _open (filename, flags, mode);
  28. #else
  29. return open (filename, flags, mode);
  30. #endif
  31. }
  32. /* Specification. */
  33. /* Write "fcntl.h" here, not <fcntl.h>, otherwise OSF/1 5.1 DTK cc eliminates
  34. this include because of the preliminary #include <fcntl.h> above. */
  35. #include "fcntl.h"
  36. #include "cloexec.h"
  37. #include <errno.h>
  38. #include <stdarg.h>
  39. #include <string.h>
  40. #include <sys/types.h>
  41. #include <sys/stat.h>
  42. #include <unistd.h>
  43. #ifndef REPLACE_OPEN_DIRECTORY
  44. # define REPLACE_OPEN_DIRECTORY 0
  45. #endif
  46. int
  47. open (const char *filename, int flags, ...)
  48. {
  49. /* 0 = unknown, 1 = yes, -1 = no. */
  50. #if GNULIB_defined_O_CLOEXEC
  51. int have_cloexec = -1;
  52. #else
  53. static int have_cloexec;
  54. #endif
  55. mode_t mode;
  56. int fd;
  57. mode = 0;
  58. if (flags & O_CREAT)
  59. {
  60. va_list arg;
  61. va_start (arg, flags);
  62. /* We have to use PROMOTED_MODE_T instead of mode_t, otherwise GCC 4
  63. creates crashing code when 'mode_t' is smaller than 'int'. */
  64. mode = va_arg (arg, PROMOTED_MODE_T);
  65. va_end (arg);
  66. }
  67. #if GNULIB_defined_O_NONBLOCK
  68. /* The only known platform that lacks O_NONBLOCK is mingw, but it
  69. also lacks named pipes and Unix sockets, which are the only two
  70. file types that require non-blocking handling in open().
  71. Therefore, it is safe to ignore O_NONBLOCK here. It is handy
  72. that mingw also lacks openat(), so that is also covered here. */
  73. flags &= ~O_NONBLOCK;
  74. #endif
  75. #if defined _WIN32 && ! defined __CYGWIN__
  76. if (strcmp (filename, "/dev/null") == 0)
  77. filename = "NUL";
  78. #endif
  79. #if OPEN_TRAILING_SLASH_BUG
  80. /* Fail if one of O_CREAT, O_WRONLY, O_RDWR is specified and the filename
  81. ends in a slash, as POSIX says such a filename must name a directory
  82. <https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html#tag_04_13>:
  83. "A pathname that contains at least one non-<slash> character and that
  84. ends with one or more trailing <slash> characters shall not be resolved
  85. successfully unless the last pathname component before the trailing
  86. <slash> characters names an existing directory"
  87. If the named file already exists as a directory, then
  88. - if O_CREAT is specified, open() must fail because of the semantics
  89. of O_CREAT,
  90. - if O_WRONLY or O_RDWR is specified, open() must fail because POSIX
  91. <https://pubs.opengroup.org/onlinepubs/9699919799/functions/open.html>
  92. says that it fails with errno = EISDIR in this case.
  93. If the named file does not exist or does not name a directory, then
  94. - if O_CREAT is specified, open() must fail since open() cannot create
  95. directories,
  96. - if O_WRONLY or O_RDWR is specified, open() must fail because the
  97. file does not contain a '.' directory. */
  98. if ((flags & O_CREAT)
  99. || (flags & O_ACCMODE) == O_RDWR
  100. || (flags & O_ACCMODE) == O_WRONLY)
  101. {
  102. size_t len = strlen (filename);
  103. if (len > 0 && filename[len - 1] == '/')
  104. {
  105. errno = EISDIR;
  106. return -1;
  107. }
  108. }
  109. #endif
  110. fd = orig_open (filename,
  111. flags & ~(have_cloexec < 0 ? O_CLOEXEC : 0), mode);
  112. if (flags & O_CLOEXEC)
  113. {
  114. if (! have_cloexec)
  115. {
  116. if (0 <= fd)
  117. have_cloexec = 1;
  118. else if (errno == EINVAL)
  119. {
  120. fd = orig_open (filename, flags & ~O_CLOEXEC, mode);
  121. have_cloexec = -1;
  122. }
  123. }
  124. if (have_cloexec < 0 && 0 <= fd)
  125. set_cloexec_flag (fd, true);
  126. }
  127. #if REPLACE_FCHDIR
  128. /* Implementing fchdir and fdopendir requires the ability to open a
  129. directory file descriptor. If open doesn't support that (as on
  130. mingw), we use a dummy file that behaves the same as directories
  131. on Linux (ie. always reports EOF on attempts to read()), and
  132. override fstat() in fchdir.c to hide the fact that we have a
  133. dummy. */
  134. if (REPLACE_OPEN_DIRECTORY && fd < 0 && errno == EACCES
  135. && ((flags & O_ACCMODE) == O_RDONLY
  136. || (O_SEARCH != O_RDONLY && (flags & O_ACCMODE) == O_SEARCH)))
  137. {
  138. struct stat statbuf;
  139. if (stat (filename, &statbuf) == 0 && S_ISDIR (statbuf.st_mode))
  140. {
  141. /* Maximum recursion depth of 1. */
  142. fd = open ("/dev/null", flags, mode);
  143. if (0 <= fd)
  144. fd = _gl_register_fd (fd, filename);
  145. }
  146. else
  147. errno = EACCES;
  148. }
  149. #endif
  150. #if OPEN_TRAILING_SLASH_BUG
  151. /* If the filename ends in a slash and fd does not refer to a directory,
  152. then fail.
  153. Rationale: POSIX says such a filename must name a directory
  154. <https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html#tag_04_13>:
  155. "A pathname that contains at least one non-<slash> character and that
  156. ends with one or more trailing <slash> characters shall not be resolved
  157. successfully unless the last pathname component before the trailing
  158. <slash> characters names an existing directory"
  159. If the named file without the slash is not a directory, open() must fail
  160. with ENOTDIR. */
  161. if (fd >= 0)
  162. {
  163. /* We know len is positive, since open did not fail with ENOENT. */
  164. size_t len = strlen (filename);
  165. if (filename[len - 1] == '/')
  166. {
  167. struct stat statbuf;
  168. if (fstat (fd, &statbuf) >= 0 && !S_ISDIR (statbuf.st_mode))
  169. {
  170. close (fd);
  171. errno = ENOTDIR;
  172. return -1;
  173. }
  174. }
  175. }
  176. #endif
  177. #if REPLACE_FCHDIR
  178. if (!REPLACE_OPEN_DIRECTORY && 0 <= fd)
  179. fd = _gl_register_fd (fd, filename);
  180. #endif
  181. return fd;
  182. }