~ubuntu-branches/ubuntu/feisty/apache2/feisty

« back to all changes in this revision

Viewing changes to srclib/apr/file_io/unix/open.c

  • Committer: Bazaar Package Importer
  • Author(s): Andreas Barth
  • Date: 2006-12-09 21:05:45 UTC
  • mfrom: (0.6.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20061209210545-h70s0xaqc2v8vqr2
Tags: 2.2.3-3.2
* Non-maintainer upload.
* 043_ajp_connection_reuse: Patch from upstream Bugzilla, fixing a critical
  issue with regard to connection reuse in mod_proxy_ajp.
  Closes: #396265

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright 2000-2005 The Apache Software Foundation or its licensors, as
 
2
 * applicable.
 
3
 *
 
4
 * Licensed under the Apache License, Version 2.0 (the "License");
 
5
 * you may not use this file except in compliance with the License.
 
6
 * 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
apr_status_t apr_unix_file_cleanup(void *thefile)
 
30
{
 
31
    apr_file_t *file = thefile;
 
32
    apr_status_t flush_rv = APR_SUCCESS, rv = APR_SUCCESS;
 
33
 
 
34
    if (file->buffered) {
 
35
        flush_rv = apr_file_flush(file);
 
36
    }
 
37
    if (close(file->filedes) == 0) {
 
38
        file->filedes = -1;
 
39
        if (file->flags & APR_DELONCLOSE) {
 
40
            unlink(file->fname);
 
41
        }
 
42
#if APR_HAS_THREADS
 
43
        if (file->thlock) {
 
44
            rv = apr_thread_mutex_destroy(file->thlock);
 
45
        }
 
46
#endif
 
47
    }
 
48
    else {
 
49
        /* Are there any error conditions other than EINTR or EBADF? */
 
50
        rv = errno;
 
51
    }
 
52
#ifndef WAITIO_USES_POLL
 
53
    if (file->pollset != NULL) {
 
54
        int pollset_rv = apr_pollset_destroy(file->pollset);
 
55
        /* If the file close failed, return its error value,
 
56
         * not apr_pollset_destroy()'s.
 
57
         */
 
58
        if (rv == APR_SUCCESS) {
 
59
            rv = pollset_rv;
 
60
        }
 
61
    }
 
62
#endif /* !WAITIO_USES_POLL */
 
63
    return rv != APR_SUCCESS ? rv : flush_rv;
 
64
}
 
65
 
 
66
APR_DECLARE(apr_status_t) apr_file_open(apr_file_t **new, 
 
67
                                        const char *fname, 
 
68
                                        apr_int32_t flag, 
 
69
                                        apr_fileperms_t perm, 
 
70
                                        apr_pool_t *pool)
 
71
{
 
72
    apr_os_file_t fd;
 
73
    int oflags = 0;
 
74
#if APR_HAS_THREADS
 
75
    apr_thread_mutex_t *thlock;
 
76
    apr_status_t rv;
 
77
#endif
 
78
 
 
79
    if ((flag & APR_READ) && (flag & APR_WRITE)) {
 
80
        oflags = O_RDWR;
 
81
    }
 
82
    else if (flag & APR_READ) {
 
83
        oflags = O_RDONLY;
 
84
    }
 
85
    else if (flag & APR_WRITE) {
 
86
        oflags = O_WRONLY;
 
87
    }
 
88
    else {
 
89
        return APR_EACCES; 
 
90
    }
 
91
 
 
92
    if (flag & APR_CREATE) {
 
93
        oflags |= O_CREAT; 
 
94
        if (flag & APR_EXCL) {
 
95
            oflags |= O_EXCL;
 
96
        }
 
97
    }
 
98
    if ((flag & APR_EXCL) && !(flag & APR_CREATE)) {
 
99
        return APR_EACCES;
 
100
    }   
 
101
 
 
102
    if (flag & APR_APPEND) {
 
103
        oflags |= O_APPEND;
 
104
    }
 
105
    if (flag & APR_TRUNCATE) {
 
106
        oflags |= O_TRUNC;
 
107
    }
 
108
#ifdef O_BINARY
 
109
    if (flag & APR_BINARY) {
 
110
        oflags |= O_BINARY;
 
111
    }
 
112
#endif
 
113
    
 
114
#if APR_HAS_LARGE_FILES && defined(_LARGEFILE64_SOURCE)
 
115
    oflags |= O_LARGEFILE;
 
116
#elif defined(O_LARGEFILE)
 
117
    if (flag & APR_LARGEFILE) {
 
118
        oflags |= O_LARGEFILE;
 
119
    }
 
120
#endif
 
121
 
 
122
#if APR_HAS_THREADS
 
123
    if ((flag & APR_BUFFERED) && (flag & APR_XTHREAD)) {
 
124
        rv = apr_thread_mutex_create(&thlock,
 
125
                                     APR_THREAD_MUTEX_DEFAULT, pool);
 
126
        if (rv) {
 
127
            return rv;
 
128
        }
 
129
    }
 
130
#endif
 
131
 
 
132
    if (perm == APR_OS_DEFAULT) {
 
133
        fd = open(fname, oflags, 0666);
 
134
    }
 
135
    else {
 
136
        fd = open(fname, oflags, apr_unix_perms2mode(perm));
 
137
    } 
 
138
    if (fd < 0) {
 
139
       return errno;
 
140
    }
 
141
 
 
142
    (*new) = (apr_file_t *)apr_pcalloc(pool, sizeof(apr_file_t));
 
143
    (*new)->pool = pool;
 
144
    (*new)->flags = flag;
 
145
    (*new)->filedes = fd;
 
146
 
 
147
    (*new)->fname = apr_pstrdup(pool, fname);
 
148
 
 
149
    (*new)->blocking = BLK_ON;
 
150
    (*new)->buffered = (flag & APR_BUFFERED) > 0;
 
151
 
 
152
    if ((*new)->buffered) {
 
153
        (*new)->buffer = apr_palloc(pool, APR_FILE_BUFSIZE);
 
154
#if APR_HAS_THREADS
 
155
        if ((*new)->flags & APR_XTHREAD) {
 
156
            (*new)->thlock = thlock;
 
157
        }
 
158
#endif
 
159
    }
 
160
    else {
 
161
        (*new)->buffer = NULL;
 
162
    }
 
163
 
 
164
    (*new)->is_pipe = 0;
 
165
    (*new)->timeout = -1;
 
166
    (*new)->ungetchar = -1;
 
167
    (*new)->eof_hit = 0;
 
168
    (*new)->filePtr = 0;
 
169
    (*new)->bufpos = 0;
 
170
    (*new)->dataRead = 0;
 
171
    (*new)->direction = 0;
 
172
#ifndef WAITIO_USES_POLL
 
173
    /* Start out with no pollset.  apr_wait_for_io_or_timeout() will
 
174
     * initialize the pollset if needed.
 
175
     */
 
176
    (*new)->pollset = NULL;
 
177
#endif
 
178
    if (!(flag & APR_FILE_NOCLEANUP)) {
 
179
        apr_pool_cleanup_register((*new)->pool, (void *)(*new), 
 
180
                                  apr_unix_file_cleanup, 
 
181
                                  apr_unix_file_cleanup);
 
182
    }
 
183
    return APR_SUCCESS;
 
184
}
 
185
 
 
186
APR_DECLARE(apr_status_t) apr_file_close(apr_file_t *file)
 
187
{
 
188
    return apr_pool_cleanup_run(file->pool, file, apr_unix_file_cleanup);
 
189
}
 
190
 
 
191
APR_DECLARE(apr_status_t) apr_file_remove(const char *path, apr_pool_t *pool)
 
192
{
 
193
    if (unlink(path) == 0) {
 
194
        return APR_SUCCESS;
 
195
    }
 
196
    else {
 
197
        return errno;
 
198
    }
 
199
}
 
200
 
 
201
APR_DECLARE(apr_status_t) apr_file_rename(const char *from_path, 
 
202
                                          const char *to_path,
 
203
                                          apr_pool_t *p)
 
204
{
 
205
    if (rename(from_path, to_path) != 0) {
 
206
        return errno;
 
207
    }
 
208
    return APR_SUCCESS;
 
209
}
 
210
 
 
211
APR_DECLARE(apr_status_t) apr_os_file_get(apr_os_file_t *thefile, 
 
212
                                          apr_file_t *file)
 
213
{
 
214
    *thefile = file->filedes;
 
215
    return APR_SUCCESS;
 
216
}
 
217
 
 
218
APR_DECLARE(apr_status_t) apr_os_file_put(apr_file_t **file, 
 
219
                                          apr_os_file_t *thefile,
 
220
                                          apr_int32_t flags, apr_pool_t *pool)
 
221
{
 
222
    int *dafile = thefile;
 
223
    
 
224
    (*file) = apr_pcalloc(pool, sizeof(apr_file_t));
 
225
    (*file)->pool = pool;
 
226
    (*file)->eof_hit = 0;
 
227
    (*file)->blocking = BLK_UNKNOWN; /* in case it is a pipe */
 
228
    (*file)->timeout = -1;
 
229
    (*file)->ungetchar = -1; /* no char avail */
 
230
    (*file)->filedes = *dafile;
 
231
    (*file)->flags = flags | APR_FILE_NOCLEANUP;
 
232
    (*file)->buffered = (flags & APR_BUFFERED) > 0;
 
233
 
 
234
#ifndef WAITIO_USES_POLL
 
235
    /* Start out with no pollset.  apr_wait_for_io_or_timeout() will
 
236
     * initialize the pollset if needed.
 
237
     */
 
238
    (*file)->pollset = NULL;
 
239
#endif
 
240
 
 
241
    if ((*file)->buffered) {
 
242
        (*file)->buffer = apr_palloc(pool, APR_FILE_BUFSIZE);
 
243
#if APR_HAS_THREADS
 
244
        if ((*file)->flags & APR_XTHREAD) {
 
245
            apr_status_t rv;
 
246
            rv = apr_thread_mutex_create(&((*file)->thlock),
 
247
                                         APR_THREAD_MUTEX_DEFAULT, pool);
 
248
            if (rv) {
 
249
                return rv;
 
250
            }
 
251
        }
 
252
#endif
 
253
    }
 
254
    return APR_SUCCESS;
 
255
}    
 
256
 
 
257
APR_DECLARE(apr_status_t) apr_file_eof(apr_file_t *fptr)
 
258
{
 
259
    if (fptr->eof_hit == 1) {
 
260
        return APR_EOF;
 
261
    }
 
262
    return APR_SUCCESS;
 
263
}   
 
264
 
 
265
APR_DECLARE(apr_status_t) apr_file_open_stderr(apr_file_t **thefile, 
 
266
                                               apr_pool_t *pool)
 
267
{
 
268
    int fd = STDERR_FILENO;
 
269
 
 
270
    return apr_os_file_put(thefile, &fd, 0, pool);
 
271
}
 
272
 
 
273
APR_DECLARE(apr_status_t) apr_file_open_stdout(apr_file_t **thefile, 
 
274
                                               apr_pool_t *pool)
 
275
{
 
276
    int fd = STDOUT_FILENO;
 
277
 
 
278
    return apr_os_file_put(thefile, &fd, 0, pool);
 
279
}
 
280
 
 
281
APR_DECLARE(apr_status_t) apr_file_open_stdin(apr_file_t **thefile, 
 
282
                                              apr_pool_t *pool)
 
283
{
 
284
    int fd = STDIN_FILENO;
 
285
 
 
286
    return apr_os_file_put(thefile, &fd, 0, pool);
 
287
}
 
288
 
 
289
APR_IMPLEMENT_INHERIT_SET(file, flags, pool, apr_unix_file_cleanup)
 
290
 
 
291
APR_IMPLEMENT_INHERIT_UNSET(file, flags, pool, apr_unix_file_cleanup)
 
292
 
 
293
APR_POOL_IMPLEMENT_ACCESSOR(file)