upload http
[bottlenecks.git] / rubbos / app / httpd-2.0.64 / srclib / apr / threadproc / unix / procsup.c
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
19 APR_DECLARE(apr_status_t) apr_proc_detach(int daemonize)
20 {
21     int x;
22
23     chdir("/");
24 #if !defined(MPE) && !defined(OS2) && !defined(TPF) && !defined(BEOS)
25     /* Don't detach for MPE because child processes can't survive the death of
26      * the parent. */
27     if (daemonize) {
28             if ((x = fork()) > 0) {
29                 exit(0);
30         }
31             else if (x == -1) {
32                 perror("fork");
33                 fprintf(stderr, "unable to fork new process\n");
34                 exit(1);  /* we can't do anything here, so just exit. */
35             }
36             /* RAISE_SIGSTOP(DETACH); */
37     }
38 #endif
39
40 #ifdef HAVE_SETSID
41     /* A setsid() failure is not fatal if we didn't just fork().
42      * The calling process may be the process group leader, in
43      * which case setsid() will fail with EPERM.
44      */
45     if (setsid() == -1 && daemonize) {
46         return errno;
47     }
48 #elif defined(NEXT) || defined(NEWSOS)
49     if (setpgrp(0, getpid()) == -1) {
50         return errno;
51     }
52 #elif defined(OS2) || defined(TPF) || defined(MPE)
53     /* do nothing */
54 #else
55     if (setpgid(0, 0) == -1) {
56         return errno;
57     }
58 #endif
59
60     /* close out the standard file descriptors */
61     if (freopen("/dev/null", "r", stdin) == NULL) {
62         return errno;
63         /* continue anyhow -- note we can't close out descriptor 0 because we
64          * have nothing to replace it with, and if we didn't have a descriptor
65          * 0 the next file would be created with that value ... leading to
66          * havoc.
67          */
68     }
69     if (freopen("/dev/null", "w", stdout) == NULL) {
70         return errno;
71     }
72      /* We are going to reopen this again in a little while to the error
73       * log file, but better to do it twice and suffer a small performance
74       * hit for consistancy than not reopen it here.
75       */
76     if (freopen("/dev/null", "w", stderr) == NULL) {
77         return errno;
78     }
79     return APR_SUCCESS;
80 }
81
82 #if (!HAVE_WAITPID)
83 /* From ikluft@amdahl.com
84  * this is not ideal but it works for SVR3 variants
85  * Modified by dwd@bell-labs.com to call wait3 instead of wait because
86  *   apache started to use the WNOHANG option.
87  */
88 int waitpid(pid_t pid, int *statusp, int options)
89 {
90     int tmp_pid;
91     if (kill(pid, 0) == -1) {
92         errno = ECHILD;
93         return -1;
94     }
95     while (((tmp_pid = wait3(statusp, options, 0)) != pid) &&
96                 (tmp_pid != -1) && (tmp_pid != 0) && (pid != -1))
97         ;
98     return tmp_pid;
99 }
100 #endif
101