[PATCHSET] Clean up debug.c

Volker Lendecke Volker.Lendecke at SerNet.DE
Wed Jul 30 09:55:36 MDT 2014


Hi!

Attached find a patchset with two trivial cleanup patches at
the beginning and a bunch of patches that reduce the
dependencies of debug.c a bit. The goal is to make DEBUG()
usable in lowlevel code without pulling in too much stuff. I
have a few more patches for this that work on close_low_fds,
but when that is done, debug.c is pretty slim wrt deps.

Comments / Review would be appreciated!

Thanks,

Volker

-- 
SerNet GmbH, Bahnhofsallee 1b, 37081 Göttingen
phone: +49-551-370000-0, fax: +49-551-370000-9
AG Göttingen, HRB 2816, GF: Dr. Johannes Loxen
http://www.sernet.de, mailto:kontakt at sernet.de
-------------- next part --------------
From 4e23fa941c5d65e1d1d30cffcb006bab5de4a7d0 Mon Sep 17 00:00:00 2001
From: Volker Lendecke <vl at samba.org>
Date: Tue, 29 Jul 2014 10:05:42 +0000
Subject: [PATCH 01/28] libcli: Fix a typo

Signed-off-by: Volker Lendecke <vl at samba.org>
---
 source4/libcli/resolve/dns_ex.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/source4/libcli/resolve/dns_ex.c b/source4/libcli/resolve/dns_ex.c
index 1226ed6..0935928 100644
--- a/source4/libcli/resolve/dns_ex.c
+++ b/source4/libcli/resolve/dns_ex.c
@@ -494,7 +494,7 @@ static void pipe_handler(struct tevent_context *ev, struct tevent_fd *fde,
 		return;
 	}
 
-	/* enusre the address looks good */
+	/* ensure the address looks good */
 	address[ret] = 0;
 
 	addrs = str_list_make(state, address, ",");
-- 
1.7.9.5


From 56be2993769cca3c30ffc996a813dfb905801b8d Mon Sep 17 00:00:00 2001
From: Volker Lendecke <vl at samba.org>
Date: Mon, 28 Jul 2014 12:27:29 +0000
Subject: [PATCH 02/28] lib: Remove unused serverid_register_msg_flags()

Signed-off-by: Volker Lendecke <vl at samba.org>
---
 source3/include/serverid.h |    6 -----
 source3/lib/serverid.c     |   55 --------------------------------------------
 2 files changed, 61 deletions(-)

diff --git a/source3/include/serverid.h b/source3/include/serverid.h
index 9c84b74..d1d405a 100644
--- a/source3/include/serverid.h
+++ b/source3/include/serverid.h
@@ -34,12 +34,6 @@ bool serverid_register(const struct server_id id, uint32_t msg_flags);
 bool serverid_deregister(const struct server_id id);
 
 /*
- * (De)register additional message flags
- */
-bool serverid_register_msg_flags(const struct server_id id, bool do_reg,
-				 uint32_t msg_flags);
-
-/*
  * Check existence of a server id
  */
 bool serverid_exists(const struct server_id *id);
diff --git a/source3/lib/serverid.c b/source3/lib/serverid.c
index f982098..ee479b4 100644
--- a/source3/lib/serverid.c
+++ b/source3/lib/serverid.c
@@ -124,61 +124,6 @@ done:
 	return ret;
 }
 
-bool serverid_register_msg_flags(const struct server_id id, bool do_reg,
-				 uint32_t msg_flags)
-{
-	struct db_context *db;
-	struct serverid_key key;
-	struct serverid_data *data;
-	struct db_record *rec;
-	TDB_DATA tdbkey;
-	TDB_DATA value;
-	NTSTATUS status;
-	bool ret = false;
-
-	db = serverid_db();
-	if (db == NULL) {
-		return false;
-	}
-
-	serverid_fill_key(&id, &key);
-	tdbkey = make_tdb_data((uint8_t *)&key, sizeof(key));
-
-	rec = dbwrap_fetch_locked(db, talloc_tos(), tdbkey);
-	if (rec == NULL) {
-		DEBUG(1, ("Could not fetch_lock serverid.tdb record\n"));
-		return false;
-	}
-
-	value = dbwrap_record_get_value(rec);
-
-	if (value.dsize != sizeof(struct serverid_data)) {
-		DEBUG(1, ("serverid record has unexpected size %d "
-			  "(wanted %d)\n", (int)value.dsize,
-			  (int)sizeof(struct serverid_data)));
-		goto done;
-	}
-
-	data = (struct serverid_data *)value.dptr;
-
-	if (do_reg) {
-		data->msg_flags |= msg_flags;
-	} else {
-		data->msg_flags &= ~msg_flags;
-	}
-
-	status = dbwrap_record_store(rec, value, 0);
-	if (!NT_STATUS_IS_OK(status)) {
-		DEBUG(1, ("Storing serverid.tdb record failed: %s\n",
-			  nt_errstr(status)));
-		goto done;
-	}
-	ret = true;
-done:
-	TALLOC_FREE(rec);
-	return ret;
-}
-
 bool serverid_deregister(struct server_id id)
 {
 	struct db_context *db;
-- 
1.7.9.5


From 2916ca70f74817af834bcfc6b6a35ae0bb61ccd6 Mon Sep 17 00:00:00 2001
From: Volker Lendecke <vl at samba.org>
Date: Tue, 29 Jul 2014 10:23:04 +0000
Subject: [PATCH 03/28] debug: Fix indentation in debug_parse_params

Signed-off-by: Volker Lendecke <vl at samba.org>
---
 lib/util/debug.c |   11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

diff --git a/lib/util/debug.c b/lib/util/debug.c
index 80a3460..b7337a7 100644
--- a/lib/util/debug.c
+++ b/lib/util/debug.c
@@ -365,16 +365,17 @@ static bool debug_parse_params(char **params)
 	for (ndx = DBGC_ALL; ndx < debug_num_classes; ndx++) {
 		DEBUGLEVEL_CLASS[ndx] = DEBUGLEVEL_CLASS[DBGC_ALL];
 	}
-		
+
 	/* Fill in new debug class levels */
 	for (; i < debug_num_classes && params[i]; i++) {
 		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);
+		    (class_level = strtok_r(NULL, "\0", &saveptr)) &&
+		    ((ndx = debug_lookup_classname(class_name)) != -1)) {
+			DEBUGLEVEL_CLASS[ndx] = atoi(class_level);
 		} else {
-			DEBUG(0,("debug_parse_params: unrecognized debug class name or format [%s]\n", params[i]));
+			DEBUG(0,("debug_parse_params: unrecognized debug "
+				 "class name or format [%s]\n", params[i]));
 			return false;
 		}
 	}
-- 
1.7.9.5


From 1848f9e6d69c2c96b81f7d4a04fc48fc39e985a4 Mon Sep 17 00:00:00 2001
From: Volker Lendecke <vl at samba.org>
Date: Tue, 29 Jul 2014 10:25:32 +0000
Subject: [PATCH 04/28] debug: Move variables where they are used

Signed-off-by: Volker Lendecke <vl at samba.org>
---
 lib/util/debug.c |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/lib/util/debug.c b/lib/util/debug.c
