~ubuntu-branches/ubuntu/feisty/apache2/feisty

« back to all changes in this revision

Viewing changes to srclib/apr/threadproc/unix/proc.c

  • Committer: Bazaar Package Importer
  • Author(s): Andreas Barth
  • Date: 2006-12-09 21:05:45 UTC
  • mfrom: (0.6.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20061209210545-h70s0xaqc2v8vqr2
Tags: 2.2.3-3.2
* Non-maintainer upload.
* 043_ajp_connection_reuse: Patch from upstream Bugzilla, fixing a critical
  issue with regard to connection reuse in mod_proxy_ajp.
  Closes: #396265

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright 2000-2005 The Apache Software Foundation or its licensors, as
 
2
 * applicable.
 
3
 *
 
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
 
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
#include "apr_portable.h"
 
20
#include "apr_signal.h"
 
21
#include "apr_random.h"
 
22
 
 
23
APR_DECLARE(apr_status_t) apr_procattr_create(apr_procattr_t **new,
 
24
                                              apr_pool_t *pool)
 
25
{
 
26
    (*new) = (apr_procattr_t *)apr_pcalloc(pool, sizeof(apr_procattr_t));
 
27
 
 
28
    if ((*new) == NULL) {
 
29
        return APR_ENOMEM;
 
30
    }
 
31
    (*new)->pool = pool;
 
32
    (*new)->cmdtype = APR_PROGRAM;
 
33
    (*new)->uid = (*new)->gid = -1;
 
34
    return APR_SUCCESS;
 
35
}
 
36
 
 
37
APR_DECLARE(apr_status_t) apr_procattr_io_set(apr_procattr_t *attr,
 
38
                                              apr_int32_t in,
 
39
                                              apr_int32_t out,
 
40
                                              apr_int32_t err)
 
41
{
 
42
    apr_status_t status;
 
43
    if (in != 0) {
 
44
        if ((status = apr_file_pipe_create(&attr->child_in, &attr->parent_in,
 
45
                                           attr->pool)) != APR_SUCCESS) {
 
46
            return status;
 
47
        }
 
48
 
 
49
        switch (in) {
 
50
        case APR_FULL_BLOCK:
 
51
            break;
 
52
        case APR_PARENT_BLOCK:
 
53
            apr_file_pipe_timeout_set(attr->child_in, 0);
 
54
            break;
 
55
        case APR_CHILD_BLOCK:
 
56
            apr_file_pipe_timeout_set(attr->parent_in, 0);
 
57
            break;
 
58
        default:
 
59
            apr_file_pipe_timeout_set(attr->child_in, 0);
 
60
            apr_file_pipe_timeout_set(attr->parent_in, 0);
 
61
        }
 
62
    }
 
63
 
 
64
    if (out) {
 
65
        if ((status = apr_file_pipe_create(&attr->parent_out, &attr->child_out,
 
66
                                           attr->pool)) != APR_SUCCESS) {
 
67
            return status;
 
68
        }
 
69
 
 
70
        switch (out) {
 
71
        case APR_FULL_BLOCK:
 
72
            break;
 
73
        case APR_PARENT_BLOCK:
 
74
            apr_file_pipe_timeout_set(attr->child_out, 0);
 
75
            break;
 
76
        case APR_CHILD_BLOCK:
 
77
            apr_file_pipe_timeout_set(attr->parent_out, 0);
 
78
            break;
 
79
        default:
 
80
            apr_file_pipe_timeout_set(attr->child_out, 0);
 
81
            apr_file_pipe_timeout_set(attr->parent_out, 0);
 
82
        }
 
83
    }
 
84
 
 
85
    if (err) {
 
86
        if ((status = apr_file_pipe_create(&attr->parent_err, &attr->child_err,
 
87
                                           attr->pool)) != APR_SUCCESS) {
 
88
            return status;
 
89
        }
 
90
 
 
91
        switch (err) {
 
92
        case APR_FULL_BLOCK:
 
93
            break;
 
94
        case APR_PARENT_BLOCK:
 
95
            apr_file_pipe_timeout_set(attr->child_err, 0);
 
96
            break;
 
97
        case APR_CHILD_BLOCK:
 
98
            apr_file_pipe_timeout_set(attr->parent_err, 0);
 
99
            break;
 
100
        default:
 
101
            apr_file_pipe_timeout_set(attr->child_err, 0);
 
102
            apr_file_pipe_timeout_set(attr->parent_err, 0);
 
103
        }
 
104
    }
 
105
 
 
106
    return APR_SUCCESS;
 
107
}
 
108
 
 
109
 
 
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)
 
