set_global_myname( "" );
/* set default debug level to 1 regardless of what smb.conf sets */
- setup_logging( "smbclient", true );
+ setup_logging( "smbclient", DEBUG_DEFAULT_STDERR );
DEBUGLEVEL_CLASS[DBGC_ALL] = 1;
- if ((dbf = x_fdup(x_stderr))) {
- x_setbuf( dbf, NULL );
- }
load_case_tables();
}
break;
case 'E':
- if (dbf) {
- x_fclose(dbf);
- }
- dbf = x_stderr;
+ setup_logging("smbclient", DEBUG_STDERR );
display_set_stderr();
break;
}
if ( override_logfile )
- setup_logging( lp_logfile(), false );
+ setup_logging( lp_logfile(), DEBUG_FILE );
if (!lp_load(get_dyn_CONFIGFILE(),true,false,false,true)) {
fprintf(stderr, "%s: Can't load %s - run testparm to debug it\n",
* tar output
*/
if (tarhandle == 1) {
- dbf = x_stderr;
+ setup_logging("smbclient", DEBUG_STDERR);
}
if (!argv[Optind]) {
DEBUG(0,("Must specify tar filename\n"));
* Setup the SAMBA server state...
*/
- setup_logging("smbspool", True);
+ setup_logging("smbspool", DEBUG_STDOUT);
lp_set_in_client(True); /* Make sure that we tell lp_load we are */
#pragma mips_frequency_hint NEVER dbghdr
#endif
-extern XFILE *dbf;
-
/* If we have these macros, we can add additional info to the header. */
#ifdef HAVE_FUNCTION_MACRO
/* The following definitions come from lib/debug.c */
+/* Possible destinations for the debug log (in order of precedence,
+ * only a higher value will override a lower value */
+enum debug_logtype {DEBUG_DEFAULT_STDERR = 0, DEBUG_STDOUT = 1, DEBUG_FILE = 2, DEBUG_STDERR = 3};
+
+void setup_logging(const char *prog_name, enum debug_logtype new_logtype);
+
+void debug_close_dbf(void);
void gfree_debugsyms(void);
const char *debug_classname_from_index(int ndx);
int debug_add_class(const char *classname);
void debug_message(struct messaging_context *msg_ctx, void *private_data, uint32_t msg_type, struct server_id src, DATA_BLOB *data);
void debug_init(void);
void debug_register_msgs(struct messaging_context *msg_ctx);
-void setup_logging(const char *pname, bool interactive);
-void setup_logging_stdout( void );
void debug_set_logfile(const char *name);
bool reopen_logs( void );
void force_check_log_size( void );
bool dbghdr(int level, const char *location, const char *func);
#endif
+
* a newline.
*/
+/* state variables for the debug system */
+static struct {
+ XFILE *dbf; /* The log file handle */
+ enum debug_logtype logtype; /* The type of logging we are doing: eg stdout, file, stderr */
+ const char *prog_name;
+ bool reopening_logs;
+} state;
+
/* -------------------------------------------------------------------------- **
* External variables.
*
- * dbf - Global debug file handle.
* debugf - Debug file name.
* DEBUGLEVEL - System-wide debug message limit. Messages with message-
* levels higher than DEBUGLEVEL will not be processed.
*/
-XFILE *dbf = NULL;
static char *debugf = NULL;
bool debug_warn_unknown_class = True;
bool debug_auto_add_unknown_class = True;
/* -------------------------------------------------------------------------- **
* Internal variables.
*
- * stdout_logging - Default False, if set to True then dbf will be set to
- * stdout and debug output will go to dbf only, and not
- * to syslog. Set in setup_logging() and read in Debug1().
- *
* debug_count - Number of debug messages that have been output.
* Used to check log size.
*
* are unable to open a new log file for some reason.
*/
-static bool stdout_logging = False;
static int debug_count = 0;
#ifdef WITH_SYSLOG
static int syslog_level = 0;
debuglevel_message);
}
-/***************************************************************************
- Get ready for syslog stuff
-**************************************************************************/
-
-void setup_logging(const char *pname, bool interactive)
+/**
+ control the name of the logfile and whether logging will be to stdout, stderr
+ or a file, and set up syslog
+*/
+void setup_logging(const char *prog_name, enum debug_logtype new_logtype)
{
debug_init();
-
- /* reset to allow multiple setup calls, going from interactive to
- non-interactive */
- stdout_logging = False;
- if (dbf) {
- x_fflush(dbf);
- if (dbf != x_stdout) {
- (void) x_fclose(dbf);
- }
+ if (state.logtype < new_logtype) {
+ state.logtype = new_logtype;
}
-
- dbf = NULL;
-
- if (interactive) {
- stdout_logging = True;
- dbf = x_stdout;
- x_setbuf( x_stdout, NULL );
+ if (prog_name) {
+ state.prog_name = prog_name;
}
+ reopen_logs();
+
+ if (state.logtype == DEBUG_FILE) {
#ifdef WITH_SYSLOG
- else {
- const char *p = strrchr_m( pname,'/' );
+ const char *p = strrchr_m( prog_name,'/' );
if (p)
- pname = p + 1;
+ prog_name = p + 1;
#ifdef LOG_DAEMON
- openlog( pname, LOG_PID, SYSLOG_FACILITY );
+ openlog( prog_name, LOG_PID, SYSLOG_FACILITY );
#else
/* for old systems that have no facility codes. */
- openlog( pname, LOG_PID );
+ openlog( prog_name, LOG_PID );
#endif
- }
#endif
+ }
}
-/**
- Just run logging to stdout for this program
-*/
-_PUBLIC_ void setup_logging_stdout(void)
-{
- setup_logging(NULL, True);
-}
-
-
/***************************************************************************
Set the logfile name.
**************************************************************************/
debugf = SMB_STRDUP(name);
}
+static void debug_close_xfile(XFILE *dbf)
+{
+ if (dbf && (dbf != x_stderr && dbf != x_stdout)) {
+ x_fclose(dbf);
+ }
+}
+
+void debug_close_dbf(void)
+{
+ debug_close_xfile(state.dbf);
+ state.dbf = NULL;
+}
/**************************************************************************
reopen the log files
note that we now do this unconditionally
Fix from dgibson@linuxcare.com.
**************************************************************************/
-bool reopen_logs( void )
+/**
+ reopen the log file (usually called because the log file name might have changed)
+*/
+bool reopen_logs(void)
{
- char *fname = NULL;
mode_t oldumask;
XFILE *new_dbf = NULL;
XFILE *old_dbf = NULL;
bool ret = True;
- if (stdout_logging)
- return True;
+ char *fname = NULL;
+ if (state.reopening_logs) {
+ return true;
+ }
+
+ switch (state.logtype) {
+ case DEBUG_STDOUT:
+ state.dbf = x_stdout;
+ x_setbuf( x_stdout, NULL );
+ return true;
+
+ case DEBUG_DEFAULT_STDERR:
+ case DEBUG_STDERR:
+ debug_close_xfile(state.dbf);
+ state.dbf = x_stderr;
+ x_setbuf( x_stderr, NULL );
+ return true;
+
+ case DEBUG_FILE:
+ break;
+ }
oldumask = umask( 022 );
log_overflow = True;
DEBUG(0, ("Unable to open new log file %s: %s\n", debugf, strerror(errno)));
log_overflow = False;
- if (dbf)
- x_fflush(dbf);
+ if (state.dbf)
+ x_fflush(state.dbf);
ret = False;
} else {
x_setbuf(new_dbf, NULL);
- old_dbf = dbf;
- dbf = new_dbf;
+ old_dbf = state.dbf;
+ state.dbf = new_dbf;
if (old_dbf)
- (void) x_fclose(old_dbf);
+ debug_close_xfile(old_dbf);
}
/* Fix from klausr@ITAP.Physik.Uni-Stuttgart.De
(void)umask(oldumask);
/* Take over stderr to catch output into logs */
- if (dbf && dup2(x_fileno(dbf), 2) == -1) {
+ if (state.dbf && dup2(x_fileno(state.dbf), 2) == -1) {
close_low_fds(True); /* Close stderr too, if dup2 can't point it
at the logfile */
}
return( False );
maxlog = lp_max_log_size() * 1024;
- if( !dbf || maxlog <= 0 ) {
+ if( !state.dbf || maxlog <= 0 ) {
debug_count = 0;
return(False);
}
maxlog = lp_max_log_size() * 1024;
- if(sys_fstat(x_fileno(dbf), &st, false) == 0
+ if(sys_fstat(x_fileno(state.dbf), &st, false) == 0
&& st.st_ex_size > maxlog ) {
(void)reopen_logs();
- if( dbf && get_file_size( debugf ) > maxlog ) {
+ if( state.dbf && get_file_size( debugf ) > maxlog ) {
char *name = NULL;
if (asprintf(&name, "%s.old", debugf ) < 0) {
* Here's where we need to panic if dbf == NULL..
*/
- if(dbf == NULL) {
+ if(state.dbf == NULL) {
/* This code should only be reached in very strange
* circumstances. If we merely fail to open the new log we
* should stick with the old one. ergo this should only be
* startup or when the log level is increased from zero.
* -dwg 6 June 2000
*/
- dbf = x_fopen( "/dev/console", O_WRONLY, 0);
- if(dbf) {
+ state.dbf = x_fopen( "/dev/console", O_WRONLY, 0);
+ if(state.dbf) {
DEBUG(0,("check_log_size: open of debug file %s failed - using console.\n",
debugf ));
} else {
debug_count++;
- if( stdout_logging ) {
+ if ( state.logtype != DEBUG_FILE ) {
va_start( ap, format_str );
- if(dbf)
- (void)x_vfprintf( dbf, format_str, ap );
+ if(state.dbf)
+ (void)x_vfprintf( state.dbf, format_str, ap );
va_end( ap );
errno = old_errno;
goto done;
if( !lp_syslog_only() )
#endif
{
- if( !dbf ) {
+ if( !state.dbf ) {
mode_t oldumask = umask( 022 );
- dbf = x_fopen( debugf, O_WRONLY|O_APPEND|O_CREAT, 0644 );
+ state.dbf = x_fopen( debugf, O_WRONLY|O_APPEND|O_CREAT, 0644 );
(void)umask( oldumask );
- if( dbf ) {
- x_setbuf( dbf, NULL );
+ if( state.dbf ) {
+ x_setbuf( state.dbf, NULL );
} else {
errno = old_errno;
goto done;
#endif
{
va_start( ap, format_str );
- if(dbf)
- (void)x_vfprintf( dbf, format_str, ap );
+ if(state.dbf)
+ (void)x_vfprintf( state.dbf, format_str, ap );
va_end( ap );
- if(dbf)
- (void)x_fflush( dbf );
+ if(state.dbf)
+ (void)x_fflush( state.dbf );
}
done:
static void format_debug_text( const char *msg )
{
size_t i;
- bool timestamp = (!stdout_logging && (lp_timestamp_logs() || !(lp_loaded())));
+ bool timestamp = (state.logtype == DEBUG_FILE && (lp_timestamp_logs() || !(lp_loaded())));
if (!format_bufr) {
debug_init();
void dbgflush( void )
{
bufr_print();
- if(dbf)
- (void)x_fflush( dbf );
+ if(state.dbf)
+ (void)x_fflush( state.dbf );
}
/***************************************************************************
#endif
/* Don't print a header if we're logging to stdout. */
- if( stdout_logging )
+ if ( state.logtype != DEBUG_FILE ) {
return( True );
+ }
/* Print the header if timestamps are turned on. If parameters are
* not yet loaded, then default to timestamps on.
}
/* prevent setup_logging() from closing x_stderr... */
- dbf = 0;
- setup_logging("libnetapi", true);
+ setup_logging("libnetapi", DEBUG_STDERR);
- dbf = x_stderr;
- x_setbuf(x_stderr, NULL);
AllowDebugChange = false;
load_case_tables();
};
load_case_tables();
- dbf = x_stderr;
+ setup_logging(argv[0], DEBUG_STDERR);
/* parse options */
pc = poptGetContext("smbconftort", argc, (const char **)argv,
load_case_tables();
- setup_logging("libsmbclient", True);
+ setup_logging("libsmbclient", DEBUG_STDOUT);
/* Here we would open the smb.conf file if needed ... */
* leave it up to the user. If any one context spefies debug to
* stderr then all will be.
*/
- dbf = x_stderr;
- x_setbuf(x_stderr, NULL);
+ setup_logging("libsmbclient", DEBUG_STDERR);
}
if ((!smbc_getFunctionAuthData(context) &&
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 );
+ if (log_stdout) {
+ setup_logging( argv[0], DEBUG_STDOUT);
+ } else {
+ setup_logging( argv[0], DEBUG_FILE);
+ }
reopen_logs();
TALLOC_FREE(frame);
process();
- if (dbf)
- x_fclose(dbf);
+ debug_close_dbf();
+
kill_async_dns_child();
return(0);
}
while(poptGetNextOpt(pc) != -1);
- setup_logging(poptGetArg(pc), True);
+ setup_logging(poptGetArg(pc), DEBUG_STDERR);
if (poptPeekArg(pc)) {
config_file = poptGetArg(pc);
count = atoi(count_str);
}
- dbf = x_stderr;
/* Don't let the debuglevel be changed by smb.conf. */
AllowDebugChange = False;
/* the following functions are part of the Samba debugging
facilities. See lib/debug.c */
- setup_logging("rpcclient", True);
+ setup_logging("rpcclient", DEBUG_STDOUT);
rpcclient_auth_info = user_auth_info_init(frame);
if (rpcclient_auth_info == NULL) {
log_stdout = True;
}
- setup_logging(argv[0],log_stdout);
+ if (log_stdout) {
+ setup_logging(argv[0], DEBUG_STDOUT);
+ } else {
+ setup_logging(argv[0], DEBUG_FILE);
+ }
if (print_build_options) {
build_options(True); /* Display output to screen as well as debug */
load_case_tables();
- dbf = x_stderr;
-
if (argc < 3 || argv[1][0] == '-') {
usage();
exit(1);
}
- setup_logging(argv[0],True);
+ setup_logging(argv[0], DEBUG_STDOUT);
for (server=0;server<NSERVERS;server++) {
share[server] = argv[1+server];
setlinebuf(stdout);
- dbf = x_stderr;
-
if (argc < 5 || argv[1][0] == '-') {
usage();
exit(1);
}
+ setup_logging(argv[0], DEBUG_STDOUT);
+
share1 = argv[1];
share2 = argv[2];
nfspath1 = argv[3];
all_string_sub(share1,"/","\\",0);
all_string_sub(share2,"/","\\",0);
- setup_logging(argv[0],True);
-
argc -= 4;
argv += 4;
setlinebuf(stdout);
- dbf = x_stderr;
-
DEBUGLEVEL = 0;
AllowDebugChange = False;
all_string_sub(share,"/","\\",0);
- setup_logging(argv[0],True);
+ setup_logging(argv[0], DEBUG_STDERR);
argc -= 1;
argv += 1;
load_case_tables();
- setup_logging(argv[0],True);
+ setup_logging(argv[0], DEBUG_STDOUT);
lp_load(get_dyn_CONFIGFILE(),False,False,False,True);
/* Load configuration */
lp_load(get_dyn_CONFIGFILE(), False, False, True, True);
- setup_logging("pdbtest", True);
+ setup_logging("pdbtest", DEBUG_STDOUT);
if (backend == NULL) {
backend = lp_passdb_backend();
/* the following functions are part of the Samba debugging
facilities. See lib/debug.c */
- setup_logging("smbiconv", True);
+ setup_logging("smbiconv", DEBUG_STDOUT);
if (preload_modules[0]) smb_load_modules(preload_modules);
TALLOC_CTX *frame = talloc_stackframe();
int seed = time(NULL);
- dbf = x_stdout;
-
#ifdef HAVE_SETBUFFER
setbuffer(stdout, NULL, 0);
#endif
- load_case_tables();
+ setup_logging("smbtorture", DEBUG_STDOUT);
- setup_logging("smbtorture", true);
+ load_case_tables();
if (is_default_dyn_CONFIGFILE()) {
if(getenv("SMB_CONF_PATH")) {
/* the following functions are part of the Samba debugging
facilities. See lib/debug.c */
- setup_logging("vfstest", True);
+ setup_logging("vfstest", DEBUG_STDOUT);
/* Load command lists */
load_case_tables();
DEBUGLEVEL_CLASS[DBGC_ALL] = 0;
- dbf = x_stderr;
+ setup_logging(argv[0], DEBUG_STDERR);
AllowDebugChange = false;
lp_load(get_dyn_CONFIGFILE(), true, false, false, true);
setlinebuf(stdout);
}
+ setup_logging(argv[0], DEBUG_STDERR);
DEBUGLEVEL_CLASS[DBGC_ALL] = 0;
pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
}
load_case_tables();
- dbf = x_stderr;
AllowDebugChange = false;
lp_load(get_dyn_CONFIGFILE(), true, false, false, true);
zero_sockaddr(&c->opt_dest_ip);
+ setup_logging(argv[0], DEBUG_STDERR);
+
load_case_tables();
setlocale(LC_ALL, "");
/* set default debug level to 0 regardless of what smb.conf sets */
DEBUGLEVEL_CLASS[DBGC_ALL] = 0;
- dbf = x_stderr;
c->private_data = net_func;
pc = poptGetContext(NULL, argc, (const char **) argv, long_options,
load_case_tables();
- setup_logging(argv[0],True);
+ setup_logging(argv[0], DEBUG_STDOUT);
pc = poptGetContext("nmblookup", argc, (const char **)argv,
long_options, POPT_CONTEXT_KEEP_FIRST);
/* Samba client initialisation */
load_case_tables();
- dbf = x_stderr;
+ setup_logging("ntlm_auth", DEBUG_STDERR);
/* Parse options */
load_case_tables();
- setup_logging("pdbedit", True);
+ setup_logging("pdbedit", DEBUG_STDOUT);
pc = poptGetContext(NULL, argc, (const char **) argv, long_options,
POPT_CONTEXT_KEEP_FIRST);
/* setup logging options */
- setup_logging( "profiles", True );
- dbf = x_stderr;
- x_setbuf( x_stderr, NULL );
+ setup_logging( "profiles", DEBUG_STDERR);
pc = poptGetContext("profiles", argc, (const char **)argv, long_options,
POPT_CONTEXT_KEEP_FIRST);
}
/* set default debug level to 1 regardless of what smb.conf sets */
- setup_logging( "sharesec", True );
+ setup_logging( "sharesec", DEBUG_STDERR);
DEBUGLEVEL_CLASS[DBGC_ALL] = 1;
- dbf = x_stderr;
- x_setbuf( x_stderr, NULL );
pc = poptGetContext("sharesec", argc, argv, long_options, 0);
/* set default debug level to 1 regardless of what smb.conf sets */
- setup_logging( "smbcacls", True );
+ setup_logging( "smbcacls", DEBUG_STDERR);
DEBUGLEVEL_CLASS[DBGC_ALL] = 1;
- dbf = x_stderr;
- x_setbuf( x_stderr, NULL );
AllowDebugChange = false;
setlinebuf(stdout);
load_case_tables();
- setup_logging(argv[0],True);
+ setup_logging(argv[0], DEBUG_STDOUT);
/* Parse command line arguments using popt */
ZERO_STRUCT(qt);
/* set default debug level to 1 regardless of what smb.conf sets */
- setup_logging( "smbcquotas", True );
+ setup_logging( "smbcquotas", DEBUG_STDERR);
DEBUGLEVEL_CLASS[DBGC_ALL] = 1;
- dbf = x_stderr;
- x_setbuf( x_stderr, NULL );
setlinebuf(stdout);
load_case_tables();
- setup_logging(argv[0],True);
+ setup_logging(argv[0], DEBUG_STDOUT);
configfile = get_dyn_CONFIGFILE();
local_flags = process_options(argc, argv, local_flags);
- setup_logging("smbpasswd", True);
+ setup_logging("smbpasswd", DEBUG_STDERR);
/*
* Set the machine NETBIOS name if not already
setlinebuf(stdout);
- dbf = x_stderr;
-
- setup_logging(argv[0],True);
+ setup_logging(argv[0], DEBUG_STDERR);
auth_info = user_auth_info_init(frame);
if (auth_info == NULL) {
while(poptGetNextOpt(pc) != -1);
- setup_logging(poptGetArg(pc), true);
+ setup_logging(poptGetArg(pc), DEBUG_STDERR);
sequence = poptGetArg(pc);
return 1;
}
- dbf = x_stderr;
DEBUGLEVEL = 0;
AllowDebugChange = false;
sec_init();
load_case_tables();
- setup_logging(argv[0],True);
-
- dbf = x_stderr;
+ setup_logging(argv[0], DEBUG_STDERR);
if (getuid() != geteuid()) {
d_printf("smbstatus should not be run setuid\n");
exit(0);
}
- setup_logging(poptGetArg(pc), True);
+ setup_logging(poptGetArg(pc), DEBUG_STDERR);
if (poptPeekArg(pc))
config_file = poptGetArg(pc);
goto done;
}
- dbf = x_stderr;
/* Don't let the debuglevel be changed by smb.conf. */
AllowDebugChange = False;
/* we don't want any SIGPIPE messages */
BlockSignals(True,SIGPIPE);
- dbf = x_fopen("/dev/null", O_WRONLY, 0);
- if (!dbf) dbf = x_stderr;
+ debug_set_logfile("/dev/null");
/* we don't want stderr screwing us up */
close(2);
open("/dev/null", O_WRONLY);
+ setup_logging("swat", DEBUG_FILE);
pc = poptGetContext("swat", argc, (const char **) argv, long_options, 0);
poptFreeContext(pc);
load_case_tables();
-
- setup_logging(argv[0],False);
+
+ /* This should set a more apporiate log file */
load_config(True);
+ reopen_logs();
load_interfaces();
iNumNonAutoPrintServices = lp_numservices();
load_printers(server_event_context(), server_messaging_context());
SAFE_FREE(lfile);
}
}
- setup_logging("winbindd", log_stdout);
+ if (log_stdout) {
+ setup_logging("winbindd", DEBUG_STDOUT);
+ } else {
+ setup_logging("winbindd", DEBUG_FILE);
+ }
reopen_logs();
DEBUG(0,("winbindd version %s started.\n", samba_version_string()));