From 2d0871e8b363db23b86b493d99b51e416baf85db Mon Sep 17 00:00:00 2001 From: Aaron Bannert Date: Thu, 21 Feb 2002 18:45:07 +0000 Subject: 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 Submitted by: Aaron Bannert git-svn-id: https://svn.apache.org/repos/asf/apr/apr/trunk@63044 13f79535-47bb-0310-9956-ffa450edef68 --- threadproc/unix/procsup.c | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) (limited to 'threadproc/unix/procsup.c') 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; -- cgit v1.2.3