From 203f5b058f561ce2713e0b58b5096bff0030524d Mon Sep 17 00:00:00 2001
From: Hallvard Furuseth <hallvard@openldap.org>
Date: Sat, 4 Sep 1999 20:24:40 +0000
Subject: [PATCH] Change -d option to not detach even with -d 0 or compiled
 with --disable-debug.

---
 doc/man/man8/slapd.8     |  5 ++---
 doc/man/man8/slurpd.8    |  2 +-
 servers/ldapd/main.c     | 13 ++++++-------
 servers/slapd/main.c     | 20 ++++++++------------
 servers/slurpd/args.c    | 18 ++++++++++++------
 servers/slurpd/globals.c |  1 +
 servers/slurpd/globals.h |  2 ++
 servers/slurpd/main.c    | 10 +++-------
 8 files changed, 35 insertions(+), 36 deletions(-)

diff --git a/doc/man/man8/slapd.8 b/doc/man/man8/slapd.8
index ddd811b8a8..6fc43e11ba 100644
--- a/doc/man/man8/slapd.8
+++ b/doc/man/man8/slapd.8
@@ -38,8 +38,7 @@ file ( see
 ).
 If the
 .B \-d
-flag is given and debugging is set to some non-zero
-value,
+flag is given, even with a zero argument,
 .B slapd
 will not fork and disassociate from the invoking tty.
 .LP
@@ -59,7 +58,7 @@ See "The SLAPD and SLURPD Administrator's Guide" for more details on
 .BI \-d " debug\-level"
 Turn on debugging as defined by
 .I debug\-level.
-If this option is specified,
+If this option is specified, even with a zero argument,
 .B slapd
 will not fork or disassociate from the invoking terminal.  Some general
 operation and status messages are printed for any value of \fIdebug\-level\fP.
diff --git a/doc/man/man8/slurpd.8 b/doc/man/man8/slurpd.8
index d2f7e4945a..51c8c14b0d 100644
--- a/doc/man/man8/slurpd.8
+++ b/doc/man/man8/slurpd.8
@@ -62,7 +62,7 @@ for details on the standalone LDAP daemon.
 .BI \-d " debug\-level"
 Turn on debugging as defined by
 .I debug\-level.
-If this option is specified,
+If this option is specified, even with a zero argument,
 .B slurpd
 will not fork or disassociate from the invoking terminal.  Some general
 operation and status messages are printed for any value of \fIdebug\-level\fP.
diff --git a/servers/ldapd/main.c b/servers/ldapd/main.c
index 7f819084bb..b013dffd3e 100644
--- a/servers/ldapd/main.c
+++ b/servers/ldapd/main.c
@@ -106,6 +106,7 @@ main( int argc, char **argv )
 	int			udps;
 #endif
 	int			myport = LDAP_PORT;
+	int			no_detach = 0;
 	int			i, pid, socktype;
 	char			*myname;
 	fd_set			readfds;
@@ -150,13 +151,15 @@ main( int argc, char **argv )
 			dsapargc = 3;
 			break;
 
-		case 'd':	/* turn on debugging */
+		case 'd':	/* set debug level and 'do not detach' flag */
+			no_detach = 1;
 #ifdef LDAP_DEBUG
 			ldap_debug = atoi( optarg );
 			if ( ldap_debug & LDAP_DEBUG_PACKETS )
 				ber_set_option( NULL, LBER_OPT_DEBUG_LEVEL, &ldap_debug );
 #else
-			fprintf( stderr, "Not compiled with -DLDAP_DEBUG!\n" );
+			if ( atoi( optarg ) != 0 )
+				fputs( "Not compiled with -DLDAP_DEBUG!\n", stderr );
 #endif
 			break;
 
@@ -261,11 +264,7 @@ main( int argc, char **argv )
 		setproctitle( "initializing" );
 #endif
 #ifndef VMS
-#  ifdef LDAP_DEBUG
-		lutil_detach( ldap_debug, 1 );
-#  else
-		lutil_detach( 0, 1 );
-#  endif
+		lutil_detach( no_detach, 1 );
 #endif
 		(void) SIGNAL( SIGCHLD, wait4child );
 		(void) SIGNAL( SIGINT, log_and_exit );
diff --git a/servers/slapd/main.c b/servers/slapd/main.c
index 8a537546b3..1c08e6bd41 100644
--- a/servers/slapd/main.c
+++ b/servers/slapd/main.c
@@ -132,7 +132,7 @@ void WINAPI ServiceMain( DWORD argc, LPTSTR *argv )
 int main( int argc, char **argv )
 #endif
 {
-	int		i;
+	int		i, no_detach = 0;
 	int		rc;
 	char *urls = NULL;
 #if defined(HAVE_SETUID) && defined(HAVE_SETGID)
@@ -233,16 +233,16 @@ int main( int argc, char **argv )
 			urls = ch_strdup( optarg );
             break;
 
+		case 'd':	/* set debug level and 'do not detach' flag */
+			no_detach = 1;
 #ifdef LDAP_DEBUG
-		case 'd':	/* turn on debugging */
 			slap_debug |= atoi( optarg );
-			break;
 #else
-		case 'd':	/* turn on debugging */
-			fprintf( stderr,
-			    "must compile with LDAP_DEBUG for debugging\n" );
-			break;
+			if ( atoi( optarg ) != 0 )
+				fputs( "must compile with LDAP_DEBUG for debugging\n",
+				       stderr );
 #endif
+			break;
 
 		case 'f':	/* read config file */
 			configfile = ch_strdup( optarg );
@@ -389,11 +389,7 @@ int main( int argc, char **argv )
 #endif
 
 #ifndef HAVE_WINSOCK
-#ifdef LDAP_DEBUG
-		lutil_detach( ldap_debug, 0 );
-#else
-		lutil_detach( 0, 0 );
-#endif
+		lutil_detach( no_detach, 0 );
 #endif /* HAVE_WINSOCK */
 
 #ifdef CSRIMALLOC
diff --git a/servers/slurpd/args.c b/servers/slurpd/args.c
index 8927be57ef..d6ebe5d772 100644
--- a/servers/slurpd/args.c
+++ b/servers/slurpd/args.c
@@ -65,9 +65,10 @@ doargs(
 
     while ( (i = getopt( argc, argv, "hd:f:r:t:k:o" )) != EOF ) {
 	switch ( i ) {
-	case 'd':	/* turn on debugging */
-#ifdef LDAP_DEBUG
+	case 'd':	/* set debug level and 'do not detach' flag */
+	    g->no_detach = 1;
 	    if ( optarg[0] == '?' ) {
+#ifdef LDAP_DEBUG
 		printf( "Debug levels:\n" );
 		printf( "\tLDAP_DEBUG_TRACE\t%d\n",
 			LDAP_DEBUG_TRACE );
@@ -87,13 +88,18 @@ doargs(
 			LDAP_DEBUG_ACL );
 		printf( "\tLDAP_DEBUG_ANY\t\t%d\n",
 			LDAP_DEBUG_ANY );
+		puts( "\tThe -d flag also prevents slurpd from detaching." );
+#endif /* LDAP_DEBUG */
+		puts( "\tDebugging is disabled.  -d 0 prevents slurpd from detaching." );
 		return( -1 );
-	    } else {
-		ldap_debug |= atoi( optarg );
 	    }
-#else /* LDAP_DEBUG */
+#ifdef LDAP_DEBUG
+	    ldap_debug |= atoi( optarg );
+#else /* !LDAP_DEBUG */
+	    if ( atoi( optarg ) != 0 )
 		/* can't enable debugging - not built with debug code */
-	    fprintf( stderr, "must compile with LDAP_DEBUG for debugging\n" );
+		fputs( "must compile with LDAP_DEBUG for debugging\n",
+		       stderr );
 #endif /* LDAP_DEBUG */
 	    break;
 	case 'f':	/* slapd config file */
diff --git a/servers/slurpd/globals.c b/servers/slurpd/globals.c
index 1871085b66..14e510a416 100644
--- a/servers/slurpd/globals.c
+++ b/servers/slurpd/globals.c
@@ -55,6 +55,7 @@ init_globals( void )
     g->slurpd_replogfile[ 0 ] = '\0';
     g->slurpd_status_file[ 0 ] = '\0';
     g->one_shot_mode = 0;
+    g->no_detach = 0;
     g->myname = NULL;
     g->srpos = 0L;
     if ( St_init( &(g->st)) < 0 ) {
diff --git a/servers/slurpd/globals.h b/servers/slurpd/globals.h
index 955ce6467a..4e05074384 100644
--- a/servers/slurpd/globals.h
+++ b/servers/slurpd/globals.h
@@ -44,6 +44,8 @@ typedef struct globals {
     char slurpd_replogfile[ MAXPATHLEN ];
     /* Non-zero if we were given a replog file to process on command-line */
     int	one_shot_mode;
+    /* Non-zero if we should not detach the process */
+    int no_detach;
     /* Name of program */
     char *myname;
     /* Current offset into slurpd replica logfile */
diff --git a/servers/slurpd/main.c b/servers/slurpd/main.c
index c2a71484fa..5e26628fcf 100644
--- a/servers/slurpd/main.c
+++ b/servers/slurpd/main.c
@@ -87,14 +87,10 @@ main(
     }
 
     /*
-     * Detach from the controlling terminal, if debug level = 0,
-     * and if not in one-shot mode.
+     * Detach from the controlling terminal
+     * unless the -d flag is given or in one-shot mode.
      */
-#ifdef LDAP_DEBUG
-    if (( ldap_debug == 0 )  && !sglob->one_shot_mode )
-#else /* LDAP_DEBUG */
-    if ( !sglob->one_shot_mode )
-#endif /* LDAP_DEBUG */
+    if ( ! (sglob->no_detach || sglob->one_shot_mode) )
 	lutil_detach( 0, 0 );
 
     /*
-- 
GitLab