patch to include support for daemontools from Michael Handler
authorGerald Carter <jerry@samba.org>
Fri, 3 Jan 2003 17:32:11 +0000 (17:32 +0000)
committerGerald Carter <jerry@samba.org>
Fri, 3 Jan 2003 17:32:11 +0000 (17:32 +0000)
docs/docbook/manpages/nmbd.8.sgml
docs/docbook/manpages/smbd.8.sgml
docs/docbook/manpages/winbindd.8.sgml
source/lib/debug.c
source/lib/util.c
source/nmbd/nmbd.c
source/nsswitch/winbindd.c
source/smbd/server.c
source/web/startstop.c
source/wrepld/server.c

index b8986110a6b37b2b36a0850b82c43700a0248da7..db920c79a1cb9aaafb69c3faa88ece2bafc5764a 100644 (file)
@@ -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>
                </para></listitem>
                </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 
                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 daemon 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>
index 4ae8b3148be6e8dd4393af1d489ed6bab71a495d..6dfb0f57463bfb141b72085d15474a297e7f8fdc 100644 (file)
@@ -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">-i</arg>
                <arg choice="opt">-h</arg>
                <arg choice="opt">-V</arg>
                </para></listitem>
                </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>-i</term>
                <listitem><para>If this parameter is specified it causes the
                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.
+               command line. <command>smbd</command> also logs to standard
+               output, as if the <command>-S</command> parameter had been
+               given. </para></listitem>
                </para></listitem>
                </varlistentry>
                
index 848f6ed3fac4c2ccd3b15f0c44853015bde87bcc..ccef2fa62316017b37aebc016b405d3a83545877 100644 (file)
@@ -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">-B</arg>
                <arg choice="opt">-d &lt;debug level&gt;</arg>
@@ -105,6 +107,28 @@ group:          files winbind
        <title>OPTIONS</title>
 
        <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 
@@ -118,7 +142,10 @@ group:          files winbind
                <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>
 
                <varlistentry>
index 0836dbe7900cb642a4a7ac3f9a39b2b7fcdfda6a..2efdd3c2a3cdcc0f517caf2f2dcd3171e7070503 100644 (file)
@@ -525,6 +525,7 @@ void setup_logging(const char *pname, BOOL interactive)
        if (interactive) {
                stdout_logging = True;
                dbf = x_stdout;
+               x_setbuf( x_stdout, NULL );
        }
 #ifdef WITH_SYSLOG
        else {
index 67de9e4bf231420950485046b359c5890e2faf2d..ec967e4abf028edb24c204a7a0505be2149e14ae 100644 (file)
@@ -873,10 +873,13 @@ void msleep(unsigned int t)
  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 */
 #ifdef HAVE_SETSID
index 988127e4318696d368e072ca4589e9c443305c23..2b7d8033a2a8a7e8300fadd78b4cf066419e4dc9 100644 (file)
@@ -32,6 +32,12 @@ extern BOOL global_in_nmbd;
 /* are we running as a daemon ? */
 static BOOL is_daemon = False;
 
+/* fork or run in foreground ? */
+static BOOL Fork = True;
+
+/* log to standard output ? */
+static BOOL log_stdout = False;
+
 /* have we found LanMan clients yet? */
 BOOL found_lm_clients = False;
 
@@ -590,6 +596,8 @@ static BOOL open_sockets(BOOL isdaemon, int port)
        POPT_AUTOHELP
        {"daemon", 'D', POPT_ARG_VAL, &is_daemon, True, "Become a daemon(default)" },
        {"interactive", 'i', POPT_ARG_VAL, &opt_interactive, True, "Run interactive (not a daemon)" },
+       {"foreground", 'F', POPT_ARG_VAL, &Fork, False, "Run daemon in foreground (for daemontools & etc)" },
+       {"log-stdout", 'S', POPT_ARG_VAL, &log_stdout, True, "Log to stdout" },
        {"hosts", 'H', POPT_ARG_STRING, dyn_LMHOSTSFILE, 'H', "Load a netbios hosts file"},
        {"port", 'p', POPT_ARG_INT, &global_nmb_port, NMB_PORT, "Listen on the specified port" },
        {NULL, 0, POPT_ARG_INCLUDE_TABLE, popt_common_debug },
@@ -639,8 +647,18 @@ static BOOL open_sockets(BOOL isdaemon, int port)
     { }
 
   poptFreeContext(pc);
-  
-  setup_logging( argv[0], opt_interactive );
+
+  if ( opt_interactive ) {
+    Fork = False;
+    log_stdout = True;
+  }
+
+  if ( log_stdout && Fork ) {
+    DEBUG(0,("ERROR: Can't log to stdout (-S) unless daemon is in foreground (-F) or interactive (-i)\n"));
+    exit(1);
+  }
+
+  setup_logging( argv[0], log_stdout );
 
   reopen_logs();
 
@@ -672,7 +690,7 @@ static BOOL open_sockets(BOOL isdaemon, int port)
   if (is_daemon && !opt_interactive)
   {
     DEBUG( 2, ( "Becoming a daemon.\n" ) );
-    become_daemon();
+    become_daemon(Fork);
   }
 
 #if HAVE_SETPGID
index 71e426e8182e6e8fb5dd0d2894c855a5625522f4..eadbf560746d8c3de240e5df6535a11a0a008aa1 100644 (file)
@@ -758,6 +758,8 @@ struct winbindd_state server_state;   /* Server state information */
 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-B                dual daemon mode\n");
        printf("\t-n                disable cacheing\n");
@@ -771,6 +773,8 @@ static void usage(void)
        extern BOOL AllowDebugChange;
        pstring logfile;
        BOOL interactive = False;
+       BOOL Fork = True;
+       BOOL log_stdout = False;
        int opt;
 
        /* glibc (?) likes to print "User defined signal 1" and exit if a
@@ -795,12 +799,20 @@ static void usage(void)
 
        /* Initialise samba/rpc client stuff */
 
-       while ((opt = getopt(argc, argv, "id:s:nhB")) != EOF) {
+       while ((opt = getopt(argc, argv, "FSid:s:nhB")) != 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;
                        break;
 
                        /* dual daemon system */
@@ -834,9 +846,15 @@ static void usage(void)
                }
        }
 
+       if (log_stdout && Fork) {
+               printf("Can't log to stdout (-S) unless daemon is in foreground +(-F) or interactive (-i)\n");
+               usage();
+               exit(1);
+       }
+
        snprintf(logfile, sizeof(logfile), "%s/log.winbindd", dyn_LOGFILEBASE);
        lp_set_logfile(logfile);
-       setup_logging("winbindd", interactive);
+       setup_logging("winbindd", log_stdout);
        reopen_logs();
 
        DEBUG(1, ("winbindd version %s started.\n", VERSION ) );
@@ -853,7 +871,7 @@ static void usage(void)
                exit(1);
 
        if (!interactive) {
-               become_daemon();
+               become_daemon(Fork);
                pidfile_create("winbindd");
        }
 
index 0a1191957710d77ad813659fbc174d7b5b317dcf..c235283f6c554e9f57e28b34522f44b33fff866f 100644 (file)
@@ -636,6 +636,8 @@ static BOOL init_structs(void )
        /* shall I run as a daemon */
        static BOOL is_daemon = False;
        static BOOL interactive = False;
+       static BOOL Fork = True;
+       static BOOL log_stdout = False;
        static char *ports = NULL;
        int opt;
        poptContext pc;
@@ -644,6 +646,8 @@ static BOOL init_structs(void )
                POPT_AUTOHELP
        {"daemon", 'D', POPT_ARG_VAL, &is_daemon, True, "Become a daemon (default)" },
        {"interactive", 'i', POPT_ARG_VAL, &interactive, True, "Run interactive (not a daemon)"},
+       {"foreground", 'F', POPT_ARG_VAL, &Fork, False, "Run daemon in foreground (for daemontools & etc)" },
+       {"log-stdout", 'S', POPT_ARG_VAL, &log_stdout, True, "Log to stdout" },
        {"build-options", 'b', POPT_ARG_NONE, NULL, 'b', "Print build options" },
        {"port", 'p', POPT_ARG_STRING, &ports, 0, "Listen on the specified ports"},
        {NULL, 0, POPT_ARG_INCLUDE_TABLE, popt_common_debug},
@@ -682,7 +686,17 @@ static BOOL init_structs(void )
 
        set_remote_machine_name("smbd");
 
-       setup_logging(argv[0],interactive);
+       if (interactive) {
+               Fork = False;
+               log_stdout = True;
+       }
+
+       if (log_stdout && Fork) {
+               DEBUG(0,("ERROR: Can't log to stdout (-S) unless daemon is in foreground (-F) or interactive (-i)\n"));
+               exit(1);
+       }
+
+       setup_logging(argv[0],log_stdout);
 
        /* we want to re-seed early to prevent time delays causing
            client problems at a later date. (tridge) */
@@ -771,7 +785,7 @@ static BOOL init_structs(void )
 
        if (is_daemon && !interactive) {
                DEBUG( 3, ( "Becoming a daemon.\n" ) );
-               become_daemon();
+               become_daemon(Fork);
        }
 
 #if HAVE_SETPGID
index e10dff411806cca65136ce30916ebc164eb7e16b..c6babff95431923c77b95da1542b164c39080813 100644 (file)
@@ -39,7 +39,7 @@ void start_smbd(void)
 
        slprintf(binfile, sizeof(pstring) - 1, "%s/smbd", dyn_SBINDIR);
 
-       become_daemon();
+       become_daemon(True);
 
        execl(binfile, binfile, "-D", NULL);
 
@@ -60,7 +60,7 @@ void start_nmbd(void)
 
        slprintf(binfile, sizeof(pstring) - 1, "%s/nmbd", dyn_SBINDIR);
        
-       become_daemon();
+       become_daemon(True);
 
        execl(binfile, binfile, "-D", NULL);
 
@@ -81,7 +81,7 @@ void start_winbindd(void)
 
        slprintf(binfile, sizeof(pstring) - 1, "%s/winbindd", dyn_SBINDIR);
 
-       become_daemon();
+       become_daemon(True);
 
        execl(binfile, binfile, NULL);
 
index e39fb148d6082739e0bc125f5dbe76f6a4ed5997..349f2a21abf16f39d07ecb6e8b6ede0bfc002201 100644 (file)
@@ -166,9 +166,11 @@ void exit_server(const char *reason)
 static void usage(char *pname)
 {
 
-       d_printf("Usage: %s [-DaioPh?V] [-d debuglevel] [-l log basename] [-p port]\n", pname);
+       d_printf("Usage: %s [-DFSaioPh?V] [-d debuglevel] [-l log basename] [-p port]\n", pname);
        d_printf("       [-O socket options] [-s services file]\n");
        d_printf("\t-D                    Become a daemon (default)\n");
+       d_printf("\t-F                    Run daemon in foreground (for daemontools, etc)\n");
+       d_printf("\t-S                    Log to stdout\n");
        d_printf("\t-a                    Append to log file (default)\n");
        d_printf("\t-i                    Run interactive (not a daemon)\n" );
        d_printf("\t-o                    Overwrite log file, don't append\n");
@@ -523,6 +525,8 @@ static void process(void)
        BOOL is_daemon = False;
        BOOL interactive = False;
        BOOL specified_logfile = False;
+       BOOL Fork = True;
+       BOOL log_stdout = False;
        int opt;
        pstring logfile;
 
@@ -536,8 +540,14 @@ static void process(void)
                argc--;
        }
 
-       while ( EOF != (opt = getopt(argc, argv, "O:l:s:d:Dp:h?Vaiof:")) )
+       while ( EOF != (opt = getopt(argc, argv, "FSO:l:s:d:Dp:h?Vaiof:")) )
                switch (opt)  {
+               case 'F':
+                       Fork = False;
+                       break;
+               case 'S':
+                       log_stdout = True;
+                       break;
                case 'O':
                        pstrcpy(user_socket_options,optarg);
                        break;
@@ -554,6 +564,8 @@ static void process(void)
 
                case 'i':
                        interactive = True;
+                       Fork = False;
+                       log_stdout = True;
                        break;
 
                case 'D':
@@ -586,6 +598,11 @@ static void process(void)
                        usage(argv[0]);
                        exit(1);
                }
+       if (log_stdout && Fork) {
+               d_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 */
@@ -604,7 +621,7 @@ static void process(void)
 
        set_remote_machine_name("wrepld");
 
-       setup_logging(argv[0],interactive);
+       setup_logging(argv[0],log_stdout);
 
        /* we want to re-seed early to prevent time delays causing
            client problems at a later date. (tridge) */
@@ -682,7 +699,7 @@ static void process(void)
 
        if (is_daemon && !interactive) {
                DEBUG( 3, ( "Becoming a daemon.\n" ) );
-               become_daemon();
+               become_daemon(Fork);
        }
 
 #if HAVE_SETPGID