113
{
 
114
    apr_status_t rv = APR_SUCCESS;
 
115
 
 
116
    if (attr->child_in == NULL && attr->parent_in == NULL)
 
117
        rv = apr_file_pipe_create(&attr->child_in, &attr->parent_in, attr->pool);
 
118
    
 
119
    if (child_in != NULL && rv == APR_SUCCESS)
 
120
        rv = apr_file_dup2(attr->child_in, child_in, attr->pool);
 
121
 
 
122
    if (parent_in != NULL && rv == APR_SUCCESS)
 
123
        rv = apr_file_dup2(attr->parent_in, parent_in, attr->pool);
 
124
 
 
125
    return rv;
 
126
}
 
127
 
 
128
 
 
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)
 
132
{
 
133
    apr_status_t rv = APR_SUCCESS;
 
134
 
 
135
    if (attr->child_out == NULL && attr->parent_out == NULL)
 
136
        rv = apr_file_pipe_create(&attr->child_out, &attr->parent_out, attr->pool);
 
137
 
 
138
    if (child_out != NULL && rv == APR_SUCCESS)
 
139
        rv = apr_file_dup2(attr->child_out, child_out, attr->pool);
 
140
 
 
141
    if (parent_out != NULL && rv == APR_SUCCESS)
 
142
        rv = apr_file_dup2(attr->parent_out, parent_out, attr->pool);
 
143
 
 
144
    return rv;
 
145
}
 
146
 
 
147
 
 
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)
 
151
{
 
152
    apr_status_t rv = APR_SUCCESS;
 
153
 
 
154
    if (attr->child_err == NULL && attr->parent_err == NULL)
 
155
        rv = apr_file_pipe_create(&attr->child_err, &attr->parent_err, attr->pool);
 
156
 
 
157
    if (child_err != NULL && rv == APR_SUCCESS)
 
158
        rv = apr_file_dup2(attr->child_err, child_err, attr->pool);
 
159
 
 
160
    if (parent_err != NULL && rv == APR_SUCCESS)
 
161
        rv = apr_file_dup2(attr->parent_err, parent_err, attr->pool);
 
162
 
 
163
    return rv;
 
164
}
 
165
 
 
166
 
 
167
APR_DECLARE(apr_status_t) apr_procattr_dir_set(apr_procattr_t *attr,
 
168
                                               const char *dir)
 
169
{
 
170
    attr->currdir = apr_pstrdup(attr->pool, dir);
 
171
    if (attr->currdir) {
 
172
        return APR_SUCCESS;
 
173
    }
 
174
 
 
175
    return APR_ENOMEM;
 
176
}
 
177
 
 
178
APR_DECLARE(apr_status_t) apr_procattr_cmdtype_set(apr_procattr_t *attr,
 
179
                                                   apr_cmdtype_e cmd)
 
180
{
 
181
    attr->cmdtype = cmd;
 
182
    return APR_SUCCESS;
 
183
}
 
184
 
 
185
APR_DECLARE(apr_status_t) apr_procattr_detach_set(apr_procattr_t *attr,
 
186
                                                  apr_int32_t detach)
 
187
{
 
188
    attr->detached = detach;
 
189
    return APR_SUCCESS;
 
190
}
 
191
 
 
192
APR_DECLARE(apr_status_t) apr_proc_fork(apr_proc_t *proc, apr_pool_t *pool)
 
