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"
22 #include "apr_arch_inherit.h"
23 #include <io.h> /* for [_open/_get]_osfhandle */
26 APR_DECLARE(apr_status_t) apr_file_dup(apr_file_t **new_file,
27 apr_file_t *old_file, apr_pool_t *p)
32 HANDLE hproc = GetCurrentProcess();
33 HANDLE newhand = NULL;
35 if (!DuplicateHandle(hproc, old_file->filehand,
36 hproc, &newhand, 0, FALSE,
37 DUPLICATE_SAME_ACCESS)) {
38 return apr_get_os_error();
41 (*new_file) = (apr_file_t *) apr_pcalloc(p, sizeof(apr_file_t));
42 (*new_file)->filehand = newhand;
43 (*new_file)->flags = old_file->flags & ~(APR_STD_FLAGS | APR_INHERIT);
44 (*new_file)->pool = p;
45 (*new_file)->fname = apr_pstrdup(p, old_file->fname);
46 (*new_file)->append = old_file->append;
47 (*new_file)->buffered = FALSE;
48 (*new_file)->ungetchar = old_file->ungetchar;
51 if (old_file->mutex) {
52 apr_thread_mutex_create(&((*new_file)->mutex),
53 APR_THREAD_MUTEX_DEFAULT, p);
57 apr_pool_cleanup_register((*new_file)->pool, (void *)(*new_file), file_cleanup,
58 apr_pool_cleanup_null);
61 #endif /* !defined(_WIN32_WCE) */
64 APR_DECLARE(apr_status_t) apr_file_dup2(apr_file_t *new_file,
65 apr_file_t *old_file, apr_pool_t *p)
71 HANDLE hproc = GetCurrentProcess();
72 HANDLE newhand = NULL;
76 if (new_file->flags & APR_STD_FLAGS)
78 if ((new_file->flags & APR_STD_FLAGS) == APR_STDERR_FLAG)
80 /* Flush stderr and unset its buffer, then commit the fd-based buffer.
81 * This is typically a noop for Win2K/XP since services with NULL std
82 * handles [but valid FILE *'s, oddly enough], but is required
83 * for NT 4.0 and to use this code outside of services.
86 setvbuf(stderr, NULL, _IONBF, 0);
87 _commit(2 /* stderr */);
89 /* Clone a handle can _close() without harming the source handle,
90 * open an MSVCRT-based pseudo-fd for the file handle, then dup2
91 * and close our temporary pseudo-fd once it's been duplicated.
92 * This will incidently keep the FILE-based stderr in sync.
93 * Note the apparently redundant _O_BINARY coersions are required.
94 * Note the _dup2 will close the previous std Win32 handle.
96 if (!DuplicateHandle(hproc, old_file->filehand, hproc, &newhand,
97 0, FALSE, DUPLICATE_SAME_ACCESS)) {
98 return apr_get_os_error();
100 fd = _open_osfhandle((INT_PTR)newhand, _O_WRONLY | _O_BINARY);
103 _setmode(2, _O_BINARY);
105 /* hPipeWrite was _close()'ed above, and _dup2()'ed
106 * to fd 2 creating a new, inherited Win32 handle.
107 * Recover that real handle from fd 2. Note that
108 * SetStdHandle(STD_ERROR_HANDLE, _get_osfhandle(2))
109 * is implicit in the dup2() call above
111 newhand = (HANDLE)_get_osfhandle(2);
113 else if ((new_file->flags & APR_STD_FLAGS) == APR_STDOUT_FLAG) {
114 /* For the process flow see the stderr case above */
116 setvbuf(stdout, NULL, _IONBF, 0);
117 _commit(1 /* stdout */);
119 if (!DuplicateHandle(hproc, old_file->filehand, hproc, &newhand,
120 0, FALSE, DUPLICATE_SAME_ACCESS)) {
121 return apr_get_os_error();
123 fd = _open_osfhandle((INT_PTR)newhand, _O_WRONLY | _O_BINARY);
126 _setmode(1, _O_BINARY);
127 newhand = (HANDLE)_get_osfhandle(1);
129 else if ((new_file->flags & APR_STD_FLAGS) == APR_STDIN_FLAG) {
130 /* For the process flow see the stderr case above */
132 setvbuf(stdin, NULL, _IONBF, 0);
133 _commit(0 /* stdin */);
135 if (!DuplicateHandle(hproc, old_file->filehand, hproc, &newhand,
136 0, FALSE, DUPLICATE_SAME_ACCESS)) {
137 return apr_get_os_error();
139 fd = _open_osfhandle((INT_PTR)newhand, _O_RDONLY | _O_BINARY);
142 _setmode(0, _O_BINARY);
143 newhand = (HANDLE)_get_osfhandle(0);
145 newflags = (new_file->flags & APR_STD_FLAGS)
146 | (old_file->flags & ~APR_STD_FLAGS) | APR_INHERIT;
148 /* No need to close the old file, _dup2() above did that for us */
151 if (!DuplicateHandle(hproc, old_file->filehand,
153 FALSE, DUPLICATE_SAME_ACCESS)) {
154 return apr_get_os_error();
156 newflags = old_file->flags & ~(APR_STD_FLAGS | APR_INHERIT);
158 if (new_file->filehand
159 && (new_file->filehand != INVALID_HANDLE_VALUE)) {
160 CloseHandle(new_file->filehand);
164 new_file->flags = newflags;
165 new_file->filehand = newhand;
166 new_file->fname = apr_pstrdup(new_file->pool, old_file->fname);
167 new_file->append = old_file->append;
168 new_file->buffered = FALSE;
169 new_file->ungetchar = old_file->ungetchar;
172 if (old_file->mutex) {
173 apr_thread_mutex_create(&(new_file->mutex),
174 APR_THREAD_MUTEX_DEFAULT, p);
179 #endif /* !defined(_WIN32_WCE) */
182 APR_DECLARE(apr_status_t) apr_file_setaside(apr_file_t **new_file,
183 apr_file_t *old_file,
186 *new_file = (apr_file_t *)apr_palloc(p, sizeof(apr_file_t));
187 memcpy(*new_file, old_file, sizeof(apr_file_t));
188 (*new_file)->pool = p;
189 if (old_file->buffered) {
190 (*new_file)->buffer = apr_palloc(p, APR_FILE_BUFSIZE);
191 if (old_file->direction == 1) {
192 memcpy((*new_file)->buffer, old_file->buffer, old_file->bufpos);
195 memcpy((*new_file)->buffer, old_file->buffer, old_file->dataRead);
198 if (old_file->mutex) {
199 apr_thread_mutex_create(&((*new_file)->mutex),
200 APR_THREAD_MUTEX_DEFAULT, p);
201 apr_thread_mutex_destroy(old_file->mutex);
203 if (old_file->fname) {
204 (*new_file)->fname = apr_pstrdup(p, old_file->fname);
206 if (!(old_file->flags & APR_FILE_NOCLEANUP)) {
207 apr_pool_cleanup_register(p, (void *)(*new_file),
212 old_file->filehand = INVALID_HANDLE_VALUE;
213 apr_pool_cleanup_kill(old_file->pool, (void *)old_file,