X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Fcryptsetup-generator.c;h=db8ebdfb1882cfe00301af142f4908bcaee9e779;hp=00f48009edd52be80c040c285022a49a71cb74f5;hb=e025b4c306d4b0895786839ebbb934188edc6e61;hpb=2a796654b9a1f84962e5dafbcf171dcc22742c99 diff --git a/src/cryptsetup-generator.c b/src/cryptsetup-generator.c index 00f48009e..db8ebdfb1 100644 --- a/src/cryptsetup-generator.c +++ b/src/cryptsetup-generator.c @@ -33,6 +33,11 @@ static bool has_option(const char *haystack, const char *needle) { const char *f = haystack; size_t l; + assert(needle); + + if (!haystack) + return false; + l = strlen(needle); while ((f = strstr(f, needle))) { @@ -42,7 +47,7 @@ static bool has_option(const char *haystack, const char *needle) { continue; } - if (f[l] != 0 && f[l] == ',') { + if (f[l] != 0 && f[l] != ',') { f++; continue; } @@ -62,10 +67,14 @@ static int create_disk( char *p = NULL, *n = NULL, *d = NULL, *u = NULL, *from = NULL, *to = NULL, *e = NULL; int r; FILE *f = NULL; + bool noauto, nofail; assert(name); assert(device); + noauto = has_option(options, "noauto"); + nofail = has_option(options, "nofail"); + if (!(n = unit_name_build_escape("cryptsetup", name, ".service"))) { r = -ENOMEM; log_error("Failed to allocate unit name."); @@ -99,12 +108,17 @@ static int create_disk( fprintf(f, "[Unit]\n" "Description=Cryptography Setup for %%I\n" + "Conflicts=umount.target\n" "DefaultDependencies=no\n" "BindTo=%s dev-mapper-%%i.device\n" "After=systemd-readahead-collect.service systemd-readahead-replay.service %s\n" - "Before=dev-mapper-%%i.device shutdown.target cryptsetup.target\n", + "Before=umount.target\n", d, d); + if (!nofail) + fprintf(f, + "Before=cryptsetup.target\n"); + if (password && (streq(password, "/dev/urandom") || streq(password, "/dev/random") || streq(password, "/dev/hw_random"))) @@ -115,19 +129,20 @@ static int create_disk( "\n[Service]\n" "Type=oneshot\n" "RemainAfterExit=yes\n" + "TimeoutSec=0\n" /* the binary handles timeouts anyway */ "ExecStart=" SYSTEMD_CRYPTSETUP_PATH " attach '%s' '%s' '%s' '%s'\n" "ExecStop=" SYSTEMD_CRYPTSETUP_PATH " detach '%s'\n", name, u, strempty(password), strempty(options), name); - if (options && has_option(options, "tmp")) + if (has_option(options, "tmp")) fprintf(f, - "ExecStartPost=/sbin/mke2fs '/dev/mapper/%s'", + "ExecStartPost=/sbin/mke2fs '/dev/mapper/%s'\n", name); - if (options && has_option(options, "swap")) + if (has_option(options, "swap")) fprintf(f, - "ExecStartPost=/sbin/mkswap '/dev/mapper/%s'", + "ExecStartPost=/sbin/mkswap '/dev/mapper/%s'\n", name); fflush(f); @@ -143,7 +158,7 @@ static int create_disk( goto fail; } - if (!options || !has_option(options, "noauto")) { + if (!noauto) { if (asprintf(&to, "%s/%s.wants/%s", arg_dest, d, n) < 0) { r = -ENOMEM; @@ -161,20 +176,22 @@ static int create_disk( free(to); to = NULL; - if (!options || !has_option(options, "nofail")) { + if (!nofail) + asprintf(&to, "%s/cryptsetup.target.requires/%s", arg_dest, n); + else + asprintf(&to, "%s/cryptsetup.target.wants/%s", arg_dest, n); - if (asprintf(&to, "%s/cryptsetup.target.wants/%s", arg_dest, n) < 0) { - r = -ENOMEM; - goto fail; - } + if (!to) { + r = -ENOMEM; + goto fail; + } - mkdir_parents(to, 0755); + mkdir_parents(to, 0755); - if (symlink(from, to) < 0) { - log_error("Failed to create symlink '%s' to '%s': %m", from, to); - r = -errno; - goto fail; - } + if (symlink(from, to) < 0) { + log_error("Failed to create symlink '%s' to '%s': %m", from, to); + r = -errno; + goto fail; } }