From e8780be8a427e56e1cc6dd5ef5a4e628ffef990d Mon Sep 17 00:00:00 2001 From: Andreas Schneider Date: Thu, 6 May 2021 11:21:53 +0200 Subject: [PATCH] lib:cmdline: We need to always set a log file We need to always set a log file name based on the process name. This defines e.g. the log file for smbd. Signed-off-by: Andreas Schneider Reviewed-by: Volker Lendecke --- lib/cmdline/cmdline.c | 73 ++++++++++++++++++++++++++++--------------- 1 file changed, 47 insertions(+), 26 deletions(-) diff --git a/lib/cmdline/cmdline.c b/lib/cmdline/cmdline.c index 2df4d438531..b5510c86ebb 100644 --- a/lib/cmdline/cmdline.c +++ b/lib/cmdline/cmdline.c @@ -282,6 +282,35 @@ poptContext samba_popt_get_context(const char * name, static bool log_to_file; +static bool set_logfile(TALLOC_CTX *mem_ctx, + struct loadparm_context *lp_ctx, + const char *log_basename, + const char *process_name) +{ + bool ok; + char *new_logfile = talloc_asprintf(mem_ctx, + "%s/log.%s", + log_basename, + process_name); + if (new_logfile == NULL) { + return false; + } + + ok = lpcfg_set_cmdline(lp_ctx, + "log file", + new_logfile); + if (!ok) { + fprintf(stderr, + "Failed to set log to %s\n", + new_logfile); + TALLOC_FREE(new_logfile); + return false; + } + TALLOC_FREE(new_logfile); + + return true; +} + static void popt_samba_callback(poptContext popt_ctx, enum poptCallbackReason reason, const struct poptOption *opt, @@ -292,12 +321,27 @@ static void popt_samba_callback(poptContext popt_ctx, const char *pname = NULL; bool ok; + /* Find out basename of current program */ + pname = strrchr_m(poptGetInvocationName(popt_ctx), '/'); + if (pname == NULL) { + pname = poptGetInvocationName(popt_ctx); + } else { + pname++; + } + if (reason == POPT_CALLBACK_REASON_PRE) { if (lp_ctx == NULL) { fprintf(stderr, "Command line parsing not initialized!\n"); exit(1); } + ok = set_logfile(mem_ctx, lp_ctx, get_dyn_LOGFILEBASE(), pname); + if (!ok) { + fprintf(stderr, + "Failed to set log file for %s\n", + pname); + exit(1); + } return; } @@ -329,14 +373,6 @@ static void popt_samba_callback(poptContext popt_ctx, return; } - /* Find out basename of current program */ - pname = strrchr_m(poptGetInvocationName(popt_ctx), '/'); - if (pname == NULL) { - pname = poptGetInvocationName(popt_ctx); - } else { - pname++; - } - switch(opt->val) { case OPT_LEAK_REPORT: talloc_enable_leak_report(); @@ -374,31 +410,16 @@ static void popt_samba_callback(poptContext popt_ctx, break; case 'l': if (arg != NULL) { - char *new_logfile = talloc_asprintf(mem_ctx, - "%s/log.%s", - arg, - pname); - if (new_logfile == NULL) { - fprintf(stderr, - "Failed to allocate memory\n"); - exit(1); - } - - ok = lpcfg_set_cmdline(lp_ctx, - "log file", - new_logfile); + ok = set_logfile(mem_ctx, lp_ctx, arg, pname); if (!ok) { fprintf(stderr, - "Failed to set log file: %s\n", - new_logfile); - TALLOC_FREE(new_logfile); + "Failed to set log file for %s\n", + arg); exit(1); } log_to_file = true; set_dyn_LOGFILEBASE(arg); - - TALLOC_FREE(new_logfile); } break; }