r7525: Unify lp_load(), load_interfaces and logging setup into popt().
authorAndrew Bartlett <abartlet@samba.org>
Mon, 13 Jun 2005 08:12:39 +0000 (08:12 +0000)
committerGerald (Jerry) Carter <jerry@samba.org>
Wed, 10 Oct 2007 18:18:06 +0000 (13:18 -0500)
There is now a new --debug-stderr option to enable debug to STDERR.

popt isn't perfect, but the callbacks are used in all the main Samba
binaries, and should be used in the rest.  This avoids duplicated
code, and ensures every binary is setup correctly.

This also ensures the setup happens early enough to have -s function,
and have a correct impact on the credentials code.  (Fixing a bug that
frustrated tridge earlier today).

The only 'subtle' aspect of all this is that I'm pretty sure that the
SAMBA_COMMON popt code must be above the CREDENTIALS code, in the
popt tables.

Andrew Bartlett
(This used to be commit 50f3c2b3a22971f40e0d3a88127b5120bfc47591)

15 files changed:
source4/client/client.c
source4/include/debug.h
source4/lib/cmdline/popt_common.c
source4/lib/debug.c
source4/lib/registry/tools/regdiff.c
source4/lib/registry/tools/regpatch.c
source4/lib/registry/tools/regshell.c
source4/lib/registry/tools/regtree.c
source4/smbd/server.c
source4/torture/smbiconv.c
source4/torture/torture.c
source4/utils/ndrdump.c
source4/utils/net/net.c
source4/utils/nmblookup.c
source4/utils/ntlm_auth.c

index 34d0fc20f1a8dbd52c377e5557471ad4b64e5f2f..afdab5928c26af436860d10a589796fbeafcfbbc 100644 (file)
@@ -3361,18 +3361,12 @@ static void remember_query_host(const char *arg,
        *query_host = 0;
        *base_directory = 0;
 
-       setup_logging(argv[0],DEBUG_STDOUT);
        mem_ctx = talloc_init("client.c/main");
        if (!mem_ctx) {
                d_printf("\nclient.c: Not enough memory\n");
                exit(1);
        }
 
-       if (!lp_load(dyn_CONFIGFILE,True,False,False)) {
-               fprintf(stderr, "%s: Can't load %s - run testparm to debug it\n",
-                       argv[0], dyn_CONFIGFILE);
-       }
-       
        pc = poptGetContext("smbclient", argc, (const char **) argv, long_options, 0);
        poptSetOtherOptionHelp(pc, "[OPTIONS] service <password>");
 
@@ -3393,10 +3387,6 @@ static void remember_query_host(const char *arg,
                case 'I':
                        dest_ip = poptGetOptArg(pc);
                        break;
-               case 'E':
-                       setup_logging("client", DEBUG_STDERR);
-                       break;
-
                case 'L':
                        remember_query_host(poptGetOptArg(pc), query_host);
                        break;
@@ -3412,8 +3402,6 @@ static void remember_query_host(const char *arg,
                }
        }
 
-       load_interfaces();
-
        smbclient_init_subsystems;
 
        if(poptPeekArg(pc)) {
index 166b7c26f584962ed325476d4ef2fe0d68acd1ad..ccd1c90040e61c346be7b8551f26c9e6e16cf176 100644 (file)
@@ -46,7 +46,7 @@ extern int DEBUGLEVEL;
 #define DEBUGADDC(class, level, body) DEBUG(level, body)
 #define DEBUGTAB(n) do_debug_tab(n)
 
-enum debug_logtype {DEBUG_FILE, DEBUG_STDOUT, DEBUG_STDERR};
+enum debug_logtype {DEBUG_STDOUT = 0, DEBUG_FILE = 1, DEBUG_STDERR = 2};
 
 /* keep some debug class defines for now to avoid changing old code too much */
 #define DBGC_AUTH 0
index 94b72c520d39f073679cf2410cdb1d5a7d559f14..fec85281ef86aeb8a28e7d607ba9769aa87d45fe 100644 (file)
@@ -38,7 +38,7 @@
  *             -i,--scope
  */
 
-enum {OPT_OPTION=1,OPT_LEAK_REPORT,OPT_LEAK_REPORT_FULL};
+enum {OPT_OPTION=1,OPT_LEAK_REPORT,OPT_LEAK_REPORT_FULL, OPT_DEBUG_STDERR};
 
 struct cli_credentials *cmdline_credentials = NULL;
 
@@ -49,6 +49,14 @@ static void popt_common_callback(poptContext con,
 {
        const char *pname;
        
+       if (reason == POPT_CALLBACK_REASON_POST) {
+               /* Hook any 'every Samba program must do this, after
+                * the smb.conf is setup' functions here */
+               lp_load(dyn_CONFIGFILE,True,False,False);
+               load_interfaces();
+               return;
+       }
+
        /* Find out basename of current program */
        pname = strrchr_m(poptGetInvocationName(con),'/');
 
@@ -58,9 +66,7 @@ static void popt_common_callback(poptContext con,
                pname++;
 
        if (reason == POPT_CALLBACK_REASON_PRE) {
-               char *logfile = talloc_asprintf(NULL, "%s/log.%s", dyn_LOGFILEBASE, pname);
-               lp_set_cmdline("log file", logfile);
-               talloc_free(logfile);
+               setup_logging(pname, DEBUG_STDOUT);
                return;
        }
 
@@ -69,6 +75,10 @@ static void popt_common_callback(poptContext con,
                lp_set_cmdline("log level", arg);
                break;
 
+       case OPT_DEBUG_STDERR:
+               setup_logging(pname, DEBUG_STDERR);
+               break;
+
        case 'V':
                printf( "Version %s\n", SAMBA_VERSION_STRING );
                exit(0);
@@ -128,6 +138,7 @@ static void popt_common_callback(poptContext con,
        case OPT_LEAK_REPORT_FULL:
                talloc_enable_leak_report_full();
                break;
+
        }
 }
 
@@ -143,8 +154,9 @@ struct poptOption popt_common_connection[] = {
 };
 
 struct poptOption popt_common_samba[] = {
-       { NULL, 0, POPT_ARG_CALLBACK|POPT_CBFLAG_PRE, popt_common_callback },
+       { NULL, 0, POPT_ARG_CALLBACK|POPT_CBFLAG_PRE|POPT_CBFLAG_POST, popt_common_callback },
        { "debuglevel",   'd', POPT_ARG_STRING, NULL, 'd', "Set debug level", "DEBUGLEVEL" },
+       { "debug-stderr", 0, POPT_ARG_NONE, NULL, OPT_DEBUG_STDERR, "Send debug output to STDERR", NULL },
        { "configfile",   's', POPT_ARG_STRING, NULL, 's', "Use alternative configuration file", "CONFIGFILE" },
        { "option",         0, POPT_ARG_STRING, NULL, OPT_OPTION, "Set smb.conf option from command line", "name=value" },
        { "log-basename", 'l', POPT_ARG_STRING, NULL, 'l', "Basename for log/debug files", "LOGFILEBASE" },
index ab012a98aa24d7923ec3253967140b2989834701..a775c46854776e44ac0a137a7c377da2bb2f534c 100644 (file)
@@ -74,8 +74,6 @@ void reopen_logs(void)
        char *fname = NULL;
        int old_fd = state.fd;
 
-       state.fd = 0;
-
        switch (state.logtype) {
        case DEBUG_STDOUT:
                state.fd = 1;
@@ -89,12 +87,20 @@ void reopen_logs(void)
                if ((*logfile) == '/') {
                        fname = strdup(logfile);
                } else {
-                       asprintf(&fname, "%s/%s.log", dyn_LOGFILEBASE, logfile);
+                       asprintf(&fname, "%s/%s.log", dyn_LOGFILEBASE, state.prog_name);
                }
                if (fname) {
-                       state.fd = open(fname, O_CREAT|O_APPEND|O_WRONLY, 0644);
+                       int newfd = open(fname, O_CREAT|O_APPEND|O_WRONLY, 0600);
+                       if (newfd == -1) {
+                               DEBUG(1, ("Failed to open new logfile: %s\n", fname));
+                       } else {
+                               state.fd = newfd;
+                       }
                        free(fname);
+               } else {
+                       DEBUG(1, ("Failed to find name for file-based logfile!\n"));
                }
+
                break;
        }
 
@@ -109,8 +115,12 @@ void reopen_logs(void)
 */
 void setup_logging(const char *prog_name, enum debug_logtype new_logtype)
 {
-       state.logtype = new_logtype;
-       state.prog_name = prog_name;
+       if (state.logtype < new_logtype) {
+               state.logtype = new_logtype;
+       }
+       if (prog_name) {
+               state.prog_name = prog_name;
+       }
        reopen_logs();
 }
 
index 4260a56142b7b1df8a095a6f9c760503ba72b859..f86c0ae3834c8474647ac0ae26fe7ce147ac8713 100644 (file)
@@ -121,21 +121,18 @@ static void writediff(struct registry_key *oldkey, struct registry_key *newkey,
        WERROR error, error2;
        struct poptOption long_options[] = {
                POPT_AUTOHELP
-               POPT_COMMON_CREDENTIALS
                {"output", 'o', POPT_ARG_STRING, &outputfile, 'o', "output file to use", NULL },
                {"null", 'n', POPT_ARG_NONE, &from_null, 'n', "Diff from NULL", NULL },
                {"remote", 'R', POPT_ARG_STRING, NULL, 0, "Connect to remote server" , NULL },
                {"local", 'L', POPT_ARG_NONE, NULL, 0, "Open local registry", NULL },
+               POPT_COMMON_SAMBA
+               POPT_COMMON_CREDENTIALS
+               POPT_COMMON_VERSION
                POPT_TABLEEND
        };
 
        regdiff_init_subsystems;
 
-       if (!lp_load(dyn_CONFIGFILE,True,False,False)) {
-               fprintf(stderr, "Can't load %s - run testparm to debug it\n", dyn_CONFIGFILE);
-       }
-
-
        pc = poptGetContext(argv[0], argc, (const char **) argv, long_options,0);
 
        while((opt = poptGetNextOpt(pc)) != -1) {
@@ -157,7 +154,6 @@ static void writediff(struct registry_key *oldkey, struct registry_key *newkey,
                        return 1;
                }
        }
-       setup_logging(argv[0], DEBUG_STDOUT);
 
        poptFreeContext(pc);
 
index 5c9851b71b72cf3fb9319e1884084a0bbf4cd1c0..02ef4d46555d38c05d198ddaed47f1b95f19c2e7 100644 (file)
@@ -749,25 +749,19 @@ static int nt_apply_reg_command_file(struct registry_context *r, const char *cmd
        WERROR error;
        struct poptOption long_options[] = {
                POPT_AUTOHELP
-               POPT_COMMON_CREDENTIALS
                {"remote", 'R', POPT_ARG_STRING, &remote, 0, "connect to specified remote server", NULL},
+               POPT_COMMON_SAMBA
+               POPT_COMMON_CREDENTIALS
                POPT_TABLEEND
        };
 
        regpatch_init_subsystems;
 
-       if (!lp_load(dyn_CONFIGFILE,True,False,False)) {
-               fprintf(stderr, "Can't load %s - run testparm to debug it\n", dyn_CONFIGFILE);
-       }
-
-
        pc = poptGetContext(argv[0], argc, (const char **) argv, long_options,0);
 
        while((opt = poptGetNextOpt(pc)) != -1) {
        }
 
-       setup_logging(argv[0], DEBUG_STDOUT);
-
        if (remote) {
                error = reg_open_remote (&h, cmdline_credentials, remote);
        } else {
index 03cb09c443aa844d09addb4a1f9622102074d3fd..0c53f737b8e104b51466d5e8081557d88a3575de 100644 (file)
@@ -374,26 +374,21 @@ static char **reg_completion(const char *text, int start, int end)
        struct registry_context *h = NULL;
        struct poptOption long_options[] = {
                POPT_AUTOHELP
-               POPT_COMMON_CREDENTIALS
                {"backend", 'b', POPT_ARG_STRING, &backend, 0, "backend to use", NULL},
                {"remote", 'R', POPT_ARG_STRING, &remote, 0, "connect to specified remote server", NULL},
+               POPT_COMMON_SAMBA
+               POPT_COMMON_CREDENTIALS
+               POPT_COMMON_VERSION
                POPT_TABLEEND
        };
 
        regshell_init_subsystems;
 
-       if (!lp_load(dyn_CONFIGFILE,True,False,False)) {
-               fprintf(stderr, "Can't load %s - run testparm to debug it\n", dyn_CONFIGFILE);
-       }
-
-       
        pc = poptGetContext(argv[0], argc, (const char **) argv, long_options,0);
        
        while((opt = poptGetNextOpt(pc)) != -1) {
        }
 
-       setup_logging("regtree", DEBUG_STDOUT);
-
        if (remote) {
                error = reg_open_remote (&h, cmdline_credentials, remote); 
        } else if (backend) {
index 2cf5f6ab962ecd60830f1639963a1709c712a557..f18467b523750d64555d4895d833f38df764e4c6 100644 (file)
@@ -82,28 +82,22 @@ static void print_tree(int l, struct registry_key *p, int fullpath, int novals)
        int fullpath = 0, no_values = 0;
        struct poptOption long_options[] = {
                POPT_AUTOHELP
-               POPT_COMMON_CREDENTIALS 
                {"backend", 'b', POPT_ARG_STRING, &backend, 0, "backend to use", NULL},
                {"fullpath", 'f', POPT_ARG_NONE, &fullpath, 0, "show full paths", NULL},
                {"remote", 'R', POPT_ARG_STRING, &remote, 0, "connect to specified remote server", NULL },
                {"no-values", 'V', POPT_ARG_NONE, &no_values, 0, "don't show values", NULL},
+               POPT_COMMON_SAMBA       
+               POPT_COMMON_CREDENTIALS 
                POPT_TABLEEND
        };
 
        regtree_init_subsystems;
 
-       if (!lp_load(dyn_CONFIGFILE,True,False,False)) {
-               fprintf(stderr, "Can't load %s - run testparm to debug it\n", dyn_CONFIGFILE);
-       }
-
-
        pc = poptGetContext(argv[0], argc, (const char **) argv, long_options,0);
        
        while((opt = poptGetNextOpt(pc)) != -1) {
        }
 
-       setup_logging("regtree", DEBUG_STDOUT);
-
        if (remote) {
                error = reg_open_remote(&h, cmdline_credentials, remote);
        } else if (backend) {
index a08202baabf1b41b539c6529fafac10ddda21044..717de0c5233d24132ce023139c2369ca9230e036 100644 (file)
@@ -163,13 +163,13 @@ static int binary_smbd_main(int argc, const char *argv[])
        int max_runtime = 0;
        struct poptOption long_options[] = {
                POPT_AUTOHELP
-               POPT_COMMON_SAMBA
                {"interactive", 'i', POPT_ARG_VAL, &interactive, True, 
                 "Run interactive (not a daemon)", NULL},
                {"model", 'M', POPT_ARG_STRING, &model, True, 
                 "Select process model", "MODEL"},
                {"maximum-runtime", 0, POPT_ARG_INT, &max_runtime, True, 
                 "set maximum time for smbd to live", "seconds"},
+               POPT_COMMON_SAMBA
                POPT_COMMON_VERSION
                POPT_TABLEEND
        };
@@ -180,15 +180,13 @@ static int binary_smbd_main(int argc, const char *argv[])
 
        poptFreeContext(pc);
 
-       setup_logging(argv[0], interactive?DEBUG_STDOUT:DEBUG_FILE);
+       setup_logging(NULL, interactive?DEBUG_STDOUT:DEBUG_FILE);
        setup_signals();
 
        /* we want total control over the permissions on created files,
           so set our umask to 0 */
        umask(0);
 
-       reopen_logs();
-
        DEBUG(0,("smbd version %s started.\n", SAMBA_VERSION_STRING));
        DEBUGADD(0,("Copyright Andrew Tridgell and the Samba Team 1992-2005\n"));
 
@@ -197,11 +195,6 @@ static int binary_smbd_main(int argc, const char *argv[])
                exit(1);
        }
 
-       lp_load(dyn_CONFIGFILE, False, False, True);
-
-       reopen_logs();
-       load_interfaces();
-
        if (!interactive) {
                DEBUG(3,("Becoming a daemon.\n"));
                become_daemon(True);
index 7ca6bb1c125262ebc517a02d72e5fa5a17c72db0..0cd16278eeeb99105468291bed644a98cefed66b 100644 (file)
@@ -195,10 +195,6 @@ int main(int argc, char *argv[])
        
        while(poptGetNextOpt(pc) != -1);
 
-       /* the following functions are part of the Samba debugging
-          facilities.  See lib/debug.c */
-       setup_logging("smbiconv", DEBUG_STDOUT);
-
        if (preload_modules[0]) smb_load_modules(preload_modules);
 
        if(output) {
index 98519a37971820a08829ce10ab1c586080ec87cf..a2ca9fc72f781b2cfd5d8e4885294ff7ea54b52e 100644 (file)
@@ -2558,8 +2558,6 @@ static void max_runtime_handler(int sig)
                POPT_TABLEEND
        };
 
-       setup_logging("smbtorture", DEBUG_STDOUT);
-
 #ifdef HAVE_SETBUFFER
        setbuffer(stdout, NULL, 0);
 #endif
@@ -2604,9 +2602,6 @@ static void max_runtime_handler(int sig)
                alarm(max_runtime);
        }
 
-       lp_load(dyn_CONFIGFILE,True,False,False);
-       load_interfaces();
-
        smbtorture_init_subsystems;
 
 
index eabeaad5374b5136a2629c7ffe01d28f87e75c0e..58cdeb13c3a4d21f423cd03bac6a1bae6a3e316a 100644 (file)
@@ -121,8 +121,6 @@ static char *stdin_load(TALLOC_CTX *mem_ctx, size_t *size)
                POPT_TABLEEND
        };
 
-       setup_logging("ndrdump", DEBUG_STDOUT);
-
        ndrdump_init_subsystems;
 
        pc = poptGetContext("ndrdump", argc, argv, long_options, 0);
index c7c6f7dec9fc9d4f47ffa1539367cd65132b6dd5..51b860234d922af2d45f80e604ede7fdffdb06db 100644 (file)
@@ -154,14 +154,12 @@ static int binary_net(int argc, const char **argv)
                POPT_TABLEEND
        };
 
-       setup_logging("net", DEBUG_STDOUT);
-
 #ifdef HAVE_SETBUFFER
        setbuffer(stdout, NULL, 0);
 #endif
 
        pc = poptGetContext("net", argc, (const char **) argv, long_options, 
-                               POPT_CONTEXT_KEEP_FIRST);
+                           POPT_CONTEXT_KEEP_FIRST);
 
        while((opt = poptGetNextOpt(pc)) != -1) {
                switch (opt) {
@@ -173,9 +171,6 @@ static int binary_net(int argc, const char **argv)
                }
        }
 
-       lp_load(dyn_CONFIGFILE,True,False,False);
-       load_interfaces();
-
        argv_new = (const char **)poptGetArgs(pc);
 
        argc_new = argc;
index fb21cb552969ad3e684bfc5695ee7b9a6b906959..900d55d6df5b18a8733266d94d7e456c75d54bc1 100644 (file)
@@ -271,8 +271,6 @@ int main(int argc,char *argv[])
                { 0, 0, 0, 0 }
        };
        
-       setup_logging(argv[0], DEBUG_STDOUT);
-
        pc = poptGetContext("nmblookup", argc, (const char **)argv, long_options, 
                            POPT_CONTEXT_KEEP_FIRST);
        
@@ -288,9 +286,6 @@ int main(int argc,char *argv[])
                exit(1);
        }
        
-       lp_load(dyn_CONFIGFILE,True,False,False);
-       load_interfaces();
-       
        while (poptPeekArg(pc)) {
                const char *name = poptGetArg(pc);
 
index 4c7286a4c1d24d8b7d2d51aa7220a5acfeb2a8b5..8e858e2970db94ddd6412abb369508a0ee34ea77 100644 (file)
@@ -865,13 +865,7 @@ enum {
 
        /* Samba client initialisation */
 
-       setup_logging("ntlm_auth", DEBUG_STDERR);
-
-       if (!lp_load(dyn_CONFIGFILE, True, False, False)) {
-               d_fprintf(stderr, "wbinfo: error opening config file %s. Error was %s\n",
-                       dyn_CONFIGFILE, strerror(errno));
-               exit(1);
-       }
+       setup_logging(NULL, DEBUG_STDERR);
 
        /* Parse options */