Attempt to use dladdr() to get the pathname of the executable image if
authorguy <guy@f5534014-38df-0310-8fa8-9805f1628bb7>
Sun, 22 Mar 2009 00:42:33 +0000 (00:42 +0000)
committerguy <guy@f5534014-38df-0310-8fa8-9805f1628bb7>
Sun, 22 Mar 2009 00:42:33 +0000 (00:42 +0000)
it's available and works.

git-svn-id: http://anonsvn.wireshark.org/wireshark/trunk@27812 f5534014-38df-0310-8fa8-9805f1628bb7

capinfos.c
configure.in
dftest.c
editcap.c
epan/filesystem.c
epan/filesystem.h
gtk/main.c
rawshark.c
tshark.c

index 1eb6b6a7e3ffd4f1a7c2492ee98cc9dc612b238b..8d09fcdf9e36493ee41842909b9d44aec1eb4164 100644 (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 {
index 75ad236a734d0b37522820c3b5ce61668d00c786..d110126399e9377873a36f167a59c744d144b6fb 100644 (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)
 
index 84b198cbde65e2272d56959c446736de152180a7..7fbadd8bc70316743fd66f9628f527e7a95c8d16 100644 (file)
--- a/dftest.c
+++ b/dftest.c
@@ -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);
index 8c541e1b9cbdba5226cb9c49e47008ec02438f4a..9f2fb28d57cbbd80105671fa4662ae82a60f9879 100644 (file)
--- a/editcap.c
+++ b/editcap.c
@@ -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 {
index 5b522d6b76f8c095654d029800c09335ce0df7e4..79312d3a053871c020fda614f33766488295f3ce 100644 (file)
 #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
index a97f8d7880639c361d0fef4d523c13337741944e..01c2a60d3ede1fdfa56cd8017844fff8a7b136c0 100644 (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.
index 7ac984b17a871731eda8f209b9f7b3ac4eea58cf..14a96ac8373ff672cb5599cae4025babf5e4c5c3 100644 (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();
index 9dc10bcb7bbe496ec6f8c1f658bb7ccfc1731906..5b3d07af23464a0c0544054dd99a68d6965d1dc3 100644 (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);
index 9ce3f57582594e2d3c0a3b4fc538b825e1a6eca3..688b0a98362932761885a91f24fbe331bb12315b 100644 (file)
--- a/tshark.c
+++ b/tshark.c
@@ -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);