1
/* Copyright 2000-2005 The Apache Software Foundation or its licensors, as
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
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);
98
sa.bInheritHandle = TRUE;
99
sa.lpSecurityDescriptor = NULL;
101
(*in) = (apr_file_t *)apr_pcalloc(p, sizeof(apr_file_t));
106
(*in)->ungetchar = -1;
111
(*in)->direction = 0;
112
(*in)->pOverlapped = NULL;
113
(void) apr_pollset_create(&(*in)->pollset, 1, p, 0);
115
(*out) = (apr_file_t *)apr_pcalloc(p, sizeof(apr_file_t));
117
(*out)->fname = NULL;
119
(*out)->timeout = -1;
120
(*out)->ungetchar = -1;
124
(*out)->dataRead = 0;
125
(*out)->direction = 0;
126
(*out)->pOverlapped = NULL;
127
(void) apr_pollset_create(&(*out)->pollset, 1, p, 0);
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);
141
sprintf(name, "\\\\.\\pipe\\apr-pipe-%u.%lu", getpid(), id++);
143
(*in)->filehand = CreateNamedPipe(name,
148
65536, //nInBufferSize,
149
1, //nDefaultTimeOut,
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);
161
(*out)->filehand = CreateFile(name,
162
GENERIC_WRITE, // access mode
164
&sa, // Security attributes
165
OPEN_EXISTING, // dwCreationDisposition
166
dwOpenMode, // Pipe attributes
167
NULL); // handle to template file
170
/* Pipes on Win9* are blocking. Live with it. */
171
if (!CreatePipe(&(*in)->filehand, &(*out)->filehand, &sa, 65536)) {
172
return apr_get_os_error();
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);
181
#endif /* _WIN32_WCE */
185
APR_DECLARE(apr_status_t) apr_file_namedpipe_create(const char *filename,
186
apr_fileperms_t perm,
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.
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 :(
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,
208
(*file) = apr_pcalloc(pool, sizeof(apr_file_t));
209
(*file)->pool = pool;
211
(*file)->timeout = -1;
212
(*file)->ungetchar = -1;
213
(*file)->filehand = *thefile;
214
(void) apr_pollset_create(&(*file)->pollset, 1, pool, 0);
216
if (register_cleanup) {
217
apr_pool_cleanup_register(pool, *file, file_cleanup,
218
apr_pool_cleanup_null);
225
APR_DECLARE(apr_status_t) apr_os_pipe_put(apr_file_t **file,
226
apr_os_file_t *thefile,
229
return apr_os_pipe_put_ex(file, thefile, 0, pool);