index b7337a7..6fba35e 100644
--- a/lib/util/debug.c
+++ b/lib/util/debug.c
@@ -344,8 +344,6 @@ static void debug_dump_status(int level)
 static bool debug_parse_params(char **params)
 {
 	int   i, ndx;
-	char *class_name;
-	char *class_level;
 
 	if (!params)
 		return false;
@@ -368,6 +366,8 @@ static bool debug_parse_params(char **params)
 
 	/* Fill in new debug class levels */
 	for (; i < debug_num_classes && params[i]; i++) {
+		char *class_name;
+		char *class_level;
 		char *saveptr;
 		if ((class_name = strtok_r(params[i],":", &saveptr)) &&
 		    (class_level = strtok_r(NULL, "\0", &saveptr)) &&
-- 
1.7.9.5


From d985f80883527648a731a4f278eab85f0969f414 Mon Sep 17 00:00:00 2001
From: Volker Lendecke <vl at samba.org>
Date: Tue, 29 Jul 2014 11:05:06 +0000
Subject: [PATCH 05/28] debug: Factor out debug_parse_param()

Signed-off-by: Volker Lendecke <vl at samba.org>
---
 lib/util/debug.c |   39 +++++++++++++++++++++++++++++++--------
 1 file changed, 31 insertions(+), 8 deletions(-)

diff --git a/lib/util/debug.c b/lib/util/debug.c
index 6fba35e..40c4634 100644
--- a/lib/util/debug.c
+++ b/lib/util/debug.c
@@ -336,6 +336,33 @@ static void debug_dump_status(int level)
 	}
 }
 
+static bool debug_parse_param(char *param)
+{
+	char *class_name;
+	char *class_level;
+	char *saveptr;
+	int ndx;
+
+	class_name = strtok_r(param, ":", &saveptr);
+	if (class_name == NULL) {
+		return false;
+	}
+
+	class_level = strtok_r(NULL, "\0", &saveptr);
+	if (class_level == NULL) {
+		return false;
+	}
+
+	ndx = debug_lookup_classname(class_name);
+	if (ndx == -1) {
+		return false;
+	}
+
+	DEBUGLEVEL_CLASS[ndx] = atoi(class_level);
+
+	return true;
+}
+
 /****************************************************************************
  parse the debug levels from smbcontrol. Example debug level parameter:
  printdrivers:7
@@ -366,14 +393,10 @@ static bool debug_parse_params(char **params)
 
 	/* Fill in new debug class levels */
 	for (; i < debug_num_classes && params[i]; i++) {
-		char *class_name;
-		char *class_level;
-		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);
-		} else {
+		bool ok;
+
+		ok = debug_parse_param(params[i]);
+		if (!ok) {
 			DEBUG(0,("debug_parse_params: unrecognized debug "
 				 "class name or format [%s]\n", params[i]));
 			return false;
-- 
1.7.9.5


From 44488532f68626cfe33a633673c38bebcd4e21b1 Mon Sep 17 00:00:00 2001
From: Volker Lendecke <vl at samba.org>
Date: Tue, 29 Jul 2014 11:20:27 +0000
Subject: [PATCH 06/28] debug: Add some readability to debug_logtype

Signed-off-by: Volker Lendecke <vl at samba.org>
---
 lib/util/debug.h |    9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/lib/util/debug.h b/lib/util/debug.h
index f7ebfc0..3dcda84 100644
--- a/lib/util/debug.h
+++ b/lib/util/debug.h
@@ -207,7 +207,14 @@ extern int  *DEBUGLEVEL_CLASS;
  * for example.  This makes it easy to override for debug to stderr on
  * the command line, as the smb.conf cannot reset it back to
  * file-based logging */
-enum debug_logtype {DEBUG_DEFAULT_STDERR = 0, DEBUG_DEFAULT_STDOUT = 1, DEBUG_FILE = 2, DEBUG_STDOUT = 3, DEBUG_STDERR = 4, DEBUG_CALLBACK = 5};
+enum debug_logtype {
+	DEBUG_DEFAULT_STDERR = 0,
+	DEBUG_DEFAULT_STDOUT = 1,
+	DEBUG_FILE = 2,
+	DEBUG_STDOUT = 3,
+	DEBUG_STDERR = 4,
+	DEBUG_CALLBACK = 5
+};
 
 struct debug_settings {
 	size_t max_log_size;
-- 
1.7.9.5


From 91efa4afff6f4d928ede127e6d3b17477520a19e Mon Sep 17 00:00:00 2001
From: Volker Lendecke <vl at samba.org>
Date: Tue, 29 Jul 2014 11:22:09 +0000
Subject: [PATCH 07/28] debug: Remove a dependency on charconv

util_str has:

        /* characters below 0x3F are guaranteed to not appear in
           non-initial position in multi-byte charsets */
        if ((c & 0xC0) == 0) {
                return strrchr(s, c);
        }

'/' is 0x2f, so there's no point in calling strrchr_m and thus pulling
in a whole lot of stuff

Signed-off-by: Volker Lendecke <vl at samba.org>
---
 lib/util/debug.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lib/util/debug.c b/lib/util/debug.c
index 40c4634..00cef7d 100644
--- a/lib/util/debug.c
+++ b/lib/util/debug.c
@@ -498,7 +498,7 @@ void setup_logging(const char *prog_name, enum debug_logtype new_logtype)
 
 	if (state.logtype == DEBUG_FILE) {
 #ifdef WITH_SYSLOG
-		const char *p = strrchr_m( prog_name,'/' );
+		const char *p = strrchr(prog_name, '/');
 		if (p)
 			prog_name = p + 1;
 #ifdef LOG_DAEMON
-- 
1.7.9.5


From 995d6c0de661118c7cfa9757a4a12a360545abb9 Mon Sep 17 00:00:00 2001
From: Volker Lendecke <vl at samba.org>
Date: Tue, 29 Jul 2014 12:11:54 +0000
Subject: [PATCH 08/28] nmbd: Call DEBUGADD instead of Debug1

These were the only external callers of Debug1. To be honest, I don't
really care if these debug messages now look a bit differently :-)

Signed-off-by: Volker Lendecke <vl at samba.org>
---
 source3/nmbd/nmbd_browserdb.c |   17 +++++++----------
 1 file changed, 7 insertions(+), 10 deletions(-)

diff --git a/source3/nmbd/nmbd_browserdb.c b/source3/nmbd/nmbd_browserdb.c
index bd02723..b36f5c7 100644
--- a/source3/nmbd/nmbd_browserdb.c
+++ b/source3/nmbd/nmbd_browserdb.c
@@ -122,12 +122,10 @@ struct browse_cache_record *create_browser_in_lmb_cache( const char *work_name,
  
 	DLIST_ADD_END(lmb_browserlist, browc, struct browse_cache_record *);
 
-	if( DEBUGLVL( 3 ) ) {
-		Debug1( "nmbd_browserdb:create_browser_in_lmb_cache()\n" );
-		Debug1( "  Added lmb cache entry for workgroup %s ", browc->work_group );
-		Debug1( "name %s IP %s ", browc->lmb_name, inet_ntoa(ip) );
-		Debug1( "ttl %d\n", (int)browc->death_time );
-	}
+	DEBUG(3, ("nmbd_browserdb:create_browser_in_lmb_cache()\n"));
+	DEBUGADD(3, ("  Added lmb cache entry for workgroup %s name %s IP %s "
+		     "ttl %d\n", browc->work_group, browc->lmb_name,
+		     inet_ntoa(ip), (int)browc->death_time));
   
 	return( browc );
 }
@@ -172,10 +170,9 @@ void expire_lmb_browsers( time_t t )
 		nextbrowc = browc->next;
 
 		if( browc->death_time < t ) {
-			if( DEBUGLVL( 3 ) ) {
-				Debug1( "nmbd_browserdb:expire_lmb_browsers()\n" );
-				Debug1( "  Removing timed out lmb entry %s\n", browc->lmb_name );
-			}
+			DEBUG(3, ("nmbd_browserdb:expire_lmb_browsers()\n"));
+			DEBUGADD(3, ("  Removing timed out lmb entry %s\n",
+				     browc->lmb_name));
 			remove_lmb_browser_entry( browc );
 		}
 	}
-- 
1.7.9.5


From 71f000b592e1453da9670f948cd8ed4cd034996e Mon Sep 17 00:00:00 2001
From: Volker Lendecke <vl at samba.org>
Date: Tue, 29 Jul 2014 12:22:05 +0000
Subject: [PATCH 09/28] debug: Make Debug1 static

Signed-off-by: Volker Lendecke <vl at samba.org>
---
 lib/util/debug.c |    2 +-
 lib/util/debug.h |    1 -
 2 files changed, 1 insertion(+), 2 deletions(-)

diff --git a/lib/util/debug.c b/lib/util/debug.c
index 00cef7d..6ff5050 100644
--- a/lib/util/debug.c
+++ b/lib/util/debug.c
@@ -767,7 +767,7 @@ void check_log_size( void )
  This is called by dbghdr() and format_debug_text().
 ************************************************************************/
 
- int Debug1( const char *format_str, ... )
+static int Debug1( const char *format_str, ... )
 {
 	va_list ap;
 	int old_errno = errno;
diff --git a/lib/util/debug.h b/lib/util/debug.h
index 3dcda84..4ba1ec1 100644
--- a/lib/util/debug.h
+++ b/lib/util/debug.h
@@ -36,7 +36,6 @@
 #define MAX_DEBUG_LEVEL 1000
 #endif
 
-int  Debug1( const char *, ... ) PRINTF_ATTRIBUTE(1,2);
 bool dbgtext( const char *, ... ) PRINTF_ATTRIBUTE(1,2);
 bool dbghdrclass( int level, int cls, const char *location, const char *func);
 bool dbghdr( int level, const char *location, const char *func);
-- 
1.7.9.5


From 6a1ddc1ea0d55dc0f1e4b1042dab96589f1314f4 Mon Sep 17 00:00:00 2001
From: Volker Lendecke <vl at samba.org>
Date: Tue, 29 Jul 2014 12:48:55 +0000
Subject: [PATCH 10/28] debug: Fix debug_parse_params

The additional check this patch removes potentially drops some debuglevel
class settings. While not documented, it is possible to add a debug class
like for example "tdb:10" multiple times with multiple values. Later
settings overwrite earlier ones, but only if the overall number of
settings does not exceed the total number of available debug classes.

This patch changes our behaviour, but I can't imagine a situation where
someone relies on this check.

Signed-off-by: Volker Lendecke <vl at samba.org>
---
 lib/util/debug.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lib/util/debug.c b/lib/util/debug.c
index 6ff5050..e4a1741 100644
--- a/lib/util/debug.c
+++ b/lib/util/debug.c
@@ -392,7 +392,7 @@ static bool debug_parse_params(char **params)
 	}
 
 	/* Fill in new debug class levels */
-	for (; i < debug_num_classes && params[i]; i++) {
+	for (; params[i]; i++) {
 		bool ok;
 
 		ok = debug_parse_param(params[i]);
-- 
1.7.9.5


From 73e9cd08f189d4b11247c1f96e743cb5859fabb9 Mon Sep 17 00:00:00 2001
From: Volker Lendecke <vl at samba.org>
Date: Tue, 29 Jul 2014 12:58:53 +0000
Subject: [PATCH 11/28] debug: Statically allocate format_bufr

Everybody needs this, so there's no point talloc'ing it. The side effect
is that it removes a dependency on smb_panic()

Signed-off-by: Volker Lendecke <vl at samba.org>
---
 lib/util/debug.c |   12 ++----------
 1 file changed, 2 insertions(+), 10 deletions(-)

diff --git a/lib/util/debug.c b/lib/util/debug.c
index e4a1741..7ea8e85 100644
--- a/lib/util/debug.c
+++ b/lib/util/debug.c
@@ -142,7 +142,7 @@ int     *DEBUGLEVEL_CLASS = discard_const_p(int, debug_class_list_initial);
 
 static int     debug_count    = 0;
 static int     current_msg_level   = 0;
-static char *format_bufr = NULL;
+static char format_bufr[FORMAT_BUFR_SIZE];
 static size_t     format_pos     = 0;
 static bool    log_overflow   = false;
 
@@ -200,8 +200,6 @@ void gfree_debugsyms(void)
 		DEBUGLEVEL_CLASS = discard_const_p(int, debug_class_list_initial);
 	}
 
-	TALLOC_FREE(format_bufr);
-
 	debug_num_classes = 0;
 
 	state.initialized = false;
@@ -461,10 +459,6 @@ static void debug_init(void)
 	for(p = default_classname_table; *p; p++) {
 		debug_add_class(*p);
 	}
-	format_bufr = talloc_array(NULL, char, FORMAT_BUFR_SIZE);
-	if (!format_bufr) {
-		smb_panic("debug_init: unable to create buffer");
-	}
 }
 
 /* This forces in some smb.conf derived values into the debug system.
@@ -906,9 +900,7 @@ static void format_debug_text( const char *msg )
 	size_t i;
 	bool timestamp = (state.logtype == DEBUG_FILE && (state.settings.timestamp_logs));
 
-	if (!format_bufr) {
-		debug_init();
-	}
+	debug_init();
 
 	for( i = 0; msg[i]; i++ ) {
 		/* Indent two spaces at each new line. */
-- 
1.7.9.5


From 1c36971dfdc6bb2952ad6e2da0f928460e274531 Mon Sep 17 00:00:00 2001
From: Volker Lendecke <vl at samba.org>
Date: Tue, 29 Jul 2014 14:43:39 +0000
Subject: [PATCH 12/28] lib: Add timeval_str_buf

Similarly to server_id_str_buf it does not do any allocation

Signed-off-by: Volker Lendecke <vl at samba.org>
---
 lib/util/time.c |   39 +++++++++++++++++++++++++++++++++++++++
 lib/util/time.h |   12 ++++++++++++
 2 files changed, 51 insertions(+)

diff --git a/lib/util/time.c b/lib/util/time.c
index 03345ec..c502dc4 100644
--- a/lib/util/time.c
+++ b/lib/util/time.c
@@ -342,6 +342,45 @@ _PUBLIC_ time_t pull_dos_date3(const uint8_t *date_ptr, int zone_offset)
 }
 
 
+char *timeval_str_buf(const struct timeval *tp, bool hires,
+		      struct timeval_buf *dst)
+{
+	time_t t;
+	struct tm *tm;
+	size_t len;
+
+	t = (time_t)tp->tv_sec;
+	tm = localtime(&t);
+
+	if (tm == NULL) {
+		if (hires) {
+			snprintf(dst->buf, sizeof(dst->buf),
+				 "%ld.%06ld seconds since the Epoch",
+				 (long)tp->tv_sec, (long)tp->tv_usec);
+		} else {
+			snprintf(dst->buf, sizeof(dst->buf),
+				 "%ld seconds since the Epoch", (long)t);
+		}
+		return dst->buf;
+	}
+
+#ifdef HAVE_STRFTIME
+	len = strftime(dst->buf, sizeof(dst->buf), "%Y/%m/%d %H:%M:%S", tm);
+#else
+	{
+		const char *asct = asctime(tm);
+		len = strlcpy(dst->buf, sizeof(dst->buf),
+			      asct ? asct : "unknown");
+	}
+#endif
+	if (hires && (len < sizeof(dst->buf))) {
+		snprintf(dst->buf + len, sizeof(dst->buf) - len,
+			 ".%06ld", (long)tp->tv_usec);
+	}
+
+	return dst->buf;
+}
+
 /****************************************************************************
  Return the date and time as a string
 ****************************************************************************/
diff --git a/lib/util/time.h b/lib/util/time.h
index 8595bb0..44d4f45 100644
--- a/lib/util/time.h
+++ b/lib/util/time.h
@@ -119,6 +119,18 @@ time_t pull_dos_date2(const uint8_t *date_ptr, int zone_offset);
 **/
 time_t pull_dos_date3(const uint8_t *date_ptr, int zone_offset);
 
+struct timeval_buf { char buf[128]; };
+
+/**
+ Put a date and time into dst->buf, return it dst->buf
+ (optionally with microseconds)
+
+ format is %Y/%m/%d %H:%M:%S if strftime is available
+**/
+
+char *timeval_str_buf(const struct timeval *tp, bool hires,
+		      struct timeval_buf *dst);
+
 /**
  Return a date and time as a string (optionally with microseconds)
 
-- 
1.7.9.5


From 343a82d90fe89aba2e9e4e8a85465e9aec1f0186 Mon Sep 17 00:00:00 2001
From: Volker Lendecke <vl at samba.org>
Date: Tue, 29 Jul 2014 14:52:42 +0000
Subject: [PATCH 13/28] lib: Use timeval_str_buf in timeval_string

Signed-off-by: Volker Lendecke <vl at samba.org>
---
 lib/util/time.c |   55 +++++++++++++++++--------------------------------------
 1 file changed, 17 insertions(+), 38 deletions(-)

diff --git a/lib/util/time.c b/lib/util/time.c
index c502dc4..55baf72 100644
--- a/lib/util/time.c
+++ b/lib/util/time.c
@@ -387,46 +387,25 @@ char *timeval_str_buf(const struct timeval *tp, bool hires,
 
 char *timeval_string(TALLOC_CTX *ctx, const struct timeval *tp, bool hires)
 {
-	time_t t;
-	struct tm *tm;
+	struct timeval_buf tmp;
+	char *result;
 
-	t = (time_t)tp->tv_sec;
-	tm = localtime(&t);
-	if (!tm) {
-		if (hires) {
-			return talloc_asprintf(ctx,
-					       "%ld.%06ld seconds since the Epoch",
-					       (long)tp->tv_sec,
-					       (long)tp->tv_usec);
-		} else {
-			return talloc_asprintf(ctx,
-					       "%ld seconds since the Epoch",
-					       (long)t);
-		}
-	} else {
-#ifdef HAVE_STRFTIME
-		char TimeBuf[60];
-		if (hires) {
-			strftime(TimeBuf,sizeof(TimeBuf)-1,"%Y/%m/%d %H:%M:%S",tm);
-			return talloc_asprintf(ctx,
-					       "%s.%06ld", TimeBuf,
-					       (long)tp->tv_usec);
-		} else {
-			strftime(TimeBuf,sizeof(TimeBuf)-1,"%Y/%m/%d %H:%M:%S",tm);
-			return talloc_strdup(ctx, TimeBuf);
-		}
-#else
-		if (hires) {
-			const char *asct = asctime(tm);
-			return talloc_asprintf(ctx, "%s.%06ld",
-					asct ? asct : "unknown",
-					(long)tp->tv_usec);
-		} else {
-			const char *asct = asctime(tm);
-			return talloc_asprintf(ctx, asct ? asct : "unknown");
-		}
-#endif
+	result = talloc_strdup(ctx, timeval_str_buf(tp, hires, &tmp));
+	if (result == NULL) {
+		return NULL;
 	}
+
+	/*
+	 * beautify the talloc_report output
+	 *
+	 * This is not just cosmetics. A C compiler might in theory make the
+	 * talloc_strdup call above a tail call with the tail call
+	 * optimization. This would render "tmp" invalid while talloc_strdup
+	 * tries to duplicate it. The talloc_set_name_const call below puts
+	 * the talloc_strdup call into non-tail position.
+	 */
+	talloc_set_name_const(result, result);
+	return result;
 }
 
 char *current_timestring(TALLOC_CTX *ctx, bool hires)
