diff --git a/src/install/dracut-install.c b/src/install/dracut-install.c index dbbeb6cd1d..4f70a5ff44 100644 --- a/src/install/dracut-install.c +++ b/src/install/dracut-install.c @@ -356,23 +356,23 @@ static int library_install(const char *src, const char *lib) char *q, *clibdir; int r, ret = 0; - p = strdup(lib); - - r = dracut_install(p, p, false, false, true); + r = dracut_install(lib, lib, false, false, true); if (r != 0) - log_error("ERROR: failed to install '%s' for '%s'", p, src); + log_error("ERROR: failed to install '%s' for '%s'", lib, src); else - log_debug("Lib install: '%s'", p); + log_debug("Lib install: '%s'", lib); ret += r; /* also install lib.so for lib.so.* files */ - q = strstr(p, ".so."); + q = strstr(lib, ".so."); if (q) { - q[3] = '\0'; + p = strndup(lib, q - lib + 3); /* ignore errors for base lib symlink */ if (dracut_install(p, p, false, false, true) == 0) log_debug("Lib install: '%s'", p); + + free(p); } /* Also try to install the same library from one directory above @@ -384,7 +384,6 @@ static int library_install(const char *src, const char *lib) libc.so.6 (libc6,64bit, OS ABI: Linux 2.6.32) => /lib64/libc.so.6 */ - free(p); p = strdup(lib); pdir = dirname_malloc(p); @@ -629,14 +628,9 @@ static int resolve_deps(const char *src) /* Install "..hmac" file for FIPS self-checks */ static int hmac_install(const char *src, const char *dst, const char *hmacpath) { - _cleanup_free_ char *srcpath = strdup(src); - _cleanup_free_ char *dstpath = strdup(dst); _cleanup_free_ char *srchmacname = NULL; _cleanup_free_ char *dsthmacname = NULL; - if (!(srcpath && dstpath)) - return -ENOMEM; - size_t dlen = dir_len(src); if (endswith(src, ".hmac")) @@ -649,16 +643,14 @@ static int hmac_install(const char *src, const char *dst, const char *hmacpath) hmac_install(src, dst, "/lib64/hmaccalc"); } - srcpath[dlen] = '\0'; - dstpath[dir_len(dst)] = '\0'; if (hmacpath) { _asprintf(&srchmacname, "%s/%s.hmac", hmacpath, &src[dlen + 1]); _asprintf(&dsthmacname, "%s/%s.hmac", hmacpath, &src[dlen + 1]); } else { - _asprintf(&srchmacname, "%s/.%s.hmac", srcpath, &src[dlen + 1]); - _asprintf(&dsthmacname, "%s/.%s.hmac", dstpath, &src[dlen + 1]); + _asprintf(&srchmacname, "%.*s/.%s.hmac", (int)dlen, src, &src[dlen + 1]); + _asprintf(&dsthmacname, "%.*s/.%s.hmac", (int)dir_len(dst), dst, &src[dlen + 1]); } - log_debug("hmac cp '%s' '%s')", srchmacname, dsthmacname); + log_debug("hmac cp '%s' '%s'", srchmacname, dsthmacname); dracut_install(srchmacname, dsthmacname, false, false, true); return 0; } @@ -722,11 +714,11 @@ static int dracut_mkdir(const char *src) return 1; } } else if (errno != ENOENT) { - log_error("ERROR: stat '%s': %s", parent, strerror(errno)); + log_error("ERROR: stat '%s': %m", parent); return 1; } else { if (mkdir(parent, 0755) < 0) { - log_error("ERROR: mkdir '%s': %s", parent, strerror(errno)); + log_error("ERROR: mkdir '%s': %m", parent); return 1; } } @@ -742,7 +734,7 @@ static int dracut_mkdir(const char *src) static int dracut_install(const char *orig_src, const char *orig_dst, bool isdir, bool resolvedeps, bool hashdst) { - struct stat sb, db; + struct stat sb; _cleanup_free_ char *fullsrcpath = NULL; _cleanup_free_ char *fulldstpath = NULL; _cleanup_free_ char *fulldstdir = NULL; @@ -752,25 +744,24 @@ static int dracut_install(const char *orig_src, const char *orig_dst, bool isdir mode_t src_mode = 0; bool dst_exists = true; char *i = NULL; - _cleanup_free_ char *src; - _cleanup_free_ char *dst; + const char *src, *dst; if (sysrootdirlen) { if (strncmp(orig_src, sysrootdir, sysrootdirlen) == 0) { - src = strdup(orig_src + sysrootdirlen); + src = orig_src + sysrootdirlen; fullsrcpath = strdup(orig_src); } else { - src = strdup(orig_src); + src = orig_src; _asprintf(&fullsrcpath, "%s%s", sysrootdir, src); } if (strncmp(orig_dst, sysrootdir, sysrootdirlen) == 0) - dst = strdup(orig_dst + sysrootdirlen); + dst = orig_dst + sysrootdirlen; else - dst = strdup(orig_dst); + dst = orig_dst; } else { - src = strdup(orig_src); + src = orig_src; fullsrcpath = strdup(src); - dst = strdup(orig_dst); + dst = orig_dst; } log_debug("dracut_install('%s', '%s', %d, %d, %d)", src, dst, isdir, resolvedeps, hashdst); @@ -826,7 +817,7 @@ static int dracut_install(const char *orig_src, const char *orig_dst, bool isdir return 1; } - ret = stat(fulldstdir, &db); + ret = access(fulldstdir, F_OK); if (ret < 0) { _cleanup_free_ char *dname = NULL; @@ -886,12 +877,12 @@ static int dracut_install(const char *orig_src, const char *orig_dst, bool isdir return 1; } - if (lstat(abspath, &sb) != 0) { + if (faccessat(AT_FDCWD, abspath, F_OK, AT_SYMLINK_NOFOLLOW) != 0) { log_debug("lstat '%s': %m", abspath); return 1; } - if (lstat(fulldstpath, &sb) != 0) { + if (faccessat(AT_FDCWD, fulldstpath, F_OK, AT_SYMLINK_NOFOLLOW) != 0) { _cleanup_free_ char *absdestpath = NULL; _asprintf(&absdestpath, "%s/%s", destrootdir, @@ -1082,10 +1073,10 @@ static int parse_argv(int argc, char *argv[]) arg_module = true; break; case 'D': - destrootdir = strdup(optarg); + destrootdir = optarg; break; case 'r': - sysrootdir = strdup(optarg); + sysrootdir = optarg; sysrootdirlen = strlen(sysrootdir); break; case 'p': @@ -1124,10 +1115,10 @@ static int parse_argv(int argc, char *argv[]) arg_mod_filter_noname = true; break; case 'L': - logdir = strdup(optarg); + logdir = optarg; break; case ARG_KERNELDIR: - kerneldir = strdup(optarg); + kerneldir = optarg; break; case ARG_FIRMWAREDIRS: firmwaredirs = strv_split(optarg, ":"); @@ -1239,7 +1230,6 @@ static char **find_binary(const char *src) char *newsrc = NULL; STRV_FOREACH(q, pathdirs) { - struct stat sb; char *fullsrcpath; _asprintf(&newsrc, "%s/%s", *q, src); @@ -1252,8 +1242,8 @@ static char **find_binary(const char *src) continue; } - if (lstat(fullsrcpath, &sb) != 0) { - log_debug("stat(%s) != 0", fullsrcpath); + if (faccessat(AT_FDCWD, fullsrcpath, F_OK, AT_SYMLINK_NOFOLLOW) != 0) { + log_debug("lstat(%s) != 0", fullsrcpath); free(newsrc); newsrc = NULL; free(fullsrcpath); @@ -1336,8 +1326,7 @@ static int install_all(int argc, char **argv) } else { if (strchr(argv[i], '*') == NULL) { - _cleanup_free_ char *dest = strdup(argv[i]); - ret = dracut_install(argv[i], dest, arg_createdir, arg_resolvedeps, true); + ret = dracut_install(argv[i], argv[i], arg_createdir, arg_resolvedeps, true); } else { _cleanup_free_ char *realsrc = NULL; _cleanup_globfree_ glob_t globbuf; @@ -1349,11 +1338,9 @@ static int install_all(int argc, char **argv) size_t j; for (j = 0; j < globbuf.gl_pathc; j++) { - char *dest = strdup(globbuf.gl_pathv[j] + sysrootdirlen); - ret |= - dracut_install(globbuf.gl_pathv[j] + sysrootdirlen, dest, - arg_createdir, arg_resolvedeps, true); - free(dest); + ret |= dracut_install(globbuf.gl_pathv[j] + sysrootdirlen, + globbuf.gl_pathv[j] + sysrootdirlen, + arg_createdir, arg_resolvedeps, true); } } } @@ -1371,9 +1358,8 @@ static int install_firmware_fullpath(const char *fwpath) { const char *fw = fwpath; _cleanup_free_ char *fwpath_compressed = NULL; - struct stat sb; int ret; - if (stat(fwpath, &sb) != 0) { + if (access(fwpath, F_OK) != 0) { _asprintf(&fwpath_compressed, "%s.zst", fwpath); if (access(fwpath_compressed, F_OK) != 0) { strcpy(fwpath_compressed + strlen(fwpath) + 1, "xz"); @@ -1416,12 +1402,11 @@ static int install_firmware(struct kmod_module *mod) ret = -1; STRV_FOREACH(q, firmwaredirs) { _cleanup_free_ char *fwpath = NULL; - struct stat sb; _asprintf(&fwpath, "%s/%s", *q, value); - if ((strstr(value, "*") != 0 || strstr(value, "?") != 0 || strstr(value, "[") != 0) - && stat(fwpath, &sb) != 0) { + if (strpbrk(value, "*?[") != NULL + && access(fwpath, F_OK) != 0) { size_t i; _cleanup_globfree_ glob_t globbuf; @@ -2027,7 +2012,6 @@ int main(int argc, char **argv) log_error("Environment DESTROOTDIR or argument -D is not set!"); usage(EXIT_FAILURE); } - destrootdir = strdup(destrootdir); } if (strcmp(destrootdir, "/") == 0) { @@ -2036,13 +2020,11 @@ int main(int argc, char **argv) } i = destrootdir; - destrootdir = realpath(destrootdir, NULL); - if (!destrootdir) { + if (!(destrootdir = realpath(i, NULL))) { log_error("Environment DESTROOTDIR or argument -D is set to '%s': %m", i); r = EXIT_FAILURE; - goto finish; + goto finish2; } - free(i); items = hashmap_new(string_hash_func, string_compare_func); items_failed = hashmap_new(string_hash_func, string_compare_func); @@ -2050,7 +2032,7 @@ int main(int argc, char **argv) if (!items || !items_failed || !modules_loaded) { log_error("Out of memory"); r = EXIT_FAILURE; - goto finish; + goto finish1; } if (logdir) { @@ -2060,7 +2042,7 @@ int main(int argc, char **argv) if (logfile_f == NULL) { log_error("Could not open %s for logging: %m", logfile); r = EXIT_FAILURE; - goto finish; + goto finish1; } } @@ -2093,7 +2075,9 @@ int main(int argc, char **argv) if (arg_optional) r = EXIT_SUCCESS; -finish: +finish1: + free(destrootdir); +finish2: if (logfile_f) fclose(logfile_f); @@ -2110,7 +2094,6 @@ int main(int argc, char **argv) hashmap_free(items_failed); hashmap_free(modules_loaded); - free(destrootdir); strv_free(firmwaredirs); strv_free(pathdirs); return r; diff --git a/src/install/hashmap.lo b/src/install/hashmap.lo deleted file mode 100644 index c7260f22eb..0000000000 --- a/src/install/hashmap.lo +++ /dev/null @@ -1,12 +0,0 @@ -# src/shared/hashmap.lo - a libtool object file -# Generated by libtool (GNU libtool) 2.4.2 -# -# Please DO NOT delete this file! -# It is necessary for linking the library. - -# Name of the PIC object. -pic_object='.libs/hashmap.o' - -# Name of the non-PIC object -non_pic_object='hashmap.o' -