Don't use dladdr() to get a pathname for the current executable().
Change-Id: I24ad11a659c2cb936f873339dc2b36ac9944280a Reviewed-on: https://code.wireshark.org/review/27359 Petri-Dish: Guy Harris <guy@alum.mit.edu> Tested-by: Petri Dish Buildbot Reviewed-by: Guy Harris <guy@alum.mit.edu>
This commit is contained in:
parent
41445d0e97
commit
15cfc41e94
|
@ -1458,7 +1458,7 @@ main(int argc, char *argv[])
|
||||||
* Attempt to get the pathname of the directory containing the
|
* Attempt to get the pathname of the directory containing the
|
||||||
* executable file.
|
* executable file.
|
||||||
*/
|
*/
|
||||||
init_progfile_dir_error = init_progfile_dir(argv[0], main);
|
init_progfile_dir_error = init_progfile_dir(argv[0]);
|
||||||
if (init_progfile_dir_error != NULL) {
|
if (init_progfile_dir_error != NULL) {
|
||||||
fprintf(stderr,
|
fprintf(stderr,
|
||||||
"capinfos: Can't get pathname of directory containing the capinfos program: %s.\n",
|
"capinfos: Can't get pathname of directory containing the capinfos program: %s.\n",
|
||||||
|
|
|
@ -134,7 +134,7 @@ main(int argc, char *argv[])
|
||||||
* Attempt to get the pathname of the directory containing the
|
* Attempt to get the pathname of the directory containing the
|
||||||
* executable file.
|
* executable file.
|
||||||
*/
|
*/
|
||||||
init_progfile_dir_error = init_progfile_dir(argv[0], main);
|
init_progfile_dir_error = init_progfile_dir(argv[0]);
|
||||||
if (init_progfile_dir_error != NULL) {
|
if (init_progfile_dir_error != NULL) {
|
||||||
fprintf(stderr,
|
fprintf(stderr,
|
||||||
"captype: Can't get pathname of directory containing the captype program: %s.\n",
|
"captype: Can't get pathname of directory containing the captype program: %s.\n",
|
||||||
|
|
|
@ -53,9 +53,6 @@
|
||||||
/* Define to use c-ares library */
|
/* Define to use c-ares library */
|
||||||
#cmakedefine HAVE_C_ARES 1
|
#cmakedefine HAVE_C_ARES 1
|
||||||
|
|
||||||
/* Define to 1 if you have the `dladdr' function. */
|
|
||||||
#cmakedefine HAVE_DLADDR 1
|
|
||||||
|
|
||||||
/* Define to 1 if you have the <fcntl.h> header file. */
|
/* Define to 1 if you have the <fcntl.h> header file. */
|
||||||
#cmakedefine HAVE_FCNTL_H 1
|
#cmakedefine HAVE_FCNTL_H 1
|
||||||
|
|
||||||
|
|
2
dftest.c
2
dftest.c
|
@ -58,7 +58,7 @@ main(int argc, char **argv)
|
||||||
* Attempt to get the pathname of the directory containing the
|
* Attempt to get the pathname of the directory containing the
|
||||||
* executable file.
|
* executable file.
|
||||||
*/
|
*/
|
||||||
init_progfile_dir_error = init_progfile_dir(argv[0], main);
|
init_progfile_dir_error = init_progfile_dir(argv[0]);
|
||||||
if (init_progfile_dir_error != NULL) {
|
if (init_progfile_dir_error != NULL) {
|
||||||
fprintf(stderr, "dftest: Can't get pathname of directory containing the dftest program: %s.\n",
|
fprintf(stderr, "dftest: Can't get pathname of directory containing the dftest program: %s.\n",
|
||||||
init_progfile_dir_error);
|
init_progfile_dir_error);
|
||||||
|
|
|
@ -1021,7 +1021,7 @@ main(int argc, char *argv[])
|
||||||
* Attempt to get the pathname of the directory containing the
|
* Attempt to get the pathname of the directory containing the
|
||||||
* executable file.
|
* executable file.
|
||||||
*/
|
*/
|
||||||
init_progfile_dir_error = init_progfile_dir(argv[0], main);
|
init_progfile_dir_error = init_progfile_dir(argv[0]);
|
||||||
if (init_progfile_dir_error != NULL) {
|
if (init_progfile_dir_error != NULL) {
|
||||||
fprintf(stderr,
|
fprintf(stderr,
|
||||||
"editcap: Can't get pathname of directory containing the editcap program: %s.\n",
|
"editcap: Can't get pathname of directory containing the editcap program: %s.\n",
|
||||||
|
|
|
@ -288,7 +288,7 @@ main(int argc, char *argv[])
|
||||||
* Attempt to get the pathname of the directory containing the
|
* Attempt to get the pathname of the directory containing the
|
||||||
* executable file.
|
* executable file.
|
||||||
*/
|
*/
|
||||||
init_progfile_dir_error = init_progfile_dir(argv[0], main);
|
init_progfile_dir_error = init_progfile_dir(argv[0]);
|
||||||
if (init_progfile_dir_error != NULL) {
|
if (init_progfile_dir_error != NULL) {
|
||||||
fprintf(stderr,
|
fprintf(stderr,
|
||||||
"mergecap: Can't get pathname of directory containing the mergecap program: %s.\n",
|
"mergecap: Can't get pathname of directory containing the mergecap program: %s.\n",
|
||||||
|
|
|
@ -127,7 +127,7 @@ main(int argc, char **argv)
|
||||||
* Attempt to get the pathname of the directory containing the
|
* Attempt to get the pathname of the directory containing the
|
||||||
* executable file.
|
* executable file.
|
||||||
*/
|
*/
|
||||||
init_progfile_dir_error = init_progfile_dir(argv[0], main);
|
init_progfile_dir_error = init_progfile_dir(argv[0]);
|
||||||
if (init_progfile_dir_error != NULL) {
|
if (init_progfile_dir_error != NULL) {
|
||||||
fprintf(stderr,
|
fprintf(stderr,
|
||||||
"capinfos: Can't get pathname of directory containing the capinfos program: %s.\n",
|
"capinfos: Can't get pathname of directory containing the capinfos program: %s.\n",
|
||||||
|
|
|
@ -486,7 +486,7 @@ main(int argc, char *argv[])
|
||||||
* Attempt to get the pathname of the directory containing the
|
* Attempt to get the pathname of the directory containing the
|
||||||
* executable file.
|
* executable file.
|
||||||
*/
|
*/
|
||||||
init_progfile_dir_error = init_progfile_dir(argv[0], main);
|
init_progfile_dir_error = init_progfile_dir(argv[0]);
|
||||||
if (init_progfile_dir_error != NULL) {
|
if (init_progfile_dir_error != NULL) {
|
||||||
fprintf(stderr, "rawshark: Can't get pathname of rawshark program: %s.\n",
|
fprintf(stderr, "rawshark: Can't get pathname of rawshark program: %s.\n",
|
||||||
init_progfile_dir_error);
|
init_progfile_dir_error);
|
||||||
|
|
|
@ -222,7 +222,7 @@ main(int argc, char *argv[])
|
||||||
* Attempt to get the pathname of the directory containing the
|
* Attempt to get the pathname of the directory containing the
|
||||||
* executable file.
|
* executable file.
|
||||||
*/
|
*/
|
||||||
init_progfile_dir_error = init_progfile_dir(argv[0], main);
|
init_progfile_dir_error = init_progfile_dir(argv[0]);
|
||||||
if (init_progfile_dir_error != NULL) {
|
if (init_progfile_dir_error != NULL) {
|
||||||
fprintf(stderr,
|
fprintf(stderr,
|
||||||
"reordercap: Can't get pathname of directory containing the reordercap program: %s.\n",
|
"reordercap: Can't get pathname of directory containing the reordercap program: %s.\n",
|
||||||
|
|
2
sharkd.c
2
sharkd.c
|
@ -124,7 +124,7 @@ main(int argc, char *argv[])
|
||||||
/*
|
/*
|
||||||
* Attempt to get the pathname of the executable file.
|
* Attempt to get the pathname of the executable file.
|
||||||
*/
|
*/
|
||||||
init_progfile_dir_error = init_progfile_dir(argv[0], main);
|
init_progfile_dir_error = init_progfile_dir(argv[0]);
|
||||||
if (init_progfile_dir_error != NULL) {
|
if (init_progfile_dir_error != NULL) {
|
||||||
fprintf(stderr, "sharkd: Can't get pathname of sharkd program: %s.\n",
|
fprintf(stderr, "sharkd: Can't get pathname of sharkd program: %s.\n",
|
||||||
init_progfile_dir_error);
|
init_progfile_dir_error);
|
||||||
|
|
|
@ -378,7 +378,7 @@ main(int argc, char *argv[])
|
||||||
* Attempt to get the pathname of the directory containing the
|
* Attempt to get the pathname of the directory containing the
|
||||||
* executable file.
|
* executable file.
|
||||||
*/
|
*/
|
||||||
init_progfile_dir_error = init_progfile_dir(argv[0], main);
|
init_progfile_dir_error = init_progfile_dir(argv[0]);
|
||||||
if (init_progfile_dir_error != NULL) {
|
if (init_progfile_dir_error != NULL) {
|
||||||
fprintf(stderr,
|
fprintf(stderr,
|
||||||
"tfshark: Can't get pathname of directory containing the tfshark program: %s.\n",
|
"tfshark: Can't get pathname of directory containing the tfshark program: %s.\n",
|
||||||
|
|
|
@ -234,7 +234,7 @@ fuzz_init(int argc _U_, char **argv)
|
||||||
/*
|
/*
|
||||||
* Attempt to get the pathname of the executable file.
|
* Attempt to get the pathname of the executable file.
|
||||||
*/
|
*/
|
||||||
init_progfile_dir_error = init_progfile_dir(argv[0], fuzz_init);
|
init_progfile_dir_error = init_progfile_dir(argv[0]);
|
||||||
if (init_progfile_dir_error != NULL)
|
if (init_progfile_dir_error != NULL)
|
||||||
fprintf(stderr, "fuzzshark: Can't get pathname of oss-fuzzshark program: %s.\n", init_progfile_dir_error);
|
fprintf(stderr, "fuzzshark: Can't get pathname of oss-fuzzshark program: %s.\n", init_progfile_dir_error);
|
||||||
|
|
||||||
|
|
2
tshark.c
2
tshark.c
|
@ -783,7 +783,7 @@ main(int argc, char *argv[])
|
||||||
* Attempt to get the pathname of the directory containing the
|
* Attempt to get the pathname of the directory containing the
|
||||||
* executable file.
|
* executable file.
|
||||||
*/
|
*/
|
||||||
init_progfile_dir_error = init_progfile_dir(argv[0], main);
|
init_progfile_dir_error = init_progfile_dir(argv[0]);
|
||||||
if (init_progfile_dir_error != NULL) {
|
if (init_progfile_dir_error != NULL) {
|
||||||
fprintf(stderr,
|
fprintf(stderr,
|
||||||
"tshark: Can't get pathname of directory containing the tshark program: %s.\n"
|
"tshark: Can't get pathname of directory containing the tshark program: %s.\n"
|
||||||
|
|
|
@ -2013,7 +2013,7 @@ main(int argc, char *argv[])
|
||||||
* Attempt to get the pathname of the directory containing the
|
* Attempt to get the pathname of the directory containing the
|
||||||
* executable file.
|
* executable file.
|
||||||
*/
|
*/
|
||||||
init_progfile_dir_error = init_progfile_dir(argv[0], main);
|
init_progfile_dir_error = init_progfile_dir(argv[0]);
|
||||||
|
|
||||||
/* initialize the funnel mini-api */
|
/* initialize the funnel mini-api */
|
||||||
initialize_funnel_ops();
|
initialize_funnel_ops();
|
||||||
|
|
|
@ -422,8 +422,7 @@ int main(int argc, char *qt_argv[])
|
||||||
* Attempt to get the pathname of the directory containing the
|
* Attempt to get the pathname of the directory containing the
|
||||||
* executable file.
|
* executable file.
|
||||||
*/
|
*/
|
||||||
/* init_progfile_dir_error = */ init_progfile_dir(argv[0],
|
/* init_progfile_dir_error = */ init_progfile_dir(argv[0]);
|
||||||
(int (*)(int, char **)) get_gui_compiled_info);
|
|
||||||
g_log(NULL, G_LOG_LEVEL_DEBUG, "progfile_dir: %s", get_progfile_dir());
|
g_log(NULL, G_LOG_LEVEL_DEBUG, "progfile_dir: %s", get_progfile_dir());
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
|
|
|
@ -10,13 +10,6 @@
|
||||||
|
|
||||||
#include <config.h>
|
#include <config.h>
|
||||||
|
|
||||||
/*
|
|
||||||
* Required with GNU libc to get dladdr().
|
|
||||||
* We define it here because <dlfcn.h> apparently gets included by
|
|
||||||
* one of the headers we include below.
|
|
||||||
*/
|
|
||||||
#define _GNU_SOURCE
|
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
@ -273,15 +266,31 @@ static gboolean running_in_build_directory_flag = FALSE;
|
||||||
* passed to the program, so it shouldn't be fooled by an argv[0]
|
* passed to the program, so it shouldn't be fooled by an argv[0]
|
||||||
* that doesn't match the executable path.
|
* that doesn't match the executable path.
|
||||||
*
|
*
|
||||||
* Sadly, not all UN*Xes necessarily have dladdr(), and those that
|
* We don't use dladdr() because:
|
||||||
* do don't necessarily have dladdr(main) return information about
|
*
|
||||||
* the executable image, and those that do aren't necessarily running
|
* not all UN*Xes necessarily have dladdr();
|
||||||
* on a platform wherein the executable image can get its own path
|
*
|
||||||
* from the kernel (either by a call or by it being handed to it along
|
* those that do have it don't necessarily have dladdr(main)
|
||||||
* with argv[] and the environment), and those that can don't
|
* return information about the executable image;
|
||||||
* necessarily use that to supply the path you get from dladdr(main),
|
*
|
||||||
* so we try this first and, if that fails, use dladdr(main) if
|
* those that do have a dladdr() where dladdr(main) returns
|
||||||
* available.
|
* information about the executable image don't necessarily
|
||||||
|
* have a mechanism by which the executable image can get
|
||||||
|
* its own path from the kernel (either by a call or by it
|
||||||
|
* being handed to it along with argv[] and the environment),
|
||||||
|
* so they just fall back on getting it from argv[0], which we
|
||||||
|
* already have code to do;
|
||||||
|
*
|
||||||
|
* those that do have such a mechanism don't necessarily use
|
||||||
|
* it in dladdr(), and, instead, just fall back on getting it
|
||||||
|
* from argv[0];
|
||||||
|
*
|
||||||
|
* so the only places where it's worth bothering to use dladdr()
|
||||||
|
* are platforms where dladdr(main) return information about the
|
||||||
|
* executable image by getting it from the kernel rather than
|
||||||
|
* by looking at argv[0], and where we can't get at that information
|
||||||
|
* ourselves, and we haven't seen any indication that there are any
|
||||||
|
* such platforms.
|
||||||
*
|
*
|
||||||
* In particular, some dynamic linkers supply a dladdr() such that
|
* In particular, some dynamic linkers supply a dladdr() such that
|
||||||
* dladdr(main) just returns something derived from argv[0], so
|
* dladdr(main) just returns something derived from argv[0], so
|
||||||
|
@ -289,21 +298,11 @@ static gboolean running_in_build_directory_flag = FALSE;
|
||||||
* another mechanism that can get you a more reliable version of
|
* another mechanism that can get you a more reliable version of
|
||||||
* the executable path.
|
* the executable path.
|
||||||
*
|
*
|
||||||
* However, at least in newer versions of DragonFly BSD, the dynamic
|
* So, on platforms where we know of a mechanism to get that path
|
||||||
* linker *does* get it from the aux vector passed to the program
|
* (where getting that path doesn't involve argv[0], which is not
|
||||||
* by the kernel, readlink /proc/curproc/file - which came first?
|
* guaranteed to reflect the path to the binary), this routine
|
||||||
*
|
* attempsts to use that platform's mechanism. On other platforms,
|
||||||
* On OpenBSD, dladdr(main) returns a value derived from argv[0],
|
* it just returns NULL.
|
||||||
* and there doesn't appear to be any way to get the executable path
|
|
||||||
* from the kernel, so we're out of luck there.
|
|
||||||
*
|
|
||||||
* So, on platforms where some versions have a version of dladdr()
|
|
||||||
* that gives an argv[0]-based path and that also have a mechanism
|
|
||||||
* to get a more reliable version of the path, we try that. On
|
|
||||||
* other platforms, we return NULL. If our caller gets back a NULL
|
|
||||||
* from us, it falls back on dladdr(main) if dladdr() is available,
|
|
||||||
* and if that fails or is unavailable, it falls back on processing
|
|
||||||
* argv[0] itself.
|
|
||||||
*
|
*
|
||||||
* This is not guaranteed to return an absolute path; if it doesn't,
|
* This is not guaranteed to return an absolute path; if it doesn't,
|
||||||
* our caller must prepend the current directory if it's a path.
|
* our caller must prepend the current directory if it's a path.
|
||||||
|
@ -456,10 +455,6 @@ init_progfile_dir(const char *arg0
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
_U_
|
_U_
|
||||||
#endif
|
#endif
|
||||||
, int (*function_addr)(int, char **)
|
|
||||||
#if defined(_WIN32) || !defined(HAVE_DLADDR)
|
|
||||||
_U_
|
|
||||||
#endif
|
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
|
@ -522,9 +517,6 @@ init_progfile_dir(const char *arg0
|
||||||
msg, error);
|
msg, error);
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
#ifdef HAVE_DLADDR
|
|
||||||
Dl_info info;
|
|
||||||
#endif
|
|
||||||
const char *execname;
|
const char *execname;
|
||||||
char *prog_pathname;
|
char *prog_pathname;
|
||||||
char *curdir;
|
char *curdir;
|
||||||
|
@ -550,24 +542,6 @@ init_progfile_dir(const char *arg0
|
||||||
running_in_build_directory_flag = TRUE;
|
running_in_build_directory_flag = TRUE;
|
||||||
|
|
||||||
execname = get_executable_path();
|
execname = get_executable_path();
|
||||||
#ifdef HAVE_DLADDR
|
|
||||||
if (function_addr != NULL && execname == NULL) {
|
|
||||||
/*
|
|
||||||
* Try to use dladdr() to find the pathname of the executable.
|
|
||||||
* dladdr() is not guaranteed to give you anything better than
|
|
||||||
* argv[0] (i.e., it might not contain a / at all, much less
|
|
||||||
* being an absolute path), and doesn't appear to do so on
|
|
||||||
* Linux, but on other platforms it could give you an absolute
|
|
||||||
* path and obviate the need for us to determine the absolute
|
|
||||||
* path.
|
|
||||||
*/
|
|
||||||
DIAG_OFF(pedantic)
|
|
||||||
if (dladdr((void *)function_addr, &info)) {
|
|
||||||
DIAG_ON(pedantic)
|
|
||||||
execname = info.dli_fname;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
if (execname == NULL) {
|
if (execname == NULL) {
|
||||||
/*
|
/*
|
||||||
* OK, guess based on argv[0].
|
* OK, guess based on argv[0].
|
||||||
|
@ -578,7 +552,7 @@ DIAG_ON(pedantic)
|
||||||
/*
|
/*
|
||||||
* Try to figure out the directory in which the currently running
|
* Try to figure out the directory in which the currently running
|
||||||
* program resides, given something purporting to be the executable
|
* program resides, given something purporting to be the executable
|
||||||
* name (from dladdr() or from the argv[0] it was started with.
|
* name (from an OS mechanism or from the argv[0] it was started with).
|
||||||
* That might be the absolute path of the program, or a path relative
|
* That might be the absolute path of the program, or a path relative
|
||||||
* to the current directory of the process that started it, or
|
* to the current directory of the process that started it, or
|
||||||
* just a name for the program if it was started from the command
|
* just a name for the program if it was started from the command
|
||||||
|
|
|
@ -29,7 +29,7 @@ extern "C" {
|
||||||
* and save it for future use. Returns NULL on success, and a
|
* and save it for future use. Returns NULL on success, and a
|
||||||
* g_mallocated string containing an error on failure.
|
* g_mallocated string containing an error on failure.
|
||||||
*/
|
*/
|
||||||
WS_DLL_PUBLIC char *init_progfile_dir(const char *arg0, int (*function_addr)(int, char **));
|
WS_DLL_PUBLIC char *init_progfile_dir(const char *arg0);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Get the directory in which the program resides.
|
* Get the directory in which the program resides.
|
||||||
|
|
Loading…
Reference in New Issue