commit 7b2deddbb0ef350e189fe42c025b07c943aedc4c Author: Raul E Rangel Date: Thu Jul 25 15:49:52 2019 -0600 Kconfig: Write tmp files into same directory as target files This removes the need for COREBOOT_BUILD_DIR in Kconfig. Since the original files will be replaced with the tmp file, the parent directory already needs to be writable. Before this change, the tmp files would be created in the CWD (src) if COREBOOT_BUILD_DIR was not specified. BUG=b:112267918 TEST=emerge-grunt coreboot and verified no tmp files were created in the src directory. Change-Id: Icdaf2ff3dd1ec98813b75ef55b96e38e1ca19ec7 Signed-off-by: Raul E Rangel Reviewed-on: https://review.coreboot.org/c/coreboot/+/34244 Reviewed-by: Martin Roth Tested-by: build bot (Jenkins) Index: kconfig/confdata.c =================================================================== --- kconfig.orig/confdata.c +++ kconfig/confdata.c @@ -880,6 +880,16 @@ next_menu: return 0; } + +static int conf_mktemp(const char *path, char *tmpfile) +{ + if (snprintf(tmpfile, PATH_MAX, "%s.tmp.XXXXXX", path) >= PATH_MAX) { + errno = EOVERFLOW; + return -1; + } + return mkstemp(tmpfile); +} + int conf_write(const char *name) { FILE *out; @@ -1001,7 +1011,14 @@ static int conf_write_dep(const char *na struct file *file; FILE *out; - out = fopen("..config.tmp", "w"); + if (make_parent_dir(name)) + return 1; + char filename[PATH_MAX]; + int fd = conf_mktemp(name, filename); + if (fd == -1) + return 1; + + out = fdopen(fd, "w"); if (!out) return 1; fprintf(out, "deps_config := \\\n"); @@ -1019,9 +1036,7 @@ static int conf_write_dep(const char *na fprintf(out, "\n$(deps_config): ;\n"); fclose(out); - if (make_parent_dir(name)) - return 1; - rename("..config.tmp", name); + rename(filename, name); return 0; } @@ -1117,11 +1132,26 @@ int conf_write_autoconf(int overwrite) if (conf_touch_deps()) return 1; - out = fopen(".tmpconfig", "w"); + if (make_parent_dir(autoconf_name)) + return 1; + char filename[PATH_MAX]; + int fd = conf_mktemp(autoconf_name, filename); + if (fd == -1) + return 1; + out = fdopen(fd, "w"); if (!out) return 1; - out_h = fopen(".tmpconfig.h", "w"); + name = getenv("KCONFIG_AUTOHEADER"); + if (!name) + name = "include/generated/autoconf.h"; + if (make_parent_dir(name)) + return 1; + char filename_h[PATH_MAX]; + int fd_h = conf_mktemp(name, filename_h); + if (fd_h == -1) + return 1; + out_h = fdopen(fd_h, "w"); if (!out_h) { fclose(out); return 1; @@ -1144,21 +1174,14 @@ int conf_write_autoconf(int overwrite) fclose(out); fclose(out_h); - name = getenv("KCONFIG_AUTOHEADER"); - if (!name) - name = "include/generated/autoconf.h"; - if (make_parent_dir(name)) - return 1; - if (rename(".tmpconfig.h", name)) + if (rename(filename_h, name)) return 1; - if (make_parent_dir(autoconf_name)) - return 1; /* * This must be the last step, kbuild has a dependency on auto.conf * and this marks the successful completion of the previous steps. */ - if (rename(".tmpconfig", autoconf_name)) + if (rename(filename, autoconf_name)) return 1; return 0;