Skip to content

Commit

Permalink
wslog: Check environment initialization for errors
Browse files Browse the repository at this point in the history
Initialiaze the cmdarg error stream earlier.

Dumpcap also needs to know earlier if it is running in capture
child mode.
  • Loading branch information
randstr committed Jun 25, 2021
1 parent c2c256f commit c6a9206
Show file tree
Hide file tree
Showing 26 changed files with 215 additions and 135 deletions.
5 changes: 2 additions & 3 deletions capinfos.c
Original file line number Diff line number Diff line change
Expand Up @@ -1581,11 +1581,10 @@ main(int argc, char *argv[])
setlocale(LC_ALL, "");
#endif

cmdarg_err_init(capinfos_cmdarg_err, capinfos_cmdarg_err_cont);

/* 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);
ws_log_init("capinfos", vcmdarg_err);

/* Early logging command-line initialization. */
ws_log_parse_args(&argc, argv, vcmdarg_err, INVALID_OPTION);
Expand Down
6 changes: 3 additions & 3 deletions captype.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,11 @@ 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);

/* Initialize log handler early so we can have proper logging during startup. */
ws_log_init("captype", vcmdarg_err);

/* Early logging command-line initialization. */
ws_log_parse_args(&argc, argv, vcmdarg_err, 1);

Expand Down
3 changes: 2 additions & 1 deletion debian/libwsutil0.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,8 @@ libwsutil.so.0 libwsutil0 #MINVER#
ws_log_full@Base 3.5.0
ws_log_get_level@Base 3.5.0
ws_log_init@Base 3.5.0
ws_log_init_with_data@Base 3.5.0
ws_log_init_with_writer@Base 3.5.0
ws_log_init_with_writer_and_data@Base 3.5.0
ws_log_level_to_string@Base 3.5.0
ws_log_msg_is_active@Base 3.5.0
ws_log_parse_args@Base 3.5.0
Expand Down
6 changes: 3 additions & 3 deletions dftest.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ 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);

/* Initialize log handler early so we can have proper logging during startup. */
ws_log_init("dftest", vcmdarg_err);

/* Early logging command-line initialization. */
ws_log_parse_args(&argc, argv, vcmdarg_err, 1);

Expand Down
78 changes: 39 additions & 39 deletions dumpcap.c
Original file line number Diff line number Diff line change
Expand Up @@ -4854,11 +4854,48 @@ main(int argc, char *argv[])
#endif
GString *str;

/* Initialize log handler early so we can have proper logging during startup. */
ws_log_init("dumpcap", dumpcap_log_writer);
/*
* Determine if dumpcap is being requested to run in a special
* capture_child mode by going thru the command line args to see if
* a -Z is present. (-Z is a hidden option).
*
* The primary result of running in capture_child mode is that
* all messages sent out on stderr are in a special type/len/string
* format to allow message processing by type. These messages include
* error messages if dumpcap fails to start the operation it was
* requested to do, as well as various "status" messages which are sent
* when an actual capture is in progress, and a "success" message sent
* if dumpcap was requested to perform an operation other than a
* capture.
*
* Capture_child mode would normally be requested by a parent process
* which invokes dumpcap and obtains dumpcap stderr output via a pipe
* to which dumpcap stderr has been redirected. It might also have
* another pipe to obtain dumpcap stdout output; for operations other
* than a capture, that information is formatted specially for easier
* parsing by the parent process.
*
* Capture_child mode needs to be determined immediately upon
* startup so that any messages generated by dumpcap in this mode
* (eg: during initialization) will be formatted properly.
*/

for (i=1; i<argc; i++) {
if (strcmp("-Z", argv[i]) == 0) {
capture_child = TRUE;
machine_readable = TRUE; /* request machine-readable output */
#ifdef _WIN32
/* set output pipe to binary mode, to avoid ugly text conversions */
_setmode(2, O_BINARY);
#endif
}
}

