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)
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(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)
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(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_FILE_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);
166 APR_DECLARE(apr_status_t) apr_os_pipe_put(apr_file_t **file,
167 apr_os_file_t *thefile,
170 return apr_os_pipe_put_ex(file, thefile, 0, pool);
173 APR_DECLARE(apr_status_t) apr_file_pipe_create(apr_file_t **in, apr_file_t **out, apr_pool_t *pool)
177 if (pipe(filedes) == -1) {
181 (*in) = (apr_file_t *)apr_pcalloc(pool, sizeof(apr_file_t));
183 (*in)->filedes = filedes[0];
187 (*in)->blocking = BLK_ON;
189 (*in)->ungetchar = -1;
190 (*in)->flags = APR_INHERIT;
192 (*in)->thlock = NULL;
195 (*out) = (apr_file_t *)apr_pcalloc(pool, sizeof(apr_file_t));
197 (*out)->filedes = filedes[1];
199 (*out)->fname = NULL;
200 (*out)->buffered = 0;
201 (*out)->blocking = BLK_ON;
202 (*out)->flags = APR_INHERIT;
203 (*out)->timeout = -1;
205 (*out)->thlock = NULL;
208 apr_pool_cleanup_register((*in)->pool, (void *)(*in), apr_unix_file_cleanup,
209 apr_pool_cleanup_null);
210 apr_pool_cleanup_register((*out)->pool, (void *)(*out), apr_unix_file_cleanup,
211 apr_pool_cleanup_null);
215 APR_DECLARE(apr_status_t) apr_file_namedpipe_create(const char *filename,
216 apr_fileperms_t perm, apr_pool_t *pool)
218 mode_t mode = apr_unix_perms2mode(perm);
220 if (mkfifo(filename, mode) == -1) {