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"
19
#include "apr_portable.h"
20
#include "apr_signal.h"
21
#include "apr_random.h"
23
APR_DECLARE(apr_status_t) apr_procattr_create(apr_procattr_t **new,
26
(*new) = (apr_procattr_t *)apr_pcalloc(pool, sizeof(apr_procattr_t));
32
(*new)->cmdtype = APR_PROGRAM;
33
(*new)->uid = (*new)->gid = -1;
37
APR_DECLARE(apr_status_t) apr_procattr_io_set(apr_procattr_t *attr,
44
if ((status = apr_file_pipe_create(&attr->child_in, &attr->parent_in,
45
attr->pool)) != APR_SUCCESS) {
52
case APR_PARENT_BLOCK:
53
apr_file_pipe_timeout_set(attr->child_in, 0);
56
apr_file_pipe_timeout_set(attr->parent_in, 0);
59
apr_file_pipe_timeout_set(attr->child_in, 0);
60
apr_file_pipe_timeout_set(attr->parent_in, 0);
65
if ((status = apr_file_pipe_create(&attr->parent_out, &attr->child_out,
66
attr->pool)) != APR_SUCCESS) {
73
case APR_PARENT_BLOCK:
74
apr_file_pipe_timeout_set(attr->child_out, 0);
77
apr_file_pipe_timeout_set(attr->parent_out, 0);
80
apr_file_pipe_timeout_set(attr->child_out, 0);
81
apr_file_pipe_timeout_set(attr->parent_out, 0);
86
if ((status = apr_file_pipe_create(&attr->parent_err, &attr->child_err,
87
attr->pool)) != APR_SUCCESS) {
94
case APR_PARENT_BLOCK:
95
apr_file_pipe_timeout_set(attr->child_err, 0);
98
apr_file_pipe_timeout_set(attr->parent_err, 0);
101
apr_file_pipe_timeout_set(attr->child_err, 0);
102
apr_file_pipe_timeout_set(attr->parent_err, 0);
110
APR_DECLARE(apr_status_t) apr_procattr_child_in_set(apr_procattr_t *attr,
111
apr_file_t *child_in,
112
apr_file_t *parent_in)
114
apr_status_t rv = APR_SUCCESS;
116
if (attr->child_in == NULL && attr->parent_in == NULL)
117
rv = apr_file_pipe_create(&attr->child_in, &attr->parent_in, attr->pool);
119
if (child_in != NULL && rv == APR_SUCCESS)
120
rv = apr_file_dup2(attr->child_in, child_in, attr->pool);
122
if (parent_in != NULL && rv == APR_SUCCESS)
123
rv = apr_file_dup2(attr->parent_in, parent_in, attr->pool);
129
APR_DECLARE(apr_status_t) apr_procattr_child_out_set(apr_procattr_t *attr,
130
apr_file_t *child_out,
131
apr_file_t *parent_out)
133
apr_status_t rv = APR_SUCCESS;
135
if (attr->child_out == NULL && attr->parent_out == NULL)
136
rv = apr_file_pipe_create(&attr->child_out, &attr->parent_out, attr->pool);
138
if (child_out != NULL && rv == APR_SUCCESS)
139
rv = apr_file_dup2(attr->child_out, child_out, attr->pool);
141
if (parent_out != NULL && rv == APR_SUCCESS)
142
rv = apr_file_dup2(attr->parent_out, parent_out, attr->pool);
148
APR_DECLARE(apr_status_t) apr_procattr_child_err_set(apr_procattr_t *attr,
149
apr_file_t *child_err,
150
apr_file_t *parent_err)
152
apr_status_t rv = APR_SUCCESS;
154
if (attr->child_err == NULL && attr->parent_err == NULL)
155
rv = apr_file_pipe_create(&attr->child_err, &attr->parent_err, attr->pool);
157
if (child_err != NULL && rv == APR_SUCCESS)
158
rv = apr_file_dup2(attr->child_err, child_err, attr->pool);
160
if (parent_err != NULL && rv == APR_SUCCESS)
161
rv = apr_file_dup2(attr->parent_err, parent_err, attr->pool);
167
APR_DECLARE(apr_status_t) apr_procattr_dir_set(apr_procattr_t *attr,
170
attr->currdir = apr_pstrdup(attr->pool, dir);
178
APR_DECLARE(apr_status_t) apr_procattr_cmdtype_set(apr_procattr_t *attr,
185
APR_DECLARE(apr_status_t) apr_procattr_detach_set(apr_procattr_t *attr,
188
attr->detached = detach;
192
APR_DECLARE(apr_status_t) apr_proc_fork(apr_proc_t *proc, apr_pool_t *pool)
196
if ((pid = fork()) < 0) {
205
apr_random_after_fork(proc);
218
static apr_status_t limit_proc(apr_procattr_t *attr)
220
#if APR_HAVE_STRUCT_RLIMIT && APR_HAVE_SETRLIMIT
222
if (attr->limit_cpu != NULL) {
223
if ((setrlimit(RLIMIT_CPU, attr->limit_cpu)) != 0) {
229
if (attr->limit_nproc != NULL) {
230
if ((setrlimit(RLIMIT_NPROC, attr->limit_nproc)) != 0) {
236
if (attr->limit_nofile != NULL) {
237
if ((setrlimit(RLIMIT_NOFILE, attr->limit_nofile)) != 0) {
242
#if defined(RLIMIT_AS)
243
if (attr->limit_mem != NULL) {
244
if ((setrlimit(RLIMIT_AS, attr->limit_mem)) != 0) {
248
#elif defined(RLIMIT_DATA)
249
if (attr->limit_mem != NULL) {
250
if ((setrlimit(RLIMIT_DATA, attr->limit_mem)) != 0) {
254
#elif defined(RLIMIT_VMEM)
255
if (attr->limit_mem != NULL) {
256
if ((setrlimit(RLIMIT_VMEM, attr->limit_mem)) != 0) {
263
* Maybe make a note in error_log that setrlimit isn't supported??
270
APR_DECLARE(apr_status_t) apr_procattr_child_errfn_set(apr_procattr_t *attr,
271
apr_child_errfn_t *errfn)
277
APR_DECLARE(apr_status_t) apr_procattr_error_check_set(apr_procattr_t *attr,
284
APR_DECLARE(apr_status_t) apr_procattr_addrspace_set(apr_procattr_t *attr,
285
apr_int32_t addrspace)
287
/* won't ever be used on this platform, so don't save the flag */
291
APR_DECLARE(apr_status_t) apr_procattr_user_set(apr_procattr_t *attr,
292
const char *username,
293
const char *password)
298
if ((rv = apr_uid_get(&attr->uid, &gid, username,
299
attr->pool)) != APR_SUCCESS) {
304
/* Use default user group if not already set */
305
if (attr->gid == -1) {
311
APR_DECLARE(apr_status_t) apr_procattr_group_set(apr_procattr_t *attr,
312
const char *groupname)
316
if ((rv = apr_gid_get(&attr->gid, groupname, attr->pool)) != APR_SUCCESS)
321
APR_DECLARE(apr_status_t) apr_proc_create(apr_proc_t *new,
322
const char *progname,
323
const char * const *args,
324
const char * const *env,
325
apr_procattr_t *attr,
329
const char * const empty_envp[] = {NULL};
331
if (!env) { /* Specs require an empty array instead of NULL;
332
* Purify will trigger a failure, even if many
333
* implementations don't.
338
new->in = attr->parent_in;
339
new->err = attr->parent_err;
340
new->out = attr->parent_out;
344
if (access(attr->currdir, X_OK) == -1) {
345
/* chdir() in child wouldn't have worked */
350
if (attr->cmdtype == APR_PROGRAM ||
351
attr->cmdtype == APR_PROGRAM_ENV ||
353
/* for both of these values of cmdtype, caller must pass
354
* full path, so it is easy to check;
355
* caller can choose to pass full path for other
358
if (access(progname, R_OK|X_OK) == -1) {
359
/* exec*() in child wouldn't have worked */
364
/* todo: search PATH for progname then try to access it */
368
if ((new->pid = fork()) < 0) {
371
else if (new->pid == 0) {
376
* If we do exec cleanup before the dup2() calls to set up pipes
377
* on 0-2, we accidentally close the pipes used by programs like
380
* If we do exec cleanup after the dup2() calls, cleanup can accidentally
381
* close our pipes which replaced any files which previously had
384
* The solution is to kill the cleanup for the pipes, then do
385
* exec cleanup, then do the dup2() calls.
388
if (attr->child_in) {
389
apr_pool_cleanup_kill(apr_file_pool_get(attr->child_in),
390
attr->child_in, apr_unix_file_cleanup);
393
if (attr->child_out) {
394
apr_pool_cleanup_kill(apr_file_pool_get(attr->child_out),
395
attr->child_out, apr_unix_file_cleanup);
398
if (attr->child_err) {
399
apr_pool_cleanup_kill(apr_file_pool_get(attr->child_err),
400
attr->child_err, apr_unix_file_cleanup);
403
apr_pool_cleanup_for_exec();
405
if (attr->child_in) {
406
apr_file_close(attr->parent_in);
407
dup2(attr->child_in->filedes, STDIN_FILENO);
408
apr_file_close(attr->child_in);
411
if (attr->child_out) {
412
apr_file_close(attr->parent_out);
413
dup2(attr->child_out->filedes, STDOUT_FILENO);
414
apr_file_close(attr->child_out);
417
if (attr->child_err) {
418
apr_file_close(attr->parent_err);
419
dup2(attr->child_err->filedes, STDERR_FILENO);
420
apr_file_close(attr->child_err);
423
apr_signal(SIGCHLD, SIG_DFL); /* not sure if this is needed or not */
425
if (attr->currdir != NULL) {
426
if (chdir(attr->currdir) == -1) {
428
attr->errfn(pool, errno, "change of working directory failed");
430
exit(-1); /* We have big problems, the child should exit. */
434
/* Only try to switch if we are running as root */
435
if (attr->gid != -1 && !geteuid()) {
436
if ((status = setgid(attr->gid))) {
438
attr->errfn(pool, errno, "setting of group failed");
440
exit(-1); /* We have big problems, the child should exit. */
444
if (attr->uid != -1 && !geteuid()) {
445
if ((status = setuid(attr->uid))) {
447
attr->errfn(pool, errno, "setting of user failed");
449
exit(-1); /* We have big problems, the child should exit. */
453
if ((status = limit_proc(attr)) != APR_SUCCESS) {
455
attr->errfn(pool, errno, "setting of resource limits failed");
457
exit(-1); /* We have big problems, the child should exit. */
460
if (attr->cmdtype == APR_SHELLCMD ||
461
attr->cmdtype == APR_SHELLCMD_ENV) {
463
const char *newargs[4];
465
newargs[0] = SHELL_PATH;
470
onearg_len += strlen(args[i]);
471
onearg_len++; /* for space delimiter */
477
/* bad parameters; we're doomed */
480
/* no args, or caller already built a single string from
483
newargs[2] = args[0];
489
ch = onearg = apr_palloc(pool, onearg_len);
492
size_t len = strlen(args[i]);
494
memcpy(ch, args[i], len);
500
--ch; /* back up to trailing blank */
508
if (attr->detached) {
509
apr_proc_detach(APR_PROC_DETACH_DAEMONIZE);
512
if (attr->cmdtype == APR_SHELLCMD) {
513
execve(SHELL_PATH, (char * const *) newargs, (char * const *)env);
516
execv(SHELL_PATH, (char * const *)newargs);
519
else if (attr->cmdtype == APR_PROGRAM) {
520
if (attr->detached) {
521
apr_proc_detach(APR_PROC_DETACH_DAEMONIZE);
524
execve(progname, (char * const *)args, (char * const *)env);
526
else if (attr->cmdtype == APR_PROGRAM_ENV) {
527
if (attr->detached) {
528
apr_proc_detach(APR_PROC_DETACH_DAEMONIZE);
531
execv(progname, (char * const *)args);
534
/* APR_PROGRAM_PATH */
535
if (attr->detached) {
536
apr_proc_detach(APR_PROC_DETACH_DAEMONIZE);
539
execvp(progname, (char * const *)args);
544
desc = apr_psprintf(pool, "exec of '%s' failed",
546
attr->errfn(pool, errno, desc);
549
exit(-1); /* if we get here, there is a problem, so exit with an
554
if (attr->child_in) {
555
apr_file_close(attr->child_in);
558
if (attr->child_out) {
559
apr_file_close(attr->child_out);
562
if (attr->child_err) {
563
apr_file_close(attr->child_err);
569
APR_DECLARE(apr_status_t) apr_proc_wait_all_procs(apr_proc_t *proc,
571
apr_exit_why_e *exitwhy,
572
apr_wait_how_e waithow,
576
return apr_proc_wait(proc, exitcode, exitwhy, waithow);
579
APR_DECLARE(apr_status_t) apr_proc_wait(apr_proc_t *proc,
580
int *exitcode, apr_exit_why_e *exitwhy,
581
apr_wait_how_e waithow)
584
int waitpid_options = WUNTRACED;
587
apr_exit_why_e ignorewhy;
589
if (exitcode == NULL) {
593
if (exitwhy == NULL) {
594
exitwhy = &ignorewhy;
597
if (waithow != APR_WAIT) {
598
waitpid_options |= WNOHANG;
602
pstatus = waitpid(proc->pid, &exit_int, waitpid_options);
603
} while (pstatus < 0 && errno == EINTR);
608
if (WIFEXITED(exit_int)) {
609
*exitwhy = APR_PROC_EXIT;
610
*exitcode = WEXITSTATUS(exit_int);
612
else if (WIFSIGNALED(exit_int)) {
613
*exitwhy = APR_PROC_SIGNAL;
616
if (WCOREDUMP(exit_int)) {
617
*exitwhy |= APR_PROC_SIGNAL_CORE;
621
*exitcode = WTERMSIG(exit_int);
624
/* unexpected condition */
628
return APR_CHILD_DONE;
630
else if (pstatus == 0) {
631
return APR_CHILD_NOTDONE;
637
APR_DECLARE(apr_status_t) apr_procattr_limit_set(apr_procattr_t *attr,
639
struct rlimit *limit)
644
attr->limit_cpu = limit;
651
#if defined (RLIMIT_DATA) || defined (RLIMIT_VMEM) || defined(RLIMIT_AS)
652
attr->limit_mem = limit;
658
case APR_LIMIT_NPROC:
660
attr->limit_nproc = limit;
666
case APR_LIMIT_NOFILE:
668
attr->limit_nofile = limit;