-- 
1.7.9.5


From 14a88e6c5a31d4b1e505ecfc7c6a96da98d9f1bb Mon Sep 17 00:00:00 2001
From: Volker Lendecke <vl at samba.org>
Date: Tue, 29 Jul 2014 20:54:44 +0200
Subject: [PATCH 14/28] lib: Create time_basic.[ch]

Make the two functions that debug.c needs a subsystem of their own. The
goal is to put debug.c on a diet. Anybody who wants to use it should
not be forced to pull in half of Samba :-)

Signed-off-by: Volker Lendecke <vl at samba.org>
---
 lib/util/debug.c       |    2 +-
 lib/util/time.c        |   53 +-------------------------------
 lib/util/time.h        |   12 --------
 lib/util/time_basic.c  |   80 ++++++++++++++++++++++++++++++++++++++++++++++++
 lib/util/time_basic.h  |   42 +++++++++++++++++++++++++
 lib/util/wscript_build |    7 ++++-
 6 files changed, 130 insertions(+), 66 deletions(-)
 create mode 100644 lib/util/time_basic.c
 create mode 100644 lib/util/time_basic.h

diff --git a/lib/util/debug.c b/lib/util/debug.c
index 7ea8e85..04d02a1 100644
--- a/lib/util/debug.c
+++ b/lib/util/debug.c
@@ -22,7 +22,7 @@
 #include "includes.h"
 #include "system/filesys.h"
 #include "system/syslog.h"
