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

« back to all changes in this revision

Viewing changes to srclib/apr/file_io/win32/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 "win32/apr_arch_file_io.h"
 
18
#include "apr_file_io.h"
 
19
#include "apr_general.h"
 
20
#include "apr_strings.h"
 
21
#include "apr_lib.h"
 
22
#include "apr_errno.h"
 
23
#include <malloc.h>
 
24
#include "apr_arch_atime.h"
 
25
#include "apr_arch_misc.h"
 
26
 
 
27
/*
 
28
 * read_with_timeout() 
 
29
 * Uses async i/o to emulate unix non-blocking i/o with timeouts.
 
30
 */
 
31
static apr_status_t read_with_timeout(apr_file_t *file, void *buf, apr_size_t len_in, apr_size_t *nbytes)
 
32
{
 
33
    apr_status_t rv;
 
34
    DWORD len = (DWORD)len_in;
 
35
    DWORD bytesread = 0;
 
36
 
 
37
    /* Handle the zero timeout non-blocking case */
 
38
    if (file->timeout == 0) {
 
39
        /* Peek at the pipe. If there is no data available, return APR_EAGAIN.
 
40
         * If data is available, go ahead and read it.
 
41
         */
 
42
        if (file->pipe) {
 
43
            DWORD bytes;
 
44
            if (!PeekNamedPipe(file->filehand, NULL, 0, NULL, &bytes, NULL)) {
 
45
                rv = apr_get_os_error();
 
46
                if (rv == APR_FROM_OS_ERROR(ERROR_BROKEN_PIPE)) {
 
47
                    rv = APR_EOF;
 
48
                }
 
49
                *nbytes = 0;
 
50
                return rv;
 
51
            }
 
52
            else {
 
53
                if (bytes == 0) {
 
54
                    *nbytes = 0;
 
55
                    return APR_EAGAIN;
 
56
                }
 
57
                if (len > bytes) {
 
58
                    len = bytes;
 
59
                }
 
60
            }
 
61
        }
 
62
        else {
 
63
            /* ToDo: Handle zero timeout non-blocking file i/o 
 
64
             * This is not needed until an APR application needs to
 
65
             * timeout file i/o (which means setting file i/o non-blocking)
 
66
             */
 
67
        }
 
68
    }
 
69
 
 
70
    if (file->pOverlapped && !file->pipe) {
 
71
        file->pOverlapped->Offset     = (DWORD)file->filePtr;
 
72
        file->pOverlapped->OffsetHigh = (DWORD)(file->filePtr >> 32);
 
73
    }
 
74
 
 
75
    rv = ReadFile(file->filehand, buf, len, 
 
76
                  &bytesread, file->pOverlapped);
 
77
    *nbytes = bytesread;
 
78
 
 
79
    if (!rv) {
 
80
        rv = apr_get_os_error();
 
81
        if (rv == APR_FROM_OS_ERROR(ERROR_IO_PENDING)) {
 
82
            /* Wait for the pending i/o */
 
83
            if (file->timeout > 0) {
 
84
                /* timeout in milliseconds... */
 
85
                rv = WaitForSingleObject(file->pOverlapped->hEvent, 
 
86
                                         (DWORD)(file->timeout/1000)); 
 
87
            }
 
88
            else if (file->timeout == -1) {
 
89
                rv = WaitForSingleObject(file->pOverlapped->hEvent, INFINITE);
 
90
            }
 
91
            switch (rv) {
 
92
                case WAIT_OBJECT_0:
 
93
                    GetOverlappedResult(file->filehand, file->pOverlapped, 
 
94
                                        &bytesread, TRUE);
 
95
                    *nbytes = bytesread;
 
96
                    rv = APR_SUCCESS;
 
97
                    break;
 
98
 
 
99
                case WAIT_TIMEOUT:
 
100
                    rv = APR_TIMEUP;
 
101
                    break;
 
102
 
 
103
                case WAIT_FAILED:
 
104
                    rv = apr_get_os_error();
 
105
                    break;
 
106
 
 
107
                default:
 
108
                    break;
 
109
            }
 
110
 
 
111
            if (rv != APR_SUCCESS) {
 
112
                if (apr_os_level >= APR_WIN_98) {
 
113
                    CancelIo(file->filehand);
 
114
                }
 
115
            }
 
116
        }
 
117
        else if (rv == APR_FROM_OS_ERROR(ERROR_BROKEN_PIPE)) {
 
118
            /* Assume ERROR_BROKEN_PIPE signals an EOF reading from a pipe */
 
119
            rv = APR_EOF;
 
120
        }
 
121
    } else {
 
122
        /* OK and 0 bytes read ==> end of file */
 
123
        if (*nbytes == 0)
 
124
            rv = APR_EOF;
 
125
        else
 
126
            rv = APR_SUCCESS;
 
127
    }
 
128
    if (rv == APR_SUCCESS && file->pOverlapped && !file->pipe) {
 
129
        file->filePtr += *nbytes;
 
130
    }
 
131
    return rv;
 
132
}
 
133
 
 
134
APR_DECLARE(apr_status_t) apr_file_read(apr_file_t *thefile, void *buf, apr_size_t *len)
 
135
{
 
136
    apr_status_t rv;
 
137
    DWORD bytes_read = 0;
 
138
 
 
139
    if (*len <= 0) {
 
140
        *len = 0;
 
141
        return APR_SUCCESS;
 
142
    }
 
143
 
 
144
    /* If the file is open for xthread support, allocate and
 
145
     * initialize the overlapped and io completion event (hEvent). 
 
146
     * Threads should NOT share an apr_file_t or its hEvent.
 
147
     */
 
148
    if ((thefile->flags & APR_XTHREAD) && !thefile->pOverlapped ) {
 
149
        thefile->pOverlapped = (OVERLAPPED*) apr_pcalloc(thefile->pool, 
 
150
                                                         sizeof(OVERLAPPED));
 
151
        thefile->pOverlapped->hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
 
152
        if (!thefile->pOverlapped->hEvent) {
 
153
            rv = apr_get_os_error();
 
154
            return rv;
 
155
        }
 
156
    }
 
157
 
 
158
    /* Handle the ungetchar if there is one */
 
159
    if (thefile->ungetchar != -1) {
 
160
        bytes_read = 1;
 
161
        *(char *)buf = (char)thefile->ungetchar;
 
162
        buf = (char *)buf + 1;
 
163
        (*len)--;
 
164
        thefile->ungetchar = -1;
 
165
        if (*len == 0) {
 
166
            *len = bytes_read;
 
167
            return APR_SUCCESS;
 
168
        }
 
169
    }
 
170
    if (thefile->buffered) {
 
171
        char *pos = (char *)buf;
 
172
        apr_size_t blocksize;
 
173
        apr_size_t size = *len;
 
174
 
 
175
        apr_thread_mutex_lock(thefile->mutex);
 
176
 
 
177
        if (thefile->direction == 1) {
 
178
            rv = apr_file_flush(thefile);
 
179
            if (rv != APR_SUCCESS) {
 
180
                apr_thread_mutex_unlock(thefile->mutex);
 
181
                return rv;
 
182
            }
 
183
            thefile->bufpos = 0;
 
184
            thefile->direction = 0;
 
185
            thefile->dataRead = 0;
 
186
        }
 
187
 
 
188
        rv = 0;
 
189
        while (rv == 0 && size > 0) {
 
190
            if (thefile->bufpos >= thefile->dataRead) {
 
191
                apr_size_t read;
 
192
                rv = read_with_timeout(thefile, thefile->buffer, 
 
193
                                       APR_FILE_BUFSIZE, &read);
 
194
                if (read == 0) {
 
195
                    if (rv == APR_EOF)
 
196
                        thefile->eof_hit = TRUE;
 
197
                    break;
 
198
                }
 
199
                else {
 
200
                    thefile->dataRead = read;
 
201
                    thefile->filePtr += thefile->dataRead;
 
202
                    thefile->bufpos = 0;
 
203
                }
 
204
            }
 
205
 
 
206
            blocksize = size > thefile->dataRead - thefile->bufpos ? thefile->dataRead - thefile->bufpos : size;
 
207
            memcpy(pos, thefile->buffer + thefile->bufpos, blocksize);
 
208
            thefile->bufpos += blocksize;
 
209
            pos += blocksize;
 
210
            size -= blocksize;
 
211
        }
 
212
 
 
213
        *len = pos - (char *)buf;
 
214
        if (*len) {
 
215
            rv = APR_SUCCESS;
 
216
        }
 
217
        apr_thread_mutex_unlock(thefile->mutex);
 
218
    } else {  
 
219
        /* Unbuffered i/o */
 
220
        apr_size_t nbytes;
 
221
        rv = read_with_timeout(thefile, buf, *len, &nbytes);
 
222
        if (rv == APR_EOF)
 
223
            thefile->eof_hit = TRUE;
 
224
        *len = nbytes;
 
225
    }
 
226
 
 
227
    return rv;
 
228
}
 
229
 
 
230
APR_DECLARE(apr_status_t) apr_file_write(apr_file_t *thefile, const void *buf, apr_size_t *nbytes)
 
231
{
 
232
    apr_status_t rv;
 
233
    DWORD bwrote;
 
234
 
 
235
    /* If the file is open for xthread support, allocate and
 
236
     * initialize the overlapped and io completion event (hEvent). 
 
237
     * Threads should NOT share an apr_file_t or its hEvent.
 
238
     */
 
239
    if ((thefile->flags & APR_XTHREAD) && !thefile->pOverlapped ) {
 
240
        thefile->pOverlapped = (OVERLAPPED*) apr_pcalloc(thefile->pool, 
 
241
                                                         sizeof(OVERLAPPED));
 
242
        thefile->pOverlapped->hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
 
243
        if (!thefile->pOverlapped->hEvent) {
 
244
            rv = apr_get_os_error();
 
245
            return rv;
 
246
        }
 
247
    }
 
248
 
 
249
    if (thefile->buffered) {
 
250
        char *pos = (char *)buf;
 
251
        apr_size_t blocksize;
 
252
        apr_size_t size = *nbytes;
 
253
 
 
254
        apr_thread_mutex_lock(thefile->mutex);
 
255
 
 
256
        if (thefile->direction == 0) {
 
257
            // Position file pointer for writing at the offset we are logically reading from
 
258
            apr_off_t offset = thefile->filePtr - thefile->dataRead + thefile->bufpos;
 
259
            DWORD offlo = (DWORD)offset;
 
260
            DWORD offhi = (DWORD)(offset >> 32);
 
261
            if (offset != thefile->filePtr)
 
262
                SetFilePointer(thefile->filehand, offlo, &offhi, FILE_BEGIN);
 
263
            thefile->bufpos = thefile->dataRead = 0;
 
264
            thefile->direction = 1;
 
265
        }
 
266
 
 
267
        rv = 0;
 
268
        while (rv == 0 && size > 0) {
 
269
            if (thefile->bufpos == APR_FILE_BUFSIZE)   // write buffer is full
 
270
                rv = apr_file_flush(thefile);
 
271
 
 
272
            blocksize = size > APR_FILE_BUFSIZE - thefile->bufpos ? APR_FILE_BUFSIZE - thefile->bufpos : size;
 
273
            memcpy(thefile->buffer + thefile->bufpos, pos, blocksize);
 
274
            thefile->bufpos += blocksize;
 
275
            pos += blocksize;
 
276
            size -= blocksize;
 
277
        }
 
278
 
 
279
        apr_thread_mutex_unlock(thefile->mutex);
 
280
        return rv;
 
281
    } else {
 
282
        if (!thefile->pipe) {
 
283
            apr_off_t offset = 0;
 
284
            apr_status_t rc;
 
285
            if (thefile->append) {
 
286
                /* apr_file_lock will mutex the file across processes.
 
287
                 * The call to apr_thread_mutex_lock is added to avoid
 
288
                 * a race condition between LockFile and WriteFile 
 
289
                 * that occasionally leads to deadlocked threads.
 
290
                 */
 
291
                apr_thread_mutex_lock(thefile->mutex);
 
292
                rc = apr_file_lock(thefile, APR_FLOCK_EXCLUSIVE);
 
293
                if (rc != APR_SUCCESS) {
 
294
                    apr_thread_mutex_unlock(thefile->mutex);
 
295
                    return rc;
 
296
                }
 
297
                rc = apr_file_seek(thefile, APR_END, &offset);
 
298
                if (rc != APR_SUCCESS) {
 
299
                    apr_thread_mutex_unlock(thefile->mutex);
 
300
                    return rc;
 
301
                }
 
302
            }
 
303
            if (thefile->pOverlapped) {
 
304
                thefile->pOverlapped->Offset     = (DWORD)thefile->filePtr;
 
305
                thefile->pOverlapped->OffsetHigh = (DWORD)(thefile->filePtr >> 32);
 
306
            }
 
307
            rv = WriteFile(thefile->filehand, buf, (DWORD)*nbytes, &bwrote,
 
308
                           thefile->pOverlapped);
 
309
            if (thefile->append) {
 
310
                apr_file_unlock(thefile);
 
311
                apr_thread_mutex_unlock(thefile->mutex);
 
312
            }
 
313
        }
 
314
        else {
 
315
            rv = WriteFile(thefile->filehand, buf, (DWORD)*nbytes, &bwrote,
 
316
                           thefile->pOverlapped);
 
317
        }
 
318
        if (rv) {
 
319
            *nbytes = bwrote;
 
320
            rv = APR_SUCCESS;
 
321
        }
 
322
        else {
 
323
            (*nbytes) = 0;
 
324
            rv = apr_get_os_error();
 
325
            if (rv == APR_FROM_OS_ERROR(ERROR_IO_PENDING)) {
 
326
 
 
327
                DWORD timeout_ms;
 
328
 
 
329
                if (thefile->timeout == 0) {
 
330
                    timeout_ms = 0;
 
331
                }
 
332
                else if (thefile->timeout < 0) {
 
333
                    timeout_ms = INFINITE;
 
334
                }
 
335
                else {
 
336
                    timeout_ms = (DWORD)(thefile->timeout / 1000);
 
337
                }
 
338
               
 
339
                rv = WaitForSingleObject(thefile->pOverlapped->hEvent, timeout_ms);
 
340
                switch (rv) {
 
341
                    case WAIT_OBJECT_0:
 
342
                        GetOverlappedResult(thefile->filehand, thefile->pOverlapped, 
 
343
                                            &bwrote, TRUE);
 
344
                        *nbytes = bwrote;
 
345
                        rv = APR_SUCCESS;
 
346
                        break;
 
347
                    case WAIT_TIMEOUT:
 
348
                        rv = APR_TIMEUP;
 
349
                        break;
 
350
                    case WAIT_FAILED:
 
351
                        rv = apr_get_os_error();
 
352
                        break;
 
353
                    default:
 
354
                        break;
 
355
                }
 
356
                if (rv != APR_SUCCESS) {
 
357
                    if (apr_os_level >= APR_WIN_98)
 
358
                        CancelIo(thefile->filehand);
 
359
                }
 
360
            }
 
361
        }
 
362
        if (rv == APR_SUCCESS && thefile->pOverlapped && !thefile->pipe) {
 
363
            thefile->filePtr += *nbytes;
 
364
        }
 
365
    }
 
366
    return rv;
 
367
}
 
368
/* ToDo: Write for it anyway and test the oslevel!
 
369
 * Too bad WriteFileGather() is not supported on 95&98 (or NT prior to SP2)
 
370
 */
 
371
APR_DECLARE(apr_status_t) apr_file_writev(apr_file_t *thefile,
 
372
                                     const struct iovec *vec,
 
373
                                     apr_size_t nvec, 
 
374
                                     apr_size_t *nbytes)
 
375
{
 
376
    apr_status_t rv = APR_SUCCESS;
 
377
    apr_size_t i;
 
378
    apr_size_t bwrote = 0;
 
379
    char *buf;
 
380
 
 
381
    *nbytes = 0;
 
382
    for (i = 0; i < nvec; i++) {
 
383
        buf = vec[i].iov_base;
 
384
        bwrote = vec[i].iov_len;
 
385
        rv = apr_file_write(thefile, buf, &bwrote);
 
386
        *nbytes += bwrote;
 
387
        if (rv != APR_SUCCESS) {
 
388
            break;
 
389
        }
 
390
    }
 
391
    return rv;
 
392
}
 
393
 
 
394
APR_DECLARE(apr_status_t) apr_file_putc(char ch, apr_file_t *thefile)
 
395
{
 
396
    apr_size_t len = 1;
 
397
 
 
398
    return apr_file_write(thefile, &ch, &len);
 
399
}
 
400
 
 
401
APR_DECLARE(apr_status_t) apr_file_ungetc(char ch, apr_file_t *thefile)
 
402
{
 
403
    thefile->ungetchar = (unsigned char) ch;
 
404
    return APR_SUCCESS;
 
405
}
 
406
 
 
407
APR_DECLARE(apr_status_t) apr_file_getc(char *ch, apr_file_t *thefile)
 
408
{
 
409
    apr_status_t rc;
 
410
    apr_size_t bread;
 
411
 
 
412
    bread = 1;
 
413
    rc = apr_file_read(thefile, ch, &bread);
 
414
 
 
415
    if (rc) {
 
416
        return rc;
 
417
    }
 
418
    
 
419
    if (bread == 0) {
 
420
        thefile->eof_hit = TRUE;
 
421
        return APR_EOF;
 
422
    }
 
423
    return APR_SUCCESS; 
 
424
}
 
425
 
 
426
APR_DECLARE(apr_status_t) apr_file_puts(const char *str, apr_file_t *thefile)
 
427
{
 
428
    apr_size_t len = strlen(str);
 
429
 
 
430
    return apr_file_write(thefile, str, &len);
 
431
}
 
432
 
 
433
APR_DECLARE(apr_status_t) apr_file_gets(char *str, int len, apr_file_t *thefile)
 
434
{
 
435
    apr_size_t readlen;
 
436
    apr_status_t rv = APR_SUCCESS;
 
437
    int i;    
 
438
 
 
439
    for (i = 0; i < len-1; i++) {
 
440
        readlen = 1;
 
441
        rv = apr_file_read(thefile, str+i, &readlen);
 
442
 
 
443
        if (rv != APR_SUCCESS && rv != APR_EOF)
 
444
            return rv;
 
445
 
 
446
        if (readlen == 0) {
 
447
            /* If we have bytes, defer APR_EOF to the next call */
 
448
            if (i > 0)
 
449
                rv = APR_SUCCESS;
 
450
            break;
 
451
        }
 
452
        
 
453
        if (str[i] == '\n') {
 
454
            i++; /* don't clobber this char below */
 
455
            break;
 
456
        }
 
457
    }
 
458
    str[i] = 0;
 
459
    return rv;
 
460
}
 
461
 
 
462
APR_DECLARE(apr_status_t) apr_file_flush(apr_file_t *thefile)
 
463
{
 
464
    if (thefile->buffered) {
 
465
        DWORD numbytes, written = 0;
 
466
        apr_status_t rc = 0;
 
467
        char *buffer;
 
468
        apr_size_t bytesleft;
 
469
 
 
470
        if (thefile->direction == 1 && thefile->bufpos) {
 
471
            buffer = thefile->buffer;
 
472
            bytesleft = thefile->bufpos;           
 
473
 
 
474
            do {
 
475
                if (bytesleft > APR_DWORD_MAX) {
 
476
                    numbytes = APR_DWORD_MAX;
 
477
                }
 
478
                else {
 
479
                    numbytes = (DWORD)bytesleft;
 
480
                }
 
481
 
 
482
                if (!WriteFile(thefile->filehand, buffer, numbytes, &written, NULL)) {
 
483
                    rc = apr_get_os_error();
 
484
                    thefile->filePtr += written;
 
485
                    break;
 
486
                }
 
487
 
 
488
                thefile->filePtr += written;
 
489
                bytesleft -= written;
 
490
                buffer += written;
 
491
 
 
492
            } while (bytesleft > 0);
 
493
 
 
494
            if (rc == 0)
 
495
                thefile->bufpos = 0;
 
496
        }
 
497
 
 
498
        return rc;
 
499
    }
 
500
 
 
501
    /* There isn't anything to do if we aren't buffering the output
 
502
     * so just return success.
 
503
     */
 
504
    return APR_SUCCESS; 
 
505
}
 
506
 
 
507
struct apr_file_printf_data {
 
508
    apr_vformatter_buff_t vbuff;
 
509
    apr_file_t *fptr;
 
510
    char *buf;
 
511
};
 
512
 
 
513
static int file_printf_flush(apr_vformatter_buff_t *buff)
 
514
{
 
515
    struct apr_file_printf_data *data = (struct apr_file_printf_data *)buff;
 
516
 
 
517
    if (apr_file_write_full(data->fptr, data->buf,
 
518
                            data->vbuff.curpos - data->buf, NULL)) {
 
519
        return -1;
 
520
    }
 
521
 
 
522
    data->vbuff.curpos = data->buf;
 
523
    return 0;
 
524
}
 
525
 
 
526
APR_DECLARE_NONSTD(int) apr_file_printf(apr_file_t *fptr, 
 
527
                                        const char *format, ...)
 
528
{
 
529
    struct apr_file_printf_data data;
 
530
    va_list ap;
 
531
    int count;
 
532
 
 
533
    data.buf = malloc(HUGE_STRING_LEN);
 
534
    if (data.buf == NULL) {
 
535
        return 0;
 
536
    }
 
537
    data.vbuff.curpos = data.buf;
 
538
    data.vbuff.endpos = data.buf + HUGE_STRING_LEN;
 
539
    data.fptr = fptr;
 
540
    va_start(ap, format);
 
541
    count = apr_vformatter(file_printf_flush,
 
542
                           (apr_vformatter_buff_t *)&data, format, ap);
 
543
    /* apr_vformatter does not call flush for the last bits */
 
544
    if (count >= 0) file_printf_flush((apr_vformatter_buff_t *)&data);
 
545
 
 
546
    va_end(ap);
 
547
 
 
548
    free(data.buf);
 
549
    return count;
 
550
}