From 759bb234d0eb4d36947213fca2bc14e386c1cc8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Valverde?= Date: Sat, 19 Jun 2021 19:44:58 +0100 Subject: [PATCH] wslog: Check if we are initialized and add missing inits Instead of receiving the program name from GLib, pass it explicitly to ws_log_init() instead and use that to initialize the GLib program name. ws_log_parse_args() will now exit the program when it encounters an argument error if exit_failure >= 0. --- capinfos.c | 8 +++++ captype.c | 7 +++++ dftest.c | 7 +++++ dumpcap.c | 10 ++---- editcap.c | 7 +++++ extcap/androiddump.c | 10 ++---- extcap/ciscodump.c | 3 ++ extcap/dpauxmon.c | 3 ++ extcap/etwdump.c | 3 ++ extcap/randpktdump.c | 10 ++---- extcap/sdjournal.c | 3 ++ extcap/sshdump.c | 3 ++ extcap/udpdump.c | 3 ++ fuzz/fuzzshark.c | 4 +++ mergecap.c | 7 +++++ randpkt.c | 11 +++++-- rawshark.c | 10 ++---- reordercap.c | 7 +++++ sharkd.c | 10 ++---- text2pcap.c | 4 +++ tshark.c | 10 ++---- ui/qt/main.cpp | 10 ++---- wsutil/wslog.c | 72 ++++++++++++++++++++++++++++++++++---------- wsutil/wslog.h | 9 ++++-- 24 files changed, 161 insertions(+), 70 deletions(-) diff --git a/capinfos.c b/capinfos.c index c81decd36f4..015e0c1e64d 100644 --- a/capinfos.c +++ b/capinfos.c @@ -84,6 +84,7 @@ #include #include #include +#include #include @@ -1580,8 +1581,15 @@ main(int argc, char *argv[]) setlocale(LC_ALL, ""); #endif + + /* Initialize log handler early so we can have proper logging during startup. */ + ws_log_init("capinfos", NULL); + cmdarg_err_init(capinfos_cmdarg_err, capinfos_cmdarg_err_cont); + /* Early logging command-line initialization. */ + ws_log_parse_args(&argc, argv, vcmdarg_err, INVALID_OPTION); + /* Get the decimal point. */ decimal_point = g_strdup(localeconv()->decimal_point); diff --git a/captype.c b/captype.c index 37f81ff4436..96edb844436 100644 --- a/captype.c +++ b/captype.c @@ -50,6 +50,7 @@ #include #include +#include #include "ui/failure_message.h" @@ -119,8 +120,14 @@ main(int argc, char *argv[]) setlocale(LC_ALL, ""); #endif + /* Initialize log handler early so we can have proper logging during startup. */ + ws_log_init("captype", NULL); + cmdarg_err_init(captype_cmdarg_err, captype_cmdarg_err_cont); + /* Early logging command-line initialization. */ + ws_log_parse_args(&argc, argv, vcmdarg_err, 1); + /* Initialize the version information. */ ws_init_version_info("Captype (Wireshark)", NULL, NULL, NULL); diff --git a/dftest.c b/dftest.c index e248b561839..4d497f52e60 100644 --- a/dftest.c +++ b/dftest.c @@ -29,6 +29,7 @@ #include #include #include +#include #include @@ -59,8 +60,14 @@ main(int argc, char **argv) dfilter_t *df; gchar *err_msg; + /* Initialize log handler early so we can have proper logging during startup. */ + ws_log_init("dftest", NULL); + cmdarg_err_init(dftest_cmdarg_err, dftest_cmdarg_err_cont); + /* Early logging command-line initialization. */ + ws_log_parse_args(&argc, argv, vcmdarg_err, 1); + /* * Get credential information for later use. */ diff --git a/dumpcap.c b/dumpcap.c index 539ed003de6..c5512aa5b59 100644 --- a/dumpcap.c +++ b/dumpcap.c @@ -4851,17 +4851,13 @@ main(int argc, char *argv[]) #endif GString *str; - g_set_prgname("dumpcap"); - /* Initialize log handler early so we can have proper logging during startup. */ - ws_log_init(dumpcap_log_writer); + ws_log_init("dumpcap", dumpcap_log_writer); cmdarg_err_init(dumpcap_cmdarg_err, dumpcap_cmdarg_err_cont); - /* Command line options are parsed too late to configure logging, do it - manually. */ - if (ws_log_parse_args(&argc, argv, cmdarg_err) != 0) - exit(1); + /* Early logging command-line initialization. */ + ws_log_parse_args(&argc, argv, vcmdarg_err, 1); #ifdef _WIN32 create_app_running_mutex(); diff --git a/editcap.c b/editcap.c index 90a39e0b1eb..07893234590 100644 --- a/editcap.c +++ b/editcap.c @@ -84,6 +84,7 @@ #include #include #include +#include #include #include "ui/failure_message.h" @@ -1153,8 +1154,14 @@ main(int argc, char *argv[]) gboolean valid_seed = FALSE; unsigned int seed = 0; + /* Initialize log handler early so we can have proper logging during startup. */ + ws_log_init("editcap", NULL); + cmdarg_err_init(editcap_cmdarg_err, editcap_cmdarg_err_cont); + /* Early logging command-line initialization. */ + ws_log_parse_args(&argc, argv, vcmdarg_err, INVALID_OPTION); + #ifdef _WIN32 create_app_running_mutex(); #endif /* _WIN32 */ diff --git a/extcap/androiddump.c b/extcap/androiddump.c index afd13c1c4a2..7cf52e452fd 100644 --- a/extcap/androiddump.c +++ b/extcap/androiddump.c @@ -2528,17 +2528,13 @@ int main(int argc, char *argv[]) { char *help_url; char *help_header = NULL; - g_set_prgname("androiddump"); - /* Initialize log handler early so we can have proper logging during startup. */ - ws_log_init(NULL); + ws_log_init("androiddump", NULL); cmdarg_err_init(androiddump_cmdarg_err, androiddump_cmdarg_err); - /* Command line options are parsed too late to configure logging, do it - manually. */ - if (ws_log_parse_args(&argc, argv, cmdarg_err) != 0) - return EXIT_FAILURE; + /* Early logging command-line initialization. */ + ws_log_parse_args(&argc, argv, vcmdarg_err, EXIT_FAILURE); /* * Get credential information for later use. diff --git a/extcap/ciscodump.c b/extcap/ciscodump.c index 80f8cc5418c..217fd838d8c 100644 --- a/extcap/ciscodump.c +++ b/extcap/ciscodump.c @@ -534,6 +534,9 @@ int main(int argc, char *argv[]) char* help_url; char* help_header = NULL; + /* Initialize log handler early so we can have proper logging during startup. */ + ws_log_init("ciscodump", NULL); + /* * Get credential information for later use. */ diff --git a/extcap/dpauxmon.c b/extcap/dpauxmon.c index f3bed612d9c..98c6b1319d8 100644 --- a/extcap/dpauxmon.c +++ b/extcap/dpauxmon.c @@ -489,6 +489,9 @@ int main(int argc, char *argv[]) extcap_parameters* extcap_conf = g_new0(extcap_parameters, 1); char* help_header = NULL; + /* Initialize log handler early so we can have proper logging during startup. */ + ws_log_init("dpauxmon", NULL); + /* * Get credential information for later use. */ diff --git a/extcap/etwdump.c b/extcap/etwdump.c index bc8fb3b04e5..9d6d8fa7ae5 100644 --- a/extcap/etwdump.c +++ b/extcap/etwdump.c @@ -122,6 +122,9 @@ int main(int argc, char* argv[]) char* help_url; char* help_header = NULL; + /* Initialize log handler early so we can have proper logging during startup. */ + ws_log_init("etwdump", NULL); + /* * Get credential information for later use. */ diff --git a/extcap/randpktdump.c b/extcap/randpktdump.c index 1b7e3dec1bc..f1f18cd19e8 100644 --- a/extcap/randpktdump.c +++ b/extcap/randpktdump.c @@ -151,17 +151,13 @@ int main(int argc, char *argv[]) char* help_url; char* help_header = NULL; - g_set_prgname("randpktdump"); - /* Initialize log handler early so we can have proper logging during startup. */ - ws_log_init(NULL); + ws_log_init("randpktdump", NULL); cmdarg_err_init(randpktdump_cmdarg_err, randpktdump_cmdarg_err); - /* Command line options are parsed too late to configure logging, do it - manually. */ - if (ws_log_parse_args(&argc, argv, cmdarg_err) != 0) - return EXIT_FAILURE; + /* Early logging command-line initialization. */ + ws_log_parse_args(&argc, argv, vcmdarg_err, EXIT_FAILURE); /* * Get credential information for later use. diff --git a/extcap/sdjournal.c b/extcap/sdjournal.c index 1e1629cb126..71b163b4703 100644 --- a/extcap/sdjournal.c +++ b/extcap/sdjournal.c @@ -341,6 +341,9 @@ int main(int argc, char **argv) char* help_url; char* help_header = NULL; + /* Initialize log handler early so we can have proper logging during startup. */ + ws_log_init("sdjournal", NULL); + /* * Get credential information for later use. */ diff --git a/extcap/sshdump.c b/extcap/sshdump.c index ae202fb22fd..80bab13f79c 100644 --- a/extcap/sshdump.c +++ b/extcap/sshdump.c @@ -362,6 +362,9 @@ int main(int argc, char *argv[]) gboolean noprom = FALSE; gchar* interface_description = g_strdup("SSH remote capture"); + /* Initialize log handler early so we can have proper logging during startup. */ + ws_log_init("sshdump", NULL); + sshdump_extcap_interface = g_path_get_basename(argv[0]); /* diff --git a/extcap/udpdump.c b/extcap/udpdump.c index ba606dda5cb..44361e17c52 100644 --- a/extcap/udpdump.c +++ b/extcap/udpdump.c @@ -368,6 +368,9 @@ int main(int argc, char *argv[]) char* payload = NULL; char* port_msg = NULL; + /* Initialize log handler early so we can have proper logging during startup. */ + ws_log_init("udpdump", NULL); + /* * Get credential information for later use. */ diff --git a/fuzz/fuzzshark.c b/fuzz/fuzzshark.c index 17dcb8315ef..c2bed25c2df 100644 --- a/fuzz/fuzzshark.c +++ b/fuzz/fuzzshark.c @@ -25,6 +25,7 @@ #include #include #include +#include #include #include @@ -234,6 +235,9 @@ fuzz_init(int argc _U_, char **argv) g_setenv("WIRESHARK_DEBUG_WMEM_OVERRIDE", "simple", 0); g_setenv("G_SLICE", "always-malloc", 0); + /* Initialize log handler early so we can have proper logging during startup. */ + ws_log_init("fuzzshark", NULL); + cmdarg_err_init(fuzzshark_cmdarg_err, fuzzshark_cmdarg_err_cont); /* diff --git a/mergecap.c b/mergecap.c index cee3834d694..5c670a2f00c 100644 --- a/mergecap.c +++ b/mergecap.c @@ -42,6 +42,7 @@ #include #include #include +#include #include #include @@ -227,8 +228,14 @@ main(int argc, char *argv[]) idb_merge_mode mode = IDB_MERGE_MODE_MAX; merge_progress_callback_t cb; + /* Initialize log handler early so we can have proper logging during startup. */ + ws_log_init("mergecap", NULL); + cmdarg_err_init(mergecap_cmdarg_err, mergecap_cmdarg_err_cont); + /* Early logging command-line initialization. */ + ws_log_parse_args(&argc, argv, vcmdarg_err, 1); + #ifdef _WIN32 create_app_running_mutex(); #endif /* _WIN32 */ diff --git a/randpkt.c b/randpkt.c index 8a85717bc5c..700303ee656 100644 --- a/randpkt.c +++ b/randpkt.c @@ -29,6 +29,7 @@ #endif #include +#include /* * If we have getopt_long() in the system library, include . @@ -137,6 +138,14 @@ main(int argc, char *argv[]) {0, 0, 0, 0 } }; + /* Initialize log handler early so we can have proper logging during startup. */ + ws_log_init("randpkt", NULL); + + cmdarg_err_init(randpkt_cmdarg_err, randpkt_cmdarg_err_cont); + + /* Early logging command-line initialization. */ + ws_log_parse_args(&argc, argv, vcmdarg_err, INVALID_OPTION); + /* * Get credential information for later use. */ @@ -158,8 +167,6 @@ main(int argc, char *argv[]) wtap_init(TRUE); - cmdarg_err_init(randpkt_cmdarg_err, randpkt_cmdarg_err_cont); - #ifdef _WIN32 create_app_running_mutex(); #endif /* _WIN32 */ diff --git a/rawshark.c b/rawshark.c index 471a89375c9..1001930ddf3 100644 --- a/rawshark.c +++ b/rawshark.c @@ -449,17 +449,13 @@ main(int argc, char *argv[]) setlocale(LC_ALL, ""); #endif - g_set_prgname("rawshark"); - /* Initialize log handler early so we can have proper logging during startup. */ - ws_log_init(NULL); + ws_log_init("rawshark", NULL); cmdarg_err_init(rawshark_cmdarg_err, rawshark_cmdarg_err_cont); - /* Command line options are parsed too late to configure logging, do it - manually. */ - if (ws_log_parse_args(&argc, argv, cmdarg_err) != 0) - return INVALID_OPTION; + /* Early logging command-line initialization. */ + ws_log_parse_args(&argc, argv, vcmdarg_err, INVALID_OPTION); /* Initialize the version information. */ ws_init_version_info("Rawshark (Wireshark)", NULL, diff --git a/reordercap.c b/reordercap.c index b79c3d61c87..27f7c5350d4 100644 --- a/reordercap.c +++ b/reordercap.c @@ -44,6 +44,7 @@ #endif #include +#include #include "ui/failure_message.h" @@ -207,8 +208,14 @@ main(int argc, char *argv[]) char *infile; const char *outfile; + /* Initialize log handler early so we can have proper logging during startup. */ + ws_log_init("reordercap", NULL); + cmdarg_err_init(reordercap_cmdarg_err, reordercap_cmdarg_err_cont); + /* Early logging command-line initialization. */ + ws_log_parse_args(&argc, argv, vcmdarg_err, INVALID_OPTION); + /* Initialize the version information. */ ws_init_version_info("Reordercap (Wireshark)", NULL, NULL, NULL); diff --git a/sharkd.c b/sharkd.c index e325fda958e..f148ca35b1c 100644 --- a/sharkd.c +++ b/sharkd.c @@ -114,17 +114,13 @@ main(int argc, char *argv[]) cfile_close_failure_message }; - g_set_prgname("sharkd"); - /* Initialize log handler early so we can have proper logging during startup. */ - ws_log_init(NULL); + ws_log_init("sharkd", NULL); cmdarg_err_init(sharkd_cmdarg_err, sharkd_cmdarg_err_cont); - /* Command line options are parsed too late to configure logging, do it - manually. */ - if (ws_log_parse_args(&argc, argv, cmdarg_err) != 0) - return INIT_FAILED; + /* Early logging command-line initialization. */ + ws_log_parse_args(&argc, argv, vcmdarg_err, INIT_FAILED); /* * Get credential information for later use, and drop privileges diff --git a/text2pcap.c b/text2pcap.c index 331ce16203f..6c7cbc70b52 100644 --- a/text2pcap.c +++ b/text2pcap.c @@ -103,6 +103,7 @@ #include #include #include +#include #ifdef _WIN32 #include /* for _setmode */ @@ -1864,6 +1865,9 @@ main(int argc, char *argv[]) { int ret = EXIT_SUCCESS; + /* Initialize log handler early so we can have proper logging during startup. */ + ws_log_init("text2pcap", NULL); + #ifdef _WIN32 create_app_running_mutex(); #endif /* _WIN32 */ diff --git a/tshark.c b/tshark.c index 5d72405acd3..74efc617301 100644 --- a/tshark.c +++ b/tshark.c @@ -775,17 +775,13 @@ main(int argc, char *argv[]) setlocale(LC_ALL, ""); #endif - g_set_prgname("tshark"); - /* Initialize log handler early so we can have proper logging during startup. */ - ws_log_init(NULL); + ws_log_init("tshark", NULL); cmdarg_err_init(tshark_cmdarg_err, tshark_cmdarg_err_cont); - /* Command line options are parsed too late to configure logging, do it - manually. */ - if (ws_log_parse_args(&argc, argv, cmdarg_err) != 0) - return INVALID_OPTION; + /* Early logging command-line initialization. */ + ws_log_parse_args(&argc, argv, vcmdarg_err, INVALID_OPTION); ws_debug("tshark started with %d args", argc); diff --git a/ui/qt/main.cpp b/ui/qt/main.cpp index 588e078d448..24290e9b03a 100644 --- a/ui/qt/main.cpp +++ b/ui/qt/main.cpp @@ -529,10 +529,8 @@ int main(int argc, char *qt_argv[]) macos_enable_layer_backing(); #endif - g_set_prgname("wireshark"); - /* Initialize log handler early so we can have proper logging during startup. */ - ws_log_init(console_log_writer); + ws_log_init("wireshark", console_log_writer); qInstallMessageHandler(qt_log_message_handler); @@ -582,10 +580,8 @@ int main(int argc, char *qt_argv[]) create_app_running_mutex(); #endif /* _WIN32 */ - /* Command line options are parsed too late to configure logging, do it - manually. */ - if (ws_log_parse_args(&argc, argv, cmdarg_err) != 0) - exit_application(INVALID_OPTION); + /* Early logging command-line initialization. */ + ws_log_parse_args(&argc, argv, vcmdarg_err, INVALID_OPTION); /* * Get credential information for later use, and drop privileges diff --git a/wsutil/wslog.c b/wsutil/wslog.c index f4d8505fd3f..45227abbae6 100644 --- a/wsutil/wslog.c +++ b/wsutil/wslog.c @@ -46,7 +46,7 @@ #define DEFAULT_LOG_LEVEL LOG_LEVEL_MESSAGE -#define DEFAULT_APPNAME "PID" +#define DEFAULT_PROGNAME "PID" #define DOMAIN_UNDEFED(domain) ((domain) == NULL || *(domain) == '\0') #define DOMAIN_DEFINED(domain) (!DOMAIN_UNDEFED(domain)) @@ -66,7 +66,7 @@ static enum ws_log_level current_log_level = LOG_LEVEL_NONE; static gboolean color_enabled = FALSE; -static const char *registered_appname = NULL; +static const char *registered_progname = DEFAULT_PROGNAME; /* List of domains to filter. */ static log_filter_t *domain_filter = NULL; @@ -87,6 +87,10 @@ static FILE *custom_log = NULL; static enum ws_log_level fatal_log_level = LOG_LEVEL_ERROR; +#ifndef WS_DISABLE_DEBUG +static gboolean init_complete = FALSE; +#endif + static void ws_log_cleanup(void); @@ -277,7 +281,26 @@ static const char *opt_debug = "--log-debug"; static const char *opt_noisy = "--log-noisy"; -int ws_log_parse_args(int *argc_ptr, char *argv[], void (*print_err)(const char *, ...)) +static void print_err(void (*log_args_print_err)(const char *, va_list ap), + int log_args_exit_failure, + const char *fmt, ...) +{ + va_list ap; + + if (log_args_print_err == NULL) + return; + + va_start(ap, fmt); + log_args_print_err(fmt, ap); + va_end(ap); + if (log_args_exit_failure >= 0) + exit(log_args_exit_failure); +} + + +int ws_log_parse_args(int *argc_ptr, char *argv[], + void (*vcmdarg_err)(const char *, va_list ap), + int exit_failure) { char **ptr = argv; int count = *argc_ptr; @@ -331,7 +354,8 @@ int ws_log_parse_args(int *argc_ptr, char *argv[], void (*print_err)(const char if (value == NULL || !*value || *value == '-') { /* If the option value after the blank starts with '-' assume * it is another option. */ - print_err("Option \"%s\" requires a value.\n", *ptr); + print_err(vcmdarg_err, exit_failure, + "Option \"%s\" requires a value.\n", *ptr); option = NULL; prune_extra = 0; ret += 1; @@ -351,7 +375,8 @@ int ws_log_parse_args(int *argc_ptr, char *argv[], void (*print_err)(const char if (option == opt_level) { if (ws_log_set_level_str(value) == LOG_LEVEL_NONE) { - print_err("Invalid log level \"%s\"\n", value); + print_err(vcmdarg_err, exit_failure, + "Invalid log level \"%s\".\n", value); ret += 1; } } @@ -361,7 +386,9 @@ int ws_log_parse_args(int *argc_ptr, char *argv[], void (*print_err)(const char else if (option == opt_file) { FILE *fp = ws_fopen(value, "w"); if (fp == NULL) { - print_err("Error opening file '%s' for writing: %s\n", value, g_strerror(errno)); + print_err(vcmdarg_err, exit_failure, + "Error opening file '%s' for writing: %s.\n", + value, g_strerror(errno)); ret += 1; } else { @@ -370,8 +397,9 @@ int ws_log_parse_args(int *argc_ptr, char *argv[], void (*print_err)(const char } else if (option == opt_fatal) { if (ws_log_set_fatal_str(value) == LOG_LEVEL_NONE) { - print_err("Fatal log level must be \"critical\" or \"warning\", " - "not \"%s\".\n", value); + print_err(vcmdarg_err, exit_failure, + "Fatal log level must be \"critical\" or " + "\"warning\", not \"%s\".\n", value); ret += 1; } } @@ -498,13 +526,14 @@ enum ws_log_level ws_log_set_fatal_str(const char *str_level) } -void ws_log_init(ws_log_writer_cb *writer) +void ws_log_init(const char *progname, ws_log_writer_cb *writer) { const char *env; - registered_appname = g_get_prgname(); - if (registered_appname == NULL) - registered_appname = DEFAULT_APPNAME; + if (progname != NULL) { + registered_progname = progname; + g_set_prgname(progname); + } if (writer) registered_log_writer = writer; @@ -543,15 +572,20 @@ void ws_log_init(ws_log_writer_cb *writer) ws_log_set_noisy_filter(env); atexit(ws_log_cleanup); + +#ifndef WS_DISABLE_DEBUG + init_complete = TRUE; +#endif } -void ws_log_init_with_data(ws_log_writer_cb *writer, void *user_data, - ws_log_writer_free_data_cb *free_user_data) +void ws_log_init_with_data(const char *progname, ws_log_writer_cb *writer, + void *user_data, + ws_log_writer_free_data_cb *free_user_data) { registered_log_writer_data = user_data; registered_log_writer_data_free = free_user_data; - ws_log_init(writer); + ws_log_init(progname, writer); } @@ -574,8 +608,14 @@ static void log_write_do_work(FILE *fp, gboolean use_color, const char *timestam const char *level_str = ws_log_level_to_string(level); gboolean doextra = (level != DEFAULT_LOG_LEVEL); +#ifndef WS_DISABLE_DEBUG + if (!init_complete) { + fprintf(fp, " ** (noinit)"); + } +#endif + if (doextra) - fprintf(fp, " ** (%s:%ld) ", registered_appname, (long)getpid()); + fprintf(fp, " ** (%s:%ld) ", registered_progname, (long)getpid()); else fputs(" ** ", fp); diff --git a/wsutil/wslog.h b/wsutil/wslog.h index 42a5869c4cb..ee539640287 100644 --- a/wsutil/wslog.h +++ b/wsutil/wslog.h @@ -132,7 +132,9 @@ enum ws_log_level ws_log_set_fatal_str(const char *str_level); * Returns zero for no error, non-zero for a bad option value. */ WS_DLL_PUBLIC -int ws_log_parse_args(int *argc_ptr, char *argv[], void (*print_err)(const char *, ...)); +int ws_log_parse_args(int *argc_ptr, char *argv[], + void (*vcmdarg_err)(const char *, va_list ap), + int exit_failure); /** Initializes the logging code. @@ -142,7 +144,7 @@ int ws_log_parse_args(int *argc_ptr, char *argv[], void (*print_err)(const char * is NULL the default log writer is used. */ WS_DLL_PUBLIC -void ws_log_init(ws_log_writer_cb *writer); +void ws_log_init(const char *progname, ws_log_writer_cb *writer); /** Initializes the logging code. @@ -152,7 +154,8 @@ void ws_log_init(ws_log_writer_cb *writer); * is passed it will be called with user_data when the program terminates. */ WS_DLL_PUBLIC -void ws_log_init_with_data(ws_log_writer_cb *writer, void *user_data, +void ws_log_init_with_data(const char *progname, ws_log_writer_cb *writer, + void *user_data, ws_log_writer_free_data_cb *free_user_data);