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_strings.h"
19#include "apr_portable.h"
20#include "apr_thread_mutex.h"
21#include "apr_arch_inherit.h"
22
25 int which_dup)
26{
27 int rv;
28#ifdef HAVE_DUP3
29 int flags = 0;
30#endif
31
32 if (which_dup == 2) {
33 if ((*new_file) == NULL) {
34 /* We can't dup2 unless we have a valid new_file */
35 return APR_EINVAL;
36 }
37#ifdef HAVE_DUP3
38 if (!((*new_file)->flags & (APR_FOPEN_NOCLEANUP|APR_INHERIT)))
40 rv = dup3(old_file->filedes, (*new_file)->filedes, flags);
41#else
42 rv = dup2(old_file->filedes, (*new_file)->filedes);
43 if (!((*new_file)->flags & (APR_FOPEN_NOCLEANUP|APR_INHERIT))) {
44 int flags;
45
46 if (rv == -1)
47 return errno;
48
49 if ((flags = fcntl((*new_file)->filedes, F_GETFD)) == -1)
50 return errno;
51
53 if (fcntl((*new_file)->filedes, F_SETFD, flags) == -1)
54 return errno;
55
56 }
57#endif
58 } else {
59 rv = dup(old_file->filedes);
60 }
61
62 if (rv == -1)
63 return errno;
64
65 if (which_dup == 1) {
66 (*new_file) = (apr_file_t *)apr_pcalloc(p, sizeof(apr_file_t));
67 (*new_file)->pool = p;
68 (*new_file)->filedes = rv;
69 }
70
71 (*new_file)->fname = apr_pstrdup(p, old_file->fname);
72 (*new_file)->buffered = old_file->buffered;
73
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!
77 */
78#if APR_HAS_THREADS
79 if ((*new_file)->buffered && !(*new_file)->thlock && old_file->thlock) {
80 apr_thread_mutex_create(&((*new_file)->thlock),
82 }
83#endif
84 /* As above, only create the buffer if we haven't already
85 * got one.
86 */
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;
90 }
91
92 /* this is the way dup() works */
93 (*new_file)->blocking = old_file->blocking;
94
95 /* make sure unget behavior is consistent */
96 (*new_file)->ungetchar = old_file->ungetchar;
97
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.
103 */
104 if (which_dup == 2) {
105 return APR_SUCCESS;
106 }
107
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.
112 */
113 (*new_file)->flags = old_file->flags
115
116 apr_pool_cleanup_register((*new_file)->pool, (void *)(*new_file),
119#ifndef WAITIO_USES_POLL
120 /* Start out with no pollset. apr_wait_for_io_or_timeout() will
121 * initialize the pollset if needed.
122 */
123 (*new_file)->pollset = NULL;
124#endif
125 return APR_SUCCESS;
126}
127
133
136{
137 return file_dup(&new_file, old_file, p, 2);
138}
139
142 apr_pool_t *p)
143{
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);
151 }
152 else {
153 memcpy((*new_file)->buffer, old_file->buffer, old_file->dataRead);
154 }
155#if APR_HAS_THREADS
156 if (old_file->thlock) {
157 apr_thread_mutex_create(&((*new_file)->thlock),
160 }
161#endif /* APR_HAS_THREADS */
162 }
163 if (old_file->fname) {
164 (*new_file)->fname = apr_pstrdup(p, old_file->fname);
165 }
171 ((*new_file)->flags & APR_INHERIT)
174 }
175
176 old_file->filedes = -1;
177#ifndef WAITIO_USES_POLL
178 (*new_file)->pollset = NULL;
179#endif
180 return APR_SUCCESS;
181}
APR Portability Routines.
APR Strings library.
APR Thread Mutex Routines.
#define APR_EINVAL
Definition apr_errno.h:711
const char apr_ssize_t int flags
Definition apr_encode.h:168
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
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
static apr_status_t file_dup(apr_file_t **new_file, apr_file_t *old_file, apr_pool_t *p)
Definition filedup.c:24
apr_int32_t flags
apr_size_t bufpos
apr_off_t dataRead
apr_pool_t * pool
apr_size_t bufsize
enum apr_file_t::@57 blocking
apr_status_t apr_unix_child_file_cleanup(void *thefile)
Definition open.c:85
apr_status_t apr_unix_file_cleanup(void *thefile)
Definition open.c:71