-#include "lib/util/time.h"
+#include "lib/util/time_basic.h"
 
 /* define what facility to use for syslog */
 #ifndef SYSLOG_FACILITY
diff --git a/lib/util/time.c b/lib/util/time.c
index 55baf72..78611e4 100644
--- a/lib/util/time.c
+++ b/lib/util/time.c
@@ -23,6 +23,7 @@
 
 #include "includes.h"
 #include "system/time.h"
+#include "lib/util/time_basic.h"
 
 /**
  * @file
@@ -46,18 +47,6 @@ _PUBLIC_ time_t get_time_t_max(void)
 }
 
 /**
-a gettimeofday wrapper
-**/
-_PUBLIC_ void GetTimeOfDay(struct timeval *tval)
-{
-#ifdef HAVE_GETTIMEOFDAY_TZ
-	gettimeofday(tval,NULL);
-#else
-	gettimeofday(tval);
-#endif
-}
-
-/**
 a wrapper to preferably get the monotonic time
 **/
 _PUBLIC_ void clock_gettime_mono(struct timespec *tp)
@@ -341,46 +330,6 @@ _PUBLIC_ time_t pull_dos_date3(const uint8_t *date_ptr, int zone_offset)
 	return t;
 }
 
-
-char *timeval_str_buf(const struct timeval *tp, bool hires,
-		      struct timeval_buf *dst)
-{
-	time_t t;
-	struct tm *tm;
-	size_t len;
-
-	t = (time_t)tp->tv_sec;
-	tm = localtime(&t);
-
-	if (tm == NULL) {
-		if (hires) {
-			snprintf(dst->buf, sizeof(dst->buf),
-				 "%ld.%06ld seconds since the Epoch",
-				 (long)tp->tv_sec, (long)tp->tv_usec);
-		} else {
-			snprintf(dst->buf, sizeof(dst->buf),
-				 "%ld seconds since the Epoch", (long)t);
-		}
-		return dst->buf;
-	}
-
-#ifdef HAVE_STRFTIME
-	len = strftime(dst->buf, sizeof(dst->buf), "%Y/%m/%d %H:%M:%S", tm);
-#else
-	{
-		const char *asct = asctime(tm);
-		len = strlcpy(dst->buf, sizeof(dst->buf),
-			      asct ? asct : "unknown");
-	}
-#endif
-	if (hires && (len < sizeof(dst->buf))) {
-		snprintf(dst->buf + len, sizeof(dst->buf) - len,
-			 ".%06ld", (long)tp->tv_usec);
-	}
-
-	return dst->buf;
-}
-
 /****************************************************************************
  Return the date and time as a string
 ****************************************************************************/
diff --git a/lib/util/time.h b/lib/util/time.h
index 44d4f45..8595bb0 100644
--- a/lib/util/time.h
+++ b/lib/util/time.h
@@ -119,18 +119,6 @@ time_t pull_dos_date2(const uint8_t *date_ptr, int zone_offset);
 **/
 time_t pull_dos_date3(const uint8_t *date_ptr, int zone_offset);
 
-struct timeval_buf { char buf[128]; };
-
-/**
- Put a date and time into dst->buf, return it dst->buf
- (optionally with microseconds)
-
- format is %Y/%m/%d %H:%M:%S if strftime is available
-**/
-
-char *timeval_str_buf(const struct timeval *tp, bool hires,
-		      struct timeval_buf *dst);
-
 /**
  Return a date and time as a string (optionally with microseconds)
 
diff --git a/lib/util/time_basic.c b/lib/util/time_basic.c
new file mode 100644
index 0000000..b6e7317
--- /dev/null
+++ b/lib/util/time_basic.c
@@ -0,0 +1,80 @@
+/*
+ * Unix SMB/CIFS implementation.
+ * time handling functions
+ *
+ * Copyright (C) Andrew Tridgell 		1992-2004
+ * Copyright (C) Stefan (metze) Metzmacher	2002
+ * Copyright (C) Jeremy Allison			2007
+ * Copyright (C) Andrew Bartlett                2011
+ *
+ * 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
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "replace.h"
+#include "lib/util/time_basic.h"
+
+/**
+a gettimeofday wrapper
+**/
+_PUBLIC_ void GetTimeOfDay(struct timeval *tval)
+{
+#ifdef HAVE_GETTIMEOFDAY_TZ
+	gettimeofday(tval,NULL);
+#else
+	gettimeofday(tval);
+#endif
+}
+
+/****************************************************************************
+ Return the date and time as a string
+****************************************************************************/
+
+char *timeval_str_buf(const struct timeval *tp, bool hires,
+		      struct timeval_buf *dst)
+{
+	time_t t;
+	struct tm *tm;
+	size_t len;
+
+	t = (time_t)tp->tv_sec;
+	tm = localtime(&t);
+
+	if (tm == NULL) {
+		if (hires) {
+			snprintf(dst->buf, sizeof(dst->buf),
+				 "%ld.%06ld seconds since the Epoch",
+				 (long)tp->tv_sec, (long)tp->tv_usec);
+		} else {
+			snprintf(dst->buf, sizeof(dst->buf),
+				 "%ld seconds since the Epoch", (long)t);
+		}
+		return dst->buf;
+	}
+
+#ifdef HAVE_STRFTIME
+	len = strftime(dst->buf, sizeof(dst->buf), "%Y/%m/%d %H:%M:%S", tm);
+#else
+	{
+		const char *asct = asctime(tm);
+		len = strlcpy(dst->buf, sizeof(dst->buf),
+			      asct ? asct : "unknown");
+	}
+#endif
+	if (hires && (len < sizeof(dst->buf))) {
+		snprintf(dst->buf + len, sizeof(dst->buf) - len,
+			 ".%06ld", (long)tp->tv_usec);
+	}
+
+	return dst->buf;
+}
diff --git a/lib/util/time_basic.h b/lib/util/time_basic.h
new file mode 100644
index 0000000..58bc02d
--- /dev/null
+++ b/lib/util/time_basic.h
@@ -0,0 +1,42 @@
+/*
+ * Unix SMB/CIFS implementation.
+ * time handling functions
+ *
+ * Copyright (C) Andrew Tridgell 		1992-2004
+ * Copyright (C) Stefan (metze) Metzmacher	2002
+ * Copyright (C) Jeremy Allison			2007
+ * Copyright (C) Andrew Bartlett                2011
+ *
+ * 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
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "replace.h"
+#include "system/time.h"
+
+/**
+a gettimeofday wrapper
+**/
+_PUBLIC_ void GetTimeOfDay(struct timeval *tval);
+
+struct timeval_buf { char buf[128]; };
+
+/**
+ Put a date and time into dst->buf, return it dst->buf
+ (optionally with microseconds)
+
+ format is %Y/%m/%d %H:%M:%S if strftime is available
+**/
+
+char *timeval_str_buf(const struct timeval *tp, bool hires,
+		      struct timeval_buf *dst);
diff --git a/lib/util/wscript_build b/lib/util/wscript_build
index bcb7b66..8be2c2e 100755
--- a/lib/util/wscript_build
+++ b/lib/util/wscript_build
@@ -1,5 +1,10 @@
 #!/usr/bin/env python
 
