Attempt to use dladdr() to get the pathname of the executable image if

it's available and works.

svn path=/trunk/; revision=27812
This commit is contained in:
Guy Harris 2009-03-22 00:42:33 +00:00
parent 724cae360e
commit 7c6e2155cc
9 changed files with 78 additions and 10 deletions

View File

@ -302,7 +302,8 @@ main(int argc, char *argv[])
#ifdef HAVE_PLUGINS
/* Register wiretap plugins */
if ((init_progfile_dir_error = init_progfile_dir(argv[0]))) {
if ((init_progfile_dir_error = init_progfile_dir(argv[0],
(const void *)main))) {
g_warning("capinfos: init_progfile_dir(): %s", init_progfile_dir_error);
g_free(init_progfile_dir_error);
} else {

View File

@ -676,6 +676,41 @@ else
have_plugins=no
fi
#
# Check whether we can use dladdr to find the pathname of an executable.
#
AC_MSG_CHECKING(whether dladdr can be used to find the pathname of an executable)
ac_save_CFLAGS="$CFLAGS"
ac_save_LIBS="$LIBS"
CFLAGS="$CFLAGS $GLIB_CFLAGS"
LIBS="$GLIB_LIBS $LIBS"
AC_TRY_RUN([
#include <stdio.h>
#include <dlfcn.h>
int
main(void)
{
Dl_info info;
if (!dladdr((const void *)main, &info))
return 1; /* failure */
if (info.dli_fname[0] != '/')
return 1; /* not an absolute path - failure */
return 0; /* assume success */
}
], ac_cv_dladdr_finds_executable_path=yes, ac_cv_dladdr_finds_executable_path=no,
[echo $ac_n "cross compiling; assumed OK... $ac_c"
ac_cv_dladdr_finds_executable_path=yes])
CFLAGS="$ac_save_CFLAGS"
LIBS="$ac_save_LIBS"
if test x$ac_cv_dladdr_finds_executable_path = xyes
then
AC_DEFINE(DLADDR_FINDS_EXECUTABLE_PATH, 1, [Define if dladdr can be used to find the path of the executable])
fi
AC_MSG_RESULT($ac_cv_dladdr_finds_executable_path)
dnl IGE Mac integration check
AC_MSG_CHECKING(whether to use IGE Mac integration functions)

View File

@ -75,7 +75,8 @@ main(int argc, char **argv)
/*
* Attempt to get the pathname of the executable file.
*/
init_progfile_dir_error = init_progfile_dir(argv[0]);
init_progfile_dir_error = init_progfile_dir(argv[0],
(const void *)main);
if (init_progfile_dir_error != NULL) {
fprintf(stderr, "dftest: Can't get pathname of dftest program: %s.\n",
init_progfile_dir_error);

View File

@ -424,7 +424,8 @@ main(int argc, char *argv[])
#ifdef HAVE_PLUGINS
/* Register wiretap plugins */
if ((init_progfile_dir_error = init_progfile_dir(argv[0]))) {
if ((init_progfile_dir_error = init_progfile_dir(argv[0],
(const void *)main))) {
g_warning("capinfos: init_progfile_dir(): %s", init_progfile_dir_error);
g_free(init_progfile_dir_error);
} else {

View File

@ -55,9 +55,12 @@
#include <tchar.h>
#include <shlobj.h>
#include <wsutil/unicode-utils.h>
#else
#else /* _WIN32 */
#ifdef DLADDR_FINDS_EXECUTABLE_PATH
#include <dlfcn.h>
#endif /* DLADDR_FINDS_EXECUTABLE_PATH */
#include <pwd.h>
#endif
#endif /* _WIN32 */
#include "filesystem.h"
#include "report_err.h"
@ -246,6 +249,10 @@ init_progfile_dir(const char *arg0
#ifdef _WIN32
_U_
#endif
, const void *main_addr
#if defined(_WIN32) || !defined(DLADDR_FINDS_EXECUTABLE_PATH)
_U_
#endif
)
{
char *dir_end;
@ -334,6 +341,9 @@ init_progfile_dir(const char *arg0
msg, error);
}
#else
#ifdef DLADDR_FINDS_EXECUTABLE_PATH
Dl_info info;
#endif
char *prog_pathname;
char *curdir;
long path_max;
@ -355,6 +365,22 @@ init_progfile_dir(const char *arg0
&& !started_with_special_privs())
running_in_build_directory_flag = TRUE;
#ifdef DLADDR_FINDS_EXECUTABLE_PATH
/*
* Try to use dladdr() to find the pathname of the executable.
*/
if (dladdr(main_addr, &info) && info.dli_fname[0] == '/') {
/*
* dladdr() succeeded, and we got an absolute path
* for the module containing main() (I don't know
* whether it's guaranteed to return an absolute path
* on all platforms), so we'll use that as the
* executable image's path.
*/
prog_pathname = g_strdup(info.dli_fname);
} else
#endif
{
/*
* Try to figure out the directory in which the currently running
* program resides, given the argv[0] it was started with. That
@ -456,6 +482,7 @@ init_progfile_dir(const char *arg0
return g_strdup("PATH isn't set");
}
}
}
/*
* OK, we have what we think is the pathname

View File

@ -36,7 +36,7 @@
* and save it for future use. Returns NULL on success, and a
* g_mallocated string containing an error on failure.
*/
extern char *init_progfile_dir(const char *arg0);
extern char *init_progfile_dir(const char *arg0, const void *main_addr);
/*
* Get the directory in which the program resides.

View File

@ -1808,7 +1808,8 @@ main(int argc, char *argv[])
/*
* Attempt to get the pathname of the executable file.
*/
init_progfile_dir_error = init_progfile_dir(argv[0]);
init_progfile_dir_error = init_progfile_dir(argv[0],
(const void *)main);
/* initialize the funnel mini-api */
initialize_funnel_ops();

View File

@ -31,7 +31,7 @@
* - Opens a specified file or named pipe
* - Applies a specfied DLT or "decode as" encapsulation
* - Reads frames prepended with a libpcap packet header.
* - Prints a status line, followed by fields from a specified list.
* - Prints a status line, followed by fields from a specified list.
*/
#ifdef HAVE_CONFIG_H
@ -460,7 +460,8 @@ main(int argc, char *argv[])
/*
* Attempt to get the pathname of the executable file.
*/
init_progfile_dir_error = init_progfile_dir(argv[0]);
init_progfile_dir_error = init_progfile_dir(argv[0],
(const void *)main);
if (init_progfile_dir_error != NULL) {
fprintf(stderr, "rawshark: Can't get pathname of rawshark program: %s.\n",
init_progfile_dir_error);

View File

@ -773,7 +773,8 @@ main(int argc, char *argv[])
/*
* Attempt to get the pathname of the executable file.
*/
init_progfile_dir_error = init_progfile_dir(argv[0]);
init_progfile_dir_error = init_progfile_dir(argv[0],
(const void *)main);
if (init_progfile_dir_error != NULL) {
fprintf(stderr, "tshark: Can't get pathname of tshark program: %s.\n",
init_progfile_dir_error);