cryptsetup-generator.c revision 951657bd0a5bb32b5f56cc6b91ad2ea4094bdfaf
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2010 Lennart Poettering
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include "log.h"
#include "util.h"
#include "unit-name.h"
#include "mkdir.h"
#include "virt.h"
#include "strv.h"
#include "fileio.h"
static const char *arg_dest = "/tmp";
static bool arg_enabled = true;
static bool arg_read_crypttab = true;
const char *f = haystack;
size_t l;
if (!haystack)
return false;
f++;
continue;
}
if (f[l] != 0 && f[l] != ',') {
f++;
continue;
}
return true;
}
return false;
}
static int create_disk(
const char *name,
const char *device,
const char *password,
const char *options) {
char _cleanup_free_ *p = NULL, *n = NULL, *d = NULL, *u = NULL, *from = NULL, *to = NULL, *e = NULL;
if (!n)
return log_oom();
if (!p)
return log_oom();
if (!u)
return log_oom();
d = unit_name_from_path(u, ".device");
if (!d)
return log_oom();
f = fopen(p, "wxe");
if (!f) {
log_error("Failed to create unit file %s: %m", p);
return -errno;
}
"# Automatically generated by systemd-cryptsetup-generator\n\n"
"[Unit]\n"
"Description=Cryptography Setup for %I\n"
"Documentation=man:systemd-cryptsetup@.service(8) man:crypttab(5)\n"
"Conflicts=umount.target\n"
"DefaultDependencies=no\n"
"BindsTo=dev-mapper-%i.device\n"
"After=systemd-readahead-collect.service systemd-readahead-replay.service\n",
f);
if (!nofail)
fprintf(f,
"Before=cryptsetup.target\n");
if (password) {
fputs("After=systemd-random-seed-load.service\n", f);
fprintf(f,
"RequiresMountsFor=%s\n",
password);
}
if (is_device_path(u))
fprintf(f,
"BindsTo=%s\n"
"After=%s\n"
"Before=umount.target\n",
d, d);
else
fprintf(f,
"RequiresMountsFor=%s\n",
u);
fprintf(f,
"\n[Service]\n"
"Type=oneshot\n"
"RemainAfterExit=yes\n"
"TimeoutSec=0\n" /* the binary handles timeouts anyway */
name);
fprintf(f,
name);
fprintf(f,
name);
fflush(f);
if (ferror(f)) {
log_error("Failed to write file %s: %m", p);
return -errno;
}
return log_oom();
if (!noauto) {
if (!to)
return log_oom();
return -errno;
}
if (!nofail)
else
if (!to)
return log_oom();
return -errno;
}
}
e = unit_name_escape(name);
if (!e)
return log_oom();
if (!to)
return log_oom();
return -errno;
}
int r;
free(p);
if (!p)
return log_oom();
mkdir_parents_label(p, 0755);
r = write_string_file(p,
"# Automatically generated by systemd-cryptsetup-generator\n\n"
"[Unit]\n"
"JobTimeoutSec=0\n"); /* the binary handles timeouts anyway */
if (r)
return r;
}
return 0;
}
int r;
size_t l;
if (detect_container(NULL) > 0)
return 0;
if (r < 0) {
return 0;
}
if (!word)
return log_oom();
if (r < 0)
else
arg_enabled = r;
if (in_initrd()) {
if (r < 0)
else
arg_enabled = r;
}
if (r < 0)
else
arg_read_crypttab = r;
if (in_initrd()) {
if (r < 0)
else
arg_read_crypttab = r;
}
return log_oom();
if (in_initrd()) {
return log_oom();
}
if (! arg_proc_cmdline_keyfile)
return log_oom();
if (in_initrd()) {
if (*arg_proc_cmdline_keyfile)
if (!arg_proc_cmdline_keyfile)
return log_oom();
}
}
}
return 0;
}
unsigned n = 0;
int r = EXIT_SUCCESS;
char **i;
log_error("This program takes three or no arguments.");
return EXIT_FAILURE;
}
if (argc > 1)
log_open();
umask(0022);
return EXIT_FAILURE;
if (!arg_enabled)
return EXIT_SUCCESS;
if (arg_read_crypttab) {
if (!f) {
r = EXIT_SUCCESS;
else {
r = EXIT_FAILURE;
}
} else for (;;) {
int k;
break;
n++;
if (*l == '#' || *l == 0)
continue;
if (k < 2 || k > 4) {
r = EXIT_FAILURE;
continue;
}
if (arg_proc_cmdline_disks) {
/*
If luks UUIDs are specified on the kernel command line, use them as a filter
*/
const char *p = *i;
if (startswith(p, "luks-"))
p += 5;
if (!proc_name || !proc_device)
return log_oom();
r = EXIT_FAILURE;
if (strv_extend(&arg_proc_cmdline_disks_done, p) < 0)
return log_oom();
}
}
} else {
r = EXIT_FAILURE;
}
}
}
/*
Generate units for those UUIDs, which were specified
on the kernel command line and not yet written.
*/
const char *p = *i;
if (startswith(p, "luks-"))
p += 5;
if (strv_contains(arg_proc_cmdline_disks_done, p))
continue;
return log_oom();
r = EXIT_FAILURE;
}
return r;
}