You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@apr.apache.org by aa...@apache.org on 2002/02/21 19:45:07 UTC
cvs commit: apr/threadproc/unix proc.c procsup.c
aaron 02/02/21 10:45:07
Modified: include apr_thread_proc.h
threadproc/netware procsup.c
threadproc/os2 proc.c
threadproc/unix proc.c procsup.c
Log:
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 <jo...@cncdsl.com>
Submitted by: Aaron Bannert
Revision Changes Path
1.81 +7 -1 apr/include/apr_thread_proc.h
Index: apr_thread_proc.h
===================================================================
RCS file: /home/cvs/apr/include/apr_thread_proc.h,v
retrieving revision 1.80
retrieving revision 1.81
diff -u -r1.80 -r1.81
--- apr_thread_proc.h 15 Feb 2002 21:15:44 -0000 1.80
+++ apr_thread_proc.h 21 Feb 2002 18:45:07 -0000 1.81
@@ -578,10 +578,16 @@
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
1.2 +1 -1 apr/threadproc/netware/procsup.c
Index: procsup.c
===================================================================
RCS file: /home/cvs/apr/threadproc/netware/procsup.c,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- procsup.c 2 Aug 2001 20:29:14 -0000 1.1
+++ procsup.c 21 Feb 2002 18:45:07 -0000 1.2
@@ -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;
1.49 +1 -1 apr/threadproc/os2/proc.c
Index: proc.c
===================================================================
RCS file: /home/cvs/apr/threadproc/os2/proc.c,v
retrieving revision 1.48
retrieving revision 1.49
diff -u -r1.48 -r1.49
--- proc.c 28 Jan 2002 21:58:15 -0000 1.48
+++ proc.c 21 Feb 2002 18:45:07 -0000 1.49
@@ -622,7 +622,7 @@
-APR_DECLARE(apr_status_t) apr_proc_detach()
+APR_DECLARE(apr_status_t) apr_proc_detach(int daemonize)
{
return APR_ENOTIMPL;
}
1.56 +4 -4 apr/threadproc/unix/proc.c
Index: proc.c
===================================================================
RCS file: /home/cvs/apr/threadproc/unix/proc.c,v
retrieving revision 1.55
retrieving revision 1.56
diff -u -r1.55 -r1.56
--- proc.c 28 Jan 2002 21:58:15 -0000 1.55
+++ proc.c 21 Feb 2002 18:45:07 -0000 1.56
@@ -362,26 +362,26 @@
}
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);
}
1.34 +14 -10 apr/threadproc/unix/procsup.c
Index: procsup.c
===================================================================
RCS file: /home/cvs/apr/threadproc/unix/procsup.c,v
retrieving revision 1.33
retrieving revision 1.34
diff -u -r1.33 -r1.34
--- procsup.c 28 Dec 2001 19:03:48 -0000 1.33
+++ procsup.c 21 Feb 2002 18:45:07 -0000 1.34
@@ -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;