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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "win32/apr_arch_file_io.h"
18 #include "apr_file_io.h"
19 #include "apr_general.h"
20 #include "apr_strings.h"
26 #if APR_HAVE_SYS_TYPES_H
27 #include <sys/types.h>
29 #if APR_HAVE_SYS_STAT_H
32 #include "apr_arch_misc.h"
34 APR_DECLARE(apr_status_t) apr_file_pipe_timeout_set(apr_file_t *thepipe, apr_interval_time_t timeout)
36 /* Always OK to unset timeouts */
38 thepipe->timeout = timeout;
44 if (timeout && !(thepipe->pOverlapped)) {
45 /* Cannot be nonzero if a pipe was opened blocking
49 thepipe->timeout = timeout;
53 APR_DECLARE(apr_status_t) apr_file_pipe_timeout_get(apr_file_t *thepipe, apr_interval_time_t *timeout)
55 /* Always OK to get the timeout (even if it's unset ... -1) */
56 *timeout = thepipe->timeout;
60 APR_DECLARE(apr_status_t) apr_file_pipe_create(apr_file_t **in, apr_file_t **out, apr_pool_t *p)
62 /* Unix creates full blocking pipes. */
63 return apr_create_nt_pipe(in, out, APR_FULL_BLOCK, p);
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.
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.
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.
84 apr_status_t apr_create_nt_pipe(apr_file_t **in, apr_file_t **out,
85 apr_int32_t blocking_mode,
91 SECURITY_ATTRIBUTES sa;
92 static unsigned long id = 0;
97 sa.nLength = sizeof(sa);
99 #if APR_HAS_UNICODE_FS
101 sa.bInheritHandle = FALSE;
105 sa.bInheritHandle = TRUE;
107 sa.lpSecurityDescriptor = NULL;
109 (*in) = (apr_file_t *)apr_pcalloc(p, sizeof(apr_file_t));
114 (*in)->ungetchar = -1;
119 (*in)->direction = 0;
120 (*in)->pOverlapped = NULL;
122 (*out) = (apr_file_t *)apr_pcalloc(p, sizeof(apr_file_t));
124 (*out)->fname = NULL;
126 (*out)->timeout = -1;
127 (*out)->ungetchar = -1;
131 (*out)->dataRead = 0;
132 (*out)->direction = 0;
133 (*out)->pOverlapped = NULL;
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);
147 sprintf(name, "\\\\.\\pipe\\apr-pipe-%u.%lu", getpid(), id++);
149 (*in)->filehand = CreateNamedPipe(name,
154 65536, //nInBufferSize,
155 1, //nDefaultTimeOut,
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);
167 (*out)->filehand = CreateFile(name,
168 GENERIC_WRITE, // access mode
170 &sa, // Security attributes
171 OPEN_EXISTING, // dwCreationDisposition
172 dwOpenMode, // Pipe attributes
173 NULL); // handle to template file
176 /* Pipes on Win9* are blocking. Live with it. */
177 if (!CreatePipe(&(*in)->filehand, &(*out)->filehand, &sa, 65536)) {
178 return apr_get_os_error();
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);
187 #endif /* _WIN32_WCE */
191 APR_DECLARE(apr_status_t) apr_file_namedpipe_create(const char *filename,
192 apr_fileperms_t perm,
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.
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 :(
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,
214 (*file) = apr_pcalloc(pool, sizeof(apr_file_t));
215 (*file)->pool = pool;
217 (*file)->timeout = -1;
218 (*file)->ungetchar = -1;
219 (*file)->filehand = *thefile;
221 if (register_cleanup) {
222 apr_pool_cleanup_register(pool, *file, file_cleanup,
223 apr_pool_cleanup_null);
230 APR_DECLARE(apr_status_t) apr_os_pipe_put(apr_file_t **file,
231 apr_os_file_t *thefile,
234 return apr_os_pipe_put_ex(file, thefile, 0, pool);