[PATCH] -F (foreground) and -S (stdout logging) for nmbd, smbd, winbindd (daemontools)

Michael Handler handler-list-samba-technical at grendel.net
Tue Jun 4 08:36:03 GMT 2002


ok, here's the second revision of my patches to nmbd, smbd, and winbindd.
to summarize:

* the -F option makes each server program operate normally, but the
  main process doesn't fork & disassociate.

* the -S option makes each server program log to stdout, unbuffered.

the -F option is designed to permit nmbd, smbd, and winbindd to run under
Dan Bernstein's daemontools package (http://cr.yp.to/daemontools.html).

please note that the patch design is deliberate: setsid(2) must
still be called when in -F (foreground) mode, because the server
processes kill their process group during shutdown, and if they
don't create their own process group, that kills svscan, supervise,
and all other processes that are being controlled.

patches to the SGML documentation for nmbd, smbd, and winbindd. the patch
is generated against samba 2.2.4. i've included a copy below, but it's also
available on the web at:

http://www.sub-rosa.com/handler/pub/samba-2.2.4-daemontools-diffs-v2

please let me know if there are any issues that would prevent this
patch from being applied. thanks!

--michael

diff -ur samba-2.2.4-orig/docs/docbook/manpages/nmbd.8.sgml samba-2.2.4/docs/docbook/manpages/nmbd.8.sgml
--- samba-2.2.4-orig/docs/docbook/manpages/nmbd.8.sgml	Thu May  2 21:01:53 2002
+++ samba-2.2.4/docs/docbook/manpages/nmbd.8.sgml	Tue Jun  4 11:02:10 2002
@@ -17,6 +17,8 @@
 	<cmdsynopsis>
 		<command>nmbd</command>
 		<arg choice="opt">-D</arg>
+		<arg choice="opt">-F</arg>
+		<arg choice="opt">-S</arg>
 		<arg choice="opt">-a</arg>
 		<arg choice="opt">-i</arg>
 		<arg choice="opt">-o</arg>
@@ -88,6 +90,28 @@
 		</varlistentry>
 
 		<varlistentry>
+		<term>-F</term>
+		<listitem><para>If specified, this parameter causes
+		the main <command>nmbd</command> process to not daemonize,
+		i.e. double-fork and disassociate with the terminal.
+		Child processes are still created as normal to service
+		each connection request, but the main process does not
+		exit. This operation mode is suitable for running
+		<command>nmbd</command> under process supervisors such
+		as <command>supervise</command> and <command>svscan</command>
+		from Daniel J. Bernstein's <command>daemontools</command>
+		package, or the AIX process monitor.
+		</para></listitem>
+		</varlistentry>
+
+		<varlistentry>
+		<term>-S</term>
+		<listitem><para>If specified, this parameter causes
+		<command>nmbd</command> to log to standard output rather
+		than a file.</para></listitem>
+		</varlistentry>
+
+		<varlistentry>
 		<term>-a</term>
 		<listitem><para>If this parameter is specified, each new 
 		connection will append log messages to the log file.  
@@ -100,8 +124,9 @@
 		server to run "interactively", not as a daemon, even if the
 		server is executed on the command line of a shell. Setting this
 		parameter negates the implicit deamon mode when run from the
-		command line.
-		</para></listitem>
+		command line. <command>nmbd</command> also logs to standard
+		output, as if the <command>-S</command> parameter had been
+		given. </para></listitem>
 		</varlistentry>
 
 		<varlistentry>
diff -ur samba-2.2.4-orig/docs/docbook/manpages/smbd.8.sgml samba-2.2.4/docs/docbook/manpages/smbd.8.sgml
--- samba-2.2.4-orig/docs/docbook/manpages/smbd.8.sgml	Thu May  2 21:01:54 2002
+++ samba-2.2.4/docs/docbook/manpages/smbd.8.sgml	Tue Jun  4 11:01:31 2002
@@ -16,6 +16,8 @@
 	<cmdsynopsis>
 		<command>smbd</command>
 		<arg choice="opt">-D</arg>
