@matheusbsbs / matheus-bagliano / commits / 507d7804c0
Fork 0

pager: don't use unsafe functions in signal handlers

Since the commit a3da8821208d (pager: do wait_for_pager on signal death), we call wait_for_pager() in the pager's signal handler. The recent bug report revealed that this causes a deadlock in glibc at aborting "git log" [*1*]. When this happens, git process is left unterminated, and it can't be killed by SIGTERM but only by SIGKILL. The problem is that wait_for_pager() function does more than waiting for pager process's termination, but it does cleanups and printing errors. Unfortunately, the functions that may be used in a signal handler are very limited [*2*]. Particularly, malloc(), free() and the variants can't be used in a signal handler because they take a mutex internally in glibc. This was the cause of the deadlock above. Other than the direct calls of malloc/free, many functions calling malloc/free can't be used. strerror() is such one, either. Also the usage of fflush() and printf() in a signal handler is bad, although it seems working so far. In a safer side, we should avoid them, too. This patch tries to reduce the calls of such functions in signal handlers. wait_for_signal() takes a flag and avoids the unsafe calls. Also, finish_command_in_signal() is introduced for the same reason. There the free() calls are removed, and only waits for the children without whining at errors. [*1*] https://bugzilla.opensuse.org/show_bug.cgi?id=942297 [*2*] http://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html#tag_15_04_03 Signed-off-by: Takashi Iwai <tiwai@suse.de> Reviewed-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Takashi Iwai committed Sep 4, 2015 at 11:35 UTC 507d7804c0b094889cd20f23ad9a48e2b76791f3
3 files changed +34 -14
pager.c
+16 -6
@@ -14,19 +14,29 @@
14 static const char *pager_argv[] = { NULL, NULL };
15 static struct child_process pager_process = CHILD_PROCESS_INIT;
16
17 -static void wait_for_pager(void)
17 +static void wait_for_pager(int in_signal)
18 {
19 - fflush(stdout);
20 - fflush(stderr);
19 + if (!in_signal) {
20 + fflush(stdout);
21 + fflush(stderr);
22 + }
23 /* signal EOF to pager */
24 close(1);
25 close(2);
24 - finish_command(&pager_process);
26 + if (in_signal)
27 + finish_command_in_signal(&pager_process);
28 + else
29 + finish_command(&pager_process);
30 +}
31 +
32 +static void wait_for_pager_atexit(void)
33 +{
34 + wait_for_pager(0);
35 }
36
37 static void wait_for_pager_signal(int signo)
38 {
29 - wait_for_pager();
39 + wait_for_pager(1);
40 sigchain_pop(signo);
41 raise(signo);
42 }
@@ -90,7 +100,7 @@ void setup_pager(void)
100
101 /* this makes sure that the parent terminates after the pager */
102 sigchain_push_common(wait_for_pager_signal);
93 - atexit(wait_for_pager);
103 + atexit(wait_for_pager_atexit);
104 }
105
106 int pager_in_use(void)
run-command.c
+17 -8
@@ -18,26 +18,27 @@ struct child_to_clean {
18 static struct child_to_clean *children_to_clean;
19 static int installed_child_cleanup_handler;
20
21 -static void cleanup_children(int sig)
21 +static void cleanup_children(int sig, int in_signal)
22 {
23 while (children_to_clean) {
24 struct child_to_clean *p = children_to_clean;
25 children_to_clean = p->next;
26 kill(p->pid, sig);
27 - free(p);
27 + if (!in_signal)
28 + free(p);
29 }
30 }
31
32 static void cleanup_children_on_signal(int sig)
33 {
33 - cleanup_children(sig);
34 + cleanup_children(sig, 1);
35 sigchain_pop(sig);
36 raise(sig);
37 }
38
39 static void cleanup_children_on_exit(void)
40 {
40 - cleanup_children(SIGTERM);
41 + cleanup_children(SIGTERM, 0);
42 }
43
44 static void mark_child_for_cleanup(pid_t pid)
@@ -232,7 +233,7 @@ static inline void set_cloexec(int fd)
233 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
234 }
235
235 -static int wait_or_whine(pid_t pid, const char *argv0)
236 +static int wait_or_whine(pid_t pid, const char *argv0, int in_signal)
237 {
238 int status, code = -1;
239 pid_t waiting;
@@ -240,6 +241,8 @@ static int wait_or_whine(pid_t pid, const char *argv0)
241
242 while ((waiting = waitpid(pid, &status, 0)) < 0 && errno == EINTR)
243 ; /* nothing */
244 + if (in_signal)
245 + return 0;
246
247 if (waiting < 0) {
248 failed_errno = errno;
@@ -450,7 +453,7 @@ fail_pipe:
453 * At this point we know that fork() succeeded, but execvp()
454 * failed. Errors have been reported to our stderr.
455 */
453 - wait_or_whine(cmd->pid, cmd->argv[0]);
456 + wait_or_whine(cmd->pid, cmd->argv[0], 0);
457 failed_errno = errno;
458 cmd->pid = -1;
459 }
@@ -549,12 +552,18 @@ fail_pipe:
552
553 int finish_command(struct child_process *cmd)
554 {
552 - int ret = wait_or_whine(cmd->pid, cmd->argv[0]);
555 + int ret = wait_or_whine(cmd->pid, cmd->argv[0], 0);
556 argv_array_clear(&cmd->args);
557 argv_array_clear(&cmd->env_array);
558 return ret;
559 }
560
561 +int finish_command_in_signal(struct child_process *cmd)
562 +{
563 + return wait_or_whine(cmd->pid, cmd->argv[0], 1);
564 +}
565 +
566 +
567 int run_command(struct child_process *cmd)
568 {
569 int code;
@@ -785,7 +794,7 @@ error:
794 int finish_async(struct async *async)
795 {
796 #ifdef NO_PTHREADS
788 - return wait_or_whine(async->pid, "child process");
797 + return wait_or_whine(async->pid, "child process", 0);
798 #else
799 void *ret = (void *)(intptr_t)(-1);
800
run-command.h
+1
@@ -50,6 +50,7 @@ void child_process_init(struct child_process *);
50
51 int start_command(struct child_process *);
52 int finish_command(struct child_process *);
53 +int finish_command_in_signal(struct child_process *);
54 int run_command(struct child_process *);
55
56 extern const char *find_hook(const char *name);