Clean up initialization code for programs.

Make the init_progfile_dir() call unconditionally, even if plugins
aren't supported, as that doesn't necessarily mean nobody uses the
directory containing the executable.

Report the error the same way in all programs, and free the error string
after we're finished with it.

Make the error - and the comment before the code - reflect what
init_progfile_dir() is actually doing (the goal is to get the full
pathname of the directory *containing* the executable; that's generally
done by getting the pathname of the executable and stripping off the
name of the executable, but that's won't necessarily always be the
case).  Also note for TShark that we won't be able to capture traffic,
just as we do for Wireshark (if we don't have the pathname of the
program file, we don't have a pathname to use to find dumpcap).

Have the plugin scanner just fail silently if we weren't able to get the
plugin directory path, so we don't have to worry about calling it if
init_progfile_dir() fails.

Clean up white space while we're at it.

Change-Id: I8e580c719aab6fbf74a764bf6629962394fff7c8
Reviewed-on: https://code.wireshark.org/review/19076
Reviewed-by: Guy Harris <guy@alum.mit.edu>
This commit is contained in:
Guy Harris 2016-12-04 20:25:51 -08:00
parent faf70602a8
commit 037c64aa34
13 changed files with 168 additions and 126 deletions

View File

@ -1405,6 +1405,7 @@ main(int argc, char *argv[])
{
GString *comp_info_str;
GString *runtime_info_str;
char *init_progfile_dir_error;
wtap *wth;
int err;
gchar *err_info;
@ -1417,9 +1418,6 @@ main(int argc, char *argv[])
};
int status = 0;
#ifdef HAVE_PLUGINS
char *init_progfile_dir_error;
#endif
#ifdef HAVE_LIBGCRYPT
FILE *fh;
char *hash_buf = NULL;
@ -1459,26 +1457,33 @@ main(int argc, char *argv[])
*/
init_process_policies();
/*
* Attempt to get the pathname of the directory containing the
* executable file.
*/
init_progfile_dir_error = init_progfile_dir(argv[0], main);
if (init_progfile_dir_error != NULL) {
fprintf(stderr,
"capinfos: Can't get pathname of directory containing the capinfos program: %s.\n",
init_progfile_dir_error);
g_free(init_progfile_dir_error);
}
wtap_init();
#ifdef HAVE_PLUGINS
if ((init_progfile_dir_error = init_progfile_dir(argv[0], main))) {
g_warning("capinfos: init_progfile_dir(): %s", init_progfile_dir_error);
g_free(init_progfile_dir_error);
} else {
init_report_err(failure_message, NULL, NULL, NULL);
init_report_err(failure_message, NULL, NULL, NULL);
/* Scan for plugins. This does *not* call their registration routines;
that's done later.
/* Scan for plugins. This does *not* call their registration routines;
that's done later.
Don't report failures to load plugins because most (non-wiretap)
plugins *should* fail to load (because we're not linked against
libwireshark and dissector plugins need libwireshark). */
scan_plugins(DONT_REPORT_LOAD_FAILURE);
Don't report failures to load plugins because most (non-wiretap)
plugins *should* fail to load (because we're not linked against
libwireshark and dissector plugins need libwireshark). */
scan_plugins(DONT_REPORT_LOAD_FAILURE);
/* Register all libwiretap plugin modules. */
register_all_wiretap_modules();
}
/* Register all libwiretap plugin modules. */
register_all_wiretap_modules();
#endif
/* Process the options */

View File

@ -86,6 +86,7 @@ main(int argc, char *argv[])
{
GString *comp_info_str;
GString *runtime_info_str;
char *init_progfile_dir_error;
wtap *wth;
int err;
gchar *err_info;
@ -98,10 +99,6 @@ main(int argc, char *argv[])
{0, 0, 0, 0 }
};
#ifdef HAVE_PLUGINS
char *init_progfile_dir_error;
#endif
/* Set the C-language locale to the native environment. */
setlocale(LC_ALL, "");
@ -131,26 +128,33 @@ main(int argc, char *argv[])
*/
init_process_policies();
/*
* Attempt to get the pathname of the directory containing the
* executable file.
*/
init_progfile_dir_error = init_progfile_dir(argv[0], main);
if (init_progfile_dir_error != NULL) {
fprintf(stderr,
"captype: Can't get pathname of directory containing the captype program: %s.\n",
init_progfile_dir_error);
g_free(init_progfile_dir_error);
}
wtap_init();
#ifdef HAVE_PLUGINS
if ((init_progfile_dir_error = init_progfile_dir(argv[0], main))) {
g_warning("captype: init_progfile_dir(): %s", init_progfile_dir_error);
g_free(init_progfile_dir_error);
} else {
init_report_err(failure_message,NULL,NULL,NULL);
init_report_err(failure_message,NULL,NULL,NULL);
/* Scan for plugins. This does *not* call their registration routines;
that's done later.
/* Scan for plugins. This does *not* call their registration routines;
that's done later.
Don't report failures to load plugins because most (non-wiretap)
plugins *should* fail to load (because we're not linked against
libwireshark and dissector plugins need libwireshark). */
scan_plugins(DONT_REPORT_LOAD_FAILURE);
Don't report failures to load plugins because most (non-wiretap)
plugins *should* fail to load (because we're not linked against
libwireshark and dissector plugins need libwireshark). */
scan_plugins(DONT_REPORT_LOAD_FAILURE);
/* Register all libwiretap plugin modules. */
register_all_wiretap_modules();
}
/* Register all libwiretap plugin modules. */
register_all_wiretap_modules();
#endif
/* Process the options */

View File

@ -68,12 +68,14 @@ main(int argc, char **argv)
init_process_policies();
/*
* Attempt to get the pathname of the executable file.
* Attempt to get the pathname of the directory containing the
* executable file.
*/
init_progfile_dir_error = init_progfile_dir(argv[0], main);
if (init_progfile_dir_error != NULL) {
fprintf(stderr, "dftest: Can't get pathname of dftest program: %s.\n",
fprintf(stderr, "dftest: Can't get pathname of directory containing the dftest program: %s.\n",
init_progfile_dir_error);
g_free(init_progfile_dir_error);
}
init_report_err(failure_message, open_failure_message,

View File

@ -942,6 +942,7 @@ main(int argc, char *argv[])
{
GString *comp_info_str;
GString *runtime_info_str;
char *init_progfile_dir_error;
wtap *wth;
int i, j, read_err, write_err;
gchar *read_err_info, *write_err_info;
@ -982,10 +983,6 @@ main(int argc, char *argv[])
GArray *nrb_hdrs = NULL;
char *shb_user_appl;
#ifdef HAVE_PLUGINS
char* init_progfile_dir_error;
#endif
cmdarg_err_init(failure_message, failure_message_cont);
#ifdef _WIN32
@ -1014,27 +1011,34 @@ main(int argc, char *argv[])
*/
init_process_policies();
/*
* Attempt to get the pathname of the directory containing the
* executable file.
*/
init_progfile_dir_error = init_progfile_dir(argv[0], main);
if (init_progfile_dir_error != NULL) {
fprintf(stderr,
"editcap: Can't get pathname of directory containing the editcap program: %s.\n",
init_progfile_dir_error);
g_free(init_progfile_dir_error);
}
wtap_init();
#ifdef HAVE_PLUGINS
/* Register wiretap plugins */
if ((init_progfile_dir_error = init_progfile_dir(argv[0], main))) {
g_warning("editcap: init_progfile_dir(): %s", init_progfile_dir_error);
g_free(init_progfile_dir_error);
} else {
init_report_err(failure_message,NULL,NULL,NULL);
init_report_err(failure_message,NULL,NULL,NULL);
/* Scan for plugins. This does *not* call their registration routines;
that's done later.
/* Scan for plugins. This does *not* call their registration routines;
that's done later.
Don't report failures to load plugins because most (non-wiretap)
plugins *should* fail to load (because we're not linked against
libwireshark and dissector plugins need libwireshark). */
scan_plugins(DONT_REPORT_LOAD_FAILURE);
Don't report failures to load plugins because most (non-wiretap)
plugins *should* fail to load (because we're not linked against
libwireshark and dissector plugins need libwireshark). */
scan_plugins(DONT_REPORT_LOAD_FAILURE);
/* Register all libwiretap plugin modules. */
register_all_wiretap_modules();
}
/* Register all libwiretap plugin modules. */
register_all_wiretap_modules();
#endif
/* Process the options */

View File

@ -242,6 +242,7 @@ main(int argc, char *argv[])
{
GString *comp_info_str;
GString *runtime_info_str;
char *init_progfile_dir_error;
int opt;
static const struct option long_options[] = {
{"help", no_argument, NULL, 'h'},
@ -266,10 +267,6 @@ main(int argc, char *argv[])
gboolean use_stdout = FALSE;
merge_progress_callback_t cb;
#ifdef HAVE_PLUGINS
char *init_progfile_dir_error;
#endif
cmdarg_err_init(mergecap_cmdarg_err, mergecap_cmdarg_err_cont);
#ifdef _WIN32
@ -298,27 +295,33 @@ main(int argc, char *argv[])
*/
init_process_policies();
/*
* Attempt to get the pathname of the directory containing the
* executable file.
*/
init_progfile_dir_error = init_progfile_dir(argv[0], main);
if (init_progfile_dir_error != NULL) {
fprintf(stderr,
"mergecap: Can't get pathname of directory containing the mergecap program: %s.\n",
init_progfile_dir_error);
g_free(init_progfile_dir_error);
}
wtap_init();
#ifdef HAVE_PLUGINS
/* Register wiretap plugins */
if ((init_progfile_dir_error = init_progfile_dir(argv[0], main))) {
g_warning("mergecap: init_progfile_dir(): %s", init_progfile_dir_error);
g_free(init_progfile_dir_error);
} else {
init_report_err(failure_message,NULL,NULL,NULL);
init_report_err(failure_message,NULL,NULL,NULL);
/* Scan for plugins. This does *not* call their registration routines;
that's done later.
/* Scan for plugins. This does *not* call their registration routines;
that's done later.
Don't report failures to load plugins because most (non-wiretap)
plugins *should* fail to load (because we're not linked against
libwireshark and dissector plugins need libwireshark).*/
scan_plugins(DONT_REPORT_LOAD_FAILURE);
Don't report failures to load plugins because most (non-wiretap)
plugins *should* fail to load (because we're not linked against
libwireshark and dissector plugins need libwireshark).*/
scan_plugins(DONT_REPORT_LOAD_FAILURE);
/* Register all libwiretap plugin modules. */
register_all_wiretap_modules();
}
/* Register all libwiretap plugin modules. */
register_all_wiretap_modules();
#endif
/* Process the options first */

View File

@ -110,6 +110,7 @@ usage(gboolean is_error)
int
main(int argc, char **argv)
{
char *init_progfile_dir_error;
int opt;
int produce_type = -1;
char *produce_filename = NULL;
@ -124,15 +125,23 @@ main(int argc, char **argv)
{0, 0, 0, 0 }
};
#ifdef HAVE_PLUGINS
char *init_progfile_dir_error;
#endif
/*
* Get credential information for later use.
*/
init_process_policies();
/*
* Attempt to get the pathname of the directory containing the
* executable file.
*/
init_progfile_dir_error = init_progfile_dir(argv[0], main);
if (init_progfile_dir_error != NULL) {
fprintf(stderr,
"capinfos: Can't get pathname of directory containing the capinfos program: %s.\n",
init_progfile_dir_error);
g_free(init_progfile_dir_error);
}
wtap_init();
cmdarg_err_init(failure_message, failure_message_cont);
@ -144,24 +153,19 @@ main(int argc, char **argv)
#ifdef HAVE_PLUGINS
/* Register wiretap plugins */
if ((init_progfile_dir_error = init_progfile_dir(argv[0], main))) {
g_warning("randpkt: init_progfile_dir(): %s", init_progfile_dir_error);
g_free(init_progfile_dir_error);
} else {
init_report_err(failure_message,NULL,NULL,NULL);
init_report_err(failure_message,NULL,NULL,NULL);
/* Scan for plugins. This does *not* call their registration routines;
that's done later.
/* Scan for plugins. This does *not* call their registration routines;
that's done later.
Don't report failures to load plugins because most
(non-wiretap) plugins *should* fail to load (because
we're not linked against libwireshark and dissector
plugins need libwireshark). */
scan_plugins(DONT_REPORT_LOAD_FAILURE);
Don't report failures to load plugins because most
(non-wiretap) plugins *should* fail to load (because
we're not linked against libwireshark and dissector
plugins need libwireshark). */
scan_plugins(DONT_REPORT_LOAD_FAILURE);
/* Register all libwiretap plugin modules. */
register_all_wiretap_modules();
}
/* Register all libwiretap plugin modules. */
register_all_wiretap_modules();
#endif
while ((opt = getopt_long(argc, argv, "b:c:ht:r", long_options, NULL)) != -1) {

View File

@ -485,7 +485,8 @@ main(int argc, char *argv[])
string_fmts = g_ptr_array_new();
/*
* Attempt to get the pathname of the executable file.
* Attempt to get the pathname of the directory containing the
* executable file.
*/
init_progfile_dir_error = init_progfile_dir(argv[0], main);
if (init_progfile_dir_error != NULL) {

View File

@ -167,6 +167,7 @@ main(int argc, char *argv[])
{
GString *comp_info_str;
GString *runtime_info_str;
char *init_progfile_dir_error;
wtap *wth = NULL;
wtap_dumper *pdh = NULL;
struct wtap_pkthdr dump_phdr;
@ -195,10 +196,6 @@ main(int argc, char *argv[])
char *infile;
const char *outfile;
#ifdef HAVE_PLUGINS
char *init_progfile_dir_error;
#endif
/* Get the compile-time version information string */
comp_info_str = get_compiled_version_info(NULL, NULL);
@ -215,32 +212,39 @@ main(int argc, char *argv[])
g_string_free(comp_info_str, TRUE);
g_string_free(runtime_info_str, TRUE);
/*
* Get credential information for later use.
*/
init_process_policies();
/*
* Get credential information for later use.
*/
init_process_policies();
wtap_init();
/*
* Attempt to get the pathname of the directory containing the
* executable file.
*/
init_progfile_dir_error = init_progfile_dir(argv[0], main);
if (init_progfile_dir_error != NULL) {
fprintf(stderr,
"reordercap: Can't get pathname of directory containing the reordercap program: %s.\n",
init_progfile_dir_error);
g_free(init_progfile_dir_error);
}
wtap_init();
#ifdef HAVE_PLUGINS
/* Register wiretap plugins */
if ((init_progfile_dir_error = init_progfile_dir(argv[0], main))) {
g_warning("reordercap: init_progfile_dir(): %s", init_progfile_dir_error);
g_free(init_progfile_dir_error);
} else {
init_report_err(failure_message,NULL,NULL,NULL);
init_report_err(failure_message,NULL,NULL,NULL);
/* Scan for plugins. This does *not* call their registration routines;
that's done later.
/* Scan for plugins. This does *not* call their registration routines;
that's done later.
Don't report failures to load plugins because most (non-wiretap)
plugins *should* fail to load (because we're not linked against
libwireshark and dissector plugins need libwireshark). */
scan_plugins(DONT_REPORT_LOAD_FAILURE);
Don't report failures to load plugins because most (non-wiretap)
plugins *should* fail to load (because we're not linked against
libwireshark and dissector plugins need libwireshark). */
scan_plugins(DONT_REPORT_LOAD_FAILURE);
/* Register all libwiretap plugin modules. */
register_all_wiretap_modules();
}
/* Register all libwiretap plugin modules. */
register_all_wiretap_modules();
#endif
/* Process the options first */

View File

@ -393,12 +393,15 @@ main(int argc, char *argv[])
print_current_user();
/*
* Attempt to get the pathname of the executable file.
* Attempt to get the pathname of the directory containing the
* executable file.
*/
init_progfile_dir_error = init_progfile_dir(argv[0], main);
if (init_progfile_dir_error != NULL) {
fprintf(stderr, "tfshark: Can't get pathname of tfshark program: %s.\n",
fprintf(stderr,
"tfshark: Can't get pathname of directory containing the tfshark program: %s.\n",
init_progfile_dir_error);
g_free(init_progfile_dir_error);
}
initialize_funnel_ops();

View File

@ -736,12 +736,17 @@ main(int argc, char *argv[])
print_current_user();
/*
* Attempt to get the pathname of the executable file.
* Attempt to get the pathname of the directory containing the
* executable file.
*/
init_progfile_dir_error = init_progfile_dir(argv[0], main);
if (init_progfile_dir_error != NULL) {
fprintf(stderr, "tshark: Can't get pathname of tshark program: %s.\n",
fprintf(stderr,
"tshark: Can't get pathname of directory containing the tshark program: %s.\n"
"It won't be possible to capture traffic.\n"
"Report this to the Wireshark developers.",
init_progfile_dir_error);
g_free(init_progfile_dir_error);
}
initialize_funnel_ops();

View File

@ -2115,7 +2115,8 @@ main(int argc, char *argv[])
relinquish_special_privs_perm();
/*
* Attempt to get the pathname of the executable file.
* Attempt to get the pathname of the directory containing the
* executable file.
*/
init_progfile_dir_error = init_progfile_dir(argv[0], main);
@ -2280,7 +2281,7 @@ main(int argc, char *argv[])
splash_win = splash_new("Loading Wireshark ...");
if (init_progfile_dir_error != NULL) {
simple_dialog(ESD_TYPE_WARN, ESD_BTN_OK,
"Can't get pathname of Wireshark: %s.\n"
"Can't get pathname of directory containing Wireshark: %s.\n"
"It won't be possible to capture traffic.\n"
"Report this to the Wireshark developers.",
init_progfile_dir_error);

View File

@ -375,7 +375,8 @@ int main(int argc, char *qt_argv[])
relinquish_special_privs_perm();
/*
* Attempt to get the pathname of the executable file.
* Attempt to get the pathname of the directory containing the
* executable file.
*/
/* init_progfile_dir_error = */ init_progfile_dir(argv[0],
(int (*)(int, char **)) get_gui_compiled_info);

View File

@ -276,9 +276,9 @@ scan_plugins(plugin_load_failure_mode mode)
char *plugin_dir_path;
char *plugins_pers_dir;
WS_DIR *dir; /* scanned directory */
WS_DIRENT *file; /* current file */
WS_DIRENT *file; /* current file */
if (plugin_list == NULL) /* ensure scan_plugins is only run once */
if (plugin_list == NULL) /* only scan for plugins once */
{
/*
* Scan the global plugin directory.
@ -288,6 +288,11 @@ scan_plugins(plugin_load_failure_mode mode)
* they will contain plugins in the case of an in-tree build.
*/
plugin_dir = get_plugin_dir();
if (plugin_dir == NULL)
{
/* We couldn't find the plugin directory. */
return;
}
if (running_in_build_directory())
{
if ((dir = ws_dir_open(plugin_dir, 0, NULL)) != NULL)