common-main: stop munging argv[0] path
authorJeff King <peff@peff.net>
Sun, 27 Nov 2016 04:31:13 +0000 (23:31 -0500)
committerJunio C Hamano <gitster@pobox.com>
Tue, 29 Nov 2016 19:01:48 +0000 (11:01 -0800)
Since 650c44925 (common-main: call git_extract_argv0_path(),
2016-07-01), the argv[0] that is seen in cmd_main() of
individual programs is always the basename of the
executable, as common-main strips off the full path. This
can produce confusing results for git-daemon, which wants to
re-exec itself.

For instance, if the program was originally run as
"/usr/lib/git/git-daemon", it will try just re-execing
"git-daemon", which will find the first instance in $PATH.
If git's exec-path has not been prepended to $PATH, we may
find the git-daemon from a different version (or no
git-daemon at all).

Normally this isn't a problem. Git commands are run as "git
daemon", the git wrapper puts the exec-path at the front of
$PATH, and argv[0] is already "daemon" anyway. But running
git-daemon via its full exec-path, while not really a
recommended method, did work prior to 650c44925. Let's make
it work again.

The real goal of 650c44925 was not to munge argv[0], but to
reliably set the argv0_path global. The only reason it
munges at all is that one caller, the git.c wrapper,
piggy-backed on that computation to find the command
basename. Instead, let's leave argv[0] untouched in
common-main, and have git.c do its own basename computation.

While we're at it, let's drop the return value from
git_extract_argv0_path(). It was only ever used in this one
callsite, and its dual purposes is what led to this
confusion in the first place.

Note that by changing the interface, the compiler can
confirm for us that there are no other callers storing the
return value. But the compiler can't tell us whether any of
the cmd_main() functions (besides git.c) were relying on the
basename munging. However, we can observe that prior to
650c44925, no other cmd_main() functions did that munging,
and no new cmd_main() functions have been introduced since
then. So we can't be regressing any of those cases.

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
common-main.c
exec_cmd.c
exec_cmd.h
git.c
index 44a29e8b13cb5203e2d35c99b8e2b5fec49f6981..c654f95551c33d6b346ae7fe43a6cac987dd6b75 100644 (file)
@@ -33,7 +33,7 @@ int main(int argc, const char **argv)
 
        git_setup_gettext();
 
-       argv[0] = git_extract_argv0_path(argv[0]);
+       git_extract_argv0_path(argv[0]);
 
        restore_sigpipe_to_default();
 
index 9d5703a157fe8e9d8396e0771798e54dc5eea6eb..19ac2146d0c49be8f5ef3739664dc3105544b4c2 100644 (file)
@@ -38,21 +38,17 @@ char *system_path(const char *path)
        return strbuf_detach(&d, NULL);
 }
 
-const char *git_extract_argv0_path(const char *argv0)
+void git_extract_argv0_path(const char *argv0)
 {
        const char *slash;
 
        if (!argv0 || !*argv0)
-               return NULL;
+               return;
 
        slash = find_last_dir_sep(argv0);
 
-       if (slash) {
+       if (slash)
                argv0_path = xstrndup(argv0, slash - argv0);
-               return slash + 1;
-       }
-
-       return argv0;
 }
 
 void git_set_argv_exec_path(const char *exec_path)
index 1f6b43378ba3866c07578b2222154e3a760440d1..ff0b48048a6ba82827b4d441654c34c21cfb7b9a 100644 (file)
@@ -4,7 +4,7 @@
 struct argv_array;
 
 extern void git_set_argv_exec_path(const char *exec_path);
-extern const char *git_extract_argv0_path(const char *path);
+extern void git_extract_argv0_path(const char *path);
 extern const char *git_exec_path(void);
 extern void setup_path(void);
 extern const char **prepare_git_cmd(struct argv_array *out, const char **argv);
diff --git a/git.c b/git.c
index 0f1937fd0c23da7c316540b8f9a6b05746011506..5e50a41285bd7129aa1e1373276a6f22206831a5 100644 (file)
--- a/git.c
+++ b/git.c
@@ -617,6 +617,11 @@ int cmd_main(int argc, const char **argv)
        cmd = argv[0];
        if (!cmd)
                cmd = "git-help";
+       else {
+               const char *slash = find_last_dir_sep(cmd);
+               if (slash)
+                       cmd = slash + 1;
+       }
 
        trace_command_performance(argv);