From 5a040c0b4b4fdf25d76d744428ad6490a4e11b75 Mon Sep 17 00:00:00 2001 From: Daniele Pezzini Date: Fri, 16 Mar 2018 23:28:56 +0100 Subject: [PATCH 1/2] Revert "Problem: nutscan_init() called before debug is enabled" This reverts commit f613d45f78c96c290a1d63d4a2c3f28385d90d42. When parsing the command line (or showing help), many options rely on a nutscan_avail_* var already initialized by nutscan_init() -- moving that call after the parsing of opts renders those options unavailable. --- tools/nut-scanner/nut-scanner.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/tools/nut-scanner/nut-scanner.c b/tools/nut-scanner/nut-scanner.c index 9fea9276c4..0e65fdf957 100644 --- a/tools/nut-scanner/nut-scanner.c +++ b/tools/nut-scanner/nut-scanner.c @@ -246,6 +246,8 @@ int main(int argc, char *argv[]) xml_sec.usec_timeout = -1; /* Override with the "timeout" common setting later */ xml_sec.peername = NULL; + nutscan_init(); + display_func = nutscan_display_ups_conf; /* Note: the getopts print an error message about unknown arguments @@ -466,12 +468,6 @@ int main(int argc, char *argv[]) /* BEWARE: allow_all does not include allow_eaton_serial! */ } - /* TODO: nutscan_init() should consider (via args? shared global vars?) - * which scan types we desire at this run, and not try to load irrelevant - * libraries. - */ - nutscan_init(); - /* TODO/discuss : Should the #else...#endif code below for lack of pthreads * during build also serve as a fallback for pthread failure at runtime? */ From 45b01b7846a9fcdbfb0388792ac60a86faf292ad Mon Sep 17 00:00:00 2001 From: Daniele Pezzini Date: Fri, 16 Mar 2018 23:53:16 +0100 Subject: [PATCH 2/2] nutscanner: parse debug level opts separately before anything else In order to have early the value of the desired debug level (needed to correctly debug nutscan_init()), split the parsing of command line options into two loops: - the first one, before the call to nutscan_init(), will only care about debug level, - the second one, after the call to nutscan_init(), will consider the remaining (and possibly depending on a nutscan_avail_* var already initialized by nutscan_init()) arguments (and moan in case of errors). Closes https://github.com/networkupstools/nut/issues/500 --- tools/nut-scanner/nut-scanner.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/tools/nut-scanner/nut-scanner.c b/tools/nut-scanner/nut-scanner.c index 0e65fdf957..8b41a3dbf9 100644 --- a/tools/nut-scanner/nut-scanner.c +++ b/tools/nut-scanner/nut-scanner.c @@ -246,10 +246,22 @@ int main(int argc, char *argv[]) xml_sec.usec_timeout = -1; /* Override with the "timeout" common setting later */ xml_sec.peername = NULL; + /* Parse command line options -- First loop: only get debug level */ + /* Suppress error messages, for now -- leave them to the second loop. */ + opterr = 0; + while((opt_ret = getopt_long(argc, argv, optstring, longopts, NULL)) != -1) + if (opt_ret == 'D') + nut_debug_level++; + nutscan_init(); display_func = nutscan_display_ups_conf; + /* Parse command line options -- Second loop: everything else */ + /* Restore error messages... */ + opterr = 1; + /* ...and index of the item to be processed by getopt(). */ + optind = 1; /* Note: the getopts print an error message about unknown arguments * or arguments which need a second token and that is missing now */ while((opt_ret = getopt_long(argc, argv, optstring, longopts, NULL))!=-1) { @@ -280,7 +292,7 @@ int main(int argc, char *argv[]) cidr = strdup(optarg); break; case 'D': - nut_debug_level++; + /* nothing to do, here */ break; case 'c': if(!nutscan_avail_snmp) {