]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - contrib/apr/file_io/unix/open.c
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / contrib / apr / file_io / unix / open.c
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
17 #include "apr_arch_file_io.h"
18 #include "apr_strings.h"
19 #include "apr_portable.h"
20 #include "apr_thread_mutex.h"
21 #include "apr_arch_inherit.h"
22
23 #ifdef NETWARE
24 #include "nks/dirio.h"
25 #include "apr_hash.h"
26 #include "fsio.h"
27 #endif
28
29 static apr_status_t file_cleanup(apr_file_t *file, int is_child)
30 {
31     apr_status_t rv = APR_SUCCESS;
32     int fd = file->filedes;
33
34     /* Set file descriptor to -1 before close(), so that there is no
35      * chance of returning an already closed FD from apr_os_file_get().
36      */
37     file->filedes = -1;
38
39     if (close(fd) == 0) {
40         /* Only the parent process should delete the file! */
41         if (!is_child && (file->flags & APR_FOPEN_DELONCLOSE)) {
42             unlink(file->fname);
43         }
44 #if APR_HAS_THREADS
45         if (file->thlock) {
46             rv = apr_thread_mutex_destroy(file->thlock);
47         }
48 #endif
49     }
50     else {
51         /* Restore, close() was not successful. */
52         file->filedes = fd;
53
54         /* Are there any error conditions other than EINTR or EBADF? */
55         rv = errno;
56     }
57 #ifndef WAITIO_USES_POLL
58     if (file->pollset != NULL) {
59         apr_status_t pollset_rv = apr_pollset_destroy(file->pollset);
60         /* If the file close failed, return its error value,
61          * not apr_pollset_destroy()'s.
62          */
63         if (rv == APR_SUCCESS) {
64             rv = pollset_rv;
65         }
66     }
67 #endif /* !WAITIO_USES_POLL */
68     return rv;
69 }
70
71 apr_status_t apr_unix_file_cleanup(void *thefile)
72 {
73     apr_file_t *file = thefile;
74     apr_status_t flush_rv = APR_SUCCESS, rv = APR_SUCCESS;
75
76     if (file->buffered) {
77         flush_rv = apr_file_flush(file);
78     }
79
80     rv = file_cleanup(file, 0);
81
82     return rv != APR_SUCCESS ? rv : flush_rv;
83 }
84
85 apr_status_t apr_unix_child_file_cleanup(void *thefile)
86 {
87     return file_cleanup(thefile, 1);
88 }
89
90 APR_DECLARE(apr_status_t) apr_file_open(apr_file_t **new, 
91                                         const char *fname, 
92                                         apr_int32_t flag, 
93                                         apr_fileperms_t perm, 
94                                         apr_pool_t *pool)
95 {
96     apr_os_file_t fd;
97     int oflags = 0;
98 #if APR_HAS_THREADS
99     apr_thread_mutex_t *thlock;
100     apr_status_t rv;
101 #endif
102
103     if ((flag & APR_FOPEN_READ) && (flag & APR_FOPEN_WRITE)) {
104         oflags = O_RDWR;
105     }
106     else if (flag & APR_FOPEN_READ) {
107         oflags = O_RDONLY;
108     }
109     else if (flag & APR_FOPEN_WRITE) {
110         oflags = O_WRONLY;
111     }
112     else {
113         return APR_EACCES; 
114     }
115
116     if (flag & APR_FOPEN_CREATE) {
117         oflags |= O_CREAT;
118         if (flag & APR_FOPEN_EXCL) {
119             oflags |= O_EXCL;
120         }
121     }
122     if ((flag & APR_FOPEN_EXCL) && !(flag & APR_FOPEN_CREATE)) {
123         return APR_EACCES;
124     }   
125
126     if (flag & APR_FOPEN_APPEND) {
127         oflags |= O_APPEND;
128     }
129     if (flag & APR_FOPEN_TRUNCATE) {
130         oflags |= O_TRUNC;
131     }
132 #ifdef O_BINARY
133     if (flag & APR_FOPEN_BINARY) {
134         oflags |= O_BINARY;
135     }
136 #endif
137
138 #ifdef O_CLOEXEC
139     /* Introduced in Linux 2.6.23. Silently ignored on earlier Linux kernels.
140      */
141     if (!(flag & APR_FOPEN_NOCLEANUP)) {
142         oflags |= O_CLOEXEC;
143 }
144 #endif
145  
146 #if APR_HAS_LARGE_FILES && defined(_LARGEFILE64_SOURCE)
147     oflags |= O_LARGEFILE;
148 #elif defined(O_LARGEFILE)
149     if (flag & APR_FOPEN_LARGEFILE) {
150         oflags |= O_LARGEFILE;
151     }
152 #endif
153
154 #if APR_HAS_THREADS
155     if ((flag & APR_FOPEN_BUFFERED) && (flag & APR_FOPEN_XTHREAD)) {
156         rv = apr_thread_mutex_create(&thlock,
157                                      APR_THREAD_MUTEX_DEFAULT, pool);
158         if (rv) {
159             return rv;
160         }
161     }
162 #endif
163
164     if (perm == APR_OS_DEFAULT) {
165         fd = open(fname, oflags, 0666);
166     }
167     else {
168         fd = open(fname, oflags, apr_unix_perms2mode(perm));
169     } 
170     if (fd < 0) {
171        return errno;
172     }
173     if (!(flag & APR_FOPEN_NOCLEANUP)) {
174 #ifdef O_CLOEXEC
175         static int has_o_cloexec = 0;
176         if (!has_o_cloexec)
177 #endif
178         {
179             int flags;
180
181             if ((flags = fcntl(fd, F_GETFD)) == -1) {
182                 close(fd);
183                 return errno;
184             }
185             if ((flags & FD_CLOEXEC) == 0) {
186                 flags |= FD_CLOEXEC;
187                 if (fcntl(fd, F_SETFD, flags) == -1) {
188                     close(fd);
189                     return errno;
190                 }
191             }
192 #ifdef O_CLOEXEC
193             else {
194                 has_o_cloexec = 1;
195             }
196 #endif
197         }
198     }
199
200     (*new) = (apr_file_t *)apr_pcalloc(pool, sizeof(apr_file_t));
201     (*new)->pool = pool;
202     (*new)->flags = flag;
203     (*new)->filedes = fd;
204
205     (*new)->fname = apr_pstrdup(pool, fname);
206
207     (*new)->blocking = BLK_ON;
208     (*new)->buffered = (flag & APR_FOPEN_BUFFERED) > 0;
209
210     if ((*new)->buffered) {
211         (*new)->buffer = apr_palloc(pool, APR_FILE_DEFAULT_BUFSIZE);
212         (*new)->bufsize = APR_FILE_DEFAULT_BUFSIZE;
213 #if APR_HAS_THREADS
214         if ((*new)->flags & APR_FOPEN_XTHREAD) {
215             (*new)->thlock = thlock;
216         }
217 #endif
218     }
219     else {
220         (*new)->buffer = NULL;
221     }
222
223     (*new)->is_pipe = 0;
224     (*new)->timeout = -1;
225     (*new)->ungetchar = -1;
226     (*new)->eof_hit = 0;
227     (*new)->filePtr = 0;
228     (*new)->bufpos = 0;
229     (*new)->dataRead = 0;
230     (*new)->direction = 0;
231 #ifndef WAITIO_USES_POLL
232     /* Start out with no pollset.  apr_wait_for_io_or_timeout() will
233      * initialize the pollset if needed.
234      */
235     (*new)->pollset = NULL;
236 #endif
237     if (!(flag & APR_FOPEN_NOCLEANUP)) {
238         apr_pool_cleanup_register((*new)->pool, (void *)(*new), 
239                                   apr_unix_file_cleanup, 
240                                   apr_unix_child_file_cleanup);
241     }
242     return APR_SUCCESS;
243 }
244
245 APR_DECLARE(apr_status_t) apr_file_close(apr_file_t *file)
246 {
247     return apr_pool_cleanup_run(file->pool, file, apr_unix_file_cleanup);
248 }
249
250 APR_DECLARE(apr_status_t) apr_file_remove(const char *path, apr_pool_t *pool)
251 {
252     if (unlink(path) == 0) {
253         return APR_SUCCESS;
254     }
255     else {
256         return errno;
257     }
258 }
259
260 APR_DECLARE(apr_status_t) apr_file_rename(const char *from_path, 
261                                           const char *to_path,
262                                           apr_pool_t *p)
263 {
264     if (rename(from_path, to_path) != 0) {
265         return errno;
266     }
267     return APR_SUCCESS;
268 }
269
270 APR_DECLARE(apr_status_t) apr_os_file_get(apr_os_file_t *thefile, 
271                                           apr_file_t *file)
272 {
273     *thefile = file->filedes;
274     return APR_SUCCESS;
275 }
276
277 APR_DECLARE(apr_status_t) apr_os_file_put(apr_file_t **file, 
278                                           apr_os_file_t *thefile,
279                                           apr_int32_t flags, apr_pool_t *pool)
280 {
281     int *dafile = thefile;
282     
283     (*file) = apr_pcalloc(pool, sizeof(apr_file_t));
284     (*file)->pool = pool;
285     (*file)->eof_hit = 0;
286     (*file)->blocking = BLK_UNKNOWN; /* in case it is a pipe */
287     (*file)->timeout = -1;
288     (*file)->ungetchar = -1; /* no char avail */
289     (*file)->filedes = *dafile;
290     (*file)->flags = flags | APR_FOPEN_NOCLEANUP;
291     (*file)->buffered = (flags & APR_FOPEN_BUFFERED) > 0;
292
293 #ifndef WAITIO_USES_POLL
294     /* Start out with no pollset.  apr_wait_for_io_or_timeout() will
295      * initialize the pollset if needed.
296      */
297     (*file)->pollset = NULL;
298 #endif
299
300     if ((*file)->buffered) {
301         (*file)->buffer = apr_palloc(pool, APR_FILE_DEFAULT_BUFSIZE);
302         (*file)->bufsize = APR_FILE_DEFAULT_BUFSIZE;
303 #if APR_HAS_THREADS
304         if ((*file)->flags & APR_FOPEN_XTHREAD) {
305             apr_status_t rv;
306             rv = apr_thread_mutex_create(&((*file)->thlock),
307                                          APR_THREAD_MUTEX_DEFAULT, pool);
308             if (rv) {
309                 return rv;
310             }
311         }
312 #endif
313     }
314     return APR_SUCCESS;
315 }    
316
317 APR_DECLARE(apr_status_t) apr_file_eof(apr_file_t *fptr)
318 {
319     if (fptr->eof_hit == 1) {
320         return APR_EOF;
321     }
322     return APR_SUCCESS;
323 }   
324
325 APR_DECLARE(apr_status_t) apr_file_open_flags_stderr(apr_file_t **thefile, 
326                                                      apr_int32_t flags,
327                                                      apr_pool_t *pool)
328 {
329     int fd = STDERR_FILENO;
330
331     return apr_os_file_put(thefile, &fd, flags | APR_FOPEN_WRITE, pool);
332 }
333
334 APR_DECLARE(apr_status_t) apr_file_open_flags_stdout(apr_file_t **thefile, 
335                                                      apr_int32_t flags,
336                                                      apr_pool_t *pool)
337 {
338     int fd = STDOUT_FILENO;
339
340     return apr_os_file_put(thefile, &fd, flags | APR_FOPEN_WRITE, pool);
341 }
342
343 APR_DECLARE(apr_status_t) apr_file_open_flags_stdin(apr_file_t **thefile, 
344                                                     apr_int32_t flags,
345                                                     apr_pool_t *pool)
346 {
347     int fd = STDIN_FILENO;
348
349     return apr_os_file_put(thefile, &fd, flags | APR_FOPEN_READ, pool);
350 }
351
352 APR_DECLARE(apr_status_t) apr_file_open_stderr(apr_file_t **thefile, 
353                                                apr_pool_t *pool)
354 {
355     return apr_file_open_flags_stderr(thefile, 0, pool);
356 }
357
358 APR_DECLARE(apr_status_t) apr_file_open_stdout(apr_file_t **thefile, 
359                                                apr_pool_t *pool)
360 {
361     return apr_file_open_flags_stdout(thefile, 0, pool);
362 }
363
364 APR_DECLARE(apr_status_t) apr_file_open_stdin(apr_file_t **thefile, 
365                                               apr_pool_t *pool)
366 {
367     return apr_file_open_flags_stdin(thefile, 0, pool);
368 }
369
370 APR_IMPLEMENT_INHERIT_SET(file, flags, pool, apr_unix_file_cleanup)
371
372 /* We need to do this by hand instead of using APR_IMPLEMENT_INHERIT_UNSET
373  * because the macro sets both cleanups to the same function, which is not
374  * suitable on Unix (see PR 41119). */
375 APR_DECLARE(apr_status_t) apr_file_inherit_unset(apr_file_t *thefile)
376 {
377     if (thefile->flags & APR_FOPEN_NOCLEANUP) {
378         return APR_EINVAL;
379     }
380     if (thefile->flags & APR_INHERIT) {
381         int flags;
382
383         if ((flags = fcntl(thefile->filedes, F_GETFD)) == -1)
384             return errno;
385
386         flags |= FD_CLOEXEC;
387         if (fcntl(thefile->filedes, F_SETFD, flags) == -1)
388             return errno;
389
390         thefile->flags &= ~APR_INHERIT;
391         apr_pool_child_cleanup_set(thefile->pool,
392                                    (void *)thefile,
393                                    apr_unix_file_cleanup,
394                                    apr_unix_child_file_cleanup);
395     }
396     return APR_SUCCESS;
397 }
398
399 APR_POOL_IMPLEMENT_ACCESSOR(file)
400
401 APR_DECLARE(apr_status_t) apr_file_link(const char *from_path, 
402                                           const char *to_path)
403 {
404     if (link(from_path, to_path) == -1) {
405         return errno;
406     }
407
408     return APR_SUCCESS;
409 }