]> granicus.if.org Git - strace/commitdiff
Use program_invocation_name instead of a local progname variable
authorDmitry V. Levin <ldv@altlinux.org>
Sun, 2 Jul 2017 00:31:50 +0000 (00:31 +0000)
committerDmitry V. Levin <ldv@altlinux.org>
Sun, 2 Jul 2017 00:31:50 +0000 (00:31 +0000)
Emulate program_invocation_name only if it is not provided by libc.

* configure.ac: Check for program_invocation_name variable.
* strace.c (progname): Remove.
[!HAVE_PROGRAM_INVOCATION_NAME] (program_invocation_name): New variable.
(verror_msg, error_msg_and_help): Use it instead of progname.
(init): Initialize program_invocation_name instead of progname.

configure.ac
strace.c

index bc7b637dc960314c195ef586b8e52b28c33046fd..eb7b0e785a7fed5f73abeb1e98c7a13e4e8ada81 100644 (file)
@@ -709,6 +709,16 @@ if test "x$st_cv_have___builtin_popcount" = xyes; then
                  [Define to 1 if the system provides __builtin_popcount function])
 fi
 
+AC_CACHE_CHECK([for program_invocation_name], [st_cv_have_program_invocation_name],
+              [AC_LINK_IFELSE([AC_LANG_PROGRAM([[#include <errno.h>]],
+                                               [[return !*program_invocation_name]])],
+                              [st_cv_have_program_invocation_name=yes],
+                              [st_cv_have_program_invocation_name=no])])
+if test "x$st_cv_have_program_invocation_name" = xyes; then
+       AC_DEFINE([HAVE_PROGRAM_INVOCATION_NAME], [1],
+                 [Define to 1 if the system provides program_invocation_name variable])
+fi
+
 AC_CHECK_LIB([dl], [dladdr], [dl_LIBS='-ldl'], [dl_LIBS=])
 if test "x$ac_cv_lib_dl_dladdr" = xyes; then
        AC_DEFINE([HAVE_DLADDR], [1], [Define to 1 if the system provides dladdr])
index 2bb4ec15fe3793cac22884c24bc236674c118879..5ece0247648c874752962ade1cd9433ae4c66aa0 100644 (file)
--- a/strace.c
+++ b/strace.c
@@ -151,7 +151,10 @@ static struct tcb *current_tcp;
 
 static struct tcb **tcbtab;
 static unsigned int nprocs, tcbtabsize;
-static const char *progname;
+
+#ifndef HAVE_PROGRAM_INVOCATION_NAME
+char *program_invocation_name;
+#endif
 
 unsigned os_release; /* generated from uname()'s u.release */
 
@@ -298,13 +301,15 @@ static void verror_msg(int err_no, const char *fmt, va_list p)
        msg = NULL;
        if (vasprintf(&msg, fmt, p) >= 0) {
                if (err_no)
-                       fprintf(stderr, "%s: %s: %s\n", progname, msg, strerror(err_no));
+                       fprintf(stderr, "%s: %s: %s\n",
+                               program_invocation_name, msg, strerror(err_no));
                else
-                       fprintf(stderr, "%s: %s\n", progname, msg);
+                       fprintf(stderr, "%s: %s\n",
+                               program_invocation_name, msg);
                free(msg);
        } else {
                /* malloc in vasprintf failed, try it without malloc */
-               fprintf(stderr, "%s: ", progname);
+               fprintf(stderr, "%s: ", program_invocation_name);
                vfprintf(stderr, fmt, p);
                if (err_no)
                        fprintf(stderr, ": %s\n", strerror(err_no));
@@ -339,7 +344,8 @@ void error_msg_and_help(const char *fmt, ...)
                va_start(p, fmt);
                verror_msg(0, fmt, p);
        }
-       fprintf(stderr, "Try '%s -h' for more information.\n", progname);
+       fprintf(stderr, "Try '%s -h' for more information.\n",
+               program_invocation_name);
        die();
 }
 
@@ -1617,7 +1623,11 @@ init(int argc, char *argv[])
        int c, i;
        int optF = 0;
 
-       progname = argv[0] ? argv[0] : "strace";
+       if (!program_invocation_name || !*program_invocation_name) {
+               static char name[] = "strace";
+               program_invocation_name =
+                       (argv[0] && *argv[0]) ? argv[0] : name;
+       }
 
        strace_tracer_pid = getpid();