Skip to content

Commit 5816d3c

Browse files
committed
Merge branch 'jk/execv-dashed-external' into maint
Typing ^C to pager, which usually does not kill it, killed Git and took the pager down as a collateral damage in certain process-tree structure. This has been fixed. * jk/execv-dashed-external: execv_dashed_external: wait for child on signal death execv_dashed_external: stop exiting with negative code execv_dashed_external: use child_process struct
2 parents b32fe95 + 46df690 commit 5816d3c

File tree

3 files changed

+35
-21
lines changed

3 files changed

+35
-21
lines changed

git.c

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -575,8 +575,7 @@ static void handle_builtin(int argc, const char **argv)
575575

576576
static void execv_dashed_external(const char **argv)
577577
{
578-
struct strbuf cmd = STRBUF_INIT;
579-
const char *tmp;
578+
struct child_process cmd = CHILD_PROCESS_INIT;
580579
int status;
581580

582581
if (get_super_prefix())
@@ -586,30 +585,25 @@ static void execv_dashed_external(const char **argv)
586585
use_pager = check_pager_config(argv[0]);
587586
commit_pager_choice();
588587

589-
strbuf_addf(&cmd, "git-%s", argv[0]);
588+
argv_array_pushf(&cmd.args, "git-%s", argv[0]);
589+
argv_array_pushv(&cmd.args, argv + 1);
590+
cmd.clean_on_exit = 1;
591+
cmd.wait_after_clean = 1;
592+
cmd.silent_exec_failure = 1;
590593

591-
/*
592-
* argv[0] must be the git command, but the argv array
593-
* belongs to the caller, and may be reused in
594-
* subsequent loop iterations. Save argv[0] and
595-
* restore it on error.
596-
*/
597-
tmp = argv[0];
598-
argv[0] = cmd.buf;
599-
600-
trace_argv_printf(argv, "trace: exec:");
594+
trace_argv_printf(cmd.args.argv, "trace: exec:");
601595

602596
/*
603-
* if we fail because the command is not found, it is
604-
* OK to return. Otherwise, we just pass along the status code.
597+
* If we fail because the command is not found, it is
598+
* OK to return. Otherwise, we just pass along the status code,
599+
* or our usual generic code if we were not even able to exec
600+
* the program.
605601
*/
606-
status = run_command_v_opt(argv, RUN_SILENT_EXEC_FAILURE | RUN_CLEAN_ON_EXIT);
607-
if (status >= 0 || errno != ENOENT)
602+
status = run_command(&cmd);
603+
if (status >= 0)
608604
exit(status);
609-
610-
argv[0] = tmp;
611-
612-
strbuf_release(&cmd);
605+
else if (errno != ENOENT)
606+
exit(128);
613607
}
614608

615609
static int run_argv(int *argcp, const char ***argv)

run-command.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ static int installed_child_cleanup_handler;
2929

3030
static void cleanup_children(int sig, int in_signal)
3131
{
32+
struct child_to_clean *children_to_wait_for = NULL;
33+
3234
while (children_to_clean) {
3335
struct child_to_clean *p = children_to_clean;
3436
children_to_clean = p->next;
@@ -45,6 +47,23 @@ static void cleanup_children(int sig, int in_signal)
4547
}
4648

4749
kill(p->pid, sig);
50+
51+
if (p->process->wait_after_clean) {
52+
p->next = children_to_wait_for;
53+
children_to_wait_for = p;
54+
} else {
55+
if (!in_signal)
56+
free(p);
57+
}
58+
}
59+
60+
while (children_to_wait_for) {
61+
struct child_to_clean *p = children_to_wait_for;
62+
children_to_wait_for = p->next;
63+
64+
while (waitpid(p->pid, NULL, 0) < 0 && errno == EINTR)
65+
; /* spin waiting for process exit or error */
66+
4867
if (!in_signal)
4968
free(p);
5069
}

run-command.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ struct child_process {
4343
unsigned stdout_to_stderr:1;
4444
unsigned use_shell:1;
4545
unsigned clean_on_exit:1;
46+
unsigned wait_after_clean:1;
4647
void (*clean_on_exit_handler)(struct child_process *process);
4748
void *clean_on_exit_handler_cbdata;
4849
};

0 commit comments

Comments
 (0)