+		<arg choice="opt">-F</arg>
+		<arg choice="opt">-S</arg>
 		<arg choice="opt">-a</arg>
 		<arg choice="opt">-i</arg>
 		<arg choice="opt">-o</arg>
@@ -89,6 +91,28 @@
 		</varlistentry>
 
 		<varlistentry>
+		<term>-F</term>
+		<listitem><para>If specified, this parameter causes
+		the main <command>smbd</command> process to not daemonize,
+		i.e. double-fork and disassociate with the terminal.
+		Child processes are still created as normal to service
+		each connection request, but the main process does not
+		exit. This operation mode is suitable for running
+		<command>smbd</command> under process supervisors such
+		as <command>supervise</command> and <command>svscan</command>
+		from Daniel J. Bernstein's <command>daemontools</command>
+		package, or the AIX process monitor.
+		</para></listitem>
+		</varlistentry>
+
+		<varlistentry>
+		<term>-S</term>
+		<listitem><para>If specified, this parameter causes
+		<command>smbd</command> to log to standard output rather
+		than a file.</para></listitem>
+		</varlistentry>
+
+		<varlistentry>
 		<term>-a</term>
 		<listitem><para>If this parameter is specified, each new 
 		connection will append log messages to the log file.  
@@ -101,8 +125,9 @@
 		server to run "interactively", not as a daemon, even if the
 		server is executed on the command line of a shell. Setting this
 		parameter negates the implicit deamon mode when run from the
-		command line.
-		</para></listitem>
+		command line. <command>smbd</command> also logs to standard
+		output, as if the <command>-S</command> parameter had been
+		given. </para></listitem>
 		</varlistentry>
 		
 		<varlistentry>
diff -ur samba-2.2.4-orig/docs/docbook/manpages/winbindd.8.sgml samba-2.2.4/docs/docbook/manpages/winbindd.8.sgml
--- samba-2.2.4-orig/docs/docbook/manpages/winbindd.8.sgml	Sat Feb  2 19:45:16 2002
+++ samba-2.2.4/docs/docbook/manpages/winbindd.8.sgml	Tue Jun  4 11:02:51 2002
@@ -16,6 +16,8 @@
 <refsynopsisdiv>
 	<cmdsynopsis>
 		<command>winbindd</command>
+		<arg choice="opt">-F</arg>
+		<arg choice="opt">-S</arg>
 		<arg choice="opt">-i</arg>
 		<arg choice="opt">-d &lt;debug level&gt;</arg>
 		<arg choice="opt">-s &lt;smb config file&gt;</arg>
@@ -104,6 +106,28 @@
 
 	<variablelist>
 		<varlistentry>
+		<term>-F</term>
+		<listitem><para>If specified, this parameter causes
+		the main <command>winbindd</command> process to not daemonize,
+		i.e. double-fork and disassociate with the terminal.
+		Child processes are still created as normal to service
+		each connection request, but the main process does not
+		exit. This operation mode is suitable for running
+		<command>winbindd</command> under process supervisors such
+		as <command>supervise</command> and <command>svscan</command>
+		from Daniel J. Bernstein's <command>daemontools</command>
+		package, or the AIX process monitor.
+		</para></listitem>
+		</varlistentry>
+
+		<varlistentry>
+		<term>-S</term>
+		<listitem><para>If specified, this parameter causes
+		<command>winbindd</command> to log to standard output rather
+		than a file.</para></listitem>
+		</varlistentry>
+
+		<varlistentry>
 		<term>-d debuglevel</term>
 		<listitem><para>Sets the debuglevel to an integer between 
 		0 and 100. 0 is for no debugging and 100 is for reams and 
@@ -116,7 +140,10 @@
 		<listitem><para>Tells <command>winbindd</command> to not 
 		become a daemon and detach from the current terminal. This 
 		option is used by developers when interactive debugging 
-		of <command>winbindd</command> is required. </para></listitem>
+		of <command>winbindd</command> is required.
+		<command>winbindd</command> also logs to standard output,
+		as if the <command>-S</command> parameter had been given.</para>
+		</listitem>
 		</varlistentry>
 	</variablelist>
 </refsect1>