193
{
 
194
    int pid;
 
195
 
 
196
    if ((pid = fork()) < 0) {
 
197
        return errno;
 
198
    }
 
199
    else if (pid == 0) {
 
200
        proc->pid = pid;
 
201
        proc->in = NULL;
 
202
        proc->out = NULL;
 
203
        proc->err = NULL;
 
204
 
 
205
        apr_random_after_fork(proc);
 
206
 
 
207
        return APR_INCHILD;
 
208
    }
 
209
 
 
210
    proc->pid = pid;
 
211
    proc->in = NULL;
 
212
    proc->out = NULL;
 
213
    proc->err = NULL;
 
214
 
 
215
    return APR_INPARENT;
 
216
}
 
217
 
 
218
static apr_status_t limit_proc(apr_procattr_t *attr)
 
219
{
 
220
#if APR_HAVE_STRUCT_RLIMIT && APR_HAVE_SETRLIMIT
 
221
#ifdef RLIMIT_CPU
 
222
    if (attr->limit_cpu != NULL) {
 
223
        if ((setrlimit(RLIMIT_CPU, attr->limit_cpu)) != 0) {
 
224
            return errno;
 
225
        }
 
226
    }
 
227
#endif
 
228
#ifdef RLIMIT_NPROC
 
229
    if (attr->limit_nproc != NULL) {
 
230
        if ((setrlimit(RLIMIT_NPROC, attr->limit_nproc)) != 0) {
 
231
            return errno;
 
232
        }
 
233
    }
 
234
#endif
 
235
#ifdef RLIMIT_NOFILE
 
236
    if (attr->limit_nofile != NULL) {
 
237
        if ((setrlimit(RLIMIT_NOFILE, attr->limit_nofile)) != 0) {
 
238
            return errno;
 
239
        }
 
240
    }
 
241
#endif
 
242
#if defined(RLIMIT_AS)
 
243
    if (attr->limit_mem != NULL) {
 
244
        if ((setrlimit(RLIMIT_AS, attr->limit_mem)) != 0) {
 
245
            return errno;
 
246
        }
 
247
    }
 
248
#elif defined(RLIMIT_DATA)
 
249
    if (attr->limit_mem != NULL) {
 
250
        if ((setrlimit(RLIMIT_DATA, attr->limit_mem)) != 0) {
 
251
            return errno;
 
252
        }
 
253
    }
 
254
#elif defined(RLIMIT_VMEM)
 
255
    if (attr->limit_mem != NULL) {
 
256
        if ((setrlimit(RLIMIT_VMEM, attr->limit_mem)) != 0) {
 
257
            return errno;
 
258
        }
 
259
    }
 
260
#endif
 
261
#else
 
262
    /*
 
263
     * Maybe make a note in error_log that setrlimit isn't supported??
 
264
     */
 
265
 
 
266
#endif
 
267
    return APR_SUCCESS;
 
268
}
 
269
 
 
270
APR_DECLARE(apr_status_t) apr_procattr_child_errfn_set(apr_procattr_t *attr,
 
271
                                                       apr_child_errfn_t *errfn)
 
272
{
 
273
    attr->errfn = errfn;
 
274
    return APR_SUCCESS;
 
275
}
 
276
 
 
277
APR_DECLARE(apr_status_t) apr_procattr_error_check_set(apr_procattr_t *attr,
 
278
                                                       apr_int32_t chk)
 
279
{
 
280
    attr->errchk = chk;
 
281
    return APR_SUCCESS;
 
282
}
 
283
 
 
284
APR_DECLARE(apr_status_t) apr_procattr_addrspace_set(apr_procattr_t *attr,
 
285
                                                       apr_int32_t addrspace)
 
286
{
 
287
    /* won't ever be used on this platform, so don't save the flag */
 
288
    return APR_SUCCESS;
 
289
}
 
290
 
 
291
APR_DECLARE(apr_status_t) apr_procattr_user_set(apr_procattr_t *attr,
 
292
                                                const char *username,
 
293
                                                const char *password)
 
294
{
 
295
    apr_status_t rv;
 
296
    apr_gid_t    gid;
 
297
 
 
298
    if ((rv = apr_uid_get(&attr->uid, &gid, username,
 
299
                          attr->pool)) != APR_SUCCESS) {
 
300
        attr->uid = -1;
 
301
        return rv;
 
302
    }
 
303
    
 
304
    /* Use default user group if not already set */
 
305
    if (attr->gid == -1) {
 
306
        attr->gid = gid;
 
307
    }
 
308
    return APR_SUCCESS;
 
309
}
 
