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"
21 #include "apr_arch_inherit.h"
23 /* Figure out how to get pipe block/nonblock on BeOS...
24 * Basically, BONE7 changed things again so that ioctl didn't work,
25 * but now fcntl does, hence we need to do this extra checking.
26 * The joys of beta programs. :-)
30 # define BEOS_BLOCKING 1
32 # define BEOS_BLOCKING 0
36 static apr_status_t pipeblock(apr_file_t *thepipe)
38 #if !defined(BEOS) || !BEOS_BLOCKING
41 fd_flags = fcntl(thepipe->filedes, F_GETFL, 0);
42 # if defined(O_NONBLOCK)
43 fd_flags &= ~O_NONBLOCK;
44 # elif defined(O_NDELAY)
45 fd_flags &= ~O_NDELAY;
46 # elif defined(O_FNDELAY)
47 fd_flags &= ~O_FNDELAY;
49 /* XXXX: this breaks things, but an alternative isn't obvious...*/
52 if (fcntl(thepipe->filedes, F_SETFL, fd_flags) == -1) {
55 #else /* BEOS_BLOCKING */
57 # if BEOS_BONE /* This only works on BONE 0-6 */
59 if (ioctl(thepipe->filedes, FIONBIO, &on, sizeof(on)) < 0) {
62 # else /* "classic" BeOS doesn't support this at all */
66 #endif /* !BEOS_BLOCKING */
68 thepipe->blocking = BLK_ON;
72 static apr_status_t pipenonblock(apr_file_t *thepipe)
74 #if !defined(BEOS) || !BEOS_BLOCKING
75 int fd_flags = fcntl(thepipe->filedes, F_GETFL, 0);
77 # if defined(O_NONBLOCK)
78 fd_flags |= O_NONBLOCK;
79 # elif defined(O_NDELAY)
81 # elif defined(O_FNDELAY)
82 fd_flags |= O_FNDELAY;
84 /* XXXX: this breaks things, but an alternative isn't obvious...*/
87 if (fcntl(thepipe->filedes, F_SETFL, fd_flags) == -1) {
91 #else /* BEOS_BLOCKING */
93 # if BEOS_BONE /* This only works on BONE 0-6 */
95 if (ioctl(thepipe->filedes, FIONBIO, &on, sizeof(on)) < 0) {
98 # else /* "classic" BeOS doesn't support this at all */
102 #endif /* !BEOS_BLOCKING */
104 thepipe->blocking = BLK_OFF;
108 APR_DECLARE(apr_status_t) apr_file_pipe_timeout_set(apr_file_t *thepipe, apr_interval_time_t timeout)
110 if (thepipe->is_pipe == 1) {
111 thepipe->timeout = timeout;
113 if (thepipe->blocking != BLK_OFF) { /* blocking or unknown state */
114 return pipenonblock(thepipe);
118 if (thepipe->blocking != BLK_ON) { /* non-blocking or unknown state */
119 return pipeblock(thepipe);
127 APR_DECLARE(apr_status_t) apr_file_pipe_timeout_get(apr_file_t *thepipe, apr_interval_time_t *timeout)
129 if (thepipe->is_pipe == 1) {
130 *timeout = thepipe->timeout;
136 APR_DECLARE(apr_status_t) apr_os_pipe_put_ex(apr_file_t **file,
137 apr_os_file_t *thefile,
138 int register_cleanup,
141 int *dafile = thefile;
143 (*file) = apr_pcalloc(pool, sizeof(apr_file_t));
144 (*file)->pool = pool;
145 (*file)->eof_hit = 0;
146 (*file)->is_pipe = 1;
147 (*file)->blocking = BLK_UNKNOWN; /* app needs to make a timeout call */
148 (*file)->timeout = -1;
149 (*file)->ungetchar = -1; /* no char avail */
150 (*file)->filedes = *dafile;
151 if (!register_cleanup) {
152 (*file)->flags = APR_FOPEN_NOCLEANUP;
154 (*file)->buffered = 0;
156 (*file)->thlock = NULL;
158 if (register_cleanup) {
159 apr_pool_cleanup_register((*file)->pool, (void *)(*file),
160 apr_unix_file_cleanup,
161 apr_pool_cleanup_null);
163 #ifndef WAITIO_USES_POLL
164 /* Start out with no pollset. apr_wait_for_io_or_timeout() will
165 * initialize the pollset if needed.
167 (*file)->pollset = NULL;
172 APR_DECLARE(apr_status_t) apr_os_pipe_put(apr_file_t **file,
173 apr_os_file_t *thefile,
176 return apr_os_pipe_put_ex(file, thefile, 0, pool);
179 APR_DECLARE(apr_status_t) apr_file_pipe_create(apr_file_t **in, apr_file_t **out, apr_pool_t *pool)
183 if (pipe(filedes) == -1) {
187 (*in) = (apr_file_t *)apr_pcalloc(pool, sizeof(apr_file_t));
189 (*in)->filedes = filedes[0];
193 (*in)->blocking = BLK_ON;
195 (*in)->ungetchar = -1;
196 (*in)->flags = APR_INHERIT;
198 (*in)->thlock = NULL;
200 #ifndef WAITIO_USES_POLL
201 (*in)->pollset = NULL;
203 (*out) = (apr_file_t *)apr_pcalloc(pool, sizeof(apr_file_t));
205 (*out)->filedes = filedes[1];
207 (*out)->fname = NULL;
208 (*out)->buffered = 0;
209 (*out)->blocking = BLK_ON;
210 (*out)->flags = APR_INHERIT;
211 (*out)->timeout = -1;
213 (*out)->thlock = NULL;
215 #ifndef WAITIO_USES_POLL
216 (*out)->pollset = NULL;
218 apr_pool_cleanup_register((*in)->pool, (void *)(*in), apr_unix_file_cleanup,
219 apr_pool_cleanup_null);
220 apr_pool_cleanup_register((*out)->pool, (void *)(*out), apr_unix_file_cleanup,
221 apr_pool_cleanup_null);
225 APR_DECLARE(apr_status_t) apr_file_pipe_create_ex(apr_file_t **in,
227 apr_int32_t blocking,
232 if ((status = apr_file_pipe_create(in, out, pool)) != APR_SUCCESS)
239 apr_file_pipe_timeout_set(*out, 0);
241 case APR_WRITE_BLOCK:
242 apr_file_pipe_timeout_set(*in, 0);
245 apr_file_pipe_timeout_set(*out, 0);
246 apr_file_pipe_timeout_set(*in, 0);
252 APR_DECLARE(apr_status_t) apr_file_namedpipe_create(const char *filename,
253 apr_fileperms_t perm, apr_pool_t *pool)
255 mode_t mode = apr_unix_perms2mode(perm);
257 if (mkfifo(filename, mode) == -1) {