215
CHILD_ERR_CHDIR,
216
CHILD_ERR_DUP2,
217
CHILD_ERR_CLOSE,
218
+ CHILD_ERR_SIGPROCMASK,
219
CHILD_ERR_ENOENT,
220
CHILD_ERR_SILENT,
221
CHILD_ERR_ERRNO
304
case CHILD_ERR_CLOSE:
305
error_errno("close() in child failed");
306
break;
307
+ case CHILD_ERR_SIGPROCMASK:
308
+ error_errno("sigprocmask failed restoring signals");
309
+ break;
310
case CHILD_ERR_ENOENT:
311
error_errno("cannot run %s", cmd->argv[0]);
312
break;
402
strbuf_release(&key);
403
return childenv;
404
}
405
+
406
+struct atfork_state {
407
+#ifndef NO_PTHREADS
408
+ int cs;
409
#endif
410
+ sigset_t old;
411
+};
412
+
413
+#ifndef NO_PTHREADS
414
+static void bug_die(int err, const char *msg)
415
+{
416
+ if (err) {
417
+ errno = err;
418
+ die_errno("BUG: %s", msg);
419
+ }
420
+}
421
+#endif
422
+
423
+static void atfork_prepare(struct atfork_state *as)
424
+{
425
+ sigset_t all;
426
+
427
+ if (sigfillset(&all))
428
+ die_errno("sigfillset");
429
+#ifdef NO_PTHREADS
430
+ if (sigprocmask(SIG_SETMASK, &all, &as->old))
431
+ die_errno("sigprocmask");
432
+#else
433
+ bug_die(pthread_sigmask(SIG_SETMASK, &all, &as->old),
434
+ "blocking all signals");
435
+ bug_die(pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &as->cs),
436
+ "disabling cancellation");
437
+#endif
438
+}
439
+
440
+static void atfork_parent(struct atfork_state *as)
441
+{
442
+#ifdef NO_PTHREADS
443
+ if (sigprocmask(SIG_SETMASK, &as->old, NULL))
444
+ die_errno("sigprocmask");
445
+#else
446
+ bug_die(pthread_setcancelstate(as->cs, NULL),
447
+ "re-enabling cancellation");
448
+ bug_die(pthread_sigmask(SIG_SETMASK, &as->old, NULL),
449
+ "restoring signal mask");
450
+#endif
451
+}
452
+#endif /* GIT_WINDOWS_NATIVE */
453
454
static inline void set_cloexec(int fd)
455
{
574
char **childenv;
575
struct argv_array argv = ARGV_ARRAY_INIT;
576
struct child_err cerr;
577
+ struct atfork_state as;
578
579
if (pipe(notify_pipe))
580
notify_pipe[0] = notify_pipe[1] = -1;
588
589
prepare_cmd(&argv, cmd);
590
childenv = prep_childenv(cmd->env);
591
+ atfork_prepare(&as);
592
593
/*
594
* NOTE: In order to prevent deadlocking when using threads special
602
cmd->pid = fork();
603
failed_errno = errno;
604
if (!cmd->pid) {
605
+ int sig;
606
/*
607
* Ensure the default die/error/warn routines do not get
608
* called, they can take stdio locks and malloc.
650
if (cmd->dir && chdir(cmd->dir))
651
child_die(CHILD_ERR_CHDIR);
652
653
+ /*
654
+ * restore default signal handlers here, in case
655
+ * we catch a signal right before execve below
656
+ */
657
+ for (sig = 1; sig < NSIG; sig++) {
658
+ /* ignored signals get reset to SIG_DFL on execve */
659
+ if (signal(sig, SIG_DFL) == SIG_IGN)
660
+ signal(sig, SIG_IGN);
661
+ }
662
+
663
+ if (sigprocmask(SIG_SETMASK, &as.old, NULL) != 0)
664
+ child_die(CHILD_ERR_SIGPROCMASK);
665
+
666
/*
667
* Attempt to exec using the command and arguments starting at
668
* argv.argv[1]. argv.argv[0] contains SHELL_PATH which will
683
child_die(CHILD_ERR_ERRNO);
684
}
685
}
686
+ atfork_parent(&as);
687
if (cmd->pid < 0)
688
error_errno("cannot fork() for %s", cmd->argv[0]);
689
else if (cmd->clean_on_exit)