diff -ur samba-2.2.4-orig/source/include/proto.h samba-2.2.4/source/include/proto.h
--- samba-2.2.4-orig/source/include/proto.h	Thu May  2 21:03:05 2002
+++ samba-2.2.4/source/include/proto.h	Tue Jun  4 10:02:39 2002
@@ -1125,7 +1125,7 @@
 						ssize_t (*write_fn)(int, const void *, size_t));
 SMB_OFF_T transfer_file(int infd,int outfd,SMB_OFF_T n);
 void msleep(unsigned int t);
-void become_daemon(void);
+void become_daemon(BOOL fork);
 BOOL yesno(char *p);
 void *Realloc(void *p,size_t size);
 void safe_free(void *p);
diff -ur samba-2.2.4-orig/source/lib/debug.c samba-2.2.4/source/lib/debug.c
--- samba-2.2.4-orig/source/lib/debug.c	Thu May  2 21:03:07 2002
+++ samba-2.2.4/source/lib/debug.c	Tue Jun  4 10:02:39 2002
@@ -291,6 +291,7 @@
 	if (interactive) {
 		stdout_logging = True;
 		dbf = stdout;
+		setbuf(stdout, NULL);
 	}
 #ifdef WITH_SYSLOG
 	else {
diff -ur samba-2.2.4-orig/source/lib/util.c samba-2.2.4/source/lib/util.c
--- samba-2.2.4-orig/source/lib/util.c	Thu May  2 21:03:11 2002
+++ samba-2.2.4/source/lib/util.c	Tue Jun  4 10:02:39 2002
@@ -607,10 +607,12 @@
  Become a daemon, discarding the controlling terminal.
 ****************************************************************************/
 
-void become_daemon(void)
+void become_daemon(BOOL fork)
 {
-	if (sys_fork()) {
-		_exit(0);
+	if (fork) {
+		if (sys_fork()) {
+			_exit(0);
+		}
 	}
 
   /* detach from the terminal */
@@ -626,8 +628,10 @@
 	}
 #endif /* HAVE_SETSID */
 
-	/* Close fd's 0,1,2. Needed if started by rsh */
-	close_low_fds();
+	if (fork) {
+		/* Close fd's 0,1,2. Needed if started by rsh */
+		close_low_fds();
+	}
 }
 
 /****************************************************************************
diff -ur samba-2.2.4-orig/source/nmbd/nmbd.c samba-2.2.4/source/nmbd/nmbd.c
--- samba-2.2.4-orig/source/nmbd/nmbd.c	Thu May  2 21:03:17 2002
+++ samba-2.2.4/source/nmbd/nmbd.c	Tue Jun  4 10:02:39 2002
@@ -643,9 +643,11 @@
 static void usage(char *pname)
 {
 
-  printf( "Usage: %s [-DaiohV] [-H lmhosts file] [-d debuglevel] [-l log basename]\n", pname );
+  printf( "Usage: %s [-DFSaiohV] [-H lmhosts file] [-d debuglevel] [-l log basename]\n", pname );
   printf( "       [-n name] [-p port] [-s configuration file]\n" );
   printf( "\t-D                    Become a daemon (default)\n" );
+  printf( "\t-F                    Run daemon in foreground (for daemontools, etc)\n" );
+  printf( "\t-S                    Log to stdout\n" );
   printf( "\t-a                    Append to log file (default)\n" );
   printf( "\t-i                    Run interactive (not a daemon)\n" );
   printf( "\t-o                    Overwrite log file, don't append\n" );
@@ -673,6 +675,8 @@
   extern BOOL  append_log;
   extern BOOL AllowDebugChange;
   BOOL opt_interactive = False;
+  BOOL fork = True;
+  BOOL log_stdout = False;
   pstring logfile;
 
   append_log = True;  /* Default, override with '-o' option. */
@@ -725,10 +729,16 @@
 #endif
 
   while( EOF != 
-         (opt = getopt( argc, argv, "Vaos:T:I:C:bAB:N:Rn:l:d:Dip:hSH:G:f:" )) )
+         (opt = getopt( argc, argv, "FSVaos:T:I:C:bAB:N:Rn:l:d:Dip:hH:G:f:" )) )
     {
       switch (opt)
         {
+        case 'F':
+          fork = False;
+          break;
+        case 'S':
+          log_stdout = True;
+          break;
         case 's':
           pstrcpy(servicesf,optarg);
           break;          
@@ -758,6 +768,8 @@
           break;
         case 'i':
           opt_interactive = True;
+          log_stdout = True;
+          fork = False; /* for stdout logging check below */
           break;
         case 'D':
           is_daemon = True;
@@ -788,7 +800,13 @@
         }
     }
 
