updated the 3.0 branch from the head branch - ready for alpha18
[nivanova/samba-autobuild/.git] / source3 / lib / debug.c
index 9eb490c27ea0861ca89410d88a486477e7807266..f41c3b649760961fa8a4514036a3f731b4c9b1f8 100644 (file)
@@ -1,8 +1,9 @@
 /*
-   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
@@ -26,7 +27,7 @@
  *
  *  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 )
 XFILE   *dbf        = NULL;
 pstring debugf     = "";
 BOOL    append_log = False;
+BOOL    debug_warn_unknown_class = True;
+BOOL    debug_auto_add_unknown_class = True;
+BOOL    AllowDebugChange = True;
 
-int     DEBUGLEVEL_CLASS[DBGC_LAST];
-BOOL    DEBUGLEVEL_CLASS_ISSET[DBGC_LAST];
-int     DEBUGLEVEL = DEBUGLEVEL_CLASS;
+/*
+ * 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;
 
 
 /* -------------------------------------------------------------------------- **
@@ -125,50 +139,234 @@ 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>.. 
-*/
-char *classname_table[] = {
+ * 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",               /* DBGC_RPC          */
-       "rpc_hdr",           /* DBGC_RPC_HDR      */
-       "bdc",               /* DBGC_BDC          */
+       "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
 ****************************************************************************/
-char* debug_classname_from_index(int ndx)
+const char *debug_classname_from_index(int ndx)
 {
-       return classname_table[ndx];
+       if (ndx < 0 || ndx >= debug_num_classes)
+               return NULL;
+       else
+               return classname_table[ndx];
 }
 
 /****************************************************************************
-utility to translate names to debug class index's
+utility to translate names to debug class index's (internal version)
 ****************************************************************************/
-int debug_lookup_classname(char* classname)
+static int debug_lookup_classname_int(const char* classname)
 {
        int i;
 
        if (!classname) return -1;
 
-       for (i=0; i<DBGC_LAST; i++) {
+       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
@@ -179,11 +377,11 @@ BOOL debug_parse_params(char **params, int *debuglevel_class,
        int   i, ndx;
        char *class_name;
        char *class_level;
-       
-       /* Set the new debug level array to the current DEBUGLEVEL array */
-       memcpy(debuglevel_class, DEBUGLEVEL_CLASS, sizeof(DEBUGLEVEL_CLASS));
 
-       /* Allow DBGC_ALL to be specifies w/o requiring its class name e.g."10"  
+       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])) {
@@ -192,10 +390,10 @@ BOOL debug_parse_params(char **params, int *debuglevel_class,
                i = 1; /* start processing at the next params */
        }
        else
-               i = 0; /* DBGC_ALL not specified  OR class name was included */
+               i = 0; /* DBGC_ALL not specified OR class name was included */
 
        /* Fill in new debug class levels */
-       for (; i < DBGC_LAST && params[i]; i++) {
+       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)) {
@@ -215,81 +413,102 @@ 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(char *params_str)
+BOOL debug_parse_levels(const char *params_str)
 {
-       int  i;
-       char *params[DBGC_LAST];
-       int  debuglevel_class[DBGC_LAST];       
-       BOOL debuglevel_class_isset[DBGC_LAST];
-
-       ZERO_ARRAY(params);
-       ZERO_ARRAY(debuglevel_class);
-       ZERO_ARRAY(debuglevel_class_isset);
-
-       if ((params[0]=strtok(params_str," ,"))) {
-               for (i=1; i<DBGC_LAST;i++) {
-                       if ((params[i]=strtok(NULL," ,"))==NULL)
-                               break;
-               }
-       }
-       else
-               return False;
-
-       if (debug_parse_params(params, debuglevel_class, 
-                              debuglevel_class_isset)) {
-               debug_message(0, getpid(), (void*)debuglevel_class, sizeof(debuglevel_class));
+       char **params;
 
-               memcpy(DEBUGLEVEL_CLASS, debuglevel_class, 
-                      sizeof(debuglevel_class));
+       /* Just in case */
+       debug_init();
 
-               memcpy(DEBUGLEVEL_CLASS_ISSET, debuglevel_class_isset,
-                      sizeof(debuglevel_class_isset));
-
-               {
-                       int q;
+       if (AllowDebugChange == False)
+               return True;
 
-                       for (q = 0; q < DBGC_LAST; q++)
-                               DEBUG(5, ("%s: %d/%d\n",
-                                         classname_table[q],
-                                         DEBUGLEVEL_CLASS[q],
-                                         DEBUGLEVEL_CLASS_ISSET[q]));
-               }
+       params = str_list_make(params_str);
 
+       if (debug_parse_params(params, DEBUGLEVEL_CLASS,
+                              DEBUGLEVEL_CLASS_ISSET))
+       {
+               debug_dump_status(5);
+               str_list_free(&params);
                return True;
-       } else
+       } else {
+               str_list_free(&params);
                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)
 {
-       struct debuglevel_message *dm = (struct debuglevel_message *)buf;
-       int i;
+       const char *params_str = buf;
 
-       /* Set the new DEBUGLEVEL_CLASS array from the passed message */
-       memcpy(DEBUGLEVEL_CLASS, dm->debuglevel_class, sizeof(dm->debuglevel_class));
-       memcpy(DEBUGLEVEL_CLASS_ISSET, dm->debuglevel_class_isset, sizeof(dm->debuglevel_class_isset));
+       /* 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: Debug class %s level = %d   (pid %u from pid %u)\n",
-                       classname_table[DBGC_ALL],
-                       DEBUGLEVEL_CLASS[DBGC_ALL], (unsigned int)getpid(), (unsigned int)src));
+       DEBUG(3, ("INFO: Remote set of debug to `%s'  (pid %u from pid %u)\n",
+                 params_str, (unsigned int)getpid(), (unsigned int)src));
 
-       for (i=1; i<DBGC_LAST; i++) {
-               if (DEBUGLEVEL_CLASS[i])
-                        DEBUGADD(3,("INFO: Debug class %s level = %d\n", 
-                                               classname_table[i], DEBUGLEVEL_CLASS[i]));
-       }
+       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);
+       }
 }
 
 
@@ -297,9 +516,9 @@ void debug_message_send(pid_t pid, int level)
  * 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 */
@@ -312,12 +531,13 @@ void setup_logging(char *pname, BOOL interactive)
        }
 #ifdef WITH_SYSLOG
        else {
-               char *p = strrchr_m( 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
        }
@@ -346,8 +566,14 @@ BOOL reopen_logs( void )
        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 (append_log)
@@ -359,7 +585,8 @@ BOOL reopen_logs( void )
                log_overflow = True;
                DEBUG(0, ("Unable to open new log file %s: %s\n", debugf, strerror(errno)));
                log_overflow = False;
-               x_fflush(dbf);
+               if (dbf)
+                       x_fflush(dbf);
                ret = False;
        } else {
                x_setbuf(new_dbf, NULL);
@@ -450,12 +677,12 @@ void check_log_size( void )
 
        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
-               */
+                * 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",
@@ -654,7 +881,7 @@ void dbgflush( void )
  * 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.