diff options
author | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2017-07-09 23:31:47 -0400 |
---|---|---|
committer | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2017-07-10 00:16:36 -0400 |
commit | b559616f2321643c5194b474d39a722cefaf6059 (patch) | |
tree | d4cdc4049476a734416910813b192d55b6c0703c | |
parent | e1bc3004ab8ef0266c71516a28e92e3ead08fe5a (diff) | |
download | systemd-b559616f2321643c5194b474d39a722cefaf6059.tar.gz |
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.
-rw-r--r-- | src/cryptsetup/cryptsetup-generator.c | 51 | ||||
-rw-r--r-- | src/shared/generator.c | 15 | ||||
-rw-r--r-- | src/shared/generator.h | 2 |
3 files changed, 31 insertions, 37 deletions
diff --git a/src/cryptsetup/cryptsetup-generator.c b/src/cryptsetup/cryptsetup-generator.c index b58b6db7c9..3fdf65be65 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); @@ -121,7 +121,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); @@ -187,46 +187,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) { + r = generator_add_symlink(arg_dest, d, "wants", n); + if (r < 0) + return r; - 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, "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 6a78ebbda7..0a829dc9af 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 <dst>.<dep_type>.d/ to ../<src> */ + + 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 825d934c8e..e70016839f 100644 --- a/src/shared/generator.h +++ b/src/shared/generator.h @@ -21,6 +21,8 @@ #include <stdio.h> +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, |