Apache HTTPD
proc.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_threadproc.h"
18#include "apr_strings.h"
19
20/* Heavy on no'ops, here's what we want to pass if there is APR_NO_FILE
21 * requested for a specific child handle;
22 */
23static apr_file_t no_file = { NULL, -1, };
24
25struct send_pipe {
26 int in;
27 int out;
28 int err;
29};
30
32{
33 (*new) = (apr_procattr_t *)apr_palloc(pool,
34 sizeof(apr_procattr_t));
35
36 if ((*new) == NULL) {
37 return APR_ENOMEM;
38 }
39 (*new)->pool = pool;
40 (*new)->parent_in = NULL;
41 (*new)->child_in = NULL;
42 (*new)->parent_out = NULL;
43 (*new)->child_out = NULL;
44 (*new)->parent_err = NULL;
45 (*new)->child_err = NULL;
46 (*new)->currdir = NULL;
47 (*new)->cmdtype = APR_PROGRAM;
48 (*new)->detached = 0;
49 return APR_SUCCESS;
50}
51
56{
57 apr_status_t rv;
58
59 if ((in != APR_NO_PIPE) && (in != APR_NO_FILE)) {
60 /* APR_CHILD_BLOCK maps to APR_WRITE_BLOCK, while
61 * APR_PARENT_BLOCK maps to APR_READ_BLOCK, so transpose
62 * the CHILD/PARENT blocking flags for the stdin pipe.
63 * stdout/stderr map to the correct mode by default.
64 */
65 if (in == APR_CHILD_BLOCK)
67 else if (in == APR_PARENT_BLOCK)
69
71 in, attr->pool)) == APR_SUCCESS)
73 if (rv != APR_SUCCESS)
74 return rv;
75 }
76 else if (in == APR_NO_FILE)
78
79 if ((out != APR_NO_PIPE) && (out != APR_NO_FILE)) {
81 out, attr->pool)) == APR_SUCCESS)
83 if (rv != APR_SUCCESS)
84 return rv;
85 }
86 else if (out == APR_NO_FILE)
88
89 if ((err != APR_NO_PIPE) && (err != APR_NO_FILE)) {
91 err, attr->pool)) == APR_SUCCESS)
93 if (rv != APR_SUCCESS)
94 return rv;
95 }
96 else if (err == APR_NO_FILE)
98
99 return APR_SUCCESS;
100}
101
103 const char *dir)
104{
105 char * cwd;
106 if (dir[0] != '/') {
107 cwd = (char*)malloc(sizeof(char) * PATH_MAX);
109 attr->currdir = (char *)apr_pstrcat(attr->pool, cwd, "/", dir, NULL);
110 free(cwd);
111 } else {
112 attr->currdir = (char *)apr_pstrdup(attr->pool, dir);
113 }
114 if (attr->currdir) {
115 return APR_SUCCESS;
116 }
117 return APR_ENOMEM;
118}
119
122{
123 attr->cmdtype = cmd;
124 return APR_SUCCESS;
125}
126
128{
130 return APR_SUCCESS;
131}
132
134{
135 int pid;
136
137 if ((pid = fork()) < 0) {
138 return errno;
139 }
140 else if (pid == 0) {
141 /* This is really ugly...
142 * The semantics of BeOS's fork() are that areas (used for shared
143 * memory) get COW'd :-( The only way we can make shared memory
144 * work across fork() is therefore to find any areas that have
145 * been created and then clone them into our address space.
146 * Thankfully only COW'd areas have the lock variable set at
147 * anything but 0, so we can use that to find the areas we need to
148 * copy. Of course what makes it even worse is that the loop through
149 * the area's will go into an infinite loop, eating memory and then
150 * eventually segfault unless we know when we reach then end of the
151 * "original" areas and stop. Why? Well, we delete the area and then
152 * add another to the end of the list...
153 */
155 int32 cookie = 0;
156 area_id highest = 0;
157
158 while (get_next_area_info(0, &cookie, &ai) == B_OK)
159 if (ai.area > highest)
160 highest = ai.area;
161 cookie = 0;
162 while (get_next_area_info(0, &cookie, &ai) == B_OK) {
163 if (ai.area > highest)
164 break;
165 if (ai.lock > 0) {
167 delete_area(ai.area);
168 clone_area(ai.name, &ai.address, B_CLONE_ADDRESS,
169 ai.protection, original);
170 }
171 }
172
173 proc->pid = pid;
174 proc->in = NULL;
175 proc->out = NULL;
176 proc->err = NULL;
177 return APR_INCHILD;
178 }
179 proc->pid = pid;
180 proc->in = NULL;
181 proc->out = NULL;
182 proc->err = NULL;
183 return APR_INPARENT;
184}
185
188{
189 /* won't ever be called on this platform, so don't save the function pointer */
190 return APR_SUCCESS;
191}
192
195{
196 /* won't ever be used on this platform, so don't save the flag */
197 return APR_SUCCESS;
198}
199
202{
203 /* won't ever be used on this platform, so don't save the flag */
204 return APR_SUCCESS;
205}
206
208 const char * const *args,
209 const char * const *env,
212{
213 int i=0,nargs=0;
214 char **newargs = NULL;
216 struct send_pipe *sp;
217 char * dir = NULL;
218
219 sp = (struct send_pipe *)apr_palloc(pool, sizeof(struct send_pipe));
220
221 new->in = attr->parent_in;
222 new->err = attr->parent_err;
223 new->out = attr->parent_out;
227
228 i = 0;
229 while (args && args[i]) {
230 i++;
231 }
232
233 newargs = (char**)malloc(sizeof(char *) * (i + 4));
234 newargs[0] = strdup("/boot/home/config/bin/apr_proc_stub");
235 if (attr->currdir == NULL) {
236 /* we require the directory , so use a temp. variable */
237 dir = malloc(sizeof(char) * PATH_MAX);
239 newargs[1] = strdup(dir);
240 free(dir);
241 } else {
242 newargs[1] = strdup(attr->currdir);
243 }
244 newargs[2] = strdup(progname);
245 i=0;nargs = 3;
246
247 while (args && args[i]) {
249 i++;nargs++;
250 }
251 newargs[nargs] = NULL;
252
253 /* ### we should be looking at attr->cmdtype in here... */
254
255 newproc = load_image(nargs, (const char**)newargs, (const char**)env);
256
257 /* load_image copies the data so now we can free it... */
258 while (--nargs >= 0)
259 free (newargs[nargs]);
260 free(newargs);
261
262 if (newproc < B_NO_ERROR) {
263 return errno;
264 }
265
267
268 if (attr->child_in && (attr->child_in->filedes != -1)) {
270 }
271 if (attr->child_out && (attr->child_in->filedes != -1)) {
273 }
274 if (attr->child_err && (attr->child_in->filedes != -1)) {
276 }
277
278 send_data(newproc, 0, (void*)sp, sizeof(struct send_pipe));
279 new->pid = newproc;
280
281 /* before we go charging on we need the new process to get to a
282 * certain point. When it gets there it'll let us know and we
283 * can carry on. */
284 receive_data(&sender, (void*)NULL,0);
285
286 return APR_SUCCESS;
287}
288
290 int *exitcode,
293 apr_pool_t *p)
294{
295 proc->pid = -1;
297}
298
300 int *exitcode,
303{
306 int exit_int;
307 int ignore;
309
310 if (exitcode == NULL) {
311 exitcode = &ignore;
312 }
313 if (exitwhy == NULL) {
315 }
316
317 if (waithow != APR_WAIT) {
319 }
320
321 if ((pstatus = waitpid(proc->pid, &exit_int, waitpid_options)) > 0) {
322 proc->pid = pstatus;
323 if (WIFEXITED(exit_int)) {
326 }
327 else if (WIFSIGNALED(exit_int)) {
330 }
331 else {
332
333 /* unexpected condition */
334 return APR_EGENERAL;
335 }
336 return APR_CHILD_DONE;
337 }
338 else if (pstatus == 0) {
339 return APR_CHILD_NOTDONE;
340 }
341
342 return errno;
343}
344
347{
348 apr_status_t rv;
349
350 if (attr->child_in == NULL && attr->parent_in == NULL
351 && child_in == NULL && parent_in == NULL)
353 attr->pool)) == APR_SUCCESS)
355
356 if (child_in != NULL && rv == APR_SUCCESS) {
357 if (attr->child_in && (attr->child_in->filedes != -1))
359 else {
360 attr->child_in = NULL;
361 if ((rv = apr_file_dup(&attr->child_in, child_in, attr->pool))
362 == APR_SUCCESS)
364 }
365 }
366
367 if (parent_in != NULL && rv == APR_SUCCESS)
369
370 return rv;
371}
372
375{
376 apr_status_t rv;
377
378 if (attr->child_out == NULL && attr->parent_out == NULL
379 && child_out == NULL && parent_out == NULL)
381 attr->pool)) == APR_SUCCESS)
383
384 if (child_out != NULL && rv == APR_SUCCESS) {
385 if (attr->child_out && (attr->child_out->filedes != -1))
387 else {
390 == APR_SUCCESS)
392 }
393 }
394
395 if (parent_out != NULL && rv == APR_SUCCESS)
397
398 return rv;
399}
400
403{
404 apr_status_t rv;
405
406 if (attr->child_err == NULL && attr->parent_err == NULL
407 && child_err == NULL && parent_err == NULL)
409 attr->pool)) == APR_SUCCESS)
411
412 if (child_err != NULL && rv == APR_SUCCESS) {
413 if (attr->child_err && (attr->child_err->filedes != -1))
415 else {
418 == APR_SUCCESS)
420 }
421 }
422
423 if (parent_err != NULL && rv == APR_SUCCESS)
425
426 return rv;
427}
428
430 void *limit)
431{
432 return APR_ENOTIMPL;
433}
434
436 const char *username,
437 const char *password)
438{
439 return APR_ENOTIMPL;
440}
441
443 const char *groupname)
444{
445 return APR_ENOTIMPL;
446}
447
450 void *data,
452{
453 return APR_ENOTIMPL;
454}
char * strdup(const char *str)
APR Strings library.
static apr_file_t no_file
Definition proc.c:23
#define APR_CHILD_NOTDONE
Definition apr_errno.h:448
#define APR_EGENERAL
Definition apr_errno.h:313
#define APR_CHILD_DONE
Definition apr_errno.h:446
#define APR_INCHILD
Definition apr_errno.h:438
#define APR_ENOMEM
Definition apr_errno.h:683
#define APR_INPARENT
Definition apr_errno.h:440
#define APR_ENOTIMPL
Definition apr_errno.h:476
apr_pool_t apr_dbd_t int apr_dbd_prepared_t int nargs
Definition apr_dbd.h:414
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
const char int apr_pool_t * pool
Definition apr_cstr.h:84
#define APR_SUCCESS
Definition apr_errno.h:225
int apr_status_t
Definition apr_errno.h:44
apr_int32_t apr_fileperms_t
const char apr_fileperms_t perms
void * data
char const *const char const *const ** env
apr_status_t() apr_perms_setfn_t(void *object, apr_fileperms_t perms, apr_uid_t uid, apr_gid_t gid)
apr_dir_t * dir
apr_proc_t * proc
const char const char * password
apr_int32_t apr_int32_t apr_int32_t err
apr_file_t apr_file_t * parent_out
apr_perms_setfn_t * perms_set_fn
const char const char *const const char *const apr_procattr_t * attr
void() apr_child_errfn_t(apr_pool_t *proc, apr_status_t err, const char *description)
apr_int32_t in
apr_child_errfn_t * errfn
#define APR_WRITE_BLOCK
const char * groupname
#define APR_NO_FILE
apr_int32_t addrspace
apr_wait_how_e
int apr_exit_why_e * exitwhy
apr_cmdtype_e cmd
apr_int32_t chk
apr_file_t apr_file_t * parent_in
#define APR_PARENT_BLOCK
apr_file_t * child_err
const char const char *const * args
#define APR_NO_PIPE
const char * username
apr_exit_why_e
int apr_exit_why_e apr_wait_how_e waithow
#define APR_CHILD_BLOCK
const char * progname
apr_cmdtype_e
apr_int32_t detach
int * exitcode
apr_file_t * child_out
apr_file_t apr_file_t * parent_err
apr_file_t * child_in
#define APR_READ_BLOCK
@ APR_WAIT
@ APR_PROC_SIGNAL
@ APR_PROC_EXIT
@ APR_PROGRAM
#define PATH_MAX
Definition jlibtool.c:180
apr_pool_t * p
Definition md_event.c:32
static apr_file_t * out
Definition mod_info.c:85
static apr_status_t send_data(proxy_conn_rec *conn, struct iovec *vec, int nvec, apr_size_t *len)
return NULL
Definition mod_so.c:359
int i
Definition mod_so.c:347
apr_file_t * in
apr_file_t * out
apr_file_t * err
apr_file_t * child_err
apr_file_t * parent_in
apr_file_t * parent_out
apr_file_t * child_out
apr_file_t * child_in
apr_file_t * parent_err
int err
Definition proc.c:28
int in
Definition proc.c:26
int out
Definition proc.c:27
static apr_proc_t newproc
Definition testproc.c:30
int waitpid(pid_t pid, int *statusp, int options)
Definition procsup.c:91