[PATCH v2] build system: fix parallel building issue

Michael Olbrich m.olbrich at pengutronix.de
Thu Apr 12 08:07:34 UTC 2018


The files generated by the include/config/MARKER target are in the
dependency list for applets/applet_tables.
If applets/applet_tables is created first during applets_dir then it will
be created again later as part of $(busybox-dirs).
As a result include/applet_tables.h is created again. This time while other
build commands may need it.

Let include/config/MARKER depend on applets_dir to avoid this particular
race condition and create the header files atomically to ensure that the
compiler never sees incomplete files.

Signed-off-by: Michael Olbrich <m.olbrich at pengutronix.de>
---

I'm not really happy with this patch, but I don't have a better idea
either. It solves the immediate problem and should make things more
robust.

Regards,
Michael

 Makefile                |  2 +-
 applets/applet_tables.c | 16 ++++++++++++++--
 2 files changed, 15 insertions(+), 3 deletions(-)

diff --git a/Makefile b/Makefile
index 03a57adf3204..83f67bd46b50 100644
--- a/Makefile
+++ b/Makefile
@@ -851,7 +851,7 @@ quiet_cmd_gen_common_bufsiz = GEN     include/common_bufsiz.h
 quiet_cmd_split_autoconf   = SPLIT   include/autoconf.h -> include/config/*
       cmd_split_autoconf   = scripts/basic/split-include include/autoconf.h include/config
 #bbox# piggybacked generation of few .h files
-include/config/MARKER: scripts/basic/split-include include/autoconf.h
+include/config/MARKER: scripts/basic/split-include include/autoconf.h applets_dir
 	$(call cmd,split_autoconf)
 	$(call cmd,gen_bbconfigopts)
 	$(call cmd,gen_common_bufsiz)
diff --git a/applets/applet_tables.c b/applets/applet_tables.c
index ef911a43b36d..e3d10c83f1b9 100644
--- a/applets/applet_tables.c
+++ b/applets/applet_tables.c
@@ -10,6 +10,7 @@
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
+#include <limits.h>
 #include <stdlib.h>
 #include <string.h>
 #include <stdio.h>
@@ -61,6 +62,7 @@ static int str_isalnum_(const char *s)
 int main(int argc, char **argv)
 {
 	int i, j;
+	char tmp1[PATH_MAX], tmp2[PATH_MAX];
 
 	// In find_applet_by_name(), before linear search, narrow it down
 	// by looking at N "equidistant" names. With ~350 applets:
@@ -84,7 +86,8 @@ int main(int argc, char **argv)
 
 	if (!argv[1])
 		return 1;
-	i = open(argv[1], O_WRONLY | O_TRUNC | O_CREAT, 0666);
+	snprintf(tmp1, PATH_MAX, "%s.%u.new", argv[1], (int) getpid());
+	i = open(tmp1, O_WRONLY | O_TRUNC | O_CREAT, 0666);
 	if (i < 0)
 		return 1;
 	dup2(i, 1);
@@ -209,12 +212,21 @@ int main(int argc, char **argv)
 //			fclose(fp);
 //		}
 //		if (strcmp(line_old, line_new) != 0) {
-			fp = fopen(argv[2], "w");
+			snprintf(tmp2, PATH_MAX, "%s.%u.new", argv[2], (int) getpid());
+			fp = fopen(tmp2, "w");
 			if (!fp)
 				return 1;
 			fputs(line_new, fp);
+			if (fclose(fp))
+				return 1;
 //		}
 	}
 
+	if (fclose(stdout))
+		return 1;
+	if (rename(tmp1, argv[1]))
+		return 1;
+	if (rename(tmp2, argv[2]))
+		return 1;
 	return 0;
 }
-- 
2.17.0



More information about the busybox mailing list