Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dracut-install cleanup #1796

Merged
merged 7 commits into from
Dec 28, 2022
103 changes: 43 additions & 60 deletions src/install/dracut-install.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand Down Expand Up @@ -629,14 +628,9 @@ static int resolve_deps(const char *src)
/* Install ".<filename>.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"))
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
}
}
Expand All @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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':
Expand Down Expand Up @@ -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, ":");
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -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);
}
}
}
Expand All @@ -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");
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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) {
Expand All @@ -2036,21 +2020,19 @@ 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);

if (!items || !items_failed || !modules_loaded) {
log_error("Out of memory");
r = EXIT_FAILURE;
goto finish;
goto finish1;
}

if (logdir) {
Expand All @@ -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;
}
}

Expand Down Expand Up @@ -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);

Expand All @@ -2110,7 +2094,6 @@ int main(int argc, char **argv)
hashmap_free(items_failed);
hashmap_free(modules_loaded);

free(destrootdir);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still need free() if realpath(i, NULL) succeeds.

diff --git a/src/install/dracut-install.c b/src/install/dracut-install.c
index 2f1e00dc..9a9733d9 100644
--- a/src/install/dracut-install.c
+++ b/src/install/dracut-install.c
@@ -2028,7 +2028,7 @@ int main(int argc, char **argv)
         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;
         }

         items = hashmap_new(string_hash_func, string_compare_func);
@@ -2037,7 +2037,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) {
@@ -2047,7 +2047,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;
                 }
         }

@@ -2080,7 +2080,9 @@ int main(int argc, char **argv)
         if (arg_optional)
                 r = EXIT_SUCCESS;

-finish:
+finish1:
+        free(destrootdir);
+finish2:
         if (logfile_f)
                 fclose(logfile_f);

Too much? :)

strv_free(firmwaredirs);
strv_free(pathdirs);
return r;
Expand Down
12 changes: 0 additions & 12 deletions src/install/hashmap.lo

This file was deleted.