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 "apr_arch_file_io.h"
18 #include "apr_strings.h"
19 #include "apr_portable.h"
20 #include "apr_thread_mutex.h"
21 #include "apr_arch_inherit.h"
23 static apr_status_t file_dup(apr_file_t **new_file,
24 apr_file_t *old_file, apr_pool_t *p,
33 if ((*new_file) == NULL) {
34 /* We can't dup2 unless we have a valid new_file */
38 if (!((*new_file)->flags & (APR_FOPEN_NOCLEANUP|APR_INHERIT)))
40 rv = dup3(old_file->filedes, (*new_file)->filedes, flags);
42 rv = dup2(old_file->filedes, (*new_file)->filedes);
43 if (!((*new_file)->flags & (APR_FOPEN_NOCLEANUP|APR_INHERIT))) {
49 if ((flags = fcntl((*new_file)->filedes, F_GETFD)) == -1)
53 if (fcntl((*new_file)->filedes, F_SETFD, flags) == -1)
59 rv = dup(old_file->filedes);
66 (*new_file) = (apr_file_t *)apr_pcalloc(p, sizeof(apr_file_t));
67 (*new_file)->pool = p;
68 (*new_file)->filedes = rv;
71 (*new_file)->fname = apr_pstrdup(p, old_file->fname);
72 (*new_file)->buffered = old_file->buffered;
74 /* If the existing socket in a dup2 is already buffered, we
75 * have an existing and valid (hopefully) mutex, so we don't
76 * want to create it again as we could leak!
79 if ((*new_file)->buffered && !(*new_file)->thlock && old_file->thlock) {
80 apr_thread_mutex_create(&((*new_file)->thlock),
81 APR_THREAD_MUTEX_DEFAULT, p);
84 /* As above, only create the buffer if we haven't already
87 if ((*new_file)->buffered && !(*new_file)->buffer) {
88 (*new_file)->buffer = apr_palloc(p, old_file->bufsize);
89 (*new_file)->bufsize = old_file->bufsize;
92 /* this is the way dup() works */
93 (*new_file)->blocking = old_file->blocking;
95 /* make sure unget behavior is consistent */
96 (*new_file)->ungetchar = old_file->ungetchar;
98 /* apr_file_dup2() retains the original cleanup, reflecting
99 * the existing inherit and nocleanup flags. This means,
100 * that apr_file_dup2() cannot be called against an apr_file_t
101 * already closed with apr_file_close, because the expected
102 * cleanup was already killed.
104 if (which_dup == 2) {
108 /* apr_file_dup() retains all old_file flags with the exceptions
109 * of APR_INHERIT and APR_FOPEN_NOCLEANUP.
110 * The user must call apr_file_inherit_set() on the dupped
111 * apr_file_t when desired.
113 (*new_file)->flags = old_file->flags
114 & ~(APR_INHERIT | APR_FOPEN_NOCLEANUP);
116 apr_pool_cleanup_register((*new_file)->pool, (void *)(*new_file),
117 apr_unix_file_cleanup,
118 apr_unix_child_file_cleanup);
119 #ifndef WAITIO_USES_POLL
120 /* Start out with no pollset. apr_wait_for_io_or_timeout() will
121 * initialize the pollset if needed.
123 (*new_file)->pollset = NULL;
128 APR_DECLARE(apr_status_t) apr_file_dup(apr_file_t **new_file,
129 apr_file_t *old_file, apr_pool_t *p)
131 return file_dup(new_file, old_file, p, 1);
134 APR_DECLARE(apr_status_t) apr_file_dup2(apr_file_t *new_file,
135 apr_file_t *old_file, apr_pool_t *p)
137 return file_dup(&new_file, old_file, p, 2);
140 APR_DECLARE(apr_status_t) apr_file_setaside(apr_file_t **new_file,
141 apr_file_t *old_file,
144 *new_file = (apr_file_t *)apr_pmemdup(p, old_file, sizeof(apr_file_t));
145 (*new_file)->pool = p;
146 if (old_file->buffered) {
147 (*new_file)->buffer = apr_palloc(p, old_file->bufsize);
148 (*new_file)->bufsize = old_file->bufsize;
149 if (old_file->direction == 1) {
150 memcpy((*new_file)->buffer, old_file->buffer, old_file->bufpos);
153 memcpy((*new_file)->buffer, old_file->buffer, old_file->dataRead);
156 if (old_file->thlock) {
157 apr_thread_mutex_create(&((*new_file)->thlock),
158 APR_THREAD_MUTEX_DEFAULT, p);
159 apr_thread_mutex_destroy(old_file->thlock);
161 #endif /* APR_HAS_THREADS */
163 if (old_file->fname) {
164 (*new_file)->fname = apr_pstrdup(p, old_file->fname);
166 if (!(old_file->flags & APR_FOPEN_NOCLEANUP)) {
167 apr_pool_cleanup_register(p, (void *)(*new_file),
168 apr_unix_file_cleanup,
169 ((*new_file)->flags & APR_INHERIT)
170 ? apr_pool_cleanup_null
171 : apr_unix_child_file_cleanup);
174 old_file->filedes = -1;
175 apr_pool_cleanup_kill(old_file->pool, (void *)old_file,
176 apr_unix_file_cleanup);
177 #ifndef WAITIO_USES_POLL
178 (*new_file)->pollset = NULL;