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

« back to all changes in this revision

Viewing changes to srclib/apr/file_io/unix/readwrite.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_thread_mutex.h"
 
20
#include "apr_support.h"
 
21
 
 
22
/* The only case where we don't use wait_for_io_or_timeout is on
 
23
 * pre-BONE BeOS, so this check should be sufficient and simpler */
 
24
#if !BEOS_R5
 
25
#define USE_WAIT_FOR_IO
 
26
#endif
 
27
 
 
28
APR_DECLARE(apr_status_t) apr_file_read(apr_file_t *thefile, void *buf, apr_size_t *nbytes)
 
29
{
 
30
    apr_ssize_t rv;
 
31
    apr_size_t bytes_read;
 
32
 
 
33
    if (*nbytes <= 0) {
 
34
        *nbytes = 0;
 
35
        return APR_SUCCESS;
 
36
    }
 
37
 
 
38
    if (thefile->buffered) {
 
39
        char *pos = (char *)buf;
 
40
        apr_uint64_t blocksize;
 
41
        apr_uint64_t size = *nbytes;
 
42
 
 
43
#if APR_HAS_THREADS
 
44
        if (thefile->thlock) {
 
45
            apr_thread_mutex_lock(thefile->thlock);
 
46
        }
 
47
#endif
 
48
 
 
49
        if (thefile->direction == 1) {
 
50
            rv = apr_file_flush(thefile);
 
51
            if (rv) {
 
52
#if APR_HAS_THREADS
 
53
                if (thefile->thlock) {
 
54
                    apr_thread_mutex_unlock(thefile->thlock);
 
55
                }
 
56
#endif
 
57
                return rv;
 
58
            }
 
59
            thefile->bufpos = 0;
 
60
            thefile->direction = 0;
 
61
            thefile->dataRead = 0;
 
62
        }
 
63
 
 
64
        rv = 0;
 
65
        if (thefile->ungetchar != -1) {
 
66
            *pos = (char)thefile->ungetchar;
 
67
            ++pos;
 
68
            --size;
 
69
            thefile->ungetchar = -1;
 
70
        }
 
71
        while (rv == 0 && size > 0) {
 
72
            if (thefile->bufpos >= thefile->dataRead) {
 
73
                int bytesread = read(thefile->filedes, thefile->buffer, APR_FILE_BUFSIZE);
 
74
                if (bytesread == 0) {
 
75
                    thefile->eof_hit = TRUE;
 
76
                    rv = APR_EOF;
 
77
                    break;
 
78
                }
 
79
                else if (bytesread == -1) {
 
80
                    rv = errno;
 
81
                    break;
 
82
                }
 
83
                thefile->dataRead = bytesread;
 
84
                thefile->filePtr += thefile->dataRead;
 
85
                thefile->bufpos = 0;
 
86
            }
 
87
 
 
88
            blocksize = size > thefile->dataRead - thefile->bufpos ? thefile->dataRead - thefile->bufpos : size;
 
89
            memcpy(pos, thefile->buffer + thefile->bufpos, blocksize);
 
90
            thefile->bufpos += blocksize;
 
91
            pos += blocksize;
 
92
            size -= blocksize;
 
93
        }
 
94
 
 
95
        *nbytes = pos - (char *)buf;
 
96
        if (*nbytes) {
 
97
            rv = 0;
 
98
        }
 
99
#if APR_HAS_THREADS
 
100
        if (thefile->thlock) {
 
101
            apr_thread_mutex_unlock(thefile->thlock);
 
102
        }
 
103
#endif
 
104
        return rv;
 
105
    }
 
106
    else {
 
107
        bytes_read = 0;
 
108
        if (thefile->ungetchar != -1) {
 
109
            bytes_read = 1;
 
110
            *(char *)buf = (char)thefile->ungetchar;
 
111
            buf = (char *)buf + 1;
 
112
            (*nbytes)--;
 
113
            thefile->ungetchar = -1;
 
114
            if (*nbytes == 0) {
 
115
                *nbytes = bytes_read;
 
116
                return APR_SUCCESS;
 
117
            }
 
118
        }
 
119
 
 
120
        do {
 
121
            rv = read(thefile->filedes, buf, *nbytes);
 
122
        } while (rv == -1 && errno == EINTR);
 
123
#ifdef USE_WAIT_FOR_IO
 
124
        if (rv == -1 && 
 
125
            (errno == EAGAIN || errno == EWOULDBLOCK) && 
 
126
            thefile->timeout != 0) {
 
127
            apr_status_t arv = apr_wait_for_io_or_timeout(thefile, NULL, 1);
 
128
            if (arv != APR_SUCCESS) {
 
129
                *nbytes = bytes_read;
 
130
                return arv;
 
131
            }
 
132
            else {
 
133
                do {
 
134
                    rv = read(thefile->filedes, buf, *nbytes);
 
135
                } while (rv == -1 && errno == EINTR);
 
136
            }
 
137
        }  
 
138
#endif
 
139
        *nbytes = bytes_read;
 
140
        if (rv == 0) {
 
141
            thefile->eof_hit = TRUE;
 
142
            return APR_EOF;
 
143
        }
 
144
        if (rv > 0) {
 
145
            *nbytes += rv;
 
146
            return APR_SUCCESS;
 
147
        }
 
148
        return errno;
 
149
    }
 
150
}
 
151
 
 
152
APR_DECLARE(apr_status_t) apr_file_write(apr_file_t *thefile, const void *buf, apr_size_t *nbytes)
 
153
{
 
154
    apr_size_t rv;
 
155
 
 
156
    if (thefile->buffered) {
 
157
        char *pos = (char *)buf;
 
158
        int blocksize;
 
159
        int size = *nbytes;
 
160
 
 
161
#if APR_HAS_THREADS
 
162
        if (thefile->thlock) {
 
163
            apr_thread_mutex_lock(thefile->thlock);
 
164
        }
 
165
#endif
 
166
 
 
167
        if ( thefile->direction == 0 ) {
 
168
            /* Position file pointer for writing at the offset we are 
 
169
             * logically reading from
 
170
             */
 
171
            apr_int64_t offset = thefile->filePtr - thefile->dataRead + thefile->bufpos;
 
172
            if (offset != thefile->filePtr)
 
173
                lseek(thefile->filedes, offset, SEEK_SET);
 
174
            thefile->bufpos = thefile->dataRead = 0;
 
175
            thefile->direction = 1;
 
176
        }
 
177
 
 
178
        rv = 0;
 
179
        while (rv == 0 && size > 0) {
 
180
            if (thefile->bufpos == APR_FILE_BUFSIZE)   /* write buffer is full*/
 
181
                rv = apr_file_flush(thefile);
 
182
 
 
183
            blocksize = size > APR_FILE_BUFSIZE - thefile->bufpos ? 
 
184
                        APR_FILE_BUFSIZE - thefile->bufpos : size;
 
185
            memcpy(thefile->buffer + thefile->bufpos, pos, blocksize);                      
 
186
            thefile->bufpos += blocksize;
 
187
            pos += blocksize;
 
188
            size -= blocksize;
 
189
        }
 
190
 
 
191
#if APR_HAS_THREADS
 
192
        if (thefile->thlock) {
 
193
            apr_thread_mutex_unlock(thefile->thlock);
 
194
        }
 
195
#endif
 
196
        return rv;
 
197
    }
 
198
    else {
 
199
        do {
 
200
            rv = write(thefile->filedes, buf, *nbytes);
 
201
        } while (rv == (apr_size_t)-1 && errno == EINTR);
 
202
#ifdef USE_WAIT_FOR_IO
 
203
        if (rv == (apr_size_t)-1 &&
 
204
            (errno == EAGAIN || errno == EWOULDBLOCK) && 
 
205
            thefile->timeout != 0) {
 
206
            apr_status_t arv = apr_wait_for_io_or_timeout(thefile, NULL, 0);
 
207
            if (arv != APR_SUCCESS) {
 
208
                *nbytes = 0;
 
209
                return arv;
 
210
            }
 
211
            else {
 
212
                do {
 
213
                    do {
 
214
                        rv = write(thefile->filedes, buf, *nbytes);
 
215
                    } while (rv == (apr_size_t)-1 && errno == EINTR);
 
216
                    if (rv == (apr_size_t)-1 &&
 
217
                        (errno == EAGAIN || errno == EWOULDBLOCK)) {
 
218
                        *nbytes /= 2; /* yes, we'll loop if kernel lied
 
219
                                       * and we can't even write 1 byte
 
220
                                       */
 
221
                    }
 
222
                    else {
 
223
                        break;
 
224
                    }
 
225
                } while (1);
 
226
            }
 
227
        }  
 
228
#endif
 
229
        if (rv == (apr_size_t)-1) {
 
230
            (*nbytes) = 0;
 
231
            return errno;
 
232
        }
 
233
        *nbytes = rv;
 
234
        return APR_SUCCESS;
 
235
    }
 
236
}
 
237
 
 
238
APR_DECLARE(apr_status_t) apr_file_writev(apr_file_t *thefile, const struct iovec *vec,
 
239
                                          apr_size_t nvec, apr_size_t *nbytes)
 
240
{
 
241
#ifdef HAVE_WRITEV
 
242
    int bytes;
 
243
 
 
244
    if ((bytes = writev(thefile->filedes, vec, nvec)) < 0) {
 
245
        *nbytes = 0;
 
246
        return errno;
 
247
    }
 
248
    else {
 
249
        *nbytes = bytes;
 
250
        return APR_SUCCESS;
 
251
    }
 
252
#else
 
253
    /**
 
254
     * The problem with trying to output the entire iovec is that we cannot
 
255
     * maintain the behavoir that a real writev would have.  If we iterate
 
256
     * over the iovec one at a time, we loose the atomic properties of 
 
257
     * writev().  The other option is to combine the entire iovec into one
 
258
     * buffer that we could then send in one call to write().  This is not 
 
259
     * reasonable since we do not know how much data an iovec could contain.
 
260
     *
 
261
     * The only reasonable option, that maintains the semantics of a real 
 
262
     * writev(), is to only write the first iovec.  Callers of file_writev()
 
263
     * must deal with partial writes as they normally would. If you want to 
 
264
     * ensure an entire iovec is written, use apr_file_writev_full().
 
265
     */
 
266
 
 
267
    *nbytes = vec[0].iov_len;
 
268
    return apr_file_write(thefile, vec[0].iov_base, nbytes);
 
269
#endif
 
270
}
 
271
 
 
272
APR_DECLARE(apr_status_t) apr_file_putc(char ch, apr_file_t *thefile)
 
273
{
 
274
    apr_size_t nbytes = 1;
 
275
 
 
276
    return apr_file_write(thefile, &ch, &nbytes);
 
277
}
 
278
 
 
279
APR_DECLARE(apr_status_t) apr_file_ungetc(char ch, apr_file_t *thefile)
 
280
{
 
281
    thefile->ungetchar = (unsigned char)ch;
 
282
    return APR_SUCCESS; 
 
283
}
 
284
 
 
285
APR_DECLARE(apr_status_t) apr_file_getc(char *ch, apr_file_t *thefile)
 
286
{
 
287
    apr_size_t nbytes = 1;
 
288
 
 
289
    return apr_file_read(thefile, ch, &nbytes);
 
290
}
 
291
 
 
292
APR_DECLARE(apr_status_t) apr_file_puts(const char *str, apr_file_t *thefile)
 
293
{
 
294
    return apr_file_write_full(thefile, str, strlen(str), NULL);
 
295
}
 
296
 
 
297
APR_DECLARE(apr_status_t) apr_file_flush(apr_file_t *thefile)
 
298
{
 
299
    if (thefile->buffered) {
 
300
        apr_int64_t written = 0;
 
301
 
 
302
        if (thefile->direction == 1 && thefile->bufpos) {
 
303
            do {
 
304
                written = write(thefile->filedes, thefile->buffer, thefile->bufpos);
 
305
            } while (written == (apr_int64_t)-1 && errno == EINTR);
 
306
            if (written == (apr_int64_t)-1) {
 
307
                return errno;
 
308
            }
 
309
            thefile->filePtr += written;
 
310
            thefile->bufpos = 0;
 
311
        }
 
312
    }
 
313
    /* There isn't anything to do if we aren't buffering the output
 
314
     * so just return success.
 
315
     */
 
316
    return APR_SUCCESS; 
 
317
}
 
318
 
 
319
APR_DECLARE(apr_status_t) apr_file_gets(char *str, int len, apr_file_t *thefile)
 
320
{
 
321
    apr_status_t rv = APR_SUCCESS; /* get rid of gcc warning */
 
322
    apr_size_t nbytes;
 
323
    const char *str_start = str;
 
324
    char *final = str + len - 1;
 
325
 
 
326
    if (len <= 1) {  
 
327
        /* sort of like fgets(), which returns NULL and stores no bytes 
 
328
         */
 
329
        return APR_SUCCESS;
 
330
    }
 
331
 
 
332
    /* If we have an underlying buffer, we can be *much* more efficient
 
333
     * and skip over the apr_file_read calls.
 
334
     */
 
335
    if (thefile->buffered) {
 
336
 
 
337
#if APR_HAS_THREADS
 
338
        if (thefile->thlock) {
 
339
            apr_thread_mutex_lock(thefile->thlock);
 
340
        }
 
341
#endif
 
342
 
 
343
        if (thefile->direction == 1) {
 
344
            rv = apr_file_flush(thefile);
 
345
            if (rv) {
 
346
#if APR_HAS_THREADS
 
347
                if (thefile->thlock) {
 
348
                    apr_thread_mutex_unlock(thefile->thlock);
 
349
                }
 
350
#endif
 
351
                return rv;
 
352
            }
 
353
 
 
354
            thefile->direction = 0;
 
355
            thefile->bufpos = 0;
 
356
            thefile->dataRead = 0;
 
357
        }
 
358
 
 
359
        while (str < final) { /* leave room for trailing '\0' */
 
360
            /* Force ungetc leftover to call apr_file_read. */
 
361
            if (thefile->bufpos < thefile->dataRead &&
 
362
                thefile->ungetchar == -1) {
 
363
                *str = thefile->buffer[thefile->bufpos++];
 
364
            }
 
365
            else {
 
366
                nbytes = 1;
 
367
                rv = apr_file_read(thefile, str, &nbytes);
 
368
                if (rv != APR_SUCCESS) {
 
369
                    break;
 
370
                }
 
371
            }
 
372
            if (*str == '\n') {
 
373
                ++str;
 
374
                break;
 
375
            }
 
376
            ++str;
 
377
        }
 
378
 
 
379
#if APR_HAS_THREADS
 
380
        if (thefile->thlock) {
 
381
            apr_thread_mutex_unlock(thefile->thlock);
 
382
        }
 
383
#endif
 
384
    }
 
385
    else {
 
386
        while (str < final) { /* leave room for trailing '\0' */
 
387
            nbytes = 1;
 
388
            rv = apr_file_read(thefile, str, &nbytes);
 
389
            if (rv != APR_SUCCESS) {
 
390
                break;
 
391
            }
 
392
            if (*str == '\n') {
 
393
                ++str;
 
394
                break;
 
395
            }
 
396
            ++str;
 
397
        }
 
398
    }
 
399
 
 
400
    /* We must store a terminating '\0' if we've stored any chars. We can
 
401
     * get away with storing it if we hit an error first. 
 
402
     */
 
403
    *str = '\0';
 
404
    if (str > str_start) {
 
405
        /* we stored chars; don't report EOF or any other errors;
 
406
         * the app will find out about that on the next call
 
407
         */
 
408
        return APR_SUCCESS;
 
409
    }
 
410
    return rv;
 
411
}
 
412
 
 
413
struct apr_file_printf_data {
 
414
    apr_vformatter_buff_t vbuff;
 
415
    apr_file_t *fptr;
 
416
    char *buf;
 
417
};
 
418
 
 
419
static int file_printf_flush(apr_vformatter_buff_t *buff)
 
420
{
 
421
    struct apr_file_printf_data *data = (struct apr_file_printf_data *)buff;
 
422
 
 
423
    if (apr_file_write_full(data->fptr, data->buf, 
 
424
                            data->vbuff.curpos - data->buf, NULL)) {
 
425
        return -1;
 
426
    }
 
427
 
 
428
    data->vbuff.curpos = data->buf;
 
429
    return 0;
 
430
}
 
431
 
 
432
APR_DECLARE_NONSTD(int) apr_file_printf(apr_file_t *fptr, 
 
433
                                        const char *format, ...)
 
434
{
 
435
    struct apr_file_printf_data data;
 
436
    va_list ap;
 
437
    int count;
 
438
 
 
439
    /* don't really need a HUGE_STRING_LEN anymore */
 
440
    data.buf = malloc(HUGE_STRING_LEN);
 
441
    if (data.buf == NULL) {
 
442
        return -1;
 
443
    }
 
444
    data.vbuff.curpos = data.buf;
 
445
    data.vbuff.endpos = data.buf + HUGE_STRING_LEN;
 
446
    data.fptr = fptr;
 
447
    va_start(ap, format);
 
448
    count = apr_vformatter(file_printf_flush,
 
449
                           (apr_vformatter_buff_t *)&data, format, ap);
 
450
    /* apr_vformatter does not call flush for the last bits */
 
451
    if (count >= 0) file_printf_flush((apr_vformatter_buff_t *)&data);
 
452
 
 
453
    va_end(ap);
 
454
 
 
455
    free(data.buf);
 
456
 
 
457
    return count;
 
458
}