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.
19 #include <nks/errno.h>
21 #include "apr_arch_file_io.h"
22 #include "apr_strings.h"
23 #include "apr_portable.h"
24 #include "apr_arch_inherit.h"
26 static apr_status_t pipeblock(apr_file_t *thepipe)
32 if (fcntl(thepipe->filedes, F_GETFL, &flags) != -1)
35 fcntl(thepipe->filedes, F_SETFL, flags);
39 fcntl(thepipe->filedes, F_SETFL, 0);
45 thepipe->blocking = BLK_ON;
49 static apr_status_t pipenonblock(apr_file_t *thepipe)
56 if (fcntl(thepipe->filedes, F_GETFL, &flags) != -1)
59 fcntl(thepipe->filedes, F_SETFL, flags);
63 fcntl(thepipe->filedes, F_SETFL, FNDELAY);
69 thepipe->blocking = BLK_OFF;
73 APR_DECLARE(apr_status_t) apr_file_pipe_timeout_set(apr_file_t *thepipe, apr_interval_time_t timeout)
75 if (thepipe->is_pipe == 1) {
76 thepipe->timeout = timeout;
78 if (thepipe->blocking != BLK_OFF) { /* blocking or unknown state */
79 return pipenonblock(thepipe);
83 if (thepipe->blocking != BLK_ON) { /* non-blocking or unknown state */
84 return pipeblock(thepipe);
92 APR_DECLARE(apr_status_t) apr_file_pipe_timeout_get(apr_file_t *thepipe, apr_interval_time_t *timeout)
94 if (thepipe->is_pipe == 1) {
95 *timeout = thepipe->timeout;
101 APR_DECLARE(apr_status_t) apr_os_pipe_put_ex(apr_file_t **file,
102 apr_os_file_t *thefile,
103 int register_cleanup,
106 int *dafile = thefile;
108 (*file) = apr_pcalloc(pool, sizeof(apr_file_t));
109 (*file)->pool = pool;
110 (*file)->eof_hit = 0;
111 (*file)->is_pipe = 1;
112 (*file)->blocking = BLK_UNKNOWN; /* app needs to make a timeout call */
113 (*file)->timeout = -1;
114 (*file)->ungetchar = -1; /* no char avail */
115 (*file)->filedes = *dafile;
116 if (!register_cleanup) {
117 (*file)->flags = APR_FILE_NOCLEANUP;
119 (*file)->buffered = 0;
121 (*file)->thlock = NULL;
123 if (register_cleanup) {
124 apr_pool_cleanup_register((*file)->pool, (void *)(*file),
125 apr_unix_file_cleanup,
126 apr_pool_cleanup_null);
131 APR_DECLARE(apr_status_t) apr_os_pipe_put(apr_file_t **file,
132 apr_os_file_t *thefile,
135 return apr_os_pipe_put_ex(file, thefile, 0, pool);
138 APR_DECLARE(apr_status_t) apr_file_pipe_create(apr_file_t **in, apr_file_t **out, apr_pool_t *pool)
143 if (pipe(filedes) == -1) {
147 (*in) = (apr_file_t *)apr_pcalloc(pool, sizeof(apr_file_t));
148 (*out) = (apr_file_t *)apr_pcalloc(pool, sizeof(apr_file_t));
152 (*in)->filedes = filedes[0];
153 (*out)->filedes = filedes[1];
154 (*in)->flags = APR_INHERIT;
155 (*out)->flags = APR_INHERIT;
161 (*out)->buffered = 0;
163 (*out)->blocking = BLK_ON;
165 (*out)->timeout = -1;
166 (*in)->ungetchar = -1;
168 (*out)->thlock = NULL;
170 apr_pool_cleanup_register((*in)->pool, (void *)(*in), apr_unix_file_cleanup,
171 apr_pool_cleanup_null);
172 apr_pool_cleanup_register((*out)->pool, (void *)(*out), apr_unix_file_cleanup,
173 apr_pool_cleanup_null);
178 APR_DECLARE(apr_status_t) apr_file_namedpipe_create(const char *filename,
179 apr_fileperms_t perm, apr_pool_t *pool)