open.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. /* Licensed to the Apache Software Foundation (ASF) under one or more
  2. * contributor license agreements. See the NOTICE file distributed with
  3. * this work for additional information regarding copyright ownership.
  4. * The ASF licenses this file to You under the Apache License, Version 2.0
  5. * (the "License"); you may not use this file except in compliance with
  6. * the License. You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "apr_arch_file_io.h"
  17. #include "apr_strings.h"
  18. #include "apr_portable.h"
  19. #include "apr_thread_mutex.h"
  20. #include "apr_arch_inherit.h"
  21. #ifdef NETWARE
  22. #include "nks/dirio.h"
  23. #include "apr_hash.h"
  24. #include "fsio.h"
  25. #endif
  26. static apr_status_t file_cleanup(apr_file_t *file, int is_child)
  27. {
  28. apr_status_t rv = APR_SUCCESS;
  29. int fd = file->filedes;
  30. /* Set file descriptor to -1 before close(), so that there is no
  31. * chance of returning an already closed FD from apr_os_file_get().
  32. */
  33. file->filedes = -1;
  34. if (close(fd) == 0) {
  35. /* Only the parent process should delete the file! */
  36. if (!is_child && (file->flags & APR_FOPEN_DELONCLOSE)) {
  37. unlink(file->fname);
  38. }
  39. #if APR_HAS_THREADS
  40. if (file->thlock) {
  41. rv = apr_thread_mutex_destroy(file->thlock);
  42. }
  43. #endif
  44. }
  45. else {
  46. /* Restore, close() was not successful. */
  47. file->filedes = fd;
  48. /* Are there any error conditions other than EINTR or EBADF? */
  49. rv = errno;
  50. }
  51. #ifndef WAITIO_USES_POLL
  52. if (file->pollset != NULL) {
  53. apr_status_t pollset_rv = apr_pollset_destroy(file->pollset);
  54. /* If the file close failed, return its error value,
  55. * not apr_pollset_destroy()'s.
  56. */
  57. if (rv == APR_SUCCESS) {
  58. rv = pollset_rv;
  59. }
  60. }
  61. #endif /* !WAITIO_USES_POLL */
  62. return rv;
  63. }
  64. apr_status_t apr_unix_file_cleanup(void *thefile)
  65. {
  66. apr_file_t *file = thefile;
  67. apr_status_t flush_rv = APR_SUCCESS, rv = APR_SUCCESS;
  68. if (file->buffered) {
  69. flush_rv = apr_file_flush(file);
  70. }
  71. rv = file_cleanup(file, 0);
  72. return rv != APR_SUCCESS ? rv : flush_rv;
  73. }
  74. apr_status_t apr_unix_child_file_cleanup(void *thefile)
  75. {
  76. return file_cleanup(thefile, 1);
  77. }
  78. APR_DECLARE(apr_status_t) apr_file_open(apr_file_t **new,
  79. const char *fname,
  80. apr_int32_t flag,
  81. apr_fileperms_t perm,
  82. apr_pool_t *pool)
  83. {
  84. apr_os_file_t fd;
  85. int oflags = 0;
  86. #if APR_HAS_THREADS
  87. apr_thread_mutex_t *thlock;
  88. apr_status_t rv;
  89. #endif
  90. if ((flag & APR_FOPEN_READ) && (flag & APR_FOPEN_WRITE)) {
  91. oflags = O_RDWR;
  92. }
  93. else if (flag & APR_FOPEN_READ) {
  94. oflags = O_RDONLY;
  95. }
  96. else if (flag & APR_FOPEN_WRITE) {
  97. oflags = O_WRONLY;
  98. }
  99. else {
  100. return APR_EACCES;
  101. }
  102. if (flag & APR_FOPEN_CREATE) {
  103. oflags |= O_CREAT;
  104. if (flag & APR_FOPEN_EXCL) {
  105. oflags |= O_EXCL;
  106. }
  107. }
  108. if ((flag & APR_FOPEN_EXCL) && !(flag & APR_FOPEN_CREATE)) {
  109. return APR_EACCES;
  110. }
  111. if (flag & APR_FOPEN_APPEND) {
  112. oflags |= O_APPEND;
  113. }
  114. if (flag & APR_FOPEN_TRUNCATE) {
  115. oflags |= O_TRUNC;
  116. }
  117. #ifdef O_BINARY
  118. if (flag & APR_FOPEN_BINARY) {
  119. oflags |= O_BINARY;
  120. }
  121. #endif
  122. #ifdef O_CLOEXEC
  123. /* Introduced in Linux 2.6.23. Silently ignored on earlier Linux kernels.
  124. */
  125. if (!(flag & APR_FOPEN_NOCLEANUP)) {
  126. oflags |= O_CLOEXEC;
  127. }
  128. #endif
  129. #if APR_HAS_LARGE_FILES && defined(_LARGEFILE64_SOURCE)
  130. oflags |= O_LARGEFILE;
  131. #elif defined(O_LARGEFILE)
  132. if (flag & APR_FOPEN_LARGEFILE) {
  133. oflags |= O_LARGEFILE;
  134. }
  135. #endif
  136. #if APR_HAS_THREADS
  137. if ((flag & APR_FOPEN_BUFFERED) && (flag & APR_FOPEN_XTHREAD)) {
  138. rv = apr_thread_mutex_create(&thlock,
  139. APR_THREAD_MUTEX_DEFAULT, pool);
  140. if (rv) {
  141. return rv;
  142. }
  143. }
  144. #endif
  145. if (perm == APR_OS_DEFAULT) {
  146. fd = open(fname, oflags, 0666);
  147. }
  148. else {
  149. fd = open(fname, oflags, apr_unix_perms2mode(perm));
  150. }
  151. if (fd < 0) {
  152. return errno;
  153. }
  154. if (!(flag & APR_FOPEN_NOCLEANUP)) {
  155. #ifdef O_CLOEXEC
  156. static int has_o_cloexec = 0;
  157. if (!has_o_cloexec)
  158. #endif
  159. {
  160. int flags;
  161. if ((flags = fcntl(fd, F_GETFD)) == -1) {
  162. close(fd);
  163. return errno;
  164. }
  165. if ((flags & FD_CLOEXEC) == 0) {
  166. flags |= FD_CLOEXEC;
  167. if (fcntl(fd, F_SETFD, flags) == -1) {
  168. close(fd);
  169. return errno;
  170. }
  171. }
  172. #ifdef O_CLOEXEC
  173. else {
  174. has_o_cloexec = 1;
  175. }
  176. #endif
  177. }
  178. }
  179. (*new) = (apr_file_t *)apr_pcalloc(pool, sizeof(apr_file_t));
  180. (*new)->pool = pool;
  181. (*new)->flags = flag;
  182. (*new)->filedes = fd;
  183. (*new)->fname = apr_pstrdup(pool, fname);
  184. (*new)->blocking = BLK_ON;
  185. (*new)->buffered = (flag & APR_FOPEN_BUFFERED) > 0;
  186. if ((*new)->buffered) {
  187. (*new)->buffer = apr_palloc(pool, APR_FILE_DEFAULT_BUFSIZE);
  188. (*new)->bufsize = APR_FILE_DEFAULT_BUFSIZE;
  189. #if APR_HAS_THREADS
  190. if ((*new)->flags & APR_FOPEN_XTHREAD) {
  191. (*new)->thlock = thlock;
  192. }
  193. #endif
  194. }
  195. else {
  196. (*new)->buffer = NULL;
  197. }
  198. (*new)->is_pipe = 0;
  199. (*new)->timeout = -1;
  200. (*new)->ungetchar = -1;
  201. (*new)->eof_hit = 0;
  202. (*new)->filePtr = 0;
  203. (*new)->bufpos = 0;
  204. (*new)->dataRead = 0;
  205. (*new)->direction = 0;
  206. #ifndef WAITIO_USES_POLL
  207. /* Start out with no pollset. apr_wait_for_io_or_timeout() will
  208. * initialize the pollset if needed.
  209. */
  210. (*new)->pollset = NULL;
  211. #endif
  212. if (!(flag & APR_FOPEN_NOCLEANUP)) {
  213. apr_pool_cleanup_register((*new)->pool, (void *)(*new),
  214. apr_unix_file_cleanup,
  215. apr_unix_child_file_cleanup);
  216. }
  217. return APR_SUCCESS;
  218. }
  219. APR_DECLARE(apr_status_t) apr_file_close(apr_file_t *file)
  220. {
  221. return apr_pool_cleanup_run(file->pool, file, apr_unix_file_cleanup);
  222. }
  223. APR_DECLARE(apr_status_t) apr_file_remove(const char *path, apr_pool_t *pool)
  224. {
  225. if (unlink(path) == 0) {
  226. return APR_SUCCESS;
  227. }
  228. else {
  229. return errno;
  230. }
  231. }
  232. APR_DECLARE(apr_status_t) apr_file_rename(const char *from_path,
  233. const char *to_path,
  234. apr_pool_t *p)
  235. {
  236. if (rename(from_path, to_path) != 0) {
  237. return errno;
  238. }
  239. return APR_SUCCESS;
  240. }
  241. APR_DECLARE(apr_status_t) apr_os_file_get(apr_os_file_t *thefile,
  242. apr_file_t *file)
  243. {
  244. *thefile = file->filedes;
  245. return APR_SUCCESS;
  246. }
  247. APR_DECLARE(apr_status_t) apr_os_file_put(apr_file_t **file,
  248. apr_os_file_t *thefile,
  249. apr_int32_t flags, apr_pool_t *pool)
  250. {
  251. int *dafile = thefile;
  252. (*file) = apr_pcalloc(pool, sizeof(apr_file_t));
  253. (*file)->pool = pool;
  254. (*file)->eof_hit = 0;
  255. (*file)->blocking = BLK_UNKNOWN; /* in case it is a pipe */
  256. (*file)->timeout = -1;
  257. (*file)->ungetchar = -1; /* no char avail */
  258. (*file)->filedes = *dafile;
  259. (*file)->flags = flags | APR_FOPEN_NOCLEANUP;
  260. (*file)->buffered = (flags & APR_FOPEN_BUFFERED) > 0;
  261. #ifndef WAITIO_USES_POLL
  262. /* Start out with no pollset. apr_wait_for_io_or_timeout() will
  263. * initialize the pollset if needed.
  264. */
  265. (*file)->pollset = NULL;
  266. #endif
  267. if ((*file)->buffered) {
  268. (*file)->buffer = apr_palloc(pool, APR_FILE_DEFAULT_BUFSIZE);
  269. (*file)->bufsize = APR_FILE_DEFAULT_BUFSIZE;
  270. #if APR_HAS_THREADS
  271. if ((*file)->flags & APR_FOPEN_XTHREAD) {
  272. apr_status_t rv;
  273. rv = apr_thread_mutex_create(&((*file)->thlock),
  274. APR_THREAD_MUTEX_DEFAULT, pool);
  275. if (rv) {
  276. return rv;
  277. }
  278. }
  279. #endif
  280. }
  281. return APR_SUCCESS;
  282. }
  283. APR_DECLARE(apr_status_t) apr_file_eof(apr_file_t *fptr)
  284. {
  285. if (fptr->eof_hit == 1) {
  286. return APR_EOF;
  287. }
  288. return APR_SUCCESS;
  289. }
  290. APR_DECLARE(apr_status_t) apr_file_open_flags_stderr(apr_file_t **thefile,
  291. apr_int32_t flags,
  292. apr_pool_t *pool)
  293. {
  294. int fd = STDERR_FILENO;
  295. return apr_os_file_put(thefile, &fd, flags | APR_FOPEN_WRITE, pool);
  296. }
  297. APR_DECLARE(apr_status_t) apr_file_open_flags_stdout(apr_file_t **thefile,
  298. apr_int32_t flags,
  299. apr_pool_t *pool)
  300. {
  301. int fd = STDOUT_FILENO;
  302. return apr_os_file_put(thefile, &fd, flags | APR_FOPEN_WRITE, pool);
  303. }
  304. APR_DECLARE(apr_status_t) apr_file_open_flags_stdin(apr_file_t **thefile,
  305. apr_int32_t flags,
  306. apr_pool_t *pool)
  307. {
  308. int fd = STDIN_FILENO;
  309. return apr_os_file_put(thefile, &fd, flags | APR_FOPEN_READ, pool);
  310. }
  311. APR_DECLARE(apr_status_t) apr_file_open_stderr(apr_file_t **thefile,
  312. apr_pool_t *pool)
  313. {
  314. return apr_file_open_flags_stderr(thefile, 0, pool);
  315. }
  316. APR_DECLARE(apr_status_t) apr_file_open_stdout(apr_file_t **thefile,
  317. apr_pool_t *pool)
  318. {
  319. return apr_file_open_flags_stdout(thefile, 0, pool);
  320. }
  321. APR_DECLARE(apr_status_t) apr_file_open_stdin(apr_file_t **thefile,
  322. apr_pool_t *pool)
  323. {
  324. return apr_file_open_flags_stdin(thefile, 0, pool);
  325. }
  326. APR_IMPLEMENT_INHERIT_SET(file, flags, pool, apr_unix_file_cleanup)
  327. /* We need to do this by hand instead of using APR_IMPLEMENT_INHERIT_UNSET
  328. * because the macro sets both cleanups to the same function, which is not
  329. * suitable on Unix (see PR 41119). */
  330. APR_DECLARE(apr_status_t) apr_file_inherit_unset(apr_file_t *thefile)
  331. {
  332. if (thefile->flags & APR_FOPEN_NOCLEANUP) {
  333. return APR_EINVAL;
  334. }
  335. if (thefile->flags & APR_INHERIT) {
  336. int flags;
  337. if ((flags = fcntl(thefile->filedes, F_GETFD)) == -1)
  338. return errno;
  339. flags |= FD_CLOEXEC;
  340. if (fcntl(thefile->filedes, F_SETFD, flags) == -1)
  341. return errno;
  342. thefile->flags &= ~APR_INHERIT;
  343. apr_pool_child_cleanup_set(thefile->pool,
  344. (void *)thefile,
  345. apr_unix_file_cleanup,
  346. apr_unix_child_file_cleanup);
  347. }
  348. return APR_SUCCESS;
  349. }
  350. APR_POOL_IMPLEMENT_ACCESSOR(file)
  351. APR_DECLARE(apr_status_t) apr_file_link(const char *from_path,
  352. const char *to_path)
  353. {
  354. if (link(from_path, to_path) == -1) {
  355. return errno;
  356. }
  357. return APR_SUCCESS;
  358. }