+bld.SAMBA_SUBSYSTEM('time-basic',
+                    source='time_basic.c',
+                    deps='replace',
+                    local_include=False)
+
 bld.SAMBA_LIBRARY('samba-util',
                   source='''talloc_stack.c smb_threads.c xfile.c data_blob.c
                     util_file.c time.c rbtree.c rfc1738.c select.c getpass.c
@@ -9,7 +14,7 @@ bld.SAMBA_LIBRARY('samba-util',
                     util_str.c util_str_common.c substitute.c ms_fnmatch.c
                     server_id.c dprintf.c parmlist.c bitmap.c pidfile.c
                     tevent_debug.c util_process.c memcache.c''',
-                  deps='DYNCONFIG',
+                  deps='DYNCONFIG time-basic',
                   public_deps='talloc tevent execinfo pthread LIBCRYPTO charset util_setid systemd-daemon',
                   public_headers='debug.h attr.h byteorder.h data_blob.h memory.h safe_string.h time.h talloc_stack.h xfile.h dlinklist.h samba_util.h string_wrappers.h',
                   header_path= [ ('dlinklist.h samba_util.h', '.'), ('*', 'util') ],
-- 
1.7.9.5


From cadbb6d6c2d5b2f9f89f4ee0d341719a8de9588d Mon Sep 17 00:00:00 2001
From: Volker Lendecke <vl at samba.org>
Date: Tue, 29 Jul 2014 15:02:22 +0000
Subject: [PATCH 15/28] debug: Simplify dbghdrclass with an early return

No code change, visible with "git diff -w"

Signed-off-by: Volker Lendecke <vl at samba.org>
---
 lib/util/debug.c |   84 ++++++++++++++++++++++++++++--------------------------
 1 file changed, 43 insertions(+), 41 deletions(-)

diff --git a/lib/util/debug.c b/lib/util/debug.c
index 04d02a1..2509ace 100644
--- a/lib/util/debug.c
+++ b/lib/util/debug.c
@@ -969,6 +969,8 @@ bool dbghdrclass(int level, int cls, const char *location, const char *func)
 {
 	/* Ensure we don't lose any real errno value. */
 	int old_errno = errno;
+	bool verbose = false;
+	char header_str[200];
 
 	if( format_pos ) {
 		/* This is a fudge.  If there is stuff sitting in the format_bufr, then
@@ -994,53 +996,53 @@ bool dbghdrclass(int level, int cls, const char *location, const char *func)
 	/* Print the header if timestamps are turned on.  If parameters are
 	 * not yet loaded, then default to timestamps on.
 	 */
-	if( state.settings.timestamp_logs || state.settings.debug_prefix_timestamp) {
-		bool verbose = false;
-		char header_str[200];
+	if (!(state.settings.timestamp_logs ||
+	      state.settings.debug_prefix_timestamp)) {
+		return true;
+	}
 
-		header_str[0] = '\0';
+	header_str[0] = '\0';
 
-		if (unlikely(DEBUGLEVEL_CLASS[ cls ] >= 10)) {
-			verbose = true;
-		}
+	if (unlikely(DEBUGLEVEL_CLASS[ cls ] >= 10)) {
+		verbose = true;
+	}
 
-		if (verbose || state.settings.debug_pid)
-			slprintf(header_str,sizeof(header_str)-1,", pid=%u",(unsigned int)getpid());
+	if (verbose || state.settings.debug_pid)
+		slprintf(header_str,sizeof(header_str)-1,", pid=%u",(unsigned int)getpid());
 
-		if (verbose || state.settings.debug_uid) {
-			size_t hs_len = strlen(header_str);
-			slprintf(header_str + hs_len,
-			sizeof(header_str) - 1 - hs_len,
-				", effective(%u, %u), real(%u, %u)",
-				(unsigned int)geteuid(), (unsigned int)getegid(),
-				(unsigned int)getuid(), (unsigned int)getgid());
-		}
+	if (verbose || state.settings.debug_uid) {
+		size_t hs_len = strlen(header_str);
+		slprintf(header_str + hs_len,
+			 sizeof(header_str) - 1 - hs_len,
+			 ", effective(%u, %u), real(%u, %u)",
+			 (unsigned int)geteuid(), (unsigned int)getegid(),
+			 (unsigned int)getuid(), (unsigned int)getgid());
+	}
 
-		if ((verbose || state.settings.debug_class)
-		    && (cls != DBGC_ALL)) {
-			size_t hs_len = strlen(header_str);
-			slprintf(header_str + hs_len,
-				 sizeof(header_str) -1 - hs_len,
-				 ", class=%s",
-				 classname_table[cls]);
-		}
+	if ((verbose || state.settings.debug_class)
+	    && (cls != DBGC_ALL)) {
+		size_t hs_len = strlen(header_str);
+		slprintf(header_str + hs_len,
+			 sizeof(header_str) -1 - hs_len,
+			 ", class=%s",
+			 classname_table[cls]);
+	}
 
-		/* Print it all out at once to prevent split syslog output. */
-		if( state.settings.debug_prefix_timestamp ) {
-			char *time_str = current_timestring(NULL,
-							    state.settings.debug_hires_timestamp);
-			(void)Debug1( "[%s, %2d%s] ",
-				      time_str,
-				      level, header_str);
-			talloc_free(time_str);
-		} else {
-			char *time_str = current_timestring(NULL,
-							    state.settings.debug_hires_timestamp);
-			(void)Debug1( "[%s, %2d%s] %s(%s)\n",
-				      time_str,
-				      level, header_str, location, func );
-			talloc_free(time_str);
-		}
+	/* Print it all out at once to prevent split syslog output. */
+	if( state.settings.debug_prefix_timestamp ) {
+		char *time_str = current_timestring(NULL,
+						    state.settings.debug_hires_timestamp);
+		(void)Debug1( "[%s, %2d%s] ",
+			      time_str,
+			      level, header_str);
+		talloc_free(time_str);
+	} else {
+		char *time_str = current_timestring(NULL,
+						    state.settings.debug_hires_timestamp);
+		(void)Debug1( "[%s, %2d%s] %s(%s)\n",
+			      time_str,
+			      level, header_str, location, func );
+		talloc_free(time_str);
 	}
 
 	errno = old_errno;
-- 
1.7.9.5


From a973b2a226b8247b34a487fe33db0dbec01aa164 Mon Sep 17 00:00:00 2001
From: Volker Lendecke <vl at samba.org>
Date: Tue, 29 Jul 2014 15:06:17 +0000
Subject: [PATCH 16/28] debug: Use timeval_str_buf in dbghdrclass

Signed-off-by: Volker Lendecke <vl at samba.org>
---
 lib/util/debug.c |   23 +++++++++++------------
 1 file changed, 11 insertions(+), 12 deletions(-)

diff --git a/lib/util/debug.c b/lib/util/debug.c
index 2509ace..b578ba0 100644
--- a/lib/util/debug.c
+++ b/lib/util/debug.c
@@ -971,6 +971,8 @@ bool dbghdrclass(int level, int cls, const char *location, const char *func)
 	int old_errno = errno;
 	bool verbose = false;
 	char header_str[200];
+	struct timeval tv;
+	struct timeval_buf tvbuf;
 
 	if( format_pos ) {
 		/* This is a fudge.  If there is stuff sitting in the format_bufr, then
@@ -1028,21 +1030,18 @@ bool dbghdrclass(int level, int cls, const char *location, const char *func)
 			 classname_table[cls]);
 	}
 
+	GetTimeOfDay(&tv);
+	timeval_str_buf(&tv, state.settings.debug_hires_timestamp, &tvbuf);
+
 	/* Print it all out at once to prevent split syslog output. */
 	if( state.settings.debug_prefix_timestamp ) {
-		char *time_str = current_timestring(NULL,
-						    state.settings.debug_hires_timestamp);
-		(void)Debug1( "[%s, %2d%s] ",
-			      time_str,
-			      level, header_str);
-		talloc_free(time_str);
+		(void)Debug1("[%s, %2d%s] ",
+			     tvbuf.buf,
+			     level, header_str);
 	} else {
-		char *time_str = current_timestring(NULL,
-						    state.settings.debug_hires_timestamp);
-		(void)Debug1( "[%s, %2d%s] %s(%s)\n",
-			      time_str,
-			      level, header_str, location, func );
-		talloc_free(time_str);
+		(void)Debug1("[%s, %2d%s] %s(%s)\n",
+			     tvbuf.buf,
+			     level, header_str, location, func );
 	}
 
 	errno = old_errno;
-- 
1.7.9.5


From 8e1e303cbcc37be7df567f2eed3da6d4b1cfe411 Mon Sep 17 00:00:00 2001
From: Volker Lendecke <vl at samba.org>
Date: Tue, 29 Jul 2014 15:09:00 +0000
Subject: [PATCH 17/28] debug: slprintf->snprintf

replace.h has

snprintf does not need the -1 in sizeof(buf)-1

Signed-off-by: Volker Lendecke <vl at samba.org>
---
 lib/util/debug.c |   13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)

diff --git a/lib/util/debug.c b/lib/util/debug.c
index b578ba0..e0374ec 100644
--- a/lib/util/debug.c
+++ b/lib/util/debug.c
@@ -1009,13 +1009,14 @@ bool dbghdrclass(int level, int cls, const char *location, const char *func)
 		verbose = true;
 	}
 
-	if (verbose || state.settings.debug_pid)
-		slprintf(header_str,sizeof(header_str)-1,", pid=%u",(unsigned int)getpid());
+	if (verbose || state.settings.debug_pid) {
+		snprintf(header_str, sizeof(header_str), ", pid=%u",
+			 (unsigned int)getpid());
+	}
 
 	if (verbose || state.settings.debug_uid) {
 		size_t hs_len = strlen(header_str);
-		slprintf(header_str + hs_len,
-			 sizeof(header_str) - 1 - hs_len,
+		snprintf(header_str + hs_len, sizeof(header_str) - hs_len,
 			 ", effective(%u, %u), real(%u, %u)",
 			 (unsigned int)geteuid(), (unsigned int)getegid(),
 			 (unsigned int)getuid(), (unsigned int)getgid());
@@ -1024,8 +1025,8 @@ bool dbghdrclass(int level, int cls, const char *location, const char *func)
 	if ((verbose || state.settings.debug_class)
 	    && (cls != DBGC_ALL)) {
 		size_t hs_len = strlen(header_str);
-		slprintf(header_str + hs_len,
-			 sizeof(header_str) -1 - hs_len,
+		snprintf(header_str + hs_len,
+			 sizeof(header_str) - hs_len,
 			 ", class=%s",
 			 classname_table[cls]);
 	}
-- 
1.7.9.5


From 606776f38fabd11c35492f74982e4e8374dd64de Mon Sep 17 00:00:00 2001
From: Volker Lendecke <vl at samba.org>
Date: Tue, 29 Jul 2014 15:26:22 +0000
Subject: [PATCH 18/28] debug: Move adding timeval and loglevel

The whole routine incrementally creates the hdr_string, do it for the
time string and the debuglevel as well

Signed-off-by: Volker Lendecke <vl at samba.org>
---
 lib/util/debug.c |   23 +++++++++++------------
 1 file changed, 11 insertions(+), 12 deletions(-)

diff --git a/lib/util/debug.c b/lib/util/debug.c
index e0374ec..f2bc394 100644
--- a/lib/util/debug.c
+++ b/lib/util/debug.c
@@ -1003,15 +1003,20 @@ bool dbghdrclass(int level, int cls, const char *location, const char *func)
 		return true;
 	}
 
-	header_str[0] = '\0';
+	GetTimeOfDay(&tv);
+	timeval_str_buf(&tv, state.settings.debug_hires_timestamp, &tvbuf);
+
+	snprintf(header_str, sizeof(header_str), "[%s, %2d",
+		 tvbuf.buf, level);
 
 	if (unlikely(DEBUGLEVEL_CLASS[ cls ] >= 10)) {
 		verbose = true;
 	}
 
 	if (verbose || state.settings.debug_pid) {
-		snprintf(header_str, sizeof(header_str), ", pid=%u",
-			 (unsigned int)getpid());
+		size_t hs_len = strlen(header_str);
+		snprintf(header_str + hs_len, sizeof(header_str) - hs_len,
+			 ", pid=%u", (unsigned int)getpid());
 	}
 
 	if (verbose || state.settings.debug_uid) {
@@ -1031,18 +1036,12 @@ bool dbghdrclass(int level, int cls, const char *location, const char *func)
 			 classname_table[cls]);
 	}
 
-	GetTimeOfDay(&tv);
-	timeval_str_buf(&tv, state.settings.debug_hires_timestamp, &tvbuf);
-
 	/* Print it all out at once to prevent split syslog output. */
 	if( state.settings.debug_prefix_timestamp ) {
-		(void)Debug1("[%s, %2d%s] ",
-			     tvbuf.buf,
-			     level, header_str);
+		(void)Debug1("%s] ", header_str);
 	} else {
-		(void)Debug1("[%s, %2d%s] %s(%s)\n",
-			     tvbuf.buf,
-			     level, header_str, location, func );
+		(void)Debug1("%s] %s(%s)\n",
+			     header_str, location, func );
 	}
 
 	errno = old_errno;
-- 
1.7.9.5


From 4899bad705d5620cf2dddbda0c376b5a63bc34af Mon Sep 17 00:00:00 2001
From: Volker Lendecke <vl at samba.org>
Date: Tue, 29 Jul 2014 15:31:48 +0000
Subject: [PATCH 19/28] debug: Only call Debug1 once in dbghdrclass

Signed-off-by: Volker Lendecke <vl at samba.org>
---
 lib/util/debug.c |   15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)

diff --git a/lib/util/debug.c b/lib/util/debug.c
index f2bc394..1a9938e 100644
--- a/lib/util/debug.c
+++ b/lib/util/debug.c
@@ -1036,14 +1036,17 @@ bool dbghdrclass(int level, int cls, const char *location, const char *func)
 			 classname_table[cls]);
 	}
 
-	/* Print it all out at once to prevent split syslog output. */
-	if( state.settings.debug_prefix_timestamp ) {
-		(void)Debug1("%s] ", header_str);
-	} else {
-		(void)Debug1("%s] %s(%s)\n",
-			     header_str, location, func );
+	strlcat(header_str, "] ", sizeof(header_str));
+
+	if (!state.settings.debug_prefix_timestamp) {
+		size_t hs_len = strlen(header_str);
+		snprintf(header_str + hs_len,
+			 sizeof(header_str) - hs_len,
+			 "%s(%s)\n", location, func);
 	}
 
+	(void)Debug1("%s", header_str);
+
 	errno = old_errno;
 	return( true );
 }
-- 
1.7.9.5


From 538344449ae7dbd384f061de871e86429e4a6542 Mon Sep 17 00:00:00 2001
From: Volker Lendecke <vl at samba.org>
Date: Tue, 29 Jul 2014 15:47:07 +0000
Subject: [PATCH 20/28] debug: Remove two pointless assignments

"goto done:" jumps to that exact assignment :-)

Signed-off-by: Volker Lendecke <vl at samba.org>
---
 lib/util/debug.c |    2 --
 1 file changed, 2 deletions(-)

diff --git a/lib/util/debug.c b/lib/util/debug.c
index 1a9938e..9b58c4b 100644
--- a/lib/util/debug.c
+++ b/lib/util/debug.c
@@ -789,7 +789,6 @@ static int Debug1( const char *format_str, ... )
 		if (state.fd > 0)
 			(void)vdprintf( state.fd, format_str, ap );
 		va_end( ap );
-		errno = old_errno;
 		goto done;
 	}
 
@@ -802,7 +801,6 @@ static int Debug1( const char *format_str, ... )
 			int fd = open( state.debugf, O_WRONLY|O_APPEND|O_CREAT, 0644 );
 			(void)umask( oldumask );
 			if(fd == -1) {
-				errno = old_errno;
 				goto done;
 			}
 			state.fd = fd;
-- 
1.7.9.5


From d21b1722e5594d48aff41b8d628041e831e2472b Mon Sep 17 00:00:00 2001
From: Volker Lendecke <vl at samba.org>
Date: Tue, 29 Jul 2014 15:59:06 +0000
Subject: [PATCH 21/28] debug: Remove an "else"

No else necessary, we just did a "goto done:"

Signed-off-by: Volker Lendecke <vl at samba.org>
---
 lib/util/debug.c |    4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/lib/util/debug.c b/lib/util/debug.c
index 9b58c4b..c1a6107 100644
--- a/lib/util/debug.c
+++ b/lib/util/debug.c
@@ -784,7 +784,9 @@ static int Debug1( const char *format_str, ... )
 
 		goto done;
 
-	} else if ( state.logtype != DEBUG_FILE ) {
+	}
+
+	if ( state.logtype != DEBUG_FILE ) {
 		va_start( ap, format_str );
 		if (state.fd > 0)
 			(void)vdprintf( state.fd, format_str, ap );
-- 
1.7.9.5


From 73745796f036b9798183207576b220a7bad9253f Mon Sep 17 00:00:00 2001
From: Volker Lendecke <vl at samba.org>
Date: Tue, 29 Jul 2014 16:04:25 +0000
Subject: [PATCH 22/28] debug: Simplify Debug1() -- no va_args

All callers just have "%s" as format string now, so we don't need to
vasprintf anymore.

This could speed up DEBUG a bit, we don't do a separate copy and the
printf logic

Signed-off-by: Volker Lendecke <vl at samba.org>
---
 lib/util/debug.c |   52 ++++++++++++++++++----------------------------------
 1 file changed, 18 insertions(+), 34 deletions(-)

diff --git a/lib/util/debug.c b/lib/util/debug.c
index c1a6107..71a88c3 100644
--- a/lib/util/debug.c
+++ b/lib/util/debug.c
@@ -761,36 +761,30 @@ void check_log_size( void )
  This is called by dbghdr() and format_debug_text().
 ************************************************************************/
 
-static int Debug1( const char *format_str, ... )
+static int Debug1(const char *msg)
 {
-	va_list ap;
 	int old_errno = errno;
 
 	debug_count++;
 
 	if (state.logtype == DEBUG_CALLBACK) {
-		char *msg;
-		int ret;
-		va_start( ap, format_str );
-		ret = vasprintf( &msg, format_str, ap );
-		if (ret != -1) {
-			if (msg[ret - 1] == '\n') {
-				msg[ret - 1] = '\0';
-			}
-			state.callback(state.callback_private, current_msg_level, msg);
-			free(msg);
+		size_t msg_len = strlen(msg);
+		char msg_copy[msg_len];
+
+		if ((msg_len > 0) && (msg[msg_len-1] == '\n')) {
+			memcpy(msg_copy, msg, msg_len-1);
+			msg_copy[msg_len-1] = '\0';
+			msg = msg_copy;
 		}
-		va_end( ap );
 
+		state.callback(state.callback_private, current_msg_level, msg);
 		goto done;
-
 	}
 
 	if ( state.logtype != DEBUG_FILE ) {
-		va_start( ap, format_str );
-		if (state.fd > 0)
-			(void)vdprintf( state.fd, format_str, ap );
-		va_end( ap );
+		if (state.fd > 0) {
+			write(state.fd, msg, strlen(msg));
+		}
 		goto done;
 	}
 
@@ -822,8 +816,6 @@ static int Debug1( const char *format_str, ... )
 			LOG_INFO,    /* 3 */
 		};
 		int     priority;
-		char *msgbuf = NULL;
-		int ret;
 
 		if( current_msg_level >= ARRAY_SIZE(priority_map) || current_msg_level < 0)
 			priority = LOG_DEBUG;
@@ -836,14 +828,7 @@ static int Debug1( const char *format_str, ... )
 		 */
 		priority |= SYSLOG_FACILITY;
 
-		va_start(ap, format_str);
-		ret = vasprintf(&msgbuf, format_str, ap);
-		va_end(ap);
-
-		if (ret != -1) {
-			syslog(priority, "%s", msgbuf);
-		}
-		SAFE_FREE(msgbuf);
+		syslog(priority, "%s", msg);
 	}
 #endif
 
@@ -853,10 +838,9 @@ static int Debug1( const char *format_str, ... )
 	if( !state.settings.syslog_only)
 #endif
 	{
-		va_start( ap, format_str );
-		if (state.fd > 0)
-			(void)vdprintf( state.fd, format_str, ap );
-		va_end( ap );
+		if (state.fd > 0) {
+			write(state.fd, msg, strlen(msg));
+		}
 	}
 
  done:
@@ -875,7 +859,7 @@ static int Debug1( const char *format_str, ... )
 static void bufr_print( void )
 {
 	format_bufr[format_pos] = '\0';
-	(void)Debug1( "%s", format_bufr );
+	(void)Debug1(format_bufr);
 	format_pos = 0;
 }
 
@@ -1045,7 +1029,7 @@ bool dbghdrclass(int level, int cls, const char *location, const char *func)
 			 "%s(%s)\n", location, func);
 	}
 
-	(void)Debug1("%s", header_str);
+	(void)Debug1(header_str);
 
 	errno = old_errno;
 	return( true );
-- 
1.7.9.5


From 8740feddc94df19f0da1af986cadd0c985d6dd83 Mon Sep 17 00:00:00 2001
From: Volker Lendecke <vl at samba.org>
Date: Tue, 29 Jul 2014 16:09:10 +0000
Subject: [PATCH 23/28] debug: Simplify debug_parse_levels

Just some minimal refactoring

Signed-off-by: Volker Lendecke <vl at samba.org>
---
 lib/util/debug.c |   12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/lib/util/debug.c b/lib/util/debug.c
index 71a88c3..71f2631 100644
--- a/lib/util/debug.c
+++ b/lib/util/debug.c
@@ -413,20 +413,20 @@ static bool debug_parse_params(char **params)
 bool debug_parse_levels(const char *params_str)
 {
 	char **params;
+	bool ok;
 
 	/* Just in case */
 	debug_init();
 
 	params = str_list_make(NULL, params_str, NULL);
 
-	if (debug_parse_params(params)) {
+	ok = debug_parse_params(params);
+	if (ok) {
 		debug_dump_status(5);
-		TALLOC_FREE(params);
-		return true;
-	} else {
-		TALLOC_FREE(params);
-		return false;
 	}
+
+	TALLOC_FREE(params);
+	return ok;
 }
 
 /* setup for logging of talloc warnings */
-- 
1.7.9.5


From c985c34974556ea5a67bba3554c74176601ae312 Mon Sep 17 00:00:00 2001
From: Volker Lendecke <vl at samba.org>
Date: Tue, 29 Jul 2014 20:44:28 +0200
Subject: [PATCH 24/28] debug: Fix a typo

Signed-off-by: Volker Lendecke <vl at samba.org>
---
 lib/util/debug.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lib/util/debug.c b/lib/util/debug.c
index 71f2631..8390d3d 100644
--- a/lib/util/debug.c
+++ b/lib/util/debug.c
@@ -627,7 +627,7 @@ bool reopen_logs_internal(void)
 		if (dup2(state.fd, 2) == -1) {
 			/* Close stderr too, if dup2 can't point it -
 			   at the logfile.  There really isn't much
-			   that can be done on such a fundemental
+			   that can be done on such a fundamental
 			   failure... */
 			close_low_fds(false, false, true);
 		}
-- 
1.7.9.5


From 375a30436ad689f6be81b53b07ae915fb3c8bcf3 Mon Sep 17 00:00:00 2001
From: Volker Lendecke <vl at samba.org>
Date: Wed, 30 Jul 2014 09:22:21 +0000
Subject: [PATCH 25/28] debug: In dbghdrclass, don't call strlen repeatedly

Also properly protect against header_str overflow
---
 lib/util/debug.c |   60 ++++++++++++++++++++++++++++++++++++------------------
 1 file changed, 40 insertions(+), 20 deletions(-)

diff --git a/lib/util/debug.c b/lib/util/debug.c
index 8390d3d..71ccd3c 100644
--- a/lib/util/debug.c
+++ b/lib/util/debug.c
@@ -955,6 +955,7 @@ bool dbghdrclass(int level, int cls, const char *location, const char *func)
 	int old_errno = errno;
 	bool verbose = false;
 	char header_str[200];
+	size_t hs_len;
 	struct timeval tv;
 	struct timeval_buf tvbuf;
 
@@ -990,45 +991,64 @@ bool dbghdrclass(int level, int cls, const char *location, const char *func)
 	GetTimeOfDay(&tv);
 	timeval_str_buf(&tv, state.settings.debug_hires_timestamp, &tvbuf);
 
-	snprintf(header_str, sizeof(header_str), "[%s, %2d",
-		 tvbuf.buf, level);
+	hs_len = snprintf(header_str, sizeof(header_str), "[%s, %2d",
+			  tvbuf.buf, level);
+	if (hs_len >= sizeof(header_str)) {
+		goto full;
+	}
 
 	if (unlikely(DEBUGLEVEL_CLASS[ cls ] >= 10)) {
 		verbose = true;
 	}
 
 	if (verbose || state.settings.debug_pid) {
-		size_t hs_len = strlen(header_str);
-		snprintf(header_str + hs_len, sizeof(header_str) - hs_len,
-			 ", pid=%u", (unsigned int)getpid());
+		hs_len += snprintf(
+			header_str + hs_len, sizeof(header_str) - hs_len,
+			", pid=%u", (unsigned int)getpid());
+		if (hs_len >= sizeof(header_str)) {
+			goto full;
+		}
 	}
 
 	if (verbose || state.settings.debug_uid) {
-		size_t hs_len = strlen(header_str);
-		snprintf(header_str + hs_len, sizeof(header_str) - hs_len,
-			 ", effective(%u, %u), real(%u, %u)",
-			 (unsigned int)geteuid(), (unsigned int)getegid(),
-			 (unsigned int)getuid(), (unsigned int)getgid());
+		hs_len += snprintf(
+			header_str + hs_len, sizeof(header_str) - hs_len,
+			", effective(%u, %u), real(%u, %u)",
+			(unsigned int)geteuid(), (unsigned int)getegid(),
+			(unsigned int)getuid(), (unsigned int)getgid());
+		if (hs_len >= sizeof(header_str)) {
+			goto full;
+		}
 	}
 
 	if ((verbose || state.settings.debug_class)
 	    && (cls != DBGC_ALL)) {
-		size_t hs_len = strlen(header_str);
-		snprintf(header_str + hs_len,
-			 sizeof(header_str) - hs_len,
-			 ", class=%s",
-			 classname_table[cls]);
+		hs_len += snprintf(
+			header_str + hs_len, sizeof(header_str) - hs_len,
+			", class=%s", classname_table[cls]);
+		if (hs_len >= sizeof(header_str)) {
+			goto full;
+		}
 	}
 
-	strlcat(header_str, "] ", sizeof(header_str));
+	/*
+	 * No +=, see man man strlcat
+	 */
+	hs_len = strlcat(header_str, "] ", sizeof(header_str));
+	if (hs_len >= sizeof(header_str)) {
+		goto full;
+	}
 
 	if (!state.settings.debug_prefix_timestamp) {
-		size_t hs_len = strlen(header_str);
-		snprintf(header_str + hs_len,
-			 sizeof(header_str) - hs_len,
-			 "%s(%s)\n", location, func);
+		hs_len += snprintf(
+			header_str + hs_len, sizeof(header_str) - hs_len,
+			"%s(%s)\n", location, func);
+		if (hs_len >= sizeof(header_str)) {
+			goto full;
+		}
 	}
 
+full:
 	(void)Debug1(header_str);
 
 	errno = old_errno;
-- 
1.7.9.5


From 3f0b3a8461b459b93b4cea9d113d35a27c439231 Mon Sep 17 00:00:00 2001
From: Volker Lendecke <vl at samba.org>
Date: Wed, 30 Jul 2014 09:22:59 +0000
Subject: [PATCH 26/28] debug: Increase maximum header length

With function names it can become quite long

Signed-off-by: Volker Lendecke <vl at samba.org>
---
 lib/util/debug.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lib/util/debug.c b/lib/util/debug.c
index 71ccd3c..f0d7267 100644
--- a/lib/util/debug.c
+++ b/lib/util/debug.c
@@ -954,7 +954,7 @@ bool dbghdrclass(int level, int cls, const char *location, const char *func)
 	/* Ensure we don't lose any real errno value. */
 	int old_errno = errno;
 	bool verbose = false;
-	char header_str[200];
+	char header_str[300];
 	size_t hs_len;
 	struct timeval tv;
 	struct timeval_buf tvbuf;
-- 
1.7.9.5


From 17d70959b730483b6818582502e73db926f2fa62 Mon Sep 17 00:00:00 2001
From: Volker Lendecke <vl at samba.org>
Date: Wed, 30 Jul 2014 09:35:43 +0000
Subject: [PATCH 27/28] debug: Fix blank line endings

Signed-off-by: Volker Lendecke <vl at samba.org>
---
 lib/util/debug.c |    6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/lib/util/debug.c b/lib/util/debug.c
index f0d7267..24b9f52 100644
--- a/lib/util/debug.c
+++ b/lib/util/debug.c
@@ -215,7 +215,7 @@ char *debug_list_class_names_and_levels(void)
 	unsigned int i;
 	/* prepare strings */
 	for (i = 0; i < debug_num_classes; i++) {
-		buf = talloc_asprintf_append(buf, 
+		buf = talloc_asprintf_append(buf,
 					     "%s:%d%s",
 					     classname_table[i],
 					     DEBUGLEVEL_CLASS[i],
@@ -715,12 +715,12 @@ void check_log_size( void )
 		if (state.fd > 2 && (fstat(state.fd, &st) == 0
 				     && st.st_size > maxlog)) {
 			char *name = NULL;
-			
+
 			if (asprintf(&name, "%s.old", state.debugf ) < 0) {
 				return;
 			}
 			(void)rename(state.debugf, name);
-			
+
 			if (!reopen_logs_internal()) {
 				/* We failed to reopen a log - continue using the old name. */
 				(void)rename(name, state.debugf);
-- 
1.7.9.5


From be5b3a16de4061c6f9cde7597d5a54fa357a06b6 Mon Sep 17 00:00:00 2001
From: Volker Lendecke <vl at samba.org>
Date: Tue, 29 Jul 2014 20:01:31 +0200
Subject: [PATCH 28/28] debug: Avoid dependency on str_list_make

strtok_r just also does the job and is available in libc

Signed-off-by: Volker Lendecke <vl at samba.org>
---
 lib/util/debug.c |   70 ++++++++++++++++++++++--------------------------------
 1 file changed, 29 insertions(+), 41 deletions(-)

diff --git a/lib/util/debug.c b/lib/util/debug.c
index 24b9f52..5972171 100644
--- a/lib/util/debug.c
+++ b/lib/util/debug.c
@@ -362,71 +362,59 @@ static bool debug_parse_param(char *param)
 }
 
 /****************************************************************************
- parse the debug levels from smbcontrol. Example debug level parameter:
- printdrivers:7
+ 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.
 ****************************************************************************/
 
-static bool debug_parse_params(char **params)
+bool debug_parse_levels(const char *params_str)
 {
-	int   i, ndx;
+	size_t str_len = strlen(params_str);
+	char str[str_len+1];
+	char *tok, *saveptr;
+	int i;
 
-	if (!params)
-		return false;
+	/* Just in case */
+	debug_init();
+
+	memcpy(str, params_str, str_len+1);
+
+	tok = strtok_r(str, LIST_SEP, &saveptr);
+	if (tok == NULL) {
+		return true;
+	}
 
 	/* 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]);
-		i = 1; /* start processing at the next params */
+	if (isdigit(tok[0])) {
+		DEBUGLEVEL_CLASS[DBGC_ALL] = atoi(tok);
+		tok = strtok_r(NULL, LIST_SEP, &saveptr);
 	} else {
 		DEBUGLEVEL_CLASS[DBGC_ALL] = 0;
-		i = 0; /* DBGC_ALL not specified OR class name was included */
 	}
 
 	/* Array is debug_num_classes long */
-	for (ndx = DBGC_ALL; ndx < debug_num_classes; ndx++) {
-		DEBUGLEVEL_CLASS[ndx] = DEBUGLEVEL_CLASS[DBGC_ALL];
+	for (i = DBGC_ALL+1; i < debug_num_classes; i++) {
+		DEBUGLEVEL_CLASS[i] = DEBUGLEVEL_CLASS[DBGC_ALL];
 	}
 
-	/* Fill in new debug class levels */
-	for (; params[i]; i++) {
+	while (tok != NULL) {
 		bool ok;
 
-		ok = debug_parse_param(params[i]);
+		ok = debug_parse_param(tok);
 		if (!ok) {
 			DEBUG(0,("debug_parse_params: unrecognized debug "
-				 "class name or format [%s]\n", params[i]));
+				 "class name or format [%s]\n", tok));
 			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;
-	bool ok;
-
-	/* Just in case */
-	debug_init();
-
-	params = str_list_make(NULL, params_str, NULL);
-
-	ok = debug_parse_params(params);
-	if (ok) {
-		debug_dump_status(5);
+		tok = strtok_r(NULL, LIST_SEP, &saveptr);
 	}
 
-	TALLOC_FREE(params);
-	return ok;
+	debug_dump_status(5);
+
+	return true;
 }
 
 /* setup for logging of talloc warnings */
-- 
1.7.9.5



More information about the samba-technical mailing list