upload http
[bottlenecks.git] / rubbos / app / httpd-2.0.64 / srclib / apr / file_io / win32 / pipe.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 "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     
99 #if APR_HAS_UNICODE_FS
100     IF_WIN_OS_IS_UNICODE
101         sa.bInheritHandle = FALSE;
102 #endif
103 #if APR_HAS_ANSI_FS
104     ELSE_WIN_OS_IS_ANSI
105         sa.bInheritHandle = TRUE;
106 #endif
107     sa.lpSecurityDescriptor = NULL;
108
109     (*in) = (apr_file_t *)apr_pcalloc(p, sizeof(apr_file_t));
110     (*in)->pool = p;
111     (*in)->fname = NULL;
112     (*in)->pipe = 1;
113     (*in)->timeout = -1;
114     (*in)->ungetchar = -1;
115     (*in)->eof_hit = 0;
116     (*in)->filePtr = 0;
117     (*in)->bufpos = 0;
118     (*in)->dataRead = 0;
119     (*in)->direction = 0;
120     (*in)->pOverlapped = NULL;
121
122     (*out) = (apr_file_t *)apr_pcalloc(p, sizeof(apr_file_t));
123     (*out)->pool = p;
124     (*out)->fname = NULL;
125     (*out)->pipe = 1;
126     (*out)->timeout = -1;
127     (*out)->ungetchar = -1;
128     (*out)->eof_hit = 0;
129     (*out)->filePtr = 0;
130     (*out)->bufpos = 0;
131     (*out)->dataRead = 0;
132     (*out)->direction = 0;
133     (*out)->pOverlapped = NULL;
134
135     if (apr_os_level >= APR_WIN_NT) {
136         /* Create the read end of the pipe */
137         dwOpenMode = PIPE_ACCESS_INBOUND;
138         if (blocking_mode == APR_WRITE_BLOCK /* READ_NONBLOCK */
139                || blocking_mode == APR_FULL_NONBLOCK) {
140             dwOpenMode |= FILE_FLAG_OVERLAPPED;
141             (*in)->pOverlapped = (OVERLAPPED*) apr_pcalloc(p, sizeof(OVERLAPPED));
142             (*in)->pOverlapped->hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
143         }
144
145         dwPipeMode = 0;
146
147         sprintf(name, "\\\\.\\pipe\\apr-pipe-%u.%lu", getpid(), id++);
148
149         (*in)->filehand = CreateNamedPipe(name,
150                                           dwOpenMode,
151                                           dwPipeMode,
152                                           1,            //nMaxInstances,
153                                           0,            //nOutBufferSize, 
154                                           65536,        //nInBufferSize,                   
155                                           1,            //nDefaultTimeOut,                
156                                           &sa);
157
158         /* Create the write end of the pipe */
159         dwOpenMode = FILE_ATTRIBUTE_NORMAL;
160         if (blocking_mode == APR_READ_BLOCK /* WRITE_NONBLOCK */
161                 || blocking_mode == APR_FULL_NONBLOCK) {
162             dwOpenMode |= FILE_FLAG_OVERLAPPED;
163             (*out)->pOverlapped = (OVERLAPPED*) apr_pcalloc(p, sizeof(OVERLAPPED));
164             (*out)->pOverlapped->hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
165         }
166         
167         (*out)->filehand = CreateFile(name,
168                                       GENERIC_WRITE,   // access mode
169                                       0,               // share mode
170                                       &sa,             // Security attributes
171                                       OPEN_EXISTING,   // dwCreationDisposition
172                                       dwOpenMode,      // Pipe attributes
173                                       NULL);           // handle to template file
174     }
175     else {
176         /* Pipes on Win9* are blocking. Live with it. */
177         if (!CreatePipe(&(*in)->filehand, &(*out)->filehand, &sa, 65536)) {
178             return apr_get_os_error();
179         }
180     }
181
182     apr_pool_cleanup_register((*in)->pool, (void *)(*in), file_cleanup,
183                         apr_pool_cleanup_null);
184     apr_pool_cleanup_register((*out)->pool, (void *)(*out), file_cleanup,
185                         apr_pool_cleanup_null);
186     return APR_SUCCESS;
187 #endif /* _WIN32_WCE */
188 }
189
190
191 APR_DECLARE(apr_status_t) apr_file_namedpipe_create(const char *filename,
192                                                     apr_fileperms_t perm,
193                                                     apr_pool_t *pool)
194 {
195     /* Not yet implemented, interface not suitable.
196      * Win32 requires the named pipe to be *opened* at the time it's
197      * created, and to do so, blocking or non blocking must be elected.
198      */
199     return APR_ENOTIMPL;
200 }
201
202
203 /* XXX: Problem; we need to choose between blocking and nonblocking based
204  * on how *thefile was opened, and we don't have that information :-/
205  * Hack; assume a blocking socket, since the most common use for the fn
206  * would be to handle stdio-style or blocking pipes.  Win32 doesn't have
207  * select() blocking for pipes anyways :(
208  */
209 APR_DECLARE(apr_status_t) apr_os_pipe_put_ex(apr_file_t **file,
210                                              apr_os_file_t *thefile,
211                                              int register_cleanup,
212                                              apr_pool_t *pool)
213 {
214     (*file) = apr_pcalloc(pool, sizeof(apr_file_t));
215     (*file)->pool = pool;
216     (*file)->pipe = 1;
217     (*file)->timeout = -1;
218     (*file)->ungetchar = -1;
219     (*file)->filehand = *thefile;
220
221     if (register_cleanup) {
222         apr_pool_cleanup_register(pool, *file, file_cleanup,
223                                   apr_pool_cleanup_null);
224     }
225
226     return APR_SUCCESS;
227 }
228
229
230 APR_DECLARE(apr_status_t) apr_os_pipe_put(apr_file_t **file,
231                                           apr_os_file_t *thefile,
232                                           apr_pool_t *pool)
233 {
234     return apr_os_pipe_put_ex(file, thefile, 0, pool);
235 }