310
 
 
311
APR_DECLARE(apr_status_t) apr_procattr_group_set(apr_procattr_t *attr,
 
312
                                                 const char *groupname)
 
313
{
 
314
    apr_status_t rv;
 
315
 
 
316
    if ((rv = apr_gid_get(&attr->gid, groupname, attr->pool)) != APR_SUCCESS)
 
317
        attr->gid = -1;
 
318
    return rv;
 
319
}
 
320
 
 
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,
 
326
                                          apr_pool_t *pool)
 
327
{
 
328
    int i;
 
329
    const char * const empty_envp[] = {NULL};
 
330
 
 
331
    if (!env) { /* Specs require an empty array instead of NULL;
 
332
                 * Purify will trigger a failure, even if many
 
333
                 * implementations don't.
 
334
                 */
 
335
        env = empty_envp;
 
336
    }
 
337
 
 
338
    new->in = attr->parent_in;
 
339
    new->err = attr->parent_err;
 
340
    new->out = attr->parent_out;
 
341
 
 
342
    if (attr->errchk) {
 
343
        if (attr->currdir) {
 
344
            if (access(attr->currdir, X_OK) == -1) {
 
345
                /* chdir() in child wouldn't have worked */
 
346
                return errno;
 
347
            }
 
348
        }
 
349
 
 
350
        if (attr->cmdtype == APR_PROGRAM ||
 
351
            attr->cmdtype == APR_PROGRAM_ENV ||
 
352
            *progname == '/') {
 
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
 
356
             * values of cmdtype
 
357
             */
 
358
            if (access(progname, R_OK|X_OK) == -1) {
 
359
                /* exec*() in child wouldn't have worked */
 
360
                return errno;
 
361
            }
 
362
        }
 
363
        else {
 
364
            /* todo: search PATH for progname then try to access it */
 
365
        }
 
366
    }
 
367
 
 
368
    if ((new->pid = fork()) < 0) {
 
369
        return errno;
 
370
    }
 
371
    else if (new->pid == 0) {
 
372
        int status;
 
373
        /* child process */
 
374
 
 
375
        /*
 
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
 
378
         * mod_cgid.
 
379
         *
 
380
         * If we do exec cleanup after the dup2() calls, cleanup can accidentally
 
381
         * close our pipes which replaced any files which previously had
 
382
         * descriptors 0-2.
 
383
         *
 
384
         * The solution is to kill the cleanup for the pipes, then do
 
385
         * exec cleanup, then do the dup2() calls.
 
386
         */
 
387
 
 
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);
 
391
        }
 
392
 
 
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);
 
396
        }
 
397
 
 
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);
 
401
        }
 
402
 
 
403
        apr_pool_cleanup_for_exec();
 
404
 
 
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);
 
409
        }
 
410
 
 
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);
 
415
        }
 
416
 
 
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);
 
421
        }
 
422
 
 
423
        apr_signal(SIGCHLD, SIG_DFL); /* not sure if this is needed or not */
 
424
 
 
425
        if (attr->currdir != NULL) {
 
426
            if (chdir(attr->currdir) == -1) {
 
427
                if (attr->errfn) {
 
428
                    attr->errfn(pool, errno, "change of working directory failed");
 
429
                }
 
430
                exit(-1);   /* We have big problems, the child should exit. */
 
431
            }
 
432
        }
 
433
 
 
434
        /* Only try to switch if we are running as root */
 
435
        if (attr->gid != -1 && !geteuid()) {
 
436
            if ((status = setgid(attr->gid))) {
 
437
                if (attr->errfn) {
 
438
                    attr->errfn(pool, errno, "setting of group failed");
 
439
                }
 
440
                exit(-1);   /* We have big problems, the child should exit. */
 
441
            }
 
442
        }
 
443
 
 
444
        if (attr->uid != -1 && !geteuid()) {
 
445
            if ((status = setuid(attr->uid))) {
 
446
                if (attr->errfn) {
 
447
                    attr->errfn(pool, errno, "setting of user failed");
 
448
                }
 
449
                exit(-1);   /* We have big problems, the child should exit. */
 
450
            }
 
451
        }
 
452
 
 
453
        if ((status = limit_proc(attr)) != APR_SUCCESS) {
 
454
            if (attr->errfn) {
 
455
                attr->errfn(pool, errno, "setting of resource limits failed");
 
456
            }
 
457
            exit(-1);   /* We have big problems, the child should exit. */
 
458
        }
 
459
 
 
460
        if (attr->cmdtype == APR_SHELLCMD ||
 
461
            attr->cmdtype == APR_SHELLCMD_ENV) {
 
462
            int onearg_len = 0;
 
463
            const char *newargs[4];
 
464
 
 
465
            newargs[0] = SHELL_PATH;
 
466
            newargs[1] = "-c";
 
467
 
 
468
            i = 0;
 
469
            while (args[i]) {
 
470
                onearg_len += strlen(args[i]);
 
471
                onearg_len++; /* for space delimiter */
 
472
                i++;
 
473
            }
 
474
 
 
475
            switch(i) {
 
476
            case 0:
 
477
                /* bad parameters; we're doomed */
 
478
                break;
 
479
            case 1:
 
480
                /* no args, or caller already built a single string from
 
481
                 * progname and args
 
482
                 */
 
483
                newargs[2] = args[0];
 
484
                break;
 
485
            default:
 
486
            {
 
487
                char *ch, *onearg;
 
488
                
 
489
                ch = onearg = apr_palloc(pool, onearg_len);
 
490
                i = 0;
 
491
                while (args[i]) {
 
492
                    size_t len = strlen(args[i]);
 
493
 
 
494
                    memcpy(ch, args[i], len);
 
495
                    ch += len;
 
496
                    *ch = ' ';
 
497
                    ++ch;
 
498
                    ++i;
 
499
                }
 
500
                --ch; /* back up to trailing blank */
 
501
                *ch = '\0';
 
502
                newargs[2] = onearg;
 
503
            }
 
504
            }
 
505
 
 
506
            newargs[3] = NULL;
 
507
 
 
508
            if (attr->detached) {
 
509
                apr_proc_detach(APR_PROC_DETACH_DAEMONIZE);
 
510
            }
 
511
 
 
512
            if (attr->cmdtype == APR_SHELLCMD) {
 
513
                execve(SHELL_PATH, (char * const *) newargs, (char * const *)env);
 
514
            }
 
515
            else {
 
516
                execv(SHELL_PATH, (char * const *)newargs);
 
517
            }
 
518
        }
 
519
        else if (attr->cmdtype == APR_PROGRAM) {
 
520
            if (attr->detached) {
 
521
                apr_proc_detach(APR_PROC_DETACH_DAEMONIZE);
 
522
            }
 
523
 
 
524
            execve(progname, (char * const *)args, (char * const *)env);
 
525
        }
 
526
        else if (attr->cmdtype == APR_PROGRAM_ENV) {
 
527
            if (attr->detached) {
 
528
                apr_proc_detach(APR_PROC_DETACH_DAEMONIZE);
 
529
            }
 
530
 
 
531
            execv(progname, (char * const *)args);
 
532
        }
 
533
        else {
 
534
            /* APR_PROGRAM_PATH */
 
535
            if (attr->detached) {
 
536
                apr_proc_detach(APR_PROC_DETACH_DAEMONIZE);
 
537
            }
 
538
 
 
539
            execvp(progname, (char * const *)args);
 
540
        }
 
541
        if (attr->errfn) {
 
542
            char *desc;
 
543
 
 
544
            desc = apr_psprintf(pool, "exec of '%s' failed",
 
545
                                progname);
 
546
            attr->errfn(pool, errno, desc);
 
547
        }
 
548
 
 
549
        exit(-1);  /* if we get here, there is a problem, so exit with an
 
550
                    * error code. */
 
551
    }
 
552
 
 
553
    /* Parent process */
 
554
    if (attr->child_in) {
 
555
        apr_file_close(attr->child_in);
 
556
    }
 
557
 
 
558
    if (attr->child_out) {
 
559
        apr_file_close(attr->child_out);
 
560
    }
 
561
 
 
562
    if (attr->child_err) {
 
563
        apr_file_close(attr->child_err);
 
564
    }
 
565
 
 
566
    return APR_SUCCESS;
 
567
}
 
568
 
 
569
APR_DECLARE(apr_status_t) apr_proc_wait_all_procs(apr_proc_t *proc,
 
570
                                                  int *exitcode,
 
571
                                                  apr_exit_why_e *exitwhy,
 
572
                                                  apr_wait_how_e waithow,
 
573
                                                  apr_pool_t *p)
 
574
{
 
575
    proc->pid = -1;
 
576
    return apr_proc_wait(proc, exitcode, exitwhy, waithow);
 
577
}
 
578
 
 
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)
 
582
{
 
583
    pid_t pstatus;
 
584
    int waitpid_options = WUNTRACED;
 
585
    int exit_int;
 
586
    int ignore;
 
587
    apr_exit_why_e ignorewhy;
 
588
 
 
589
    if (exitcode == NULL) {
 
590
        exitcode = &ignore;
 
591
    }
 
592
 
 
593
    if (exitwhy == NULL) {
 
594
        exitwhy = &ignorewhy;
 
595
    }
 
596
 
 
597
    if (waithow != APR_WAIT) {
 
598
        waitpid_options |= WNOHANG;
 
599
    }
 
600
 
 
601
    do {
 
602
        pstatus = waitpid(proc->pid, &exit_int, waitpid_options);
 
603
    } while (pstatus < 0 && errno == EINTR);
 
604
 
 
605
    if (pstatus > 0) {
 
606
        proc->pid = pstatus;
 
607
 
 
608
        if (WIFEXITED(exit_int)) {
 
609
            *exitwhy = APR_PROC_EXIT;
 
610
            *exitcode = WEXITSTATUS(exit_int);
 
611
        }
 
612
        else if (WIFSIGNALED(exit_int)) {
 
613
            *exitwhy = APR_PROC_SIGNAL;
 
614
 
 
615
#ifdef WCOREDUMP
 
616
            if (WCOREDUMP(exit_int)) {
 
617
                *exitwhy |= APR_PROC_SIGNAL_CORE;
 
618
            }
 
619
#endif
 
620
 
 
621
            *exitcode = WTERMSIG(exit_int);
 
622
        }
 
623
        else {
 
624
            /* unexpected condition */
 
625
            return APR_EGENERAL;
 
626
        }
 
627
 
 
628
        return APR_CHILD_DONE;
 
629
    }
 
630
    else if (pstatus == 0) {
 
631
        return APR_CHILD_NOTDONE;
 
632
    }
 
633
 
 
634
    return errno;
 
635
}
 
636
 
 
637
APR_DECLARE(apr_status_t) apr_procattr_limit_set(apr_procattr_t *attr,
 
638
                                                 apr_int32_t what,
 
639
                                                 struct rlimit *limit)
 
640
{
 
641
    switch(what) {
 
642
        case APR_LIMIT_CPU:
 
643
#ifdef RLIMIT_CPU
 
644
            attr->limit_cpu = limit;
 
645
            break;
 
646
#else
 
647
            return APR_ENOTIMPL;
 
648
#endif
 
649
 
 
650
        case APR_LIMIT_MEM:
 
651
#if defined (RLIMIT_DATA) || defined (RLIMIT_VMEM) || defined(RLIMIT_AS)
 
652
            attr->limit_mem = limit;
 
653
            break;
 
654
#else
 
655
            return APR_ENOTIMPL;
 
656
#endif
 
657
 
 
658
        case APR_LIMIT_NPROC:
 
659
#ifdef RLIMIT_NPROC
 
660
            attr->limit_nproc = limit;
 
661
            break;
 
662
#else
 
663
            return APR_ENOTIMPL;
 
664
#endif
 
665
 
 
666
        case APR_LIMIT_NOFILE:
 
667
#ifdef RLIMIT_NOFILE
 
668
            attr->limit_nofile = limit;
 
669
            break;
 
670
#else
 
671
            return APR_ENOTIMPL;
 
672
#endif
 
673
 
 
674
    }
 
675
 
 
676
    return APR_SUCCESS;
 
677
}