[PATCH] Extending the debugging system with per-class logfiles
Ralph Böhme
slow at samba.org
Tue Dec 18 07:17:00 UTC 2018
On Mon, Dec 17, 2018 at 02:14:40PM -0800, Jeremy Allison wrote:
>Great work Ralph ! Let me know if you can do those
>changes or you want me to do them.
thanks! Attached patchset should address all issues.
-slow
--
Ralph Boehme, Samba Team https://samba.org/
Samba Developer, SerNet GmbH https://sernet.de/en/samba/
GPG-Fingerprint FAE2C6088A24252051C559E4AA1E9B7126399E46
-------------- next part --------------
From fbe5ade86235ab463f2911975b78918d87695422 Mon Sep 17 00:00:00 2001
From: Ralph Boehme <slow at samba.org>
Date: Wed, 12 Dec 2018 08:57:28 +0100
Subject: [PATCH 01/17] debug: move some definitions around
They will be needed by some function in a subsequent commit.
Signed-off-by: Ralph Boehme <slow at samba.org>
---
lib/util/debug.c | 103 ++++++++++++++++++++++++-----------------------
1 file changed, 52 insertions(+), 51 deletions(-)
diff --git a/lib/util/debug.c b/lib/util/debug.c
index b5f120bb3a4..e409c548e64 100644
--- a/lib/util/debug.c
+++ b/lib/util/debug.c
@@ -103,6 +103,58 @@ static struct {
.fd = 2 /* stderr by default */
};
+static const char *default_classname_table[] = {
+ [DBGC_ALL] = "all",
+ [DBGC_TDB] = "tdb",
+ [DBGC_PRINTDRIVERS] = "printdrivers",
+ [DBGC_LANMAN] = "lanman",
+ [DBGC_SMB] = "smb",
+ [DBGC_RPC_PARSE] = "rpc_parse",
+ [DBGC_RPC_SRV] = "rpc_srv",
+ [DBGC_RPC_CLI] = "rpc_cli",
+ [DBGC_PASSDB] = "passdb",
+ [DBGC_SAM] = "sam",
+ [DBGC_AUTH] = "auth",
+ [DBGC_WINBIND] = "winbind",
+ [DBGC_VFS] = "vfs",
+ [DBGC_IDMAP] = "idmap",
+ [DBGC_QUOTA] = "quota",
+ [DBGC_ACLS] = "acls",
+ [DBGC_LOCKING] = "locking",
+ [DBGC_MSDFS] = "msdfs",
+ [DBGC_DMAPI] = "dmapi",
+ [DBGC_REGISTRY] = "registry",
+ [DBGC_SCAVENGER] = "scavenger",
+ [DBGC_DNS] = "dns",
+ [DBGC_LDB] = "ldb",
+ [DBGC_TEVENT] = "tevent",
+ [DBGC_AUTH_AUDIT] = "auth_audit",
+ [DBGC_AUTH_AUDIT_JSON] = "auth_json_audit",
+ [DBGC_KERBEROS] = "kerberos",
+ [DBGC_DRS_REPL] = "drs_repl",
+ [DBGC_SMB2] = "smb2",
+ [DBGC_SMB2_CREDITS] = "smb2_credits",
+ [DBGC_DSDB_AUDIT] = "dsdb_audit",
+ [DBGC_DSDB_AUDIT_JSON] = "dsdb_json_audit",
+ [DBGC_DSDB_PWD_AUDIT] = "dsdb_password_audit",
+ [DBGC_DSDB_PWD_AUDIT_JSON] = "dsdb_password_json_audit",
+ [DBGC_DSDB_TXN_AUDIT] = "dsdb_transaction_audit",
+ [DBGC_DSDB_TXN_AUDIT_JSON] = "dsdb_transaction_json_audit",
+ [DBGC_DSDB_GROUP_AUDIT] = "dsdb_group_audit",
+ [DBGC_DSDB_GROUP_AUDIT_JSON] = "dsdb_group_json_audit",
+};
+
+/*
+ * This is to allow reading of DEBUGLEVEL_CLASS before the debug
+ * system has been initialized.
+ */
+static int debug_class_list_initial[ARRAY_SIZE(default_classname_table)];
+
+static size_t debug_num_classes = 0;
+static int *DEBUGLEVEL_CLASS = debug_class_list_initial;
+
+static int current_msg_level = 0;
+
#if defined(WITH_SYSLOG) || defined(HAVE_LIBSYSTEMD_JOURNAL) || defined(HAVE_LIBSYSTEMD)
static int debug_level_to_priority(int level)
{
@@ -512,56 +564,6 @@ static void debug_backends_log(const char *msg, int msg_level)
*/
bool override_logfile;
-static const char *default_classname_table[] = {
- [DBGC_ALL] = "all",
- [DBGC_TDB] = "tdb",
- [DBGC_PRINTDRIVERS] = "printdrivers",
- [DBGC_LANMAN] = "lanman",
- [DBGC_SMB] = "smb",
- [DBGC_RPC_PARSE] = "rpc_parse",
- [DBGC_RPC_SRV] = "rpc_srv",
- [DBGC_RPC_CLI] = "rpc_cli",
- [DBGC_PASSDB] = "passdb",
- [DBGC_SAM] = "sam",
- [DBGC_AUTH] = "auth",
- [DBGC_WINBIND] = "winbind",
- [DBGC_VFS] = "vfs",
- [DBGC_IDMAP] = "idmap",
- [DBGC_QUOTA] = "quota",
- [DBGC_ACLS] = "acls",
- [DBGC_LOCKING] = "locking",
- [DBGC_MSDFS] = "msdfs",
- [DBGC_DMAPI] = "dmapi",
- [DBGC_REGISTRY] = "registry",
- [DBGC_SCAVENGER] = "scavenger",
- [DBGC_DNS] = "dns",
- [DBGC_LDB] = "ldb",
- [DBGC_TEVENT] = "tevent",
- [DBGC_AUTH_AUDIT] = "auth_audit",
- [DBGC_AUTH_AUDIT_JSON] = "auth_json_audit",
- [DBGC_KERBEROS] = "kerberos",
- [DBGC_DRS_REPL] = "drs_repl",
- [DBGC_SMB2] = "smb2",
- [DBGC_SMB2_CREDITS] = "smb2_credits",
- [DBGC_DSDB_AUDIT] = "dsdb_audit",
- [DBGC_DSDB_AUDIT_JSON] = "dsdb_json_audit",
- [DBGC_DSDB_PWD_AUDIT] = "dsdb_password_audit",
- [DBGC_DSDB_PWD_AUDIT_JSON] = "dsdb_password_json_audit",
- [DBGC_DSDB_TXN_AUDIT] = "dsdb_transaction_audit",
- [DBGC_DSDB_TXN_AUDIT_JSON] = "dsdb_transaction_json_audit",
- [DBGC_DSDB_GROUP_AUDIT] = "dsdb_group_audit",
- [DBGC_DSDB_GROUP_AUDIT_JSON] = "dsdb_group_json_audit",
-};
-
-/*
- * This is to allow reading of DEBUGLEVEL_CLASS before the debug
- * system has been initialized.
- */
-static int debug_class_list_initial[ARRAY_SIZE(default_classname_table)];
-
-static size_t debug_num_classes = 0;
-static int *DEBUGLEVEL_CLASS = debug_class_list_initial;
-
int debuglevel_get_class(size_t idx)
{
return DEBUGLEVEL_CLASS[idx];
@@ -597,7 +599,6 @@ void debuglevel_set_class(size_t idx, int level)
*/
static int debug_count = 0;
-static int current_msg_level = 0;
static char format_bufr[FORMAT_BUFR_SIZE];
static size_t format_pos = 0;
static bool log_overflow = false;
--
2.17.2
From 3b7361e1e8efab4e6137a9a73f439200519af7fc Mon Sep 17 00:00:00 2001
From: Ralph Boehme <slow at samba.org>
Date: Wed, 12 Dec 2018 22:19:06 +0100
Subject: [PATCH 02/17] debug: rename DEBUGLEVEL_CLASS variable to dbgc_config
Variable names should not be given in uppercase.
Signed-off-by: Ralph Boehme <slow at samba.org>
---
lib/util/debug.c | 38 +++++++++++++++++++-------------------
1 file changed, 19 insertions(+), 19 deletions(-)
diff --git a/lib/util/debug.c b/lib/util/debug.c
index e409c548e64..f77eb158ff5 100644
--- a/lib/util/debug.c
+++ b/lib/util/debug.c
@@ -145,13 +145,13 @@ static const char *default_classname_table[] = {
};
/*
- * This is to allow reading of DEBUGLEVEL_CLASS before the debug
+ * This is to allow reading of dbgc_config before the debug
* system has been initialized.
*/
static int debug_class_list_initial[ARRAY_SIZE(default_classname_table)];
static size_t debug_num_classes = 0;
-static int *DEBUGLEVEL_CLASS = debug_class_list_initial;
+static int *dbgc_config = debug_class_list_initial;
static int current_msg_level = 0;
@@ -566,12 +566,12 @@ bool override_logfile;
int debuglevel_get_class(size_t idx)
{
- return DEBUGLEVEL_CLASS[idx];
+ return dbgc_config[idx];
}
void debuglevel_set_class(size_t idx, int level)
{
- DEBUGLEVEL_CLASS[idx] = level;
+ dbgc_config[idx] = level;
}
@@ -628,9 +628,9 @@ void gfree_debugsyms(void)
TALLOC_FREE(classname_table);
- if ( DEBUGLEVEL_CLASS != debug_class_list_initial ) {
- TALLOC_FREE( DEBUGLEVEL_CLASS );
- DEBUGLEVEL_CLASS = discard_const_p(int, debug_class_list_initial);
+ if ( dbgc_config != debug_class_list_initial ) {
+ TALLOC_FREE( dbgc_config );
+ dbgc_config = discard_const_p(int, debug_class_list_initial);
}
debug_num_classes = 0;
@@ -655,7 +655,7 @@ char *debug_list_class_names_and_levels(void)
buf = talloc_asprintf_append(buf,
"%s:%d%s",
classname_table[i],
- DEBUGLEVEL_CLASS[i],
+ dbgc_config[i],
i == (debug_num_classes - 1) ? "\n" : " ");
if (buf == NULL) {
return NULL;
@@ -703,21 +703,21 @@ int debug_add_class(const char *classname)
return ndx;
ndx = debug_num_classes;
- if (DEBUGLEVEL_CLASS == debug_class_list_initial) {
+ if (dbgc_config == debug_class_list_initial) {
/* Initial loading... */
new_class_list = NULL;
} else {
- new_class_list = DEBUGLEVEL_CLASS;
+ new_class_list = dbgc_config;
}
- default_level = DEBUGLEVEL_CLASS[DBGC_ALL];
+ default_level = dbgc_config[DBGC_ALL];
new_class_list = talloc_realloc(NULL, new_class_list, int, ndx + 1);
if (!new_class_list)
return -1;
- DEBUGLEVEL_CLASS = new_class_list;
+ dbgc_config = new_class_list;
- DEBUGLEVEL_CLASS[ndx] = default_level;
+ dbgc_config[ndx] = default_level;
new_name_list = talloc_realloc(NULL, classname_table, char *, ndx + 1);
if (!new_name_list)
@@ -767,7 +767,7 @@ static void debug_dump_status(int level)
const char *classname = classname_table[q];
DEBUGADD(level, (" %s: %d\n",
classname,
- DEBUGLEVEL_CLASS[q]));
+ dbgc_config[q]));
}
}
@@ -793,7 +793,7 @@ static bool debug_parse_param(char *param)
return false;
}
- DEBUGLEVEL_CLASS[ndx] = atoi(class_level);
+ dbgc_config[ndx] = atoi(class_level);
return true;
}
@@ -825,15 +825,15 @@ bool debug_parse_levels(const char *params_str)
* v.s. "all:10", this is the traditional way to set DEBUGLEVEL
*/
if (isdigit(tok[0])) {
- DEBUGLEVEL_CLASS[DBGC_ALL] = atoi(tok);
+ dbgc_config[DBGC_ALL] = atoi(tok);
tok = strtok_r(NULL, LIST_SEP, &saveptr);
} else {
- DEBUGLEVEL_CLASS[DBGC_ALL] = 0;
+ dbgc_config[DBGC_ALL] = 0;
}
/* Array is debug_num_classes long */
for (i = DBGC_ALL+1; i < debug_num_classes; i++) {
- DEBUGLEVEL_CLASS[i] = DEBUGLEVEL_CLASS[DBGC_ALL];
+ dbgc_config[i] = dbgc_config[DBGC_ALL];
}
while (tok != NULL) {
@@ -1404,7 +1404,7 @@ bool dbghdrclass(int level, int cls, const char *location, const char *func)
goto full;
}
- if (unlikely(DEBUGLEVEL_CLASS[ cls ] >= 10)) {
+ if (unlikely(dbgc_config[cls] >= 10)) {
verbose = true;
}
--
2.17.2
From e759f18a38b15685d5e8c11f8aa015e48fedc1c2 Mon Sep 17 00:00:00 2001
From: Ralph Boehme <slow at samba.org>
Date: Wed, 12 Dec 2018 11:57:05 +0100
Subject: [PATCH 03/17] debug: README.Coding fixes
Signed-off-by: Ralph Boehme <slow at samba.org>
---
lib/util/debug.c | 31 +++++++++++++++++++------------
1 file changed, 19 insertions(+), 12 deletions(-)
diff --git a/lib/util/debug.c b/lib/util/debug.c
index f77eb158ff5..49efd66bce8 100644
--- a/lib/util/debug.c
+++ b/lib/util/debug.c
@@ -692,15 +692,17 @@ int debug_add_class(const char *classname)
char **new_name_list;
int default_level;
- if (!classname)
+ if (classname == NULL) {
return -1;
+ }
/* check the init has yet been called */
debug_init();
ndx = debug_lookup_classname_int(classname);
- if (ndx >= 0)
+ if (ndx >= 0) {
return ndx;
+ }
ndx = debug_num_classes;
if (dbgc_config == debug_class_list_initial) {
@@ -713,20 +715,24 @@ int debug_add_class(const char *classname)
default_level = dbgc_config[DBGC_ALL];
new_class_list = talloc_realloc(NULL, new_class_list, int, ndx + 1);
- if (!new_class_list)
+ if (new_class_list == NULL) {
return -1;
+ }
+
dbgc_config = new_class_list;
dbgc_config[ndx] = default_level;
new_name_list = talloc_realloc(NULL, classname_table, char *, ndx + 1);
- if (!new_name_list)
+ if (new_name_list == NULL) {
return -1;
+ }
classname_table = new_name_list;
classname_table[ndx] = talloc_strdup(classname_table, classname);
- if (! classname_table[ndx])
+ if (classname_table[ndx] == NULL) {
return -1;
+ }
debug_num_classes = ndx + 1;
@@ -1126,19 +1132,20 @@ _PUBLIC_ void debug_schedule_reopen_logs(void)
Check to see if there is any need to check if the logfile has grown too big.
**************************************************************************/
-bool need_to_check_log_size( void )
+bool need_to_check_log_size(void)
{
int maxlog;
- if( debug_count < 100)
- return( false );
+ if (debug_count < 100) {
+ return false;
+ }
maxlog = state.settings.max_log_size * 1024;
- if ( state.fd <=2 || maxlog <= 0 ) {
+ if (state.fd <= 2 || maxlog <= 0) {
debug_count = 0;
- return(false);
+ return false;
}
- return( true );
+ return true;
}
/**************************************************************************
@@ -1175,7 +1182,7 @@ void check_log_size( void )
maxlog = state.settings.max_log_size * 1024;
if (state.schedule_reopen_logs) {
- (void)reopen_logs_internal();
+ (void)reopen_logs_internal();
}
if (maxlog && (fstat(state.fd, &st) == 0
--
2.17.2
From 56db33b4be9a71acf2ef8360eb244e6b95446022 Mon Sep 17 00:00:00 2001
From: Ralph Boehme <slow at samba.org>
Date: Wed, 12 Dec 2018 22:21:57 +0100
Subject: [PATCH 04/17] debug: convert dbgc_config to an array of struct
debug_class
Signed-off-by: Ralph Boehme <slow at samba.org>
---
lib/util/debug.c | 45 +++++++++++++++++++++++++++++----------------
1 file changed, 29 insertions(+), 16 deletions(-)
diff --git a/lib/util/debug.c b/lib/util/debug.c
index 49efd66bce8..cb7d80e7c3a 100644
--- a/lib/util/debug.c
+++ b/lib/util/debug.c
@@ -103,6 +103,13 @@ static struct {
.fd = 2 /* stderr by default */
};
+struct debug_class {
+ /*
+ * The debug loglevel of the class.
+ */
+ int loglevel;
+};
+
static const char *default_classname_table[] = {
[DBGC_ALL] = "all",
[DBGC_TDB] = "tdb",
@@ -148,10 +155,10 @@ static const char *default_classname_table[] = {
* This is to allow reading of dbgc_config before the debug
* system has been initialized.
*/
-static int debug_class_list_initial[ARRAY_SIZE(default_classname_table)];
+static struct debug_class debug_class_list_initial[ARRAY_SIZE(default_classname_table)];
static size_t debug_num_classes = 0;
-static int *dbgc_config = debug_class_list_initial;
+static struct debug_class *dbgc_config = debug_class_list_initial;
static int current_msg_level = 0;
@@ -566,12 +573,12 @@ bool override_logfile;
int debuglevel_get_class(size_t idx)
{
- return dbgc_config[idx];
+ return dbgc_config[idx].loglevel;
}
void debuglevel_set_class(size_t idx, int level)
{
- dbgc_config[idx] = level;
+ dbgc_config[idx].loglevel = level;
}
@@ -630,7 +637,8 @@ void gfree_debugsyms(void)
if ( dbgc_config != debug_class_list_initial ) {
TALLOC_FREE( dbgc_config );
- dbgc_config = discard_const_p(int, debug_class_list_initial);
+ dbgc_config = discard_const_p(struct debug_class,
+ debug_class_list_initial);
}
debug_num_classes = 0;
@@ -655,7 +663,7 @@ char *debug_list_class_names_and_levels(void)
buf = talloc_asprintf_append(buf,
"%s:%d%s",
classname_table[i],
- dbgc_config[i],
+ dbgc_config[i].loglevel,
i == (debug_num_classes - 1) ? "\n" : " ");
if (buf == NULL) {
return NULL;
@@ -688,7 +696,7 @@ static int debug_lookup_classname_int(const char* classname)
int debug_add_class(const char *classname)
{
int ndx;
- int *new_class_list;
+ struct debug_class *new_class_list = NULL;
char **new_name_list;
int default_level;
@@ -712,16 +720,21 @@ int debug_add_class(const char *classname)
new_class_list = dbgc_config;
}
- default_level = dbgc_config[DBGC_ALL];
+ default_level = dbgc_config[DBGC_ALL].loglevel;
- new_class_list = talloc_realloc(NULL, new_class_list, int, ndx + 1);
+ new_class_list = talloc_realloc(NULL,
+ new_class_list,
+ struct debug_class,
+ ndx + 1);
if (new_class_list == NULL) {
return -1;
}
dbgc_config = new_class_list;
- dbgc_config[ndx] = default_level;
+ dbgc_config[ndx] = (struct debug_class) {
+ .loglevel = default_level,
+ };
new_name_list = talloc_realloc(NULL, classname_table, char *, ndx + 1);
if (new_name_list == NULL) {
@@ -773,7 +786,7 @@ static void debug_dump_status(int level)
const char *classname = classname_table[q];
DEBUGADD(level, (" %s: %d\n",
classname,
- dbgc_config[q]));
+ dbgc_config[q].loglevel));
}
}
@@ -799,7 +812,7 @@ static bool debug_parse_param(char *param)
return false;
}
- dbgc_config[ndx] = atoi(class_level);
+ dbgc_config[ndx].loglevel = atoi(class_level);
return true;
}
@@ -831,15 +844,15 @@ bool debug_parse_levels(const char *params_str)
* v.s. "all:10", this is the traditional way to set DEBUGLEVEL
*/
if (isdigit(tok[0])) {
- dbgc_config[DBGC_ALL] = atoi(tok);
+ dbgc_config[DBGC_ALL].loglevel = atoi(tok);
tok = strtok_r(NULL, LIST_SEP, &saveptr);
} else {
- dbgc_config[DBGC_ALL] = 0;
+ dbgc_config[DBGC_ALL].loglevel = 0;
}
/* Array is debug_num_classes long */
for (i = DBGC_ALL+1; i < debug_num_classes; i++) {
- dbgc_config[i] = dbgc_config[DBGC_ALL];
+ dbgc_config[i].loglevel = dbgc_config[DBGC_ALL].loglevel;
}
while (tok != NULL) {
@@ -1411,7 +1424,7 @@ bool dbghdrclass(int level, int cls, const char *location, const char *func)
goto full;
}
- if (unlikely(dbgc_config[cls] >= 10)) {
+ if (unlikely(dbgc_config[cls].loglevel >= 10)) {
verbose = true;
}
--
2.17.2
From 781cceae11c31ee2f8fc728d96867081dfef0ddc Mon Sep 17 00:00:00 2001
From: Ralph Boehme <slow at samba.org>
Date: Wed, 12 Dec 2018 22:22:44 +0100
Subject: [PATCH 05/17] debug: track current debug message class
This is analog to current_msg_level.
Signed-off-by: Ralph Boehme <slow at samba.org>
---
lib/util/debug.c | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/lib/util/debug.c b/lib/util/debug.c
index cb7d80e7c3a..38bfe7b9b4a 100644
--- a/lib/util/debug.c
+++ b/lib/util/debug.c
@@ -161,6 +161,7 @@ static size_t debug_num_classes = 0;
static struct debug_class *dbgc_config = debug_class_list_initial;
static int current_msg_level = 0;
+static int current_msg_class = 0;
#if defined(WITH_SYSLOG) || defined(HAVE_LIBSYSTEMD_JOURNAL) || defined(HAVE_LIBSYSTEMD)
static int debug_level_to_priority(int level)
@@ -1401,6 +1402,9 @@ bool dbghdrclass(int level, int cls, const char *location, const char *func)
/* Set current_msg_level. */
current_msg_level = level;
+ /* Set current message class */
+ current_msg_class = cls;
+
/* Don't print a header if we're logging to stdout. */
if ( state.logtype != DEBUG_FILE ) {
return( true );
--
2.17.2
From 330983e10d68c02abb337f942fffb45333e75ae3 Mon Sep 17 00:00:00 2001
From: Ralph Boehme <slow at samba.org>
Date: Wed, 12 Dec 2018 12:35:42 +0100
Subject: [PATCH 06/17] debug: factor out a function that opens and closes the
new and old logfile
The new function reopen_one_log() will also be used for per-class
logfiles in subsequent commmits.
Signed-off-by: Ralph Boehme <slow at samba.org>
---
lib/util/debug.c | 40 ++++++++++++++++++++++++----------------
1 file changed, 24 insertions(+), 16 deletions(-)
diff --git a/lib/util/debug.c b/lib/util/debug.c
index 38bfe7b9b4a..4318bffa732 100644
--- a/lib/util/debug.c
+++ b/lib/util/debug.c
@@ -1043,6 +1043,27 @@ static void debug_callback_log(const char *msg, int msg_level)
Fix from dgibson at linuxcare.com.
**************************************************************************/
+static bool reopen_one_log(int *fd, const char *logfile)
+{
+ int old_fd = *fd;
+ int new_fd;
+
+ new_fd = open(logfile, O_WRONLY|O_APPEND|O_CREAT, 0644);
+ if (new_fd == -1) {
+ log_overflow = true;
+ DBG_ERR("Unable to open new log file '%s': %s\n",
+ logfile, strerror(errno));
+ log_overflow = false;
+ return false;
+ }
+
+ debug_close_fd(old_fd);
+ smb_set_close_on_exec(new_fd);
+ *fd = new_fd;
+
+ return true;
+}
+
/**
reopen the log file (usually called because the log file name might have changed)
*/
@@ -1050,8 +1071,7 @@ bool reopen_logs_internal(void)
{
mode_t oldumask;
int new_fd = 0;
- int old_fd = 0;
- bool ret = true;
+ bool ok;
if (state.reopening_logs) {
return true;
@@ -1087,19 +1107,7 @@ bool reopen_logs_internal(void)
state.reopening_logs = true;
- new_fd = open( state.debugf, O_WRONLY|O_APPEND|O_CREAT, 0644);
-
- if (new_fd == -1) {
- log_overflow = true;
- DEBUG(0, ("Unable to open new log file '%s': %s\n", state.debugf, strerror(errno)));
- log_overflow = false;
- ret = false;
- } else {
- smb_set_close_on_exec(new_fd);
- old_fd = state.fd;
- state.fd = new_fd;
- debug_close_fd(old_fd);
- }
+ ok = reopen_one_log(&state.fd, state.debugf);
/* Fix from klausr at ITAP.Physik.Uni-Stuttgart.De
* to fix problem where smbd's that generate less
@@ -1124,7 +1132,7 @@ bool reopen_logs_internal(void)
state.reopening_logs = false;
- return ret;
+ return ok;
}
/**************************************************************************
--
2.17.2
From 795119f77a7dae1e1fa38bb591ee2b70eaa14a88 Mon Sep 17 00:00:00 2001
From: Ralph Boehme <slow at samba.org>
Date: Wed, 12 Dec 2018 08:48:35 +0100
Subject: [PATCH 07/17] debug: add an empty line
Signed-off-by: Ralph Boehme <slow at samba.org>
---
lib/util/debug_s3.c | 1 +
1 file changed, 1 insertion(+)
diff --git a/lib/util/debug_s3.c b/lib/util/debug_s3.c
index 99ba2e7188d..95f22938bc3 100644
--- a/lib/util/debug_s3.c
+++ b/lib/util/debug_s3.c
@@ -32,6 +32,7 @@ bool reopen_logs(void)
{
if (lp_loaded()) {
struct debug_settings settings;
+
debug_set_logfile(lp_logfile(talloc_tos()));
ZERO_STRUCT(settings);
--
2.17.2
From a2beecd09f71dae1dcb31abdfb283ead952ade00 Mon Sep 17 00:00:00 2001
From: Ralph Boehme <slow at samba.org>
Date: Wed, 12 Dec 2018 12:43:47 +0100
Subject: [PATCH 08/17] debug: add a call to debug_parse_levels() to
reopen_logs()
This allows correct refresh of the "log level" setting when reloading config.
Signed-off-by: Ralph Boehme <slow at samba.org>
---
lib/util/debug_s3.c | 1 +
1 file changed, 1 insertion(+)
diff --git a/lib/util/debug_s3.c b/lib/util/debug_s3.c
index 95f22938bc3..479348a7310 100644
--- a/lib/util/debug_s3.c
+++ b/lib/util/debug_s3.c
@@ -34,6 +34,7 @@ bool reopen_logs(void)
struct debug_settings settings;
debug_set_logfile(lp_logfile(talloc_tos()));
+ debug_parse_levels(lp_log_level(talloc_tos()));
ZERO_STRUCT(settings);
settings.max_log_size = lp_max_log_size();
--
2.17.2
From 5eda1845dd051ee9a9a747bcb22e9f5497ebda4d Mon Sep 17 00:00:00 2001
From: Ralph Boehme <slow at samba.org>
Date: Wed, 12 Dec 2018 12:45:11 +0100
Subject: [PATCH 09/17] debug: factor out logfile size check
The new function will also be used for upcoming per-debug-class logfiles.
Signed-off-by: Ralph Boehme <slow at samba.org>
---
lib/util/debug.c | 73 +++++++++++++++++++++++++++++++++++-------------
1 file changed, 54 insertions(+), 19 deletions(-)
diff --git a/lib/util/debug.c b/lib/util/debug.c
index 4318bffa732..2174add2a6c 100644
--- a/lib/util/debug.c
+++ b/lib/util/debug.c
@@ -1174,10 +1174,61 @@ bool need_to_check_log_size(void)
Check to see if the log has grown to be too big.
**************************************************************************/
-void check_log_size( void )
+static void do_one_check_log_size(off_t maxlog, int *_fd, const char *logfile)
{
- int maxlog;
+ char name[strlen(logfile) + 5];
struct stat st;
+ int fd = *_fd;
+ int ret;
+ bool ok;
+
+ if (maxlog == 0) {
+ return;
+ }
+
+ ret = fstat(fd, &st);
+ if (ret != 0) {
+ return;
+ }
+ if (st.st_size < maxlog ) {
+ return;
+ }
+
+ /* reopen_logs_internal() modifies *_fd */
+ (void)reopen_logs_internal();
+ fd = *_fd;
+
+ if (fd <= 2) {
+ return;
+ }
+ ret = fstat(fd, &st);
+ if (ret != 0) {
+ return;
+ }
+ if (st.st_size < maxlog) {
+ return;
+ }
+
+ snprintf(name, sizeof(name), "%s.old", logfile);
+
+ (void)rename(logfile, name);
+
+ ok = reopen_logs_internal();
+ if (ok) {
+ return;
+ }
+ /* We failed to reopen a log - continue using the old name. */
+ (void)rename(name, logfile);
+}
+
+static void do_check_log_size(off_t maxlog)
+{
+ do_one_check_log_size(maxlog, &state.fd, state.debugf);
+}
+
+void check_log_size( void )
+{
+ off_t maxlog;
/*
* We need to be root to check/change log-file, skip this and let the main
@@ -1207,23 +1258,7 @@ void check_log_size( void )
(void)reopen_logs_internal();
}
- if (maxlog && (fstat(state.fd, &st) == 0
- && st.st_size > maxlog )) {
- (void)reopen_logs_internal();
- if (state.fd > 2 && (fstat(state.fd, &st) == 0
- && st.st_size > maxlog)) {
- char name[strlen(state.debugf) + 5];
-
- snprintf(name, sizeof(name), "%s.old", state.debugf);
-
- (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);
- }
- }
- }
+ do_check_log_size(maxlog);
/*
* Here's where we need to panic if state.fd == 0 or -1 (invalid values)
--
2.17.2
From 7666d456965164fe184446825eb71c900ab06b52 Mon Sep 17 00:00:00 2001
From: Ralph Boehme <slow at samba.org>
Date: Wed, 12 Dec 2018 12:51:16 +0100
Subject: [PATCH 10/17] debug: add logfile and fd to struct debug_class
Initialized to -1. Already checked in debug_file_log() without affecting
behaviour until subsequent commits set per-debug-class fds.
Signed-off-by: Ralph Boehme <slow at samba.org>
---
lib/util/debug.c | 18 +++++++++++++++++-
1 file changed, 17 insertions(+), 1 deletion(-)
diff --git a/lib/util/debug.c b/lib/util/debug.c
index 2174add2a6c..d01bde7a479 100644
--- a/lib/util/debug.c
+++ b/lib/util/debug.c
@@ -108,6 +108,13 @@ struct debug_class {
* The debug loglevel of the class.
*/
int loglevel;
+
+ /*
+ * An optional class specific logfile, may be NULL in which case the
+ * "global" logfile is used and fd is -1.
+ */
+ char *logfile;
+ int fd;
};
static const char *default_classname_table[] = {
@@ -201,10 +208,18 @@ static void debug_file_log(int msg_level,
const char *msg, const char *msg_no_nl)
{
ssize_t ret;
+ int fd;
check_log_size();
+
+ if (dbgc_config[current_msg_class].fd != -1) {
+ fd = dbgc_config[current_msg_class].fd;
+ } else {
+ fd = state.fd;
+ }
+
do {
- ret = write(state.fd, msg, strlen(msg));
+ ret = write(fd, msg, strlen(msg));
} while (ret == -1 && errno == EINTR);
}
@@ -735,6 +750,7 @@ int debug_add_class(const char *classname)
dbgc_config[ndx] = (struct debug_class) {
.loglevel = default_level,
+ .fd = -1,
};
new_name_list = talloc_realloc(NULL, classname_table, char *, ndx + 1);
--
2.17.2
From 0b4c2f5b7a64034d54ea84fd13786c79b45a352b Mon Sep 17 00:00:00 2001
From: Ralph Boehme <slow at samba.org>
Date: Wed, 12 Dec 2018 13:09:03 +0100
Subject: [PATCH 11/17] debug: add resource cleanup for per debug-class
logfiles
Signed-off-by: Ralph Boehme <slow at samba.org>
---
lib/util/debug.c | 1 +
1 file changed, 1 insertion(+)
diff --git a/lib/util/debug.c b/lib/util/debug.c
index d01bde7a479..780ce9a26c2 100644
--- a/lib/util/debug.c
+++ b/lib/util/debug.c
@@ -870,6 +870,7 @@ bool debug_parse_levels(const char *params_str)
/* Array is debug_num_classes long */
for (i = DBGC_ALL+1; i < debug_num_classes; i++) {
dbgc_config[i].loglevel = dbgc_config[DBGC_ALL].loglevel;
+ TALLOC_FREE(dbgc_config[i].logfile);
}
while (tok != NULL) {
--
2.17.2
From f836e78abe17ddcf61f0162d0c62229ba6cea6ca Mon Sep 17 00:00:00 2001
From: Ralph Boehme <slow at samba.org>
Date: Wed, 12 Dec 2018 13:11:26 +0100
Subject: [PATCH 12/17] debug: add support for per debug-class logfiles
This adds support for per debug-class logfiles to the function parsing
the "log level" option.
The enhanced syntax is:
log level = CLASS:LEVEL[@PATH] [CLASS:LEVEL[@PATH] ... ]
Eg
log level = full_audit:1@/var/log/audit.logfile
While the option is already parsed and stored in in the dbgc_config[]
array, the feature is still effectively disabled, as
reopen_logs_internal() still doesn't open the per debug-class logfiles.
Signed-off-by: Ralph Boehme <slow at samba.org>
---
lib/util/debug.c | 15 ++++++++++++++-
1 file changed, 14 insertions(+), 1 deletion(-)
diff --git a/lib/util/debug.c b/lib/util/debug.c
index 780ce9a26c2..c315222a9bb 100644
--- a/lib/util/debug.c
+++ b/lib/util/debug.c
@@ -810,6 +810,7 @@ static void debug_dump_status(int level)
static bool debug_parse_param(char *param)
{
char *class_name;
+ char *class_file = NULL;
char *class_level;
char *saveptr = NULL;
int ndx;
@@ -819,11 +820,13 @@ static bool debug_parse_param(char *param)
return false;
}
- class_level = strtok_r(NULL, "\0", &saveptr);
+ class_level = strtok_r(NULL, "@\0", &saveptr);
if (class_level == NULL) {
return false;
}
+ class_file = strtok_r(NULL, "\0", &saveptr);
+
ndx = debug_lookup_classname(class_name);
if (ndx == -1) {
return false;
@@ -831,6 +834,16 @@ static bool debug_parse_param(char *param)
dbgc_config[ndx].loglevel = atoi(class_level);
+ if (class_file == NULL) {
+ return true;
+ }
+
+ TALLOC_FREE(dbgc_config[ndx].logfile);
+
+ dbgc_config[ndx].logfile = talloc_strdup(NULL, class_file);
+ if (dbgc_config[ndx].logfile == NULL) {
+ return false;
+ }
return true;
}
--
2.17.2
From a01f7bfa7ef22ce1ee2738f73039c2d62dd8883c Mon Sep 17 00:00:00 2001
From: Ralph Boehme <slow at samba.org>
Date: Wed, 12 Dec 2018 14:54:41 +0100
Subject: [PATCH 13/17] debug: update need_to_check_log_size() for per
debug-class logfiles
Signed-off-by: Ralph Boehme <slow at samba.org>
---
lib/util/debug.c | 17 +++++++++++++++--
1 file changed, 15 insertions(+), 2 deletions(-)
diff --git a/lib/util/debug.c b/lib/util/debug.c
index c315222a9bb..fb4332f4273 100644
--- a/lib/util/debug.c
+++ b/lib/util/debug.c
@@ -1187,17 +1187,30 @@ _PUBLIC_ void debug_schedule_reopen_logs(void)
bool need_to_check_log_size(void)
{
int maxlog;
+ size_t i;
if (debug_count < 100) {
return false;
}
maxlog = state.settings.max_log_size * 1024;
- if (state.fd <= 2 || maxlog <= 0) {
+ if (maxlog <= 0) {
debug_count = 0;
return false;
}
- return true;
+
+ if (state.fd > 2) {
+ return true;
+ }
+
+ for (i = DBGC_ALL + 1; i < debug_num_classes; i++) {
+ if (dbgc_config[i].fd != -1) {
+ return true;
+ }
+ }
+
+ debug_count = 0;
+ return false;
}
/**************************************************************************
--
2.17.2
From dd030050d789f1ee25b9af1914fed562ce83e954 Mon Sep 17 00:00:00 2001
From: Ralph Boehme <slow at samba.org>
Date: Wed, 12 Dec 2018 14:43:22 +0100
Subject: [PATCH 14/17] debug: update logsize checking for per debug-class
logfiles
Signed-off-by: Ralph Boehme <slow at samba.org>
---
lib/util/debug.c | 14 ++++++++++++++
1 file changed, 14 insertions(+)
diff --git a/lib/util/debug.c b/lib/util/debug.c
index fb4332f4273..81a1421eb2f 100644
--- a/lib/util/debug.c
+++ b/lib/util/debug.c
@@ -1266,7 +1266,21 @@ static void do_one_check_log_size(off_t maxlog, int *_fd, const char *logfile)
static void do_check_log_size(off_t maxlog)
{
+ size_t i;
+
do_one_check_log_size(maxlog, &state.fd, state.debugf);
+
+ for (i = DBGC_ALL + 1; i < debug_num_classes; i++) {
+ if (dbgc_config[i].fd == -1) {
+ continue;
+ }
+ if (dbgc_config[i].logfile == NULL) {
+ continue;
+ }
+ do_one_check_log_size(maxlog,
+ &dbgc_config[i].fd,
+ dbgc_config[i].logfile);
+ }
}
void check_log_size( void )
--
2.17.2
From e308670e3800561533f43b7fa58789b1d7e79ddf Mon Sep 17 00:00:00 2001
From: Ralph Boehme <slow at samba.org>
Date: Wed, 12 Dec 2018 22:49:15 +0100
Subject: [PATCH 15/17] debug: remove fd and debugf from state, use
dbgc_config[DBGC_ALL]
Signed-off-by: Ralph Boehme <slow at samba.org>
---
lib/util/debug.c | 59 ++++++++++++++++++++++++++++--------------------
1 file changed, 34 insertions(+), 25 deletions(-)
diff --git a/lib/util/debug.c b/lib/util/debug.c
index 81a1421eb2f..278562596fa 100644
--- a/lib/util/debug.c
+++ b/lib/util/debug.c
@@ -86,21 +86,18 @@
/* state variables for the debug system */
static struct {
bool initialized;
- int fd; /* The log file handle */
enum debug_logtype logtype; /* The type of logging we are doing: eg stdout, file, stderr */
const char *prog_name;
bool reopening_logs;
bool schedule_reopen_logs;
struct debug_settings settings;
- char *debugf;
debug_callback_fn callback;
void *callback_private;
} state = {
.settings = {
.timestamp_logs = true
},
- .fd = 2 /* stderr by default */
};
struct debug_class {
@@ -162,7 +159,9 @@ static const char *default_classname_table[] = {
* This is to allow reading of dbgc_config before the debug
* system has been initialized.
*/
-static struct debug_class debug_class_list_initial[ARRAY_SIZE(default_classname_table)];
+static struct debug_class debug_class_list_initial[ARRAY_SIZE(default_classname_table)] = {
+ [DBGC_ALL] = (struct debug_class) { .fd = 2 },
+};
static size_t debug_num_classes = 0;
static struct debug_class *dbgc_config = debug_class_list_initial;
@@ -215,7 +214,7 @@ static void debug_file_log(int msg_level,
if (dbgc_config[current_msg_class].fd != -1) {
fd = dbgc_config[current_msg_class].fd;
} else {
- fd = state.fd;
+ fd = dbgc_config[DBGC_ALL].fd;
}
do {
@@ -934,6 +933,7 @@ static void debug_init(void)
for (i = 0; i < ARRAY_SIZE(default_classname_table); i++) {
debug_add_class(default_classname_table[i]);
}
+ dbgc_config[DBGC_ALL].fd = 2;
for (i = 0; i < ARRAY_SIZE(debug_backends); i++) {
debug_backends[i].log_level = -1;
@@ -1016,8 +1016,8 @@ void debug_set_logfile(const char *name)
/* this copes with calls when smb.conf is not loaded yet */
return;
}
- TALLOC_FREE(state.debugf);
- state.debugf = talloc_strdup(NULL, name);
+ TALLOC_FREE(dbgc_config[DBGC_ALL].logfile);
+ dbgc_config[DBGC_ALL].logfile = talloc_strdup(NULL, name);
}
static void debug_close_fd(int fd)
@@ -1101,6 +1101,7 @@ bool reopen_logs_internal(void)
{
mode_t oldumask;
int new_fd = 0;
+ size_t i;
bool ok;
if (state.reopening_logs) {
@@ -1115,14 +1116,14 @@ bool reopen_logs_internal(void)
return true;
case DEBUG_STDOUT:
case DEBUG_DEFAULT_STDOUT:
- debug_close_fd(state.fd);
- state.fd = 1;
+ debug_close_fd(dbgc_config[DBGC_ALL].fd);
+ dbgc_config[DBGC_ALL].fd = 1;
return true;
case DEBUG_DEFAULT_STDERR:
case DEBUG_STDERR:
- debug_close_fd(state.fd);
- state.fd = 2;
+ debug_close_fd(dbgc_config[DBGC_ALL].fd);
+ dbgc_config[DBGC_ALL].fd = 2;
return true;
case DEBUG_FILE:
@@ -1131,13 +1132,19 @@ bool reopen_logs_internal(void)
oldumask = umask( 022 );
- if (!state.debugf) {
+ for (i = DBGC_ALL; i < debug_num_classes; i++) {
+ if (dbgc_config[DBGC_ALL].logfile != NULL) {
+ break;
+ }
+ }
+ if (i == debug_num_classes) {
return false;
}
state.reopening_logs = true;
- ok = reopen_one_log(&state.fd, state.debugf);
+ ok = reopen_one_log(&dbgc_config[DBGC_ALL].fd,
+ dbgc_config[DBGC_ALL].logfile);
/* Fix from klausr at ITAP.Physik.Uni-Stuttgart.De
* to fix problem where smbd's that generate less
@@ -1151,7 +1158,7 @@ bool reopen_logs_internal(void)
* catch output into logs.
*/
if (new_fd != -1) {
- if (dup2(state.fd, 2) == -1) {
+ if (dup2(dbgc_config[DBGC_ALL].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 fundamental
@@ -1199,7 +1206,7 @@ bool need_to_check_log_size(void)
return false;
}
- if (state.fd > 2) {
+ if (dbgc_config[DBGC_ALL].fd > 2) {
return true;
}
@@ -1268,9 +1275,7 @@ static void do_check_log_size(off_t maxlog)
{
size_t i;
- do_one_check_log_size(maxlog, &state.fd, state.debugf);
-
- for (i = DBGC_ALL + 1; i < debug_num_classes; i++) {
+ for (i = DBGC_ALL; i < debug_num_classes; i++) {
if (dbgc_config[i].fd == -1) {
continue;
}
@@ -1318,10 +1323,11 @@ void check_log_size( void )
do_check_log_size(maxlog);
/*
- * Here's where we need to panic if state.fd == 0 or -1 (invalid values)
+ * Here's where we need to panic if dbgc_config[DBGC_ALL].fd == 0 or -1
+ * (invalid values)
*/
- if (state.fd <= 0) {
+ if (dbgc_config[DBGC_ALL].fd <= 0) {
/* 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
@@ -1332,9 +1338,10 @@ void check_log_size( void )
int fd = open( "/dev/console", O_WRONLY, 0);
if (fd != -1) {
smb_set_close_on_exec(fd);
- state.fd = fd;
- DEBUG(0,("check_log_size: open of debug file %s failed - using console.\n",
- state.debugf ));
+ dbgc_config[DBGC_ALL].fd = fd;
+ DBG_ERR("check_log_size: open of debug file %s failed "
+ "- using console.\n",
+ dbgc_config[DBGC_ALL].logfile);
} else {
/*
* We cannot continue without a debug file handle.
@@ -1364,10 +1371,12 @@ static void Debug1(const char *msg)
case DEBUG_STDERR:
case DEBUG_DEFAULT_STDOUT:
case DEBUG_DEFAULT_STDERR:
- if (state.fd > 0) {
+ if (dbgc_config[DBGC_ALL].fd > 0) {
ssize_t ret;
do {
- ret = write(state.fd, msg, strlen(msg));
+ ret = write(dbgc_config[DBGC_ALL].fd,
+ msg,
+ strlen(msg));
} while (ret == -1 && errno == EINTR);
}
break;
--
2.17.2
From 68b491741f24550d318f18c05cfeac8926a053d9 Mon Sep 17 00:00:00 2001
From: Ralph Boehme <slow at samba.org>
Date: Wed, 12 Dec 2018 22:46:49 +0100
Subject: [PATCH 16/17] debug: enable per debug-class logfiles
This finally enables per debug-class logfiles by hooking into
reopen_logs_internal() calls to reopen_one_log() per configured
debug-class.
Signed-off-by: Ralph Boehme <slow at samba.org>
---
lib/util/debug.c | 15 +++++++++++++--
1 file changed, 13 insertions(+), 2 deletions(-)
diff --git a/lib/util/debug.c b/lib/util/debug.c
index 278562596fa..30e5a28a233 100644
--- a/lib/util/debug.c
+++ b/lib/util/debug.c
@@ -1078,6 +1078,12 @@ static bool reopen_one_log(int *fd, const char *logfile)
int old_fd = *fd;
int new_fd;
+ if (logfile == NULL) {
+ debug_close_fd(old_fd);
+ *fd = -1;
+ return true;
+ }
+
new_fd = open(logfile, O_WRONLY|O_APPEND|O_CREAT, 0644);
if (new_fd == -1) {
log_overflow = true;
@@ -1143,8 +1149,13 @@ bool reopen_logs_internal(void)
state.reopening_logs = true;
- ok = reopen_one_log(&dbgc_config[DBGC_ALL].fd,
- dbgc_config[DBGC_ALL].logfile);
+ for (i = DBGC_ALL; i < debug_num_classes; i++) {
+ ok = reopen_one_log(&dbgc_config[i].fd,
+ dbgc_config[i].logfile);
+ if (!ok) {
+ break;
+ }
+ }
/* Fix from klausr at ITAP.Physik.Uni-Stuttgart.De
* to fix problem where smbd's that generate less
--
2.17.2
From 144e2455a54ca84eb356a5f4735b1bb3f69c5d1f Mon Sep 17 00:00:00 2001
From: Ralph Boehme <slow at samba.org>
Date: Wed, 12 Dec 2018 21:04:42 +0100
Subject: [PATCH 17/17] docs-xml: document "log level" changes
"log level" now takes an optional per debug-class logfile:
log level = 1 full_audit:1@/var/log/audit.log winbind:2
Signed-off-by: Ralph Boehme <slow at samba.org>
---
docs-xml/smbdotconf/logging/loglevel.xml | 11 +++++++++--
1 file changed, 9 insertions(+), 2 deletions(-)
diff --git a/docs-xml/smbdotconf/logging/loglevel.xml b/docs-xml/smbdotconf/logging/loglevel.xml
index 42912720655..1bc4146dac9 100644
--- a/docs-xml/smbdotconf/logging/loglevel.xml
+++ b/docs-xml/smbdotconf/logging/loglevel.xml
@@ -12,8 +12,9 @@
<para>This parameter has been extended since the 2.2.x
series, now it allows one to specify the debug level for multiple
- debug classes. This is to give greater flexibility in the configuration
- of the system. The following debug classes are currently implemented:
+ debug classes and distinct logfiles for debug classes. This is to give
+ greater flexibility in the configuration of the system. The following
+ debug classes are currently implemented:
</para>
<itemizedlist>
@@ -54,6 +55,11 @@
<listitem><para><parameter moreinfo="none">dsdb_transaction_json_audit</parameter></para></listitem>
</itemizedlist>
+ <para>To configure the logging for specific classes to go into a different
+ file then <smbconfoption name="log file"/>, you can append
+ <emphasis>@PATH</emphasis> to the class, eg <parameter>log level = 1
+ full_audit:1@/var/log/audit.log</parameter>.</para>
+
<para>Authentication and authorization audit information is logged
under the auth_audit, and if Samba was not compiled with
--without-json, a JSON representation is logged under
@@ -91,4 +97,5 @@
</description>
<value type="default">0</value>
<value type="example">3 passdb:5 auth:10 winbind:2</value>
+<value type="example">1 full_audit:1@/var/log/audit.log winbind:2</value>
</samba:parameter>
--
2.17.2
More information about the samba-technical
mailing list