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