aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAaron Bannert <aaron@apache.org>2002-02-21 18:45:07 +0000
committerAaron Bannert <aaron@apache.org>2002-02-21 18:45:07 +0000
commit2d0871e8b363db23b86b493d99b51e416baf85db (patch)
tree9d62a3c030f4ed39631c4e85930e1de08d07c6ed
parent8078b110e1b3c579377327cc7cd0eb600c7a4096 (diff)
downloadapr-2d0871e8b363db23b86b493d99b51e416baf85db.tar.gz
apr-2d0871e8b363db23b86b493d99b51e416baf85db.zip
Change apr_proc_detach to take a parameter that can enable/disable automatic
forking (aka, to "daemonize"). Detailed explanation: If we are only interested in detaching from the controlling terminal, then we are only interested in creating a new process group (or creating a new session, which implicitly creates a new process group). In order to do so, we must _NOT_ already be a process group leader. The only way to ensure that is true, we normally will call fork() and allow the parent to exit, ensuring that the child is at least a child of a process group leader (and not one itself). Doing this by default prevents some process-watching tools from working with Apache. Therefore, when calling apr_proc_detach with APR_PROC_DETACH_FOREGROUND, the caller is taking responsibility for _NOT_ being a process group leader, which is guaranteed by such process management tools. [A similiar patch was originally submitted Jos and later modifed by Aaron.] Obtained from: Jos Backus <josb@cncdsl.com> Submitted by: Aaron Bannert git-svn-id: https://svn.apache.org/repos/asf/apr/apr/trunk@63044 13f79535-47bb-0310-9956-ffa450edef68
-rw-r--r--include/apr_thread_proc.h8
-rw-r--r--threadproc/netware/procsup.c2
-rw-r--r--threadproc/os2/proc.c2
-rw-r--r--threadproc/unix/proc.c8
-rw-r--r--threadproc/unix/procsup.c24
5 files changed, 27 insertions, 17 deletions
diff --git a/include/apr_thread_proc.h b/include/apr_thread_proc.h
index d8598e0f3..d6e01aab9 100644
--- a/include/apr_thread_proc.h
+++ b/include/apr_thread_proc.h
@@ -578,10 +578,16 @@ APR_DECLARE(apr_status_t) apr_proc_wait_all_procs(apr_proc_t *proc,
apr_wait_how_e waithow,
apr_pool_t *p);
+#define APR_PROC_DETACH_FOREGROUND 0
+#define APR_PROC_DETACH_DAEMONIZE 1
+
/**
* Detach the process from the controlling terminal.
+ * @param daemonize set to non-zero if the process should daemonize
+ * and become a background process, else it will
+ * stay in the foreground.
*/
-APR_DECLARE(apr_status_t) apr_proc_detach(void);
+APR_DECLARE(apr_status_t) apr_proc_detach(int daemonize);
#if APR_HAS_OTHER_CHILD
diff --git a/threadproc/netware/procsup.c b/threadproc/netware/procsup.c
index e7a3d69a4..daa1b8dc7 100644
--- a/threadproc/netware/procsup.c
+++ b/threadproc/netware/procsup.c
@@ -54,7 +54,7 @@
#include "threadproc.h"
-apr_status_t apr_proc_detach(void)
+apr_status_t apr_proc_detach(int daemonize)
{
#if 0
int x;
diff --git a/threadproc/os2/proc.c b/threadproc/os2/proc.c
index 08af08794..fdb2841ca 100644
--- a/threadproc/os2/proc.c
+++ b/threadproc/os2/proc.c
@@ -622,7 +622,7 @@ APR_DECLARE(apr_status_t) apr_get_os_proc(apr_os_proc_t *theproc, apr_proc_t *pr
-APR_DECLARE(apr_status_t) apr_proc_detach()
+APR_DECLARE(apr_status_t) apr_proc_detach(int daemonize)
{
return APR_ENOTIMPL;
}
diff --git a/threadproc/unix/proc.c b/threadproc/unix/proc.c
index ff80f2cfe..deb188a58 100644
--- a/threadproc/unix/proc.c
+++ b/threadproc/unix/proc.c
@@ -362,26 +362,26 @@ APR_DECLARE(apr_status_t) apr_proc_create(apr_proc_t *new, const char *progname,
}
newargs[i + 2] = NULL;
if (attr->detached) {
- apr_proc_detach();
+ apr_proc_detach(APR_PROC_DETACH_DAEMONIZE);
}
execve(SHELL_PATH, (char * const *) newargs, (char * const *)env);
}
else if (attr->cmdtype == APR_PROGRAM) {
if (attr->detached) {
- apr_proc_detach();
+ apr_proc_detach(APR_PROC_DETACH_DAEMONIZE);
}
execve(progname, (char * const *)args, (char * const *)env);
}
else if (attr->cmdtype == APR_PROGRAM_ENV) {
if (attr->detached) {
- apr_proc_detach();
+ apr_proc_detach(APR_PROC_DETACH_DAEMONIZE);
}
execv(progname, (char * const *)args);
}
else {
/* APR_PROGRAM_PATH */
if (attr->detached) {
- apr_proc_detach();
+ apr_proc_detach(APR_PROC_DETACH_DAEMONIZE);
}
execvp(progname, (char * const *)args);
}
diff --git a/threadproc/unix/procsup.c b/threadproc/unix/procsup.c
index b96cb48ec..391f803bd 100644
--- a/threadproc/unix/procsup.c
+++ b/threadproc/unix/procsup.c
@@ -54,24 +54,28 @@
#include "threadproc.h"
-APR_DECLARE(apr_status_t) apr_proc_detach(void)
+APR_DECLARE(apr_status_t) apr_proc_detach(int daemonize)
{
int x;
pid_t pgrp;
chdir("/");
#if !defined(MPE) && !defined(OS2) && !defined(TPF) && !defined(BEOS)
-/* Don't detach for MPE because child processes can't survive the death of
- the parent. */
- if ((x = fork()) > 0)
- exit(0);
- else if (x == -1) {
- perror("fork");
- fprintf(stderr, "unable to fork new process\n");
- exit(1); /* we can't do anything here, so just exit. */
+ /* Don't detach for MPE because child processes can't survive the death of
+ * the parent. */
+ if (daemonize) {
+ if ((x = fork()) > 0) {
+ exit(0);
+ }
+ else if (x == -1) {
+ perror("fork");
+ fprintf(stderr, "unable to fork new process\n");
+ exit(1); /* we can't do anything here, so just exit. */
+ }
+ /* RAISE_SIGSTOP(DETACH); */
}
-/* RAISE_SIGSTOP(DETACH);*/
#endif
+
#ifdef HAVE_SETSID
if ((pgrp = setsid()) == -1) {
return errno;