/*
- Unix SMB/Netbios implementation.
- Version 1.9.
+ Unix SMB/CIFS implementation.
Samba utility functions
Copyright (C) Andrew Tridgell 1992-1998
+ Copyright (C) Elrond 2002
+ Copyright (C) Simo Sorce 2002
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
*
* FORMAT_BUFR_MAX - Index of the last byte of the format buffer;
* format_bufr[FORMAT_BUFR_MAX] should always be reserved
- * for a terminating nul byte.
+ * for a terminating null byte.
*/
#define FORMAT_BUFR_MAX ( sizeof( format_bufr ) - 1 )
* levels higher than DEBUGLEVEL will not be processed.
*/
-FILE *dbf = NULL;
+XFILE *dbf = NULL;
pstring debugf = "";
BOOL append_log = False;
-int DEBUGLEVEL = 1;
+BOOL debug_warn_unknown_class = True;
+BOOL debug_auto_add_unknown_class = True;
+BOOL AllowDebugChange = True;
+
+/*
+ * This is to allow assignment to DEBUGLEVEL before the debug
+ * system has been initialised.
+ */
+static int debug_all_class_hack = 1;
+static BOOL debug_all_class_isset_hack = True;
+
+static int debug_num_classes = 0;
+int *DEBUGLEVEL_CLASS = &debug_all_class_hack;
+BOOL *DEBUGLEVEL_CLASS_ISSET = &debug_all_class_isset_hack;
+
+/* DEBUGLEVEL is #defined to *debug_level */
+int DEBUGLEVEL = &debug_all_class_hack;
/* -------------------------------------------------------------------------- **
* to build the formatted output.
*
* format_pos - Marks the first free byte of the format_bufr.
+ *
+ *
+ * log_overflow - When this variable is True, never attempt to check the
+ * size of the log. This is a hack, so that we can write
+ * a message using DEBUG, from open_logs() when we
+ * are unable to open a new log file for some reason.
*/
static BOOL stdout_logging = False;
#endif
static pstring format_bufr = { '\0' };
static size_t format_pos = 0;
+static BOOL log_overflow = False;
+
+/*
+ * Define all the debug class selection names here. Names *MUST NOT* contain
+ * white space. There must be one name for each DBGC_<class name>, and they
+ * must be in the table in the order of DBGC_<class name>..
+ */
+static const char *default_classname_table[] = {
+ "all", /* DBGC_ALL; index refs traditional DEBUGLEVEL */
+ "tdb", /* DBGC_TDB */
+ "printdrivers", /* DBGC_PRINTDRIVERS */
+ "lanman", /* DBGC_LANMAN */
+ "smb", /* DBGC_SMB */
+ "rpc_parse", /* DBGC_RPC_PARSE */
+ "rpc_srv", /* DBGC_RPC_SRV */
+ "rpc_cli", /* DBGC_RPC_CLI */
+ "passdb", /* DBGC_PASSDB */
+ "auth", /* DBGC_AUTH */
+ "winbind", /* DBGC_WINBIND */
+ NULL
+};
+
+static char **classname_table = NULL;
/* -------------------------------------------------------------------------- **
* Functions...
*/
+
+/****************************************************************************
+utility lists registered debug class names's
+****************************************************************************/
+
+#define MAX_CLASS_NAME_SIZE 1024
+
+static char *debug_list_class_names_and_levels(void)
+{
+ int i, dim;
+ char **list;
+ char *buf = NULL;
+ char *b;
+ BOOL err = False;
+
+ if (DEBUGLEVEL_CLASS == &debug_all_class_hack)
+ return NULL;
+
+ list = calloc(debug_num_classes + 1, sizeof(char *));
+ if (!list)
+ return NULL;
+
+ /* prepare strings */
+ for (i = 0, dim = 0; i < debug_num_classes; i++) {
+ int l = asprintf(&list[i],
+ "%s:%d ",
+ classname_table[i],
+ DEBUGLEVEL_CLASS_ISSET[i]?DEBUGLEVEL_CLASS[i]:DEBUGLEVEL);
+ if (l < 0 || l > MAX_CLASS_NAME_SIZE) {
+ err = True;
+ goto done;
+ }
+ dim += l;
+ }
+
+ /* create single string list */
+ b = buf = malloc(dim);
+ if (!buf) {
+ err = True;
+ goto done;
+ }
+ for (i = 0; i < debug_num_classes; i++) {
+ int l = strlen(list[i]);
+ strncpy(b, list[i], l);
+ b = b + l;
+ }
+ b[-1] = '\0';
+
+done:
+ /* free strings list */
+ for (i = 0; i < debug_num_classes; i++)
+ if (list[i]) free(list[i]);
+ free(list);
+
+ if (err) {
+ if (buf)
+ free(buf);
+ return NULL;
+ } else {
+ return buf;
+ }
+}
+
+/****************************************************************************
+utility access to debug class names's
+****************************************************************************/
+const char *debug_classname_from_index(int ndx)
+{
+ if (ndx < 0 || ndx >= debug_num_classes)
+ return NULL;
+ else
+ return classname_table[ndx];
+}
+
+/****************************************************************************
+utility to translate names to debug class index's (internal version)
+****************************************************************************/
+static int debug_lookup_classname_int(const char* classname)
+{
+ int i;
+
+ if (!classname) return -1;
+
+ for (i=0; i < debug_num_classes; i++) {
+ if (strcmp(classname, classname_table[i])==0)
+ return i;
+ }
+ return -1;
+}
+
+/****************************************************************************
+Add a new debug class to the system
+****************************************************************************/
+int debug_add_class(const char *classname)
+{
+ int ndx;
+ void *new_ptr;
+
+ if (!classname)
+ return -1;
+
+ /* check the init has yet been called */
+ debug_init();
+
+ ndx = debug_lookup_classname_int(classname);
+ if (ndx >= 0)
+ return ndx;
+ ndx = debug_num_classes;
+
+ new_ptr = DEBUGLEVEL_CLASS;
+ if (DEBUGLEVEL_CLASS == &debug_all_class_hack)
+ {
+ /* Initial loading... */
+ new_ptr = NULL;
+ }
+ new_ptr = Realloc(new_ptr,
+ sizeof(int) * (debug_num_classes + 1));
+ if (!new_ptr)
+ return -1;
+ DEBUGLEVEL_CLASS = new_ptr;
+ DEBUGLEVEL_CLASS[ndx] = 0;
+
+ /* debug_level is the pointer used for the DEBUGLEVEL-thingy */
+ if (ndx==0)
+ {
+ /* Transfer the initial level from debug_all_class_hack */
+ DEBUGLEVEL_CLASS[ndx] = DEBUGLEVEL;
+ }
+ debug_level = DEBUGLEVEL_CLASS;
+
+ new_ptr = DEBUGLEVEL_CLASS_ISSET;
+ if (new_ptr == &debug_all_class_isset_hack)
+ {
+ new_ptr = NULL;
+ }
+ new_ptr = Realloc(new_ptr,
+ sizeof(BOOL) * (debug_num_classes + 1));
+ if (!new_ptr)
+ return -1;
+ DEBUGLEVEL_CLASS_ISSET = new_ptr;
+ DEBUGLEVEL_CLASS_ISSET[ndx] = False;
+
+ new_ptr = Realloc(classname_table,
+ sizeof(char *) * (debug_num_classes + 1));
+ if (!new_ptr)
+ return -1;
+ classname_table = new_ptr;
+
+ classname_table[ndx] = strdup(classname);
+ if (! classname_table[ndx])
+ return -1;
+
+ debug_num_classes++;
+
+ return ndx;
+}
+
+/****************************************************************************
+utility to translate names to debug class index's (public version)
+****************************************************************************/
+int debug_lookup_classname(const char *classname)
+{
+ int ndx;
+
+ if (!classname || !*classname) return -1;
+
+ ndx = debug_lookup_classname_int(classname);
+
+ if (ndx != -1)
+ return ndx;
+
+ if (debug_warn_unknown_class)
+ {
+ DEBUG(0, ("debug_lookup_classname(%s): Unknown class\n",
+ classname));
+ }
+ if (debug_auto_add_unknown_class)
+ {
+ return debug_add_class(classname);
+ }
+ return -1;
+}
+
+
+/****************************************************************************
+dump the current registered denug levels
+****************************************************************************/
+static void debug_dump_status(int level)
+{
+ int q;
+
+ DEBUG(level, ("INFO: Current debug levels:\n"));
+ for (q = 0; q < debug_num_classes; q++)
+ {
+ DEBUGADD(level, (" %s: %s/%d\n",
+ classname_table[q],
+ (DEBUGLEVEL_CLASS_ISSET[q]
+ ? "True" : "False"),
+ DEBUGLEVEL_CLASS[q]));
+ }
+}
+
+/****************************************************************************
+parse the debug levels from smbcontrol. Example debug level parameter:
+ printdrivers:7
+****************************************************************************/
+BOOL debug_parse_params(char **params, int *debuglevel_class,
+ BOOL *debuglevel_class_isset)
+{
+ int i, ndx;
+ char *class_name;
+ char *class_level;
+
+ if (!params)
+ return False;
+
+ /* Allow DBGC_ALL to be specified w/o requiring its class name e.g."10"
+ * v.s. "all:10", this is the traditional way to set DEBUGLEVEL
+ */
+ if (isdigit((int)params[0][0])) {
+ debuglevel_class[DBGC_ALL] = atoi(params[0]);
+ debuglevel_class_isset[DBGC_ALL] = True;
+ i = 1; /* start processing at the next params */
+ }
+ else
+ i = 0; /* DBGC_ALL not specified OR class name was included */
+
+ /* Fill in new debug class levels */
+ for (; i < debug_num_classes && params[i]; i++) {
+ if ((class_name=strtok(params[i],":")) &&
+ (class_level=strtok(NULL, "\0")) &&
+ ((ndx = debug_lookup_classname(class_name)) != -1)) {
+ debuglevel_class[ndx] = atoi(class_level);
+ debuglevel_class_isset[ndx] = True;
+ } else {
+ DEBUG(0,("debug_parse_params: unrecognized debug class name or format [%s]\n", params[i]));
+ return False;
+ }
+ }
+
+ return True;
+}
+
+/****************************************************************************
+parse the debug levels from smb.conf. Example debug level string:
+ 3 tdb:5 printdrivers:7
+Note: the 1st param has no "name:" preceeding it.
+****************************************************************************/
+BOOL debug_parse_levels(const char *params_str)
+{
+ char **params;
+
+ /* Just in case */
+ debug_init();
+
+ if (AllowDebugChange == False)
+ return True;
+
+ params = str_list_make(params_str);
+
+ if (debug_parse_params(params, DEBUGLEVEL_CLASS,
+ DEBUGLEVEL_CLASS_ISSET))
+ {
+ debug_dump_status(5);
+ str_list_free(¶ms);
+ return True;
+ } else {
+ str_list_free(¶ms);
+ return False;
+ }
+}
+
/****************************************************************************
receive a "set debug level" message
****************************************************************************/
-void debug_message(int msg_type, pid_t src, void *buf, size_t len)
+static void debug_message(int msg_type, pid_t src, void *buf, size_t len)
{
- int level;
- memcpy(&level, buf, sizeof(int));
- DEBUGLEVEL = level;
- DEBUG(1,("INFO: Debug level set to %d from pid %d\n", level, (int)src));
+ const char *params_str = buf;
+
+ /* Check, it's a proper string! */
+ if (params_str[len-1] != '\0')
+ {
+ DEBUG(1, ("Invalid debug message from pid %u to pid %u\n",
+ (unsigned int)src, (unsigned int)getpid()));
+ return;
+ }
+
+ DEBUG(3, ("INFO: Remote set of debug to `%s' (pid %u from pid %u)\n",
+ params_str, (unsigned int)getpid(), (unsigned int)src));
+
+ debug_parse_levels(params_str);
}
+
/****************************************************************************
send a "set debug level" message
****************************************************************************/
-void debug_message_send(pid_t pid, int level)
+void debug_message_send(pid_t pid, const char *params_str)
+{
+ if (!params_str)
+ return;
+ message_send_pid(pid, MSG_DEBUG, params_str, strlen(params_str) + 1,
+ False);
+}
+
+
+/****************************************************************************
+ Return current debug level.
+****************************************************************************/
+
+static void debuglevel_message(int msg_type, pid_t src, void *buf, size_t len)
{
- message_send_pid(pid, MSG_DEBUG, &level, sizeof(int), False);
+ char *debug_level_classes;
+ DEBUG(1,("INFO: Received REQ_DEBUGLEVEL message from PID %u\n",(unsigned int)src));
+
+ if ((debug_level_classes = debug_list_class_names_and_levels())) {
+ /*{ debug_level_classes = "test:1000";*/
+ message_send_pid(src, MSG_DEBUGLEVEL, debug_level_classes, strlen(debug_level_classes) + 1, True);
+ SAFE_FREE(debug_level_classes);
+ } else {
+ DEBUG(0, ("debuglevel_message: error retrieving class levels!\n"));
+ }
+}
+
+/****************************************************************************
+Init debugging (one time stuff)
+****************************************************************************/
+void debug_init(void)
+{
+ static BOOL initialised = False;
+ const char **p;
+
+ if (initialised)
+ return;
+
+ initialised = True;
+
+ message_register(MSG_DEBUG, debug_message);
+ message_register(MSG_REQ_DEBUGLEVEL, debuglevel_message);
+
+ for(p = default_classname_table; *p; p++)
+ {
+ debug_add_class(*p);
+ }
}
* get ready for syslog stuff
* ************************************************************************** **
*/
-void setup_logging(char *pname, BOOL interactive)
+void setup_logging(const char *pname, BOOL interactive)
{
- message_register(MSG_DEBUG, debug_message);
+ debug_init();
+
+ /* reset to allow multiple setup calls, going from interactive to
+ non-interactive */
+ stdout_logging = False;
+ dbf = NULL;
if (interactive) {
stdout_logging = True;
- dbf = stdout;
+ dbf = x_stdout;
}
#ifdef WITH_SYSLOG
else {
- char *p = strrchr( pname,'/' );
+ const char *p = strrchr_m( pname,'/' );
if (p)
pname = p + 1;
#ifdef LOG_DAEMON
openlog( pname, LOG_PID, SYSLOG_FACILITY );
-#else /* for old systems that have no facility codes. */
+#else
+ /* for old systems that have no facility codes. */
openlog( pname, LOG_PID );
#endif
}
/* ************************************************************************** **
* reopen the log files
* note that we now do this unconditionally
+ * We attempt to open the new debug fp before closing the old. This means
+ * if we run out of fd's we just keep using the old fd rather than aborting.
+ * Fix from dgibson@linuxcare.com.
* ************************************************************************** **
*/
-void reopen_logs( void )
+
+BOOL reopen_logs( void )
{
pstring fname;
mode_t oldumask;
+ XFILE *new_dbf = NULL;
+ BOOL ret = True;
- if (DEBUGLEVEL <= 0) {
- if (dbf) {
- (void)fclose(dbf);
- dbf = NULL;
- }
- return;
- }
+ if (stdout_logging)
+ return True;
oldumask = umask( 022 );
pstrcpy(fname, debugf );
- if (lp_loaded() && (*lp_logfile()))
- pstrcpy(fname, lp_logfile());
+
+ if (lp_loaded()) {
+ char *logfname;
+
+ logfname = lp_logfile();
+ if (*logfname)
+ pstrcpy(fname, logfname);
+ }
pstrcpy( debugf, fname );
- if (dbf)
- (void)fclose(dbf);
if (append_log)
- dbf = sys_fopen( debugf, "a" );
+ new_dbf = x_fopen( debugf, O_WRONLY|O_APPEND|O_CREAT, 0644);
else
- dbf = sys_fopen( debugf, "w" );
+ new_dbf = x_fopen( debugf, O_WRONLY|O_CREAT|O_TRUNC, 0644 );
+
+ if (!new_dbf) {
+ 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);
+ ret = False;
+ } else {
+ x_setbuf(new_dbf, NULL);
+ if (dbf)
+ (void) x_fclose(dbf);
+ dbf = new_dbf;
+ }
+
/* Fix from klausr@ITAP.Physik.Uni-Stuttgart.De
* to fix problem where smbd's that generate less
* than 100 messages keep growing the log.
*/
force_check_log_size();
- if (dbf)
- setbuf( dbf, NULL );
(void)umask(oldumask);
-}
+ return ret;
+}
/* ************************************************************************** **
* Force a check of the log size.
void check_log_size( void )
{
- int maxlog;
- SMB_STRUCT_STAT st;
+ int maxlog;
+ SMB_STRUCT_STAT st;
- /*
- * We need to be root to check/change log-file, skip this and let the main
- * loop check do a new check as root.
- */
+ /*
+ * We need to be root to check/change log-file, skip this and let the main
+ * loop check do a new check as root.
+ */
- if( geteuid() != 0 )
- return;
+ if( geteuid() != 0 )
+ return;
- if( !need_to_check_log_size() )
- return;
+ if(log_overflow || !need_to_check_log_size() )
+ return;
- maxlog = lp_max_log_size() * 1024;
+ maxlog = lp_max_log_size() * 1024;
- if( sys_fstat( fileno( dbf ), &st ) == 0 && st.st_size > maxlog )
- {
- (void)fclose( dbf );
- dbf = NULL;
- reopen_logs();
- if( dbf && get_file_size( debugf ) > maxlog )
- {
- pstring name;
+ if( sys_fstat( x_fileno( dbf ), &st ) == 0 && st.st_size > maxlog ) {
+ (void)reopen_logs();
+ if( dbf && get_file_size( debugf ) > maxlog ) {
+ pstring name;
- (void)fclose( dbf );
- dbf = NULL;
- slprintf( name, sizeof(name)-1, "%s.old", debugf );
- (void)rename( debugf, name );
- reopen_logs();
- }
- }
- /*
- * Here's where we need to panic if dbf == NULL..
- */
- if(dbf == NULL) {
- dbf = sys_fopen( "/dev/console", "w" );
- if(dbf) {
- DEBUG(0,("check_log_size: open of debug file %s failed - using console.\n",
- debugf ));
- } else {
- /*
- * We cannot continue without a debug file handle.
- */
- abort();
- }
- }
- debug_count = 0;
+ slprintf( name, sizeof(name)-1, "%s.old", debugf );
+ (void)rename( debugf, name );
+
+ if (!reopen_logs()) {
+ /* We failed to reopen a log - continue using the old name. */
+ (void)rename(name, debugf);
+ }
+ }
+ }
+
+ /*
+ * Here's where we need to panic if dbf == NULL..
+ */
+
+ if(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
+ * reached when opening the logs for the first time: at
+ * startup or when the log level is increased from zero.
+ * -dwg 6 June 2000
+ */
+ dbf = x_fopen( "/dev/console", O_WRONLY, 0);
+ if(dbf) {
+ DEBUG(0,("check_log_size: open of debug file %s failed - using console.\n",
+ debugf ));
+ } else {
+ /*
+ * We cannot continue without a debug file handle.
+ */
+ abort();
+ }
+ }
+ debug_count = 0;
} /* check_log_size */
/* ************************************************************************** **
* This is called by dbghdr() and format_debug_text().
* ************************************************************************** **
*/
-#ifdef HAVE_STDARG_H
int Debug1( char *format_str, ... )
{
-#else
- int Debug1(va_alist)
-va_dcl
-{
- char *format_str;
-#endif
va_list ap;
int old_errno = errno;
if( stdout_logging )
{
-#ifdef HAVE_STDARG_H
va_start( ap, format_str );
-#else
- va_start( ap );
- format_str = va_arg( ap, char * );
-#endif
if(dbf)
- (void)vfprintf( dbf, format_str, ap );
+ (void)x_vfprintf( dbf, format_str, ap );
va_end( ap );
errno = old_errno;
return( 0 );
mode_t oldumask = umask( 022 );
if( append_log )
- dbf = sys_fopen( debugf, "a" );
+ dbf = x_fopen( debugf, O_WRONLY|O_APPEND|O_CREAT, 0644 );
else
- dbf = sys_fopen( debugf, "w" );
+ dbf = x_fopen( debugf, O_WRONLY|O_CREAT|O_TRUNC, 0644 );
(void)umask( oldumask );
if( dbf )
{
- setbuf( dbf, NULL );
+ x_setbuf( dbf, NULL );
}
else
{
else
priority = priority_map[syslog_level];
-#ifdef HAVE_STDARG_H
va_start( ap, format_str );
-#else
- va_start( ap );
- format_str = va_arg( ap, char * );
-#endif
vslprintf( msgbuf, sizeof(msgbuf)-1, format_str, ap );
va_end( ap );
if( !lp_syslog_only() )
#endif
{
-#ifdef HAVE_STDARG_H
va_start( ap, format_str );
-#else
- va_start( ap );
- format_str = va_arg( ap, char * );
-#endif
if(dbf)
- (void)vfprintf( dbf, format_str, ap );
+ (void)x_vfprintf( dbf, format_str, ap );
va_end( ap );
if(dbf)
- (void)fflush( dbf );
+ (void)x_fflush( dbf );
}
errno = old_errno;
{
bufr_print();
if(dbf)
- (void)fflush( dbf );
+ (void)x_fflush( dbf );
} /* dbgflush */
/* ************************************************************************** **
* Print a Debug Header.
*
* Input: level - Debug level of the message (not the system-wide debug
- * level.
+ * level. )
* file - Pointer to a string containing the name of the file
* from which this function was called, or an empty string
* if the __FILE__ macro is not implemented.
*
* ************************************************************************** **
*/
-#ifdef HAVE_STDARG_H
BOOL dbgtext( char *format_str, ... )
{
va_list ap;
return( True );
} /* dbgtext */
-#else
- BOOL dbgtext( va_alist )
- va_dcl
- {
- char *format_str;
- va_list ap;
- pstring msgbuf;
-
- va_start( ap );
- format_str = va_arg( ap, char * );
- vslprintf( msgbuf, sizeof(msgbuf)-1, format_str, ap );
- va_end( ap );
-
- format_debug_text( msgbuf );
-
- return( True );
- } /* dbgtext */
-
-#endif
/* ************************************************************************** */