From d603b2e6ac0f4c2ae69a428e5c63349c289fe1a6 Mon Sep 17 00:00:00 2001
From: Paul Smith <psmith@gnu.org>
Date: Sat, 15 Sep 2018 14:48:57 -0400
Subject: [PATCH 101/104] * src/main.c (main): Set jobserver permissions before
 re-execing

Fixes an issue seen in the Linux kernel build system, reported by
Masahiro Yamada <yamada.masahiro@socionext.com>.  Fix suggested
on the bug-make mailing list by Mike Shal <marfey@gmail.com>.
* tests/scripts/features/parallelism: Add a test to verify this.
---
 main.c                         |  6 +++++
 tests/scripts/features/parallelism | 35 ++++++++++++++++++++++++++++++
 2 files changed, 41 insertions(+)

diff --git a/main.c b/main.c
index eed4e81..bcaeab7 100644
--- a/main.c
+++ b/main.c
@@ -2461,6 +2461,9 @@ main (int argc, char **argv, char **envp)
           fflush (stdout);
           fflush (stderr);
 
+          /* The exec'd "child" will be another make, of course.  */
+          jobserver_pre_child(1);
+
 #ifdef _AMIGA
           exec_command (nargv);
           exit (0);
@@ -2491,6 +2494,9 @@ main (int argc, char **argv, char **envp)
 #endif
           exec_command ((char **)nargv, environ);
 #endif
+
+          /* We shouldn't get here but just in case.  */
+          jobserver_post_child(1);
           free (aargv);
           break;
         }
diff --git a/tests/scripts/features/parallelism b/tests/scripts/features/parallelism
index 4257947..07fb758 100644
--- a/tests/scripts/features/parallelism
+++ b/tests/scripts/features/parallelism
@@ -188,6 +188,41 @@ file2: file1 ; @touch $@
 
 rmfiles('file1', 'file2', 'file3', 'file4');
 
+# Ensure that the jobserver is preserved across make re-exec.
+
+run_make_test(q!
+all: one two
+one: ;@ #PERL# jhelp.pl -w TWO -f ONE
+two: ;@ #PERL# jhelp.pl -f TWO
+include fff1.mk
+fff1.mk: ; touch $@
+!,
+              '-j2', "touch fff1.mk\nTWO\nONE\n");
+
+unlink('fff1.mk', 'ONE', 'TWO');
+
+# Test if a sub-make needs to re-exec and the makefile is built via
+# sub-make.  Reported by Masahiro Yamada <yamada.masahiro@socionext.com>
+
+run_make_test(q!
+all: ; @$(MAKE) -f #MAKEFILE# recurse
+
+recurse: one two ; @echo $@
+one: ;@ #PERL# jhelp.pl -w TWO -f ONE
+two: ;@ #PERL# jhelp.pl -f TWO
+
+mkinclude: ; touch fff1.mk
+
+ifeq ($(MAKECMDGOALS),recurse)
+include fff1.mk
+fff1.mk: ; @$(MAKE) -f #MAKEFILE# mkinclude
+endif
+!,
+              '--no-print-directory -j2', "touch fff1.mk\nTWO\nONE\nrecurse\n");
+
+unlink('fff1.mk', 'ONE', 'TWO');
+
+
 # Make sure that all jobserver FDs are closed if we need to re-exec the
 # master copy.
 #
-- 
2.19.1