cmdarg_err_init(dumpcap_cmdarg_err, dumpcap_cmdarg_err_cont);

/* Initialize log handler early so we can have proper logging during startup. */
ws_log_init_with_writer("dumpcap", dumpcap_log_writer, vcmdarg_err);

/* Early logging command-line initialization. */
ws_log_parse_args(&argc, argv, vcmdarg_err, 1);

Expand Down Expand Up @@ -4924,43 +4961,6 @@ main(int argc, char *argv[])
}
#endif

/*
* Determine if dumpcap is being requested to run in a special
* capture_child mode by going thru the command line args to see if
* a -Z is present. (-Z is a hidden option).
*
* The primary result of running in capture_child mode is that
* all messages sent out on stderr are in a special type/len/string
* format to allow message processing by type. These messages include
* error messages if dumpcap fails to start the operation it was
* requested to do, as well as various "status" messages which are sent
* when an actual capture is in progress, and a "success" message sent
* if dumpcap was requested to perform an operation other than a
* capture.
*
* Capture_child mode would normally be requested by a parent process
* which invokes dumpcap and obtains dumpcap stderr output via a pipe
* to which dumpcap stderr has been redirected. It might also have
* another pipe to obtain dumpcap stdout output; for operations other
* than a capture, that information is formatted specially for easier
* parsing by the parent process.
*
* Capture_child mode needs to be determined immediately upon
* startup so that any messages generated by dumpcap in this mode
* (eg: during initialization) will be formatted properly.
*/

for (i=1; i<argc; i++) {
if (strcmp("-Z", argv[i]) == 0) {
capture_child = TRUE;
machine_readable = TRUE; /* request machine-readable output */
#ifdef _WIN32
/* set output pipe to binary mode, to avoid ugly text conversions */
_setmode(2, O_BINARY);
#endif
}
}

/* Initialize the pcaps list and IDBs */
global_ld.pcaps = g_array_new(FALSE, FALSE, sizeof(capture_src *));
global_ld.pcapng_passthrough = FALSE;
Expand Down
6 changes: 3 additions & 3 deletions editcap.c
Original file line number Diff line number Diff line change
Expand Up @@ -1154,11 +1154,11 @@ 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);

/* Initialize log handler early so we can have proper logging during startup. */
ws_log_init("editcap", vcmdarg_err);

/* Early logging command-line initialization. */
ws_log_parse_args(&argc, argv, vcmdarg_err, INVALID_OPTION);

Expand Down
6 changes: 3 additions & 3 deletions extcap/androiddump.c
Original file line number Diff line number Diff line change
Expand Up @@ -2528,13 +2528,13 @@ int main(int argc, char *argv[]) {
char *help_url;
char *help_header = NULL;

cmdarg_err_init(androiddump_cmdarg_err, androiddump_cmdarg_err);

/* Initialize log handler early so we can have proper logging during startup. */
ws_log_init("androiddump", NULL);

cmdarg_err_init(androiddump_cmdarg_err, androiddump_cmdarg_err);

/* Early logging command-line initialization. */
ws_log_parse_args(&argc, argv, vcmdarg_err, EXIT_FAILURE);
ws_log_parse_args(&argc, argv, NULL, LOG_ARGS_NOEXIT);

/*
* Get credential information for later use.
Expand Down
5 changes: 4 additions & 1 deletion extcap/ciscodump.c
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,10 @@ int main(int argc, char *argv[])

/* Initialize log handler early so we can have proper logging during startup. */
ws_log_init("ciscodump", NULL);


/* Early logging command-line initialization. */
ws_log_parse_args(&argc, argv, NULL, LOG_ARGS_NOEXIT);

/*
* Get credential information for later use.
*/
Expand Down
3 changes: 3 additions & 0 deletions extcap/dpauxmon.c
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,9 @@ int main(int argc, char *argv[])
/* Initialize log handler early so we can have proper logging during startup. */
ws_log_init("dpauxmon", NULL);

/* Early logging command-line initialization. */
ws_log_parse_args(&argc, argv, NULL, LOG_ARGS_NOEXIT);

/*
* Get credential information for later use.
*/
Expand Down
3 changes: 3 additions & 0 deletions extcap/etwdump.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ int main(int argc, char* argv[])
/* Initialize log handler early so we can have proper logging during startup. */
ws_log_init("etwdump", NULL);

/* Early logging command-line initialization. */
ws_log_parse_args(&argc, argv, NULL, LOG_ARGS_NOEXIT);

/*
* Get credential information for later use.
*/
Expand Down
6 changes: 3 additions & 3 deletions extcap/randpktdump.c
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,13 @@ int main(int argc, char *argv[])
char* help_url;
char* help_header = NULL;

cmdarg_err_init(randpktdump_cmdarg_err, randpktdump_cmdarg_err);

/* Initialize log handler early so we can have proper logging during startup. */
ws_log_init("randpktdump", NULL);

cmdarg_err_init(randpktdump_cmdarg_err, randpktdump_cmdarg_err);

/* Early logging command-line initialization. */
ws_log_parse_args(&argc, argv, vcmdarg_err, EXIT_FAILURE);
ws_log_parse_args(&argc, argv, NULL, LOG_ARGS_NOEXIT);

/*
* Get credential information for later use.
Expand Down
3 changes: 3 additions & 0 deletions extcap/sdjournal.c
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,9 @@ int main(int argc, char **argv)
/* Initialize log handler early so we can have proper logging during startup. */
ws_log_init("sdjournal", NULL);

/* Early logging command-line initialization. */
ws_log_parse_args(&argc, argv, NULL, LOG_ARGS_NOEXIT);

/*
* Get credential information for later use.
*/
Expand Down
3 changes: 3 additions & 0 deletions extcap/sshdump.c
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,9 @@ int main(int argc, char *argv[])
/* Initialize log handler early so we can have proper logging during startup. */
ws_log_init("sshdump", NULL);

/* Early logging command-line initialization. */
ws_log_parse_args(&argc, argv, NULL, LOG_ARGS_NOEXIT);

sshdump_extcap_interface = g_path_get_basename(argv[0]);

/*
Expand Down
3 changes: 3 additions & 0 deletions extcap/udpdump.c
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,9 @@ int main(int argc, char *argv[])
/* Initialize log handler early so we can have proper logging during startup. */
ws_log_init("udpdump", NULL);

/* Early logging command-line initialization. */
ws_log_parse_args(&argc, argv, NULL, LOG_ARGS_NOEXIT);

/*
* Get credential information for later use.
*/
Expand Down
7 changes: 5 additions & 2 deletions fuzz/fuzzshark.c
Original file line number Diff line number Diff line change
Expand Up @@ -235,10 +235,13 @@ fuzz_init(int argc _U_, char **argv)
g_setenv("WIRESHARK_DEBUG_WMEM_OVERRIDE", "simple", 0);
g_setenv("G_SLICE", "always-malloc", 0);

cmdarg_err_init(fuzzshark_cmdarg_err, fuzzshark_cmdarg_err_cont);

/* Initialize log handler early so we can have proper logging during startup. */
ws_log_init("fuzzshark", NULL);
ws_log_init("fuzzshark", vcmdarg_err);

cmdarg_err_init(fuzzshark_cmdarg_err, fuzzshark_cmdarg_err_cont);
/* Early logging command-line initialization. */
ws_log_parse_args(&argc, argv, vcmdarg_err, LOG_ARGS_NOEXIT);

/*
* Get credential information for later use, and drop privileges
Expand Down
6 changes: 3 additions & 3 deletions mergecap.c
Original file line number Diff line number Diff line change
Expand Up @@ -228,11 +228,11 @@ 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);

/* Initialize log handler early so we can have proper logging during startup. */
ws_log_init("mergecap", vcmdarg_err);

/* Early logging command-line initialization. */
ws_log_parse_args(&argc, argv, vcmdarg_err, 1);

Expand Down
6 changes: 3 additions & 3 deletions randpkt.c
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,11 @@ 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);

/* Initialize log handler early so we can have proper logging during startup. */
ws_log_init("randpkt", vcmdarg_err);

/* Early logging command-line initialization. */
ws_log_parse_args(&argc, argv, vcmdarg_err, INVALID_OPTION);

Expand Down
6 changes: 3 additions & 3 deletions rawshark.c
Original file line number Diff line number Diff line change
Expand Up @@ -449,11 +449,11 @@ main(int argc, char *argv[])
setlocale(LC_ALL, "");
#endif

/* Initialize log handler early so we can have proper logging during startup. */
ws_log_init("rawshark", NULL);

cmdarg_err_init(rawshark_cmdarg_err, rawshark_cmdarg_err_cont);

/* Initialize log handler early so we can have proper logging during startup. */
ws_log_init("rawshark", vcmdarg_err);

/* Early logging command-line initialization. */
ws_log_parse_args(&argc, argv, vcmdarg_err, INVALID_OPTION);

Expand Down
6 changes: 3 additions & 3 deletions reordercap.c
Original file line number Diff line number Diff line change
Expand Up @@ -208,11 +208,11 @@ 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);

/* Initialize log handler early so we can have proper logging during startup. */
ws_log_init("reordercap", vcmdarg_err);

/* Early logging command-line initialization. */
ws_log_parse_args(&argc, argv, vcmdarg_err, INVALID_OPTION);

Expand Down
6 changes: 3 additions & 3 deletions sharkd.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,11 @@ main(int argc, char *argv[])
cfile_close_failure_message
};

/* Initialize log handler early so we can have proper logging during startup. */
ws_log_init("sharkd", NULL);

cmdarg_err_init(sharkd_cmdarg_err, sharkd_cmdarg_err_cont);

/* Initialize log handler early so we can have proper logging during startup. */
ws_log_init("sharkd", vcmdarg_err);

/* Early logging command-line initialization. */
ws_log_parse_args(&argc, argv, vcmdarg_err, INIT_FAILED);

Expand Down
12 changes: 11 additions & 1 deletion text2pcap.c
Original file line number Diff line number Diff line change
Expand Up @@ -1860,13 +1860,23 @@ parse_options (int argc, char *argv[])
return EXIT_SUCCESS;
}

void
text2pcap_vcmdarg_err(const char *fmt, va_list ap)
{
vfprintf(stderr, fmt, ap);
fputc('\n', stderr);
}

int
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);
ws_log_init("text2pcap", text2pcap_vcmdarg_err);

/* Early logging command-line initialization. */
ws_log_parse_args(&argc, argv, text2pcap_vcmdarg_err, 1);

#ifdef _WIN32
create_app_running_mutex();
Expand Down
12 changes: 4 additions & 8 deletions tfshark.c
Original file line number Diff line number Diff line change
Expand Up @@ -350,17 +350,13 @@ main(int argc, char *argv[])
setlocale(LC_ALL, "");
#endif

g_set_prgname("tfshark");
cmdarg_err_init(tfshark_cmdarg_err, tfshark_cmdarg_err_cont);

/* Initialize log handler early so we can have proper logging during startup. */
ws_log_init(NULL);

cmdarg_err_init(tfshark_cmdarg_err, tfshark_cmdarg_err_cont);
ws_log_init("tfshark", vcmdarg_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 INVALID_OPTION;
/* Early logging command-line initialization. */
ws_log_parse_args(&argc, argv, vcmdarg_err, INVALID_OPTION);

#ifdef _WIN32
create_app_running_mutex();
Expand Down
Loading

0 comments on commit c6a9206

Please sign in to comment.