Apache HTTPD
filedup.c
Go to the documentation of this file.
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 "apr_arch_file_io.h"
18#include "apr_file_io.h"
19#include "apr_general.h"
20#include "apr_strings.h"
21#include <string.h>
22#include "apr_arch_inherit.h"
23#include <io.h> /* for [_open/_get]_osfhandle */
24
25
28{
29#ifdef _WIN32_WCE
30 return APR_ENOTIMPL;
31#else
34
36 hproc, &newhand, 0, FALSE,
38 return apr_get_os_error();
39 }
40
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;
49
50#if APR_HAS_THREADS
51 if (old_file->mutex) {
52 apr_thread_mutex_create(&((*new_file)->mutex),
54 }
55#endif
56
57 apr_pool_cleanup_register((*new_file)->pool, (void *)(*new_file), file_cleanup,
59
60#if APR_FILES_AS_SOCKETS
61 /* Create a pollset with room for one descriptor. */
62 /* ### check return codes */
63 (void) apr_pollset_create(&(*new_file)->pollset, 1, p, 0);
64#endif
65 return APR_SUCCESS;
66#endif /* !defined(_WIN32_WCE) */
67}
68
71{
72#ifdef _WIN32_WCE
73 return APR_ENOTIMPL;
74#else
78 int fd;
79
80 if (new_file->flags & APR_STD_FLAGS)
81 {
82 if ((new_file->flags & APR_STD_FLAGS) == APR_STDERR_FLAG)
83 {
84 /* Flush stderr and unset its buffer, then commit the fd-based buffer.
85 * This is typically a noop for Win2K/XP since services with NULL std
86 * handles [but valid FILE *'s, oddly enough], but is required
87 * for NT 4.0 and to use this code outside of services.
88 */
91 if (!_isatty(2)) {
92 _commit(2 /* stderr */);
93 }
94
95 /* Clone a handle can _close() without harming the source handle,
96 * open an MSVCRT-based pseudo-fd for the file handle, then dup2
97 * and close our temporary pseudo-fd once it's been duplicated.
98 * This will incidently keep the FILE-based stderr in sync.
99 * Note the apparently redundant _O_BINARY coersions are required.
100 * Note the _dup2 will close the previous std Win32 handle.
101 */
104 return apr_get_os_error();
105 }
107 _dup2(fd, 2);
108 _close(fd);
110
111 /* hPipeWrite was _close()'ed above, and _dup2()'ed
112 * to fd 2 creating a new, inherited Win32 handle.
113 * Recover that real handle from fd 2. Note that
114 * SetStdHandle(STD_ERROR_HANDLE, _get_osfhandle(2))
115 * is implicit in the dup2() call above
116 */
118 }
119 else if ((new_file->flags & APR_STD_FLAGS) == APR_STDOUT_FLAG) {
120 /* For the process flow see the stderr case above */
121 fflush(stdout);
123 if (!_isatty(1)) {
124 _commit(1 /* stdout */);
125 }
126
129 return apr_get_os_error();
130 }
132 _dup2(fd, 1);
133 _close(fd);
136 }
137 else if ((new_file->flags & APR_STD_FLAGS) == APR_STDIN_FLAG) {
138 /* For the process flow see the stderr case above */
139 fflush(stdin);
140 setvbuf(stdin, NULL, _IONBF, 0);
141
144 return apr_get_os_error();
145 }
147 _dup2(fd, 0);
148 _close(fd);
151 }
152 newflags = (new_file->flags & APR_STD_FLAGS)
154
155 /* No need to close the old file, _dup2() above did that for us */
156 }
157 else {
159 hproc, &newhand, 0,
161 return apr_get_os_error();
162 }
164
165 if (new_file->filehand
166 && (new_file->filehand != INVALID_HANDLE_VALUE)) {
167 CloseHandle(new_file->filehand);
168 }
169 }
170
171 new_file->flags = newflags;
172 new_file->filehand = newhand;
173 new_file->fname = apr_pstrdup(new_file->pool, old_file->fname);
174 new_file->append = old_file->append;
175 new_file->buffered = FALSE;
176 new_file->ungetchar = old_file->ungetchar;
177
178#if APR_HAS_THREADS
179 if (old_file->mutex) {
182 }
183#endif
184
185 return APR_SUCCESS;
186#endif /* !defined(_WIN32_WCE) */
187}
188
191 apr_pool_t *p)
192{
194 (*new_file)->pool = p;
195 if (old_file->buffered) {
196 (*new_file)->buffer = apr_palloc(p, old_file->bufsize);
197 (*new_file)->bufsize = old_file->bufsize;
198 if (old_file->direction == 1) {
199 memcpy((*new_file)->buffer, old_file->buffer, old_file->bufpos);
200 }
201 else {
202 memcpy((*new_file)->buffer, old_file->buffer, old_file->dataRead);
203 }
204 }
205 if (old_file->mutex) {
206 apr_thread_mutex_create(&((*new_file)->mutex),
209 }
210 if (old_file->fname) {
211 (*new_file)->fname = apr_pstrdup(p, old_file->fname);
212 }
219 }
220
222#if APR_FILES_AS_SOCKETS
223 /* Create a pollset with room for one descriptor. */
224 /* ### check return codes */
225 (void) apr_pollset_create(&(*new_file)->pollset, 1, p, 0);
226#endif
227 return APR_SUCCESS;
228}
#define FALSE
Definition abts.h:35
#define GetCurrentProcess()
#define DuplicateHandle(h1, h2, h3, ph4, d1, b, d2)
#define CloseHandle(h)
APR File I/O Handling.
APR Miscellaneous library routines.
APR Strings library.
#define APR_ENOTIMPL
Definition apr_errno.h:476
apr_file_t * fd
const void apr_status_t(*) apr_status_t(* APR_DECLARE)(void) apr_pool_pre_cleanup_register(apr_pool_t *p
Definition apr_pools.h:646
apr_size_t size
#define APR_SUCCESS
Definition apr_errno.h:225
#define apr_get_os_error()
Definition apr_errno.h:1217
int apr_status_t
Definition apr_errno.h:44
apr_file_t * old_file
#define APR_FOPEN_NOCLEANUP
Definition apr_file_io.h:74
#define apr_pcalloc(p, size)
Definition apr_pools.h:465
apr_pool_t * p
Definition md_event.c:32
return NULL
Definition mod_so.c:359
#define APR_INHERIT
apr_int32_t flags
apr_size_t bufpos
apr_thread_mutex_t * mutex
apr_off_t dataRead
apr_pool_t * pool
apr_size_t bufsize
static apr_status_t file_cleanup(apr_file_t *file, int is_child)
Definition open.c:29
#define APR_STDIN_FLAG
#define APR_STD_FLAGS
#define APR_STDOUT_FLAG
#define APR_STDERR_FLAG
typedef HANDLE(WINAPI *apr_winapi_fpt_CreateToolhelp32Snapshot)(DWORD dwFlags