-  setup_logging( argv[0], opt_interactive );
+  if (log_stdout && fork) {
+    printf("Can't log to stdout (-S) unless daemon is in foreground (-F) or interactive (-i)\n");
+    usage(argv[0]);
+    exit(1);
+  }
+
+  setup_logging( argv[0], log_stdout );
   reopen_logs();
 
   DEBUG( 0, ( "Netbios nameserver version %s started.\n", VERSION ) );
@@ -830,7 +848,7 @@
   if (is_daemon && !opt_interactive)
   {
     DEBUG( 2, ( "Becoming a daemon.\n" ) );
-    become_daemon();
+    become_daemon(fork);
   }
 
 #if HAVE_SETPGID
diff -ur samba-2.2.4-orig/source/nsswitch/winbindd.c samba-2.2.4/source/nsswitch/winbindd.c
--- samba-2.2.4-orig/source/nsswitch/winbindd.c	Thu May  2 21:03:20 2002
+++ samba-2.2.4/source/nsswitch/winbindd.c	Tue Jun  4 10:02:39 2002
@@ -666,6 +666,8 @@
 static void usage(void)
 {
 	printf("Usage: winbindd [options]\n");
+	printf("\t-F                daemon in foreground mode\n");
+	printf("\t-S                log to stdout\n");
 	printf("\t-i                interactive mode\n");
 	printf("\t-n                disable cacheing\n");
 	printf("\t-d level          set debug level\n");
@@ -682,6 +684,8 @@
 	pstring logfile;
 	int accept_sock;
 	BOOL interactive = False;
+	BOOL fork = True;
+	BOOL log_stdout = False;
 	int opt;
 
 	/* glibc (?) likes to print "User defined signal 1" and exit if a
@@ -707,12 +711,20 @@
 
 	/* Initialise samba/rpc client stuff */
 
-	while ((opt = getopt(argc, argv, "id:s:nh")) != EOF) {
+	while ((opt = getopt(argc, argv, "FSid:s:nh")) != EOF) {
 		switch (opt) {
 
+		case 'F':
+			fork = False;
+			break;
+		case 'S':
+			log_stdout = True;
+			break;
 			/* Don't become a daemon */
 		case 'i':
 			interactive = True;
+			log_stdout = True;
+			fork = False; /* for stdout logging check below */
 			break;
 
 			/* disable cacheing */
@@ -741,6 +753,13 @@
 		}
 	}
 
+        if (log_stdout && fork) {
+                printf("Can't log to stdout (-S) unless daemon is in foreground
+(-F) or interactive (-i)\n");
+                usage();
+                exit(1);
+        }
+
 	/* Append to log file by default as we are a single process daemon
 	   program. */
 
@@ -749,7 +768,7 @@
 	snprintf(logfile, sizeof(logfile), "%s/log.winbindd", LOGFILEBASE);
 	lp_set_logfile(logfile);
 
-	setup_logging("winbindd", interactive);
+	setup_logging("winbindd", log_stdout);
 	reopen_logs();
 
 	DEBUG(1, ("winbindd version %s started.\n", VERSION ) );
@@ -778,7 +797,7 @@
         fstrcpy(global_myworkgroup, lp_workgroup());
 
 	if (!interactive)
-		become_daemon();
+		become_daemon(fork);
 
 #if HAVE_SETPGID
 	/*
diff -ur samba-2.2.4-orig/source/smbd/server.c samba-2.2.4/source/smbd/server.c
--- samba-2.2.4-orig/source/smbd/server.c	Thu May  2 21:03:49 2002
+++ samba-2.2.4/source/smbd/server.c	Tue Jun  4 10:02:39 2002
@@ -536,9 +536,11 @@
 static void usage(char *pname)
 {
 
-	printf("Usage: %s [-DaioPh?V] [-d debuglevel] [-l log basename] [-p port]\n", pname);
+	printf("Usage: %s [-DFSaioPh?V] [-d debuglevel] [-l log basename] [-p port]\n", pname);
 	printf("       [-O socket options] [-s services file]\n");
 	printf("\t-D                    Become a daemon (default)\n");
+	printf("\t-F                    Run daemon in foreground (for daemontools, etc)\n");
+	printf("\t-S                    Log to stdout\n");
 	printf("\t-a                    Append to log file (default)\n");
 	printf("\t-i                    Run interactive (not a daemon)\n");
 	printf("\t-o                    Overwrite log file, don't append\n");
@@ -563,6 +565,8 @@
 	extern BOOL AllowDebugChange;
 	/* shall I run as a daemon */
 	BOOL is_daemon = False;
+	BOOL fork = True;
+	BOOL log_stdout = False;
 	BOOL interactive = False;
 	BOOL specified_logfile = False;
 	int port = SMB_PORT;
@@ -580,7 +584,7 @@
 		argc--;
 	}
 
-	while ( EOF != (opt = getopt(argc, argv, "O:l:s:d:Dip:h?Vaof:")) )
+	while ( EOF != (opt = getopt(argc, argv, "O:l:s:d:DFSip:h?Vaof:")) )
 		switch (opt)  {
 		case 'O':
 			pstrcpy(user_socket_options,optarg);
@@ -608,8 +612,18 @@
 			is_daemon = True;
 			break;
 
+		case 'F':
+			fork = False;
+			break;
+
+		case 'S':
+			log_stdout = True;
+			break;
+
 		case 'i':
 			interactive = True;
+			log_stdout = True;
+			fork = False; /* for stdout logging check below */
 			break;
 
 		case 'd':
@@ -640,6 +654,12 @@
 			exit(1);
 		}
 
+	if (log_stdout && fork) {
+		printf("Can't log to stdout (-S) unless daemon is in foreground (-F) or interactive (-i)\n");
+		usage(argv[0]);
+		exit(1);
+	}
+
 #ifdef HAVE_SETLUID
 	/* needed for SecureWare on SCO */
 	setluid(0);
@@ -658,7 +678,7 @@
 
 	pstrcpy(remote_machine, "smbd");
 
-	setup_logging(argv[0],interactive);
+	setup_logging(argv[0],log_stdout);
 
 	charset_initialise();
 
@@ -760,7 +780,7 @@
 
 	if (is_daemon && !interactive) {
 		DEBUG( 3, ( "Becoming a daemon.\n" ) );
-		become_daemon();
+		become_daemon(fork);
 	}
 
 #if HAVE_SETPGID
diff -ur samba-2.2.4-orig/source/web/startstop.c samba-2.2.4/source/web/startstop.c
--- samba-2.2.4-orig/source/web/startstop.c	Mon Dec 13 08:27:57 1999
+++ samba-2.2.4/source/web/startstop.c	Tue Jun  4 10:02:39 2002
@@ -39,7 +39,7 @@
 
 	slprintf(binfile, sizeof(pstring) - 1, "%s/smbd", SBINDIR);
 
-	become_daemon();
+	become_daemon(True);
 
 	execl(binfile, binfile, "-D", NULL);
 
@@ -60,7 +60,7 @@
 
 	slprintf(binfile, sizeof(pstring) - 1, "%s/nmbd", SBINDIR);
 	
-	become_daemon();
+	become_daemon(True);
 
 	execl(binfile, binfile, "-D", NULL);
 




More information about the samba-technical mailing list