Fix build with arc4.
[kai/samba.git] / source3 / lib / debug.c
index 49ec40ae848f0b72cfbd0b2c8e5d427575797094..d835ea7c176e5a824b387a587f21ba94991bf6e5 100644 (file)
@@ -90,10 +90,11 @@ bool    AllowDebugChange = True;
 */
 bool    override_logfile;
 
+static TALLOC_CTX *tmp_debug_ctx;
 
 /*
  * This is to allow assignment to DEBUGLEVEL before the debug
- * system has been initialised.
+ * system has been initialized.
  */
 static int debug_all_class_hack = 1;
 static bool debug_all_class_isset_hack = True;
@@ -182,6 +183,8 @@ static char **classname_table = NULL;
  Free memory pointed to by global pointers.
 ****************************************************************************/
 
+static bool initialized;
+
 void gfree_debugsyms(void)
 {
        int i;
@@ -193,11 +196,23 @@ void gfree_debugsyms(void)
                SAFE_FREE( classname_table );
        }
 
-       if ( DEBUGLEVEL_CLASS != &debug_all_class_hack )
+       if ( DEBUGLEVEL_CLASS != &debug_all_class_hack ) {
                SAFE_FREE( DEBUGLEVEL_CLASS );
+               DEBUGLEVEL_CLASS = &debug_all_class_hack;
+       }
 
-       if ( DEBUGLEVEL_CLASS_ISSET != &debug_all_class_isset_hack )
+       if ( DEBUGLEVEL_CLASS_ISSET != &debug_all_class_isset_hack ) {
                SAFE_FREE( DEBUGLEVEL_CLASS_ISSET );
+               DEBUGLEVEL_CLASS_ISSET = &debug_all_class_isset_hack;
+       }
+
+       SAFE_FREE(format_bufr);
+
+       debug_num_classes = 0;
+
+       debug_level = DEBUGLEVEL_CLASS;
+
+       initialized = false;
 }
 
 /****************************************************************************
@@ -426,8 +441,9 @@ static bool debug_parse_params(char **params)
 
        /* 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")) &&
+               char *saveptr;
+               if ((class_name = strtok_r(params[i],":", &saveptr)) &&
+                       (class_level = strtok_r(NULL, "\0", &saveptr)) &&
             ((ndx = debug_lookup_classname(class_name)) != -1)) {
                                DEBUGLEVEL_CLASS[ndx] = atoi(class_level);
                                DEBUGLEVEL_CLASS_ISSET[ndx] = True;
@@ -456,14 +472,14 @@ bool debug_parse_levels(const char *params_str)
        if (AllowDebugChange == False)
                return True;
 
-       params = str_list_make(params_str, NULL);
+       params = str_list_make(talloc_tos(), params_str, NULL);
 
        if (debug_parse_params(params)) {
                debug_dump_status(5);
-               str_list_free(&params);
+               TALLOC_FREE(params);
                return True;
        } else {
-               str_list_free(&params);
+               TALLOC_FREE(params);
                return False;
        }
 }
@@ -472,7 +488,7 @@ bool debug_parse_levels(const char *params_str)
  Receive a "set debug level" message.
 ****************************************************************************/
 
-static void debug_message(struct messaging_context *msg_ctx,
+void debug_message(struct messaging_context *msg_ctx,
                          void *private_data, 
                          uint32_t msg_type, 
                          struct server_id src,
@@ -526,18 +542,17 @@ Init debugging (one time stuff)
 
 void debug_init(void)
 {
-       static bool initialised = False;
        const char **p;
 
-       if (initialised)
+       if (initialized)
                return;
 
-       initialised = True;
+       initialized = true;
 
        for(p = default_classname_table; *p; p++) {
                debug_add_class(*p);
        }
-       format_bufr = SMB_MALLOC(FORMAT_BUFR_SIZE);
+       format_bufr = (char *)SMB_MALLOC(FORMAT_BUFR_SIZE);
        if (!format_bufr) {
                smb_panic("debug_init: unable to create buffer");
        }
@@ -784,13 +799,13 @@ void check_log_size( void )
                        (void)x_vfprintf( dbf, format_str, ap );
                va_end( ap );
                errno = old_errno;
-               return( 0 );
+               goto done;
        }
 
        /* prevent recursion by checking if reopen_logs() has temporaily
           set the debugf string to NULL */
        if( debugf == NULL)
-               return( 0 );
+               goto done;
 
 #ifdef WITH_SYSLOG
        if( !lp_syslog_only() )
@@ -805,7 +820,7 @@ void check_log_size( void )
                                x_setbuf( dbf, NULL );
                        } else {
                                errno = old_errno;
-                               return(0);
+                               goto done;
                        }
                }
        }
@@ -823,6 +838,7 @@ void check_log_size( void )
                };
                int     priority;
                char *msgbuf = NULL;
+               int ret;
 
                if( syslog_level >= ( sizeof(priority_map) / sizeof(priority_map[0]) ) || syslog_level < 0)
                        priority = LOG_DEBUG;
@@ -830,10 +846,10 @@ void check_log_size( void )
                        priority = priority_map[syslog_level];
 
                va_start(ap, format_str);
-               vasprintf(&msgbuf, format_str, ap);
+               ret = vasprintf(&msgbuf, format_str, ap);
                va_end(ap);
 
-               if (msgbuf) {
+               if (ret == -1) {
                        syslog(priority, "%s", msgbuf);
                }
                SAFE_FREE(msgbuf);
@@ -854,6 +870,9 @@ void check_log_size( void )
                        (void)x_fflush( dbf );
        }
 
+ done:
+       TALLOC_FREE(tmp_debug_ctx);
+
        errno = old_errno;
 
        return( 0 );
@@ -894,6 +913,10 @@ static void format_debug_text( const char *msg )
        size_t i;
        bool timestamp = (!stdout_logging && (lp_timestamp_logs() || !(lp_loaded())));
 
+       if (!format_bufr) {
+               debug_init();
+       }
+
        for( i = 0; msg[i]; i++ ) {
                /* Indent two spaces at each new line. */
                if(timestamp && 0 == format_pos) {
@@ -1017,12 +1040,14 @@ bool dbghdr(int level, int cls, const char *file, const char *func, int line)
                /* Print it all out at once to prevent split syslog output. */
                if( lp_debug_prefix_timestamp() ) {
                    (void)Debug1( "[%s, %2d%s] ",
-                       current_timestring(lp_debug_hires_timestamp()), level,
-                       header_str);
+                       current_timestring(debug_ctx(),
+                                          lp_debug_hires_timestamp()),
+                       level, header_str);
                } else {
                    (void)Debug1( "[%s, %2d%s] %s:%s(%d)\n",
-                       current_timestring(lp_debug_hires_timestamp()), level,
-                       header_str, file, func, line );
+                       current_timestring(debug_ctx(),
+                                          lp_debug_hires_timestamp()),
+                       level, header_str, file, func, line );
                }
        }
 
@@ -1048,12 +1073,13 @@ bool dbghdr(int level, int cls, const char *file, const char *func, int line)
        va_list ap;
        char *msgbuf = NULL;
        bool ret = true;
+       int res;
 
        va_start(ap, format_str);
-       vasprintf(&msgbuf, format_str, ap);
+       res = vasprintf(&msgbuf, format_str, ap);
        va_end(ap);
 
-       if (msgbuf) {
+       if (res != -1) {
                format_debug_text(msgbuf);
        } else {
                ret = false;
@@ -1061,3 +1087,14 @@ bool dbghdr(int level, int cls, const char *file, const char *func, int line)
        SAFE_FREE(msgbuf);
        return ret;
 }
+
+/*
+ * Get us a temporary talloc context usable just for DEBUG arguments
+ */
+TALLOC_CTX *debug_ctx(void)
+{
+        if (tmp_debug_ctx == NULL) {
+                tmp_debug_ctx = talloc_named_const(NULL, 0, "debug_ctx");
+        }
+        return tmp_debug_ctx;
+}