Coverage Report

Created: 2025-07-11 06:40

/src/httpd/srclib/apr/file_io/unix/pipe.c
Line
Count
Source (jump to first uncovered line)
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
21
#include "apr_arch_inherit.h"
22
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. :-)
27
 */
28
#if defined(BEOS)
29
#if !defined(BONE7)
30
# define BEOS_BLOCKING 1
31
#else
32
# define BEOS_BLOCKING 0
33
#endif
34
#endif
35
36
static apr_status_t pipeblock(apr_file_t *thepipe)
37
0
{
38
0
#if !defined(BEOS) || !BEOS_BLOCKING
39
0
      int fd_flags;
40
41
0
      fd_flags = fcntl(thepipe->filedes, F_GETFL, 0);
42
0
#  if defined(O_NONBLOCK)
43
0
      fd_flags &= ~O_NONBLOCK;
44
#  elif defined(O_NDELAY)
45
      fd_flags &= ~O_NDELAY;
46
#  elif defined(O_FNDELAY)
47
      fd_flags &= ~O_FNDELAY;
48
#  else
49
      /* XXXX: this breaks things, but an alternative isn't obvious...*/
50
      return APR_ENOTIMPL;
51
#  endif
52
0
      if (fcntl(thepipe->filedes, F_SETFL, fd_flags) == -1) {
53
0
          return errno;
54
0
      }
55
#else /* BEOS_BLOCKING */
56
57
#  if BEOS_BONE /* This only works on BONE 0-6 */
58
      int on = 0;
59
      if (ioctl(thepipe->filedes, FIONBIO, &on, sizeof(on)) < 0) {
60
          return errno;
61
      }
62
#  else /* "classic" BeOS doesn't support this at all */
63
      return APR_ENOTIMPL;
64
#  endif
65
66
#endif /* !BEOS_BLOCKING */
67
68
0
    thepipe->blocking = BLK_ON;
69
0
    return APR_SUCCESS;
70
0
}
71
72
static apr_status_t pipenonblock(apr_file_t *thepipe)
73
0
{
74
0
#if !defined(BEOS) || !BEOS_BLOCKING
75
0
      int fd_flags = fcntl(thepipe->filedes, F_GETFL, 0);
76
77
0
#  if defined(O_NONBLOCK)
78
0
      fd_flags |= O_NONBLOCK;
79
#  elif defined(O_NDELAY)
80
      fd_flags |= O_NDELAY;
81
#  elif defined(O_FNDELAY)
82
      fd_flags |= O_FNDELAY;
83
#  else
84
      /* XXXX: this breaks things, but an alternative isn't obvious...*/
85
      return APR_ENOTIMPL;
86
#  endif
87
0
      if (fcntl(thepipe->filedes, F_SETFL, fd_flags) == -1) {
88
0
          return errno;
89
0
      }
90
91
#else /* BEOS_BLOCKING */
92
93
#  if BEOS_BONE /* This only works on BONE 0-6 */
94
      int on = 1;
95
      if (ioctl(thepipe->filedes, FIONBIO, &on, sizeof(on)) < 0) {
96
          return errno;
97
      }
98
#  else /* "classic" BeOS doesn't support this at all */
99
      return APR_ENOTIMPL;
100
#  endif
101
102
#endif /* !BEOS_BLOCKING */
103
104
0
    thepipe->blocking = BLK_OFF;
105
0
    return APR_SUCCESS;
106
0
}
107
108
APR_DECLARE(apr_status_t) apr_file_pipe_timeout_set(apr_file_t *thepipe, apr_interval_time_t timeout)
109
0
{
110
0
    if (thepipe->is_pipe == 1) {
111
0
        thepipe->timeout = timeout;
112
0
        if (timeout >= 0) {
113
0
            if (thepipe->blocking != BLK_OFF) { /* blocking or unknown state */
114
0
                return pipenonblock(thepipe);
115
0
            }
116
0
        }
117
0
        else {
118
0
            if (thepipe->blocking != BLK_ON) { /* non-blocking or unknown state */
119
0
                return pipeblock(thepipe);
120
0
            }
121
0
        }
122
0
        return APR_SUCCESS;
123
0
    }
124
0
    return APR_EINVAL;
125
0
}
126
127
APR_DECLARE(apr_status_t) apr_file_pipe_timeout_get(apr_file_t *thepipe, apr_interval_time_t *timeout)
128
0
{
129
0
    if (thepipe->is_pipe == 1) {
130
0
        *timeout = thepipe->timeout;
131
0
        return APR_SUCCESS;
132
0
    }
133
0
    return APR_EINVAL;
134
0
}
135
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,
139
                                             apr_pool_t *pool)
140
0
{
141
0
    int *dafile = thefile;
142
143
0
    (*file) = apr_pcalloc(pool, sizeof(apr_file_t));
144
0
    (*file)->pool = pool;
145
0
    (*file)->eof_hit = 0;
146
0
    (*file)->is_pipe = 1;
147
0
    (*file)->blocking = BLK_UNKNOWN; /* app needs to make a timeout call */
148
0
    (*file)->timeout = -1;
149
0
    (*file)->ungetchar = -1; /* no char avail */
150
0
    (*file)->filedes = *dafile;
151
0
    if (!register_cleanup) {
152
0
        (*file)->flags = APR_FOPEN_NOCLEANUP;
153
0
    }
154
0
    (*file)->buffered = 0;
155
0
#if APR_HAS_THREADS
156
0
    (*file)->thlock = NULL;
157
0
#endif
158
0
    if (register_cleanup) {
159
0
        apr_pool_cleanup_register((*file)->pool, (void *)(*file),
160
0
                                  apr_unix_file_cleanup,
161
0
                                  apr_pool_cleanup_null);
162
0
    }
163
#ifndef WAITIO_USES_POLL
164
    /* Start out with no pollset.  apr_wait_for_io_or_timeout() will
165
     * initialize the pollset if needed.
166
     */
167
    (*file)->pollset = NULL;
168
#endif
169
0
    return APR_SUCCESS;
170
0
}
171
172
APR_DECLARE(apr_status_t) apr_os_pipe_put(apr_file_t **file,
173
                                          apr_os_file_t *thefile,
174
                                          apr_pool_t *pool)
175
0
{
176
0
    return apr_os_pipe_put_ex(file, thefile, 0, pool);
177
0
}
178
179
static apr_status_t file_pipe_create(apr_file_t **in,
180
                                     apr_file_t **out,
181
                                     apr_int32_t blocking,
182
                                     apr_pool_t *pool_in,
183
                                     apr_pool_t *pool_out)
184
0
{
185
0
    int filedes[2], fd_blocking;
186
0
    apr_interval_time_t fd_timeout;
187
188
0
#ifdef HAVE_PIPE2
189
0
    if (blocking == APR_FULL_NONBLOCK) {
190
0
        if (pipe2(filedes, O_NONBLOCK) == -1) {
191
0
            return errno;
192
0
        }
193
0
        fd_blocking = BLK_OFF;
194
0
        fd_timeout = 0;
195
0
    }
196
0
    else
197
0
#endif
198
0
    {
199
0
        if (pipe(filedes) == -1) {
200
0
            return errno;
201
0
        }
202
0
        fd_blocking = BLK_ON;
203
0
        fd_timeout = -1;
204
0
    }
205
206
0
    (*in) = (apr_file_t *)apr_pcalloc(pool_in, sizeof(apr_file_t));
207
0
    (*in)->pool = pool_in;
208
0
    (*in)->filedes = filedes[0];
209
0
    (*in)->is_pipe = 1;
210
0
    (*in)->fname = NULL;
211
0
    (*in)->buffered = 0;
212
0
    (*in)->blocking = fd_blocking;
213
0
    (*in)->timeout = fd_timeout;
214
0
    (*in)->ungetchar = -1;
215
0
    (*in)->flags = APR_INHERIT;
216
0
#if APR_HAS_THREADS
217
0
    (*in)->thlock = NULL;
218
0
#endif
219
#ifndef WAITIO_USES_POLL
220
    (*in)->pollset = NULL;
221
#endif
222
0
    (*out) = (apr_file_t *)apr_pcalloc(pool_out, sizeof(apr_file_t));
223
0
    (*out)->pool = pool_out;
224
0
    (*out)->filedes = filedes[1];
225
0
    (*out)->is_pipe = 1;
226
0
    (*out)->fname = NULL;
227
0
    (*out)->buffered = 0;
228
0
    (*out)->blocking = fd_blocking;
229
0
    (*out)->flags = APR_INHERIT;
230
0
    (*out)->timeout = fd_timeout;
231
0
#if APR_HAS_THREADS
232
0
    (*out)->thlock = NULL;
233
0
#endif
234
#ifndef WAITIO_USES_POLL
235
    (*out)->pollset = NULL;
236
#endif
237
0
    apr_pool_cleanup_register((*in)->pool, (void *)(*in), apr_unix_file_cleanup,
238
0
                         apr_pool_cleanup_null);
239
0
    apr_pool_cleanup_register((*out)->pool, (void *)(*out), apr_unix_file_cleanup,
240
0
                         apr_pool_cleanup_null);
241
242
0
    switch (blocking) {
243
0
    case APR_FULL_BLOCK:
244
0
        break;
245
0
    case APR_READ_BLOCK:
246
0
        apr_file_pipe_timeout_set(*out, 0);
247
0
        break;
248
0
    case APR_WRITE_BLOCK:
249
0
        apr_file_pipe_timeout_set(*in, 0);
250
0
        break;
251
0
    default:
252
        /* These are noops for the pipe2() case */
253
0
        apr_file_pipe_timeout_set(*out, 0);
254
0
        apr_file_pipe_timeout_set(*in, 0);
255
0
        break;
256
0
    }
257
0
    return APR_SUCCESS;
258
0
}
259
260
APR_DECLARE(apr_status_t) apr_file_pipe_create(apr_file_t **in,
261
                                               apr_file_t **out,
262
                                               apr_pool_t *pool)
263
0
{
264
    /* Default is full blocking pipes. */
265
0
    return file_pipe_create(in, out, APR_FULL_BLOCK, pool, pool);
266
0
}
267
268
APR_DECLARE(apr_status_t) apr_file_pipe_create_ex(apr_file_t **in,
269
                                                  apr_file_t **out,
270
                                                  apr_int32_t blocking,
271
                                                  apr_pool_t *pool)
272
0
{
273
0
    return file_pipe_create(in, out, blocking, pool, pool);
274
0
}
275
276
APR_DECLARE(apr_status_t) apr_file_pipe_create_pools(apr_file_t **in,
277
                                                     apr_file_t **out,
278
                                                     apr_int32_t blocking,
279
                                                     apr_pool_t *pool_in,
280
                                                     apr_pool_t *pool_out)
281
0
{
282
0
    return file_pipe_create(in, out, blocking, pool_in, pool_out);
283
0
}
284
285
APR_DECLARE(apr_status_t) apr_file_namedpipe_create(const char *filename,
286
                                                    apr_fileperms_t perm, apr_pool_t *pool)
287
0
{
288
0
    mode_t mode = apr_unix_perms2mode(perm);
289
290
0
    if (mkfifo(filename, mode) == -1) {
291
0
        return errno;
292
0
    }
293
0
    return APR_SUCCESS;
294
0
}
295
296
297