From 6172788a3f90962b42564b5248f540b4cb729470 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Sun, 9 Jul 2017 23:31:47 -0400 Subject: [PATCH] cryptsetup-generator: add a helper utility to create symlinks It seems that there's a common pattern among the various generators. Let's add a helper function for it and make use of it in cryptsetup-generator. This fixes a bunch of theoretical memleaks in error paths, since *to wasn't generally freed properly. Not thath it matters. (cherry picked from commit b559616f2321643c5194b474d39a722cefaf6059) (cherry picked from commit ea8cb69ee23cd67ef45ca34f1b192c9adb5fa878) --- src/cryptsetup/cryptsetup-generator.c | 53 ++++++++++------------------------- src/shared/generator.c | 15 ++++++++++ src/shared/generator.h | 2 ++ 3 files changed, 32 insertions(+), 38 deletions(-) diff --git a/src/cryptsetup/cryptsetup-generator.c b/src/cryptsetup/cryptsetup-generator.c index f737f82b55..f10e9fdc24 100644 --- a/src/cryptsetup/cryptsetup-generator.c +++ b/src/cryptsetup/cryptsetup-generator.c @@ -58,11 +58,11 @@ static int create_disk( const char *password, const char *options) { - _cleanup_free_ char *p = NULL, *n = NULL, *d = NULL, *u = NULL, *to = NULL, *e = NULL, + _cleanup_free_ char *p = NULL, *n = NULL, *d = NULL, *u = NULL, *e = NULL, *filtered = NULL; _cleanup_fclose_ FILE *f = NULL; + const char *dmname; bool noauto, nofail, tmp, swap; - char *from; int r; assert(name); @@ -120,7 +120,7 @@ static int create_disk( if (password) { if (STR_IN_SET(password, "/dev/urandom", "/dev/random", "/dev/hw_random")) fputs("After=systemd-random-seed.service\n", f); - else if (!streq(password, "-") && !streq(password, "none")) { + else if (!STR_IN_SET(password, "-", "none")) { _cleanup_free_ char *uu; uu = fstab_node_to_udev_node(password); @@ -186,46 +186,23 @@ static int create_disk( if (r < 0) return log_error_errno(r, "Failed to write file %s: %m", p); - from = strjoina("../", n); - if (!noauto) { - - to = strjoin(arg_dest, "/", d, ".wants/", n); - if (!to) - return log_oom(); - - mkdir_parents_label(to, 0755); - if (symlink(from, to) < 0) - return log_error_errno(errno, "Failed to create symlink %s: %m", to); - - free(to); - if (!nofail) - to = strjoin(arg_dest, "/cryptsetup.target.requires/", n); - else - to = strjoin(arg_dest, "/cryptsetup.target.wants/", n); - if (!to) - return log_oom(); - - mkdir_parents_label(to, 0755); - if (symlink(from, to) < 0) - return log_error_errno(errno, "Failed to create symlink %s: %m", to); + r = generator_add_symlink(arg_dest, d, "wants", n); + if (r < 0) + return r; + + r = generator_add_symlink(arg_dest, "cryptsetup.target", + nofail ? "wants" : "requires", n); + if (r < 0) + return r; } - free(to); - to = strjoin(arg_dest, "/dev-mapper-", e, ".device.requires/", n); - if (!to) - return log_oom(); - - mkdir_parents_label(to, 0755); - if (symlink(from, to) < 0) - return log_error_errno(errno, "Failed to create symlink %s: %m", to); + dmname = strjoina("dev-mapper-", e, ".device"); + r = generator_add_symlink(arg_dest, dmname, "requires", n); + if (r < 0) + return r; if (!noauto && !nofail) { - _cleanup_free_ char *dmname; - dmname = strjoin("dev-mapper-", e, ".device"); - if (!dmname) - return log_oom(); - r = write_drop_in(arg_dest, dmname, 90, "device-timeout", "# Automatically generated by systemd-cryptsetup-generator \n\n" "[Unit]\nJobTimeoutSec=0"); diff --git a/src/shared/generator.c b/src/shared/generator.c index 9a069b2f97..c01e9cb519 100644 --- a/src/shared/generator.c +++ b/src/shared/generator.c @@ -37,6 +37,21 @@ #include "unit-name.h" #include "util.h" +int generator_add_symlink(const char *root, const char *dst, const char *dep_type, const char *src) { + /* Adds a symlink from ..d/ to ../ */ + + const char *from, *to; + + from = strjoina("../", src); + to = strjoina(root, "/", dst, ".", dep_type, "/", src); + + mkdir_parents_label(to, 0755); + if (symlink(from, to) < 0) + return log_error_errno(errno, "Failed to create symlink \"%s\": %m", to); + + return 0; +} + static int write_fsck_sysroot_service(const char *dir, const char *what) { _cleanup_free_ char *device = NULL, *escaped = NULL; _cleanup_fclose_ FILE *f = NULL; diff --git a/src/shared/generator.h b/src/shared/generator.h index a6017c1b76..7bafda03f2 100644 --- a/src/shared/generator.h +++ b/src/shared/generator.h @@ -21,6 +21,8 @@ #include +int generator_add_symlink(const char *root, const char *dst, const char *dep_type, const char *src); + int generator_write_fsck_deps( FILE *f, const char *dir,