From 7b2deddbb0ef350e189fe42c025b07c943aedc4c Mon Sep 17 00:00:00 2001 From: Raul E Rangel Date: Thu, 25 Jul 2019 15:49:52 -0600 Subject: 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) --- util/kconfig/confdata.c | 112 +++++++++++++++++++++++++++--------------------- 1 file changed, 63 insertions(+), 49 deletions(-) (limited to 'util') diff --git a/util/kconfig/confdata.c b/util/kconfig/confdata.c index 3c0818aaae..fc76e0e880 100644 --- a/util/kconfig/confdata.c +++ b/util/kconfig/confdata.c @@ -764,6 +764,16 @@ next_menu: return 0; } + +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; @@ -806,7 +816,7 @@ int conf_write(const char *name) sprintf(newname, "%s%s", dirname, basename); env = getenv("KCONFIG_OVERWRITECONFIG"); if (!env || !*env) { - sprintf(tmpname, "%s.tmpconfig.%d", dirname, (int)getpid()); + conf_mktemp(newname, tmpname); out = fopen(tmpname, "w"); } else { *tmpname = 0; @@ -991,7 +1001,6 @@ out: int conf_write_autoconf(void) { struct symbol *sym; - const char *name; FILE *out, *tristate, *out_h; int i; int print_negatives; @@ -1008,49 +1017,41 @@ int conf_write_autoconf(void) if (conf_split_config()) return 1; - char *tmpconfig_name = malloc(PATH_MAX); - if (getenv("COREBOOT_BUILD_DIR")) { - sprintf(tmpconfig_name, "%s/.tmpconfig.XXXXXX", - getenv("COREBOOT_BUILD_DIR")); - } else { - tmpconfig_name = strdup(".tmpconfig.XXXXXX"); - } - if ((i = mkstemp(tmpconfig_name)) == -1) - return 1; + char tmpconfig_name[PATH_MAX]; + const char *config_name = conf_get_autoconfig_name(); + + i = conf_mktemp(config_name, tmpconfig_name); + if (i == -1) + goto error_auto_conf_cmd_tmp; out = fdopen(i, "w"); if (!out) - return 1; + goto error_auto_conf_cmd_open; + + char tmpconfig_triname[PATH_MAX]; + const char *config_triname = getenv("KCONFIG_TRISTATE"); + if (!config_triname) + config_triname = "include/config/tristate.conf"; + + i = conf_mktemp(config_triname, tmpconfig_triname); + if (i == -1) + goto error_tristate_tmp; - char *tmpconfig_triname = malloc(PATH_MAX); - if (getenv("COREBOOT_BUILD_DIR")) { - sprintf(tmpconfig_triname, "%s/.tmpconfig_tristate.XXXXXX", - getenv("COREBOOT_BUILD_DIR")); - } else { - tmpconfig_triname = strdup(".tmpconfig_tristate.XXXXXX"); - } - if ((i = mkstemp(tmpconfig_triname)) == -1) - return 1; tristate = fdopen(i, "w"); - if (!tristate) { - fclose(out); - return 1; - } + if (!tristate) + goto error_tristate_open; + + char tmpconfig_h[PATH_MAX]; + const char *config_h = getenv("KCONFIG_AUTOHEADER"); + if (!config_h) + config_h = "include/generated/autoconf.h"; + + i = conf_mktemp(config_h, tmpconfig_h); + if (i == -1) + goto error_auto_conf_h_tmp; - char *tmpconfig_h = malloc(PATH_MAX); - if (getenv("COREBOOT_BUILD_DIR")) { - sprintf(tmpconfig_h, "%s/.tmpconfig_tristate.XXXXXX", - getenv("COREBOOT_BUILD_DIR")); - } else { - tmpconfig_h = strdup(".tmpconfig_tristate.XXXXXX"); - } - if ((i = mkstemp(tmpconfig_h)) == -1) - return 1; out_h = fdopen(i, "w"); - if (!out_h) { - fclose(out); - fclose(tristate); - return 1; - } + if (!out_h) + goto error_auto_conf_h_open; conf_write_heading(out, &kconfig_printer_cb, NULL); @@ -1084,25 +1085,38 @@ int conf_write_autoconf(void) fclose(tristate); fclose(out_h); - name = getenv("KCONFIG_AUTOHEADER"); - if (!name) - name = "include/generated/autoconf.h"; - if (rename(tmpconfig_h, name)) + if (rename(tmpconfig_h, config_h)) return 1; - name = getenv("KCONFIG_TRISTATE"); - if (!name) - name = "include/config/tristate.conf"; - if (rename(tmpconfig_triname, name)) + + if (rename(tmpconfig_triname, config_triname)) return 1; - name = conf_get_autoconfig_name(); + /* * 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_name, name)) + if (rename(tmpconfig_name, config_name)) return 1; return 0; + +error_auto_conf_h_open: + unlink(tmpconfig_h); + +error_auto_conf_h_tmp: + fclose(tristate); + +error_tristate_open: + unlink(tmpconfig_triname); + +error_tristate_tmp: + fclose(out); + +error_auto_conf_cmd_open: + unlink(tmpconfig_name); + +error_auto_conf_cmd_tmp: + return 1; } static int sym_change_count; -- cgit v1.2.3