bottleneck testcase based on rubbos
[bottlenecks.git] / rubbos / app / httpd-2.0.64 / srclib / apr / file_io / win32 / filedup.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 #include <string.h>
22 #include "apr_arch_inherit.h"
23 #include <io.h> /* for [_open/_get]_osfhandle */
24
25
26 APR_DECLARE(apr_status_t) apr_file_dup(apr_file_t **new_file,
27                                        apr_file_t *old_file, apr_pool_t *p)
28 {
29 #ifdef _WIN32_WCE
30     return APR_ENOTIMPL;
31 #else
32     HANDLE hproc = GetCurrentProcess();
33     HANDLE newhand = NULL;
34
35     if (!DuplicateHandle(hproc, old_file->filehand, 
36                          hproc, &newhand, 0, FALSE, 
37                          DUPLICATE_SAME_ACCESS)) {
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),
53                                 APR_THREAD_MUTEX_DEFAULT, p);
54     }
55 #endif
56
57     apr_pool_cleanup_register((*new_file)->pool, (void *)(*new_file), file_cleanup,
58                         apr_pool_cleanup_null);
59
60     return APR_SUCCESS;
61 #endif /* !defined(_WIN32_WCE) */
62 }
63
64 APR_DECLARE(apr_status_t) apr_file_dup2(apr_file_t *new_file,
65                                         apr_file_t *old_file, apr_pool_t *p)
66 {
67 #ifdef _WIN32_WCE
68     return APR_ENOTIMPL;
69 #else
70     DWORD stdhandle = 0;
71     HANDLE hproc = GetCurrentProcess();
72     HANDLE newhand = NULL;
73     apr_int32_t newflags;
74     int fd;
75
76     if (new_file->flags & APR_STD_FLAGS)
77     {
78         if ((new_file->flags & APR_STD_FLAGS) == APR_STDERR_FLAG)
79         {
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.
84              */
85             fflush(stderr);
86             setvbuf(stderr, NULL, _IONBF, 0);
87             _commit(2 /* stderr */);
88
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.
95              */
96             if (!DuplicateHandle(hproc, old_file->filehand, hproc, &newhand,
97                                  0, FALSE, DUPLICATE_SAME_ACCESS)) {
98                 return apr_get_os_error();
99             }
100             fd = _open_osfhandle((INT_PTR)newhand, _O_WRONLY | _O_BINARY);
101             _dup2(fd, 2);
102             _close(fd);
103             _setmode(2, _O_BINARY);
104
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
110              */
111             newhand = (HANDLE)_get_osfhandle(2);
112         }
113         else if ((new_file->flags & APR_STD_FLAGS) == APR_STDOUT_FLAG) {
114             /* For the process flow see the stderr case above */
115             fflush(stdout);
116             setvbuf(stdout, NULL, _IONBF, 0);
117             _commit(1 /* stdout */);
118
119             if (!DuplicateHandle(hproc, old_file->filehand, hproc, &newhand,
120                                  0, FALSE, DUPLICATE_SAME_ACCESS)) {
121                 return apr_get_os_error();
122             }
123             fd = _open_osfhandle((INT_PTR)newhand, _O_WRONLY | _O_BINARY);
124             _dup2(fd, 1);
125             _close(fd);
126             _setmode(1, _O_BINARY);
127             newhand = (HANDLE)_get_osfhandle(1);
128         }
129         else if ((new_file->flags & APR_STD_FLAGS) == APR_STDIN_FLAG) {
130             /* For the process flow see the stderr case above */
131             fflush(stdin);
132             setvbuf(stdin, NULL, _IONBF, 0);
133             _commit(0 /* stdin */);
134
135             if (!DuplicateHandle(hproc, old_file->filehand, hproc, &newhand,
136                                  0, FALSE, DUPLICATE_SAME_ACCESS)) {
137                 return apr_get_os_error();
138             }
139             fd = _open_osfhandle((INT_PTR)newhand, _O_RDONLY | _O_BINARY);
140             _dup2(fd, 0);
141             _close(fd);
142             _setmode(0, _O_BINARY);
143             newhand = (HANDLE)_get_osfhandle(0);
144         }
145         newflags = (new_file->flags & APR_STD_FLAGS) 
146                  | (old_file->flags & ~APR_STD_FLAGS) | APR_INHERIT;
147
148         /* No need  to close the old file, _dup2() above did that for us */
149     }
150     else {
151         if (!DuplicateHandle(hproc, old_file->filehand, 
152                              hproc, &newhand, 0,
153                              FALSE, DUPLICATE_SAME_ACCESS)) {
154             return apr_get_os_error();
155         }
156         newflags = old_file->flags & ~(APR_STD_FLAGS | APR_INHERIT);
157
158         if (new_file->filehand
159                 && (new_file->filehand != INVALID_HANDLE_VALUE)) {
160             CloseHandle(new_file->filehand);
161         }
162     }
163
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;
170
171 #if APR_HAS_THREADS
172     if (old_file->mutex) {
173         apr_thread_mutex_create(&(new_file->mutex),
174                                 APR_THREAD_MUTEX_DEFAULT, p);
175     }
176 #endif
177
178     return APR_SUCCESS;
179 #endif /* !defined(_WIN32_WCE) */
180 }
181
182 APR_DECLARE(apr_status_t) apr_file_setaside(apr_file_t **new_file,
183                                             apr_file_t *old_file,
184                                             apr_pool_t *p)
185 {
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);
193         }
194         else {
195             memcpy((*new_file)->buffer, old_file->buffer, old_file->dataRead);
196         }
197     }
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);
202     }
203     if (old_file->fname) {
204         (*new_file)->fname = apr_pstrdup(p, old_file->fname);
205     }
206     if (!(old_file->flags & APR_FILE_NOCLEANUP)) {
207         apr_pool_cleanup_register(p, (void *)(*new_file), 
208                                   file_cleanup,
209                                   file_cleanup);
210     }
211
212     old_file->filehand = INVALID_HANDLE_VALUE;
213     apr_pool_cleanup_kill(old_file->pool, (void *)old_file,
214                           file_cleanup);
215     return APR_SUCCESS;
216 }