1
/* Copyright 2000-2005 The Apache Software Foundation or its licensors, as
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* 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_threadproc.h"
18
#include "apr_strings.h"
26
APR_DECLARE(apr_status_t) apr_procattr_create(apr_procattr_t **new, apr_pool_t *pool)
28
(*new) = (apr_procattr_t *)apr_palloc(pool,
29
sizeof(apr_procattr_t));
35
(*new)->parent_in = NULL;
36
(*new)->child_in = NULL;
37
(*new)->parent_out = NULL;
38
(*new)->child_out = NULL;
39
(*new)->parent_err = NULL;
40
(*new)->child_err = NULL;
41
(*new)->currdir = NULL;
42
(*new)->cmdtype = APR_PROGRAM;
47
APR_DECLARE(apr_status_t) apr_procattr_io_set(apr_procattr_t *attr, apr_int32_t in,
48
apr_int32_t out, apr_int32_t err)
52
if ((status = apr_file_pipe_create(&attr->child_in, &attr->parent_in,
53
attr->pool)) != APR_SUCCESS) {
58
apr_file_pipe_timeout_set(attr->child_in, -1);
59
apr_file_pipe_timeout_set(attr->parent_in, -1);
61
case APR_PARENT_BLOCK:
62
apr_file_pipe_timeout_set(attr->child_in, -1);
65
apr_file_pipe_timeout_set(attr->parent_in, -1);
72
if ((status = apr_file_pipe_create(&attr->parent_out, &attr->child_out,
73
attr->pool)) != APR_SUCCESS) {
78
apr_file_pipe_timeout_set(attr->child_out, -1);
79
apr_file_pipe_timeout_set(attr->parent_out, -1);
81
case APR_PARENT_BLOCK:
82
apr_file_pipe_timeout_set(attr->child_out, -1);
85
apr_file_pipe_timeout_set(attr->parent_out, -1);
92
if ((status = apr_file_pipe_create(&attr->parent_err, &attr->child_err,
93
attr->pool)) != APR_SUCCESS) {
98
apr_file_pipe_timeout_set(attr->child_err, -1);
99
apr_file_pipe_timeout_set(attr->parent_err, -1);
101
case APR_PARENT_BLOCK:
102
apr_file_pipe_timeout_set(attr->child_err, -1);
104
case APR_CHILD_BLOCK:
105
apr_file_pipe_timeout_set(attr->parent_err, -1);
114
APR_DECLARE(apr_status_t) apr_procattr_dir_set(apr_procattr_t *attr,
119
cwd = (char*)malloc(sizeof(char) * PATH_MAX);
120
getcwd(cwd, PATH_MAX);
121
attr->currdir = (char *)apr_pstrcat(attr->pool, cwd, "/", dir, NULL);
124
attr->currdir = (char *)apr_pstrdup(attr->pool, dir);
132
APR_DECLARE(apr_status_t) apr_procattr_cmdtype_set(apr_procattr_t *attr,
139
APR_DECLARE(apr_status_t) apr_procattr_detach_set(apr_procattr_t *attr, apr_int32_t detach)
141
attr->detached = detach;
145
APR_DECLARE(apr_status_t) apr_proc_fork(apr_proc_t *proc, apr_pool_t *pool)
149
if ((pid = fork()) < 0) {
153
/* This is really ugly...
154
* The semantics of BeOS's fork() are that areas (used for shared
155
* memory) get COW'd :-( The only way we can make shared memory
156
* work across fork() is therefore to find any areas that have
157
* been created and then clone them into our address space.
158
* Thankfully only COW'd areas have the lock variable set at
159
* anything but 0, so we can use that to find the areas we need to
160
* copy. Of course what makes it even worse is that the loop through
161
* the area's will go into an infinite loop, eating memory and then
162
* eventually segfault unless we know when we reach then end of the
163
* "original" areas and stop. Why? Well, we delete the area and then
164
* add another to the end of the list...
170
while (get_next_area_info(0, &cookie, &ai) == B_OK)
171
if (ai.area > highest)
174
while (get_next_area_info(0, &cookie, &ai) == B_OK) {
175
if (ai.area > highest)
178
area_id original = find_area(ai.name);
179
delete_area(ai.area);
180
clone_area(ai.name, &ai.address, B_CLONE_ADDRESS,
181
ai.protection, original);
198
APR_DECLARE(apr_status_t) apr_procattr_child_errfn_set(apr_procattr_t *attr,
199
apr_child_errfn_t *errfn)
201
/* won't ever be called on this platform, so don't save the function pointer */
205
APR_DECLARE(apr_status_t) apr_procattr_error_check_set(apr_procattr_t *attr,
208
/* won't ever be used on this platform, so don't save the flag */
212
APR_DECLARE(apr_status_t) apr_procattr_addrspace_set(apr_procattr_t *attr,
213
apr_int32_t addrspace)
215
/* won't ever be used on this platform, so don't save the flag */
219
APR_DECLARE(apr_status_t) apr_proc_create(apr_proc_t *new, const char *progname,
220
const char * const *args,
221
const char * const *env,
222
apr_procattr_t *attr,
226
char **newargs = NULL;
227
thread_id newproc, sender;
228
struct send_pipe *sp;
231
sp = (struct send_pipe *)apr_palloc(pool, sizeof(struct send_pipe));
233
new->in = attr->parent_in;
234
new->err = attr->parent_err;
235
new->out = attr->parent_out;
236
sp->in = attr->child_in ? attr->child_in->filedes : -1;
237
sp->out = attr->child_out ? attr->child_out->filedes : -1;
238
sp->err = attr->child_err ? attr->child_err->filedes : -1;
241
while (args && args[i]) {
245
newargs = (char**)malloc(sizeof(char *) * (i + 4));
246
newargs[0] = strdup("/boot/home/config/bin/apr_proc_stub");
247
if (attr->currdir == NULL) {
248
/* we require the directory , so use a temp. variable */
249
dir = malloc(sizeof(char) * PATH_MAX);
250
getcwd(dir, PATH_MAX);
251
newargs[1] = strdup(dir);
254
newargs[1] = strdup(attr->currdir);
256
newargs[2] = strdup(progname);
259
while (args && args[i]) {
260
newargs[nargs] = strdup(args[i]);
263
newargs[nargs] = NULL;
265
/* ### we should be looking at attr->cmdtype in here... */
267
newproc = load_image(nargs, (const char**)newargs, (const char**)env);
269
/* load_image copies the data so now we can free it... */
271
free (newargs[nargs]);
274
if (newproc < B_NO_ERROR) {
278
resume_thread(newproc);
280
if (attr->child_in) {
281
apr_file_close(attr->child_in);
283
if (attr->child_out) {
284
apr_file_close(attr->child_out);
286
if (attr->child_err) {
287
apr_file_close(attr->child_err);
290
send_data(newproc, 0, (void*)sp, sizeof(struct send_pipe));
293
/* before we go charging on we need the new process to get to a
294
* certain point. When it gets there it'll let us know and we
296
receive_data(&sender, (void*)NULL,0);
301
APR_DECLARE(apr_status_t) apr_proc_wait_all_procs(apr_proc_t *proc,
303
apr_exit_why_e *exitwhy,
304
apr_wait_how_e waithow,
308
return apr_proc_wait(proc, exitcode, exitwhy, waithow);
311
APR_DECLARE(apr_status_t) apr_proc_wait(apr_proc_t *proc,
313
apr_exit_why_e *exitwhy,
314
apr_wait_how_e waithow)
317
int waitpid_options = WUNTRACED;
320
apr_exit_why_e ignorewhy;
322
if (exitcode == NULL) {
325
if (exitwhy == NULL) {
326
exitwhy = &ignorewhy;
329
if (waithow != APR_WAIT) {
330
waitpid_options |= WNOHANG;
333
if ((pstatus = waitpid(proc->pid, &exit_int, waitpid_options)) > 0) {
335
if (WIFEXITED(exit_int)) {
336
*exitwhy = APR_PROC_EXIT;
337
*exitcode = WEXITSTATUS(exit_int);
339
else if (WIFSIGNALED(exit_int)) {
340
*exitwhy = APR_PROC_SIGNAL;
341
*exitcode = WTERMSIG(exit_int);
345
/* unexpected condition */
348
return APR_CHILD_DONE;
350
else if (pstatus == 0) {
351
return APR_CHILD_NOTDONE;
357
APR_DECLARE(apr_status_t) apr_procattr_child_in_set(apr_procattr_t *attr, apr_file_t *child_in,
358
apr_file_t *parent_in)
360
if (attr->child_in == NULL && attr->parent_in == NULL)
361
apr_file_pipe_create(&attr->child_in, &attr->parent_in, attr->pool);
363
if (child_in != NULL)
364
apr_file_dup(&attr->child_in, child_in, attr->pool);
366
if (parent_in != NULL)
367
apr_file_dup(&attr->parent_in, parent_in, attr->pool);
372
APR_DECLARE(apr_status_t) apr_procattr_child_out_set(apr_procattr_t *attr, apr_file_t *child_out,
373
apr_file_t *parent_out)
375
if (attr->child_out == NULL && attr->parent_out == NULL)
376
apr_file_pipe_create(&attr->child_out, &attr->parent_out, attr->pool);
378
if (child_out != NULL)
379
apr_file_dup(&attr->child_out, child_out, attr->pool);
381
if (parent_out != NULL)
382
apr_file_dup(&attr->parent_out, parent_out, attr->pool);
387
APR_DECLARE(apr_status_t) apr_procattr_child_err_set(apr_procattr_t *attr, apr_file_t *child_err,
388
apr_file_t *parent_err)
390
if (attr->child_err == NULL && attr->parent_err == NULL)
391
apr_file_pipe_create(&attr->child_err, &attr->parent_err, attr->pool);
393
if (child_err != NULL)
394
apr_file_dup(&attr->child_err, child_err, attr->pool);
396
if (parent_err != NULL)
397
apr_file_dup(&attr->parent_err, parent_err, attr->pool);
402
APR_DECLARE(apr_status_t) apr_procattr_limit_set(apr_procattr_t *attr, apr_int32_t what,
408
APR_DECLARE(apr_status_t) apr_procattr_user_set(apr_procattr_t *attr,
409
const char *username,
410
const char *password)
415
APR_DECLARE(apr_status_t) apr_procattr_group_set(apr_procattr_t *attr,
416
const char *groupname)