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

« back to all changes in this revision

Viewing changes to srclib/apr/file_io/win32/pipe.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
#if APR_HAVE_ERRNO_H
 
22
#include <errno.h>
 
23
#endif
 
24
#include <string.h>
 
25
#include <stdio.h>
 
26
#if APR_HAVE_SYS_TYPES_H
 
27
#include <sys/types.h>
 
28
#endif
 
29
#if APR_HAVE_SYS_STAT_H
 
30
#include <sys/stat.h>
 
31
#endif
 
32
#include "apr_arch_misc.h"
 
33
 
 
34
APR_DECLARE(apr_status_t) apr_file_pipe_timeout_set(apr_file_t *thepipe, apr_interval_time_t timeout)
 
35
{
 
36
    /* Always OK to unset timeouts */
 
37
    if (timeout == -1) {
 
38
        thepipe->timeout = timeout;
 
39
        return APR_SUCCESS;
 
40
    }
 
41
    if (!thepipe->pipe) {
 
42
        return APR_ENOTIMPL;
 
43
    }
 
44
    if (timeout && !(thepipe->pOverlapped)) {
 
45
        /* Cannot be nonzero if a pipe was opened blocking
 
46
         */
 
47
        return APR_EINVAL;
 
48
    }
 
49
    thepipe->timeout = timeout;
 
50
    return APR_SUCCESS;
 
51
}
 
52
 
 
53
APR_DECLARE(apr_status_t) apr_file_pipe_timeout_get(apr_file_t *thepipe, apr_interval_time_t *timeout)
 
54
{
 
55
    /* Always OK to get the timeout (even if it's unset ... -1) */
 
56
    *timeout = thepipe->timeout;
 
57
    return APR_SUCCESS;
 
58
}
 
59
 
 
60
APR_DECLARE(apr_status_t) apr_file_pipe_create(apr_file_t **in, apr_file_t **out, apr_pool_t *p)
 
61
{
 
62
    /* Unix creates full blocking pipes. */
 
63
    return apr_create_nt_pipe(in, out, APR_FULL_BLOCK, p);
 
64
}
 
65
 
 
66
/* apr_create_nt_pipe()
 
67
 * An internal (for now) APR function used by apr_proc_create() 
 
68
 * when setting up pipes to communicate with the child process. 
 
69
 * apr_create_nt_pipe() allows setting the blocking mode of each end of 
 
70
 * the pipe when the pipe is created (rather than after the pipe is created). 
 
71
 * A pipe handle must be opened in full async i/o mode in order to 
 
72
 * emulate Unix non-blocking pipes with timeouts. 
 
73
 *
 
74
 * In general, we don't want to enable child side pipe handles for async i/o.
 
75
 * This prevents us from enabling both ends of the pipe for async i/o in 
 
76
 * apr_file_pipe_create.
 
77
 *
 
78
 * Why not use NamedPipes on NT which support setting pipe state to
 
79
 * non-blocking? On NT, even though you can set a pipe non-blocking, 
 
80
 * there is no clean way to set event driven non-zero timeouts (e.g select(),
 
81
 * WaitForSinglelObject, et. al. will not detect pipe i/o). On NT, you 
 
82
 * have to poll the pipe to detect i/o on a non-blocking pipe.
 
83
 */
 
84
apr_status_t apr_create_nt_pipe(apr_file_t **in, apr_file_t **out, 
 
85
                                apr_int32_t blocking_mode, 
 
86
                                apr_pool_t *p)
 
87
{
 
88
#ifdef _WIN32_WCE
 
89
    return APR_ENOTIMPL;
 
90
#else
 
91
    SECURITY_ATTRIBUTES sa;
 
92
    static unsigned long id = 0;
 
93
    DWORD dwPipeMode;
 
94
    DWORD dwOpenMode;
 
95
    char name[50];
 
96
 
 
97
    sa.nLength = sizeof(sa);
 
98
    sa.bInheritHandle = TRUE;
 
99
    sa.lpSecurityDescriptor = NULL;
 
100
 
 
101
    (*in) = (apr_file_t *)apr_pcalloc(p, sizeof(apr_file_t));
 
102
    (*in)->pool = p;
 
103
    (*in)->fname = NULL;
 
104
    (*in)->pipe = 1;
 
105
    (*in)->timeout = -1;
 
106
    (*in)->ungetchar = -1;
 
107
    (*in)->eof_hit = 0;
 
108
    (*in)->filePtr = 0;
 
109
    (*in)->bufpos = 0;
 
110
    (*in)->dataRead = 0;
 
111
    (*in)->direction = 0;
 
112
    (*in)->pOverlapped = NULL;
 
113
    (void) apr_pollset_create(&(*in)->pollset, 1, p, 0);
 
114
 
 
115
    (*out) = (apr_file_t *)apr_pcalloc(p, sizeof(apr_file_t));
 
116
    (*out)->pool = p;
 
117
    (*out)->fname = NULL;
 
118
    (*out)->pipe = 1;
 
119
    (*out)->timeout = -1;
 
120
    (*out)->ungetchar = -1;
 
121
    (*out)->eof_hit = 0;
 
122
    (*out)->filePtr = 0;
 
123
    (*out)->bufpos = 0;
 
124
    (*out)->dataRead = 0;
 
125
    (*out)->direction = 0;
 
126
    (*out)->pOverlapped = NULL;
 
127
    (void) apr_pollset_create(&(*out)->pollset, 1, p, 0);
 
128
 
 
129
    if (apr_os_level >= APR_WIN_NT) {
 
130
        /* Create the read end of the pipe */
 
131
        dwOpenMode = PIPE_ACCESS_INBOUND;
 
132
        if (blocking_mode == APR_WRITE_BLOCK /* READ_NONBLOCK */
 
133
               || blocking_mode == APR_FULL_NONBLOCK) {
 
134
            dwOpenMode |= FILE_FLAG_OVERLAPPED;
 
135
            (*in)->pOverlapped = (OVERLAPPED*) apr_pcalloc(p, sizeof(OVERLAPPED));
 
136
            (*in)->pOverlapped->hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
 
137
        }
 
138
 
 
139
        dwPipeMode = 0;
 
140
 
 
141
        sprintf(name, "\\\\.\\pipe\\apr-pipe-%u.%lu", getpid(), id++);
 
142
 
 
143
        (*in)->filehand = CreateNamedPipe(name,
 
144
                                          dwOpenMode,
 
145
                                          dwPipeMode,
 
146
                                          1,            //nMaxInstances,
 
147
                                          0,            //nOutBufferSize, 
 
148
                                          65536,        //nInBufferSize,                   
 
149
                                          1,            //nDefaultTimeOut,                
 
150
                                          &sa);
 
151
 
 
152
        /* Create the write end of the pipe */
 
153
        dwOpenMode = FILE_ATTRIBUTE_NORMAL;
 
154
        if (blocking_mode == APR_READ_BLOCK /* WRITE_NONBLOCK */
 
155
                || blocking_mode == APR_FULL_NONBLOCK) {
 
156
            dwOpenMode |= FILE_FLAG_OVERLAPPED;
 
157
            (*out)->pOverlapped = (OVERLAPPED*) apr_pcalloc(p, sizeof(OVERLAPPED));
 
158
            (*out)->pOverlapped->hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
 
159
        }
 
160
        
 
161
        (*out)->filehand = CreateFile(name,
 
162
                                      GENERIC_WRITE,   // access mode
 
163
                                      0,               // share mode
 
164
                                      &sa,             // Security attributes
 
165
                                      OPEN_EXISTING,   // dwCreationDisposition
 
166
                                      dwOpenMode,      // Pipe attributes
 
167
                                      NULL);           // handle to template file
 
168
    }
 
169
    else {
 
170
        /* Pipes on Win9* are blocking. Live with it. */
 
171
        if (!CreatePipe(&(*in)->filehand, &(*out)->filehand, &sa, 65536)) {
 
172
            return apr_get_os_error();
 
173
        }
 
174
    }
 
175
 
 
176
    apr_pool_cleanup_register((*in)->pool, (void *)(*in), file_cleanup,
 
177
                        apr_pool_cleanup_null);
 
178
    apr_pool_cleanup_register((*out)->pool, (void *)(*out), file_cleanup,
 
179
                        apr_pool_cleanup_null);
 
180
    return APR_SUCCESS;
 
181
#endif /* _WIN32_WCE */
 
182
}
 
183
 
 
184
 
 
185
APR_DECLARE(apr_status_t) apr_file_namedpipe_create(const char *filename,
 
186
                                                    apr_fileperms_t perm,
 
187
                                                    apr_pool_t *pool)
 
188
{
 
189
    /* Not yet implemented, interface not suitable.
 
190
     * Win32 requires the named pipe to be *opened* at the time it's
 
191
     * created, and to do so, blocking or non blocking must be elected.
 
192
     */
 
193
    return APR_ENOTIMPL;
 
194
}
 
195
 
 
196
 
 
197
/* XXX: Problem; we need to choose between blocking and nonblocking based
 
198
 * on how *thefile was opened, and we don't have that information :-/
 
199
 * Hack; assume a blocking socket, since the most common use for the fn
 
200
 * would be to handle stdio-style or blocking pipes.  Win32 doesn't have
 
201
 * select() blocking for pipes anyways :(
 
202
 */
 
203
APR_DECLARE(apr_status_t) apr_os_pipe_put_ex(apr_file_t **file,
 
204
                                             apr_os_file_t *thefile,
 
205
                                             int register_cleanup,
 
206
                                             apr_pool_t *pool)
 
207
{
 
208
    (*file) = apr_pcalloc(pool, sizeof(apr_file_t));
 
209
    (*file)->pool = pool;
 
210
    (*file)->pipe = 1;
 
211
    (*file)->timeout = -1;
 
212
    (*file)->ungetchar = -1;
 
213
    (*file)->filehand = *thefile;
 
214
    (void) apr_pollset_create(&(*file)->pollset, 1, pool, 0);
 
215
 
 
216
    if (register_cleanup) {
 
217
        apr_pool_cleanup_register(pool, *file, file_cleanup,
 
218
                                  apr_pool_cleanup_null);
 
219
    }
 
220
 
 
221
    return APR_SUCCESS;
 
222
}
 
223
 
 
224
 
 
225
APR_DECLARE(apr_status_t) apr_os_pipe_put(apr_file_t **file,
 
226
                                          apr_os_file_t *thefile,
 
227
                                          apr_pool_t *pool)
 
228
{
 
229
    return apr_os_pipe_put_ex(file, thefile, 0, pool);
 
230
}