summaryrefslogtreecommitdiff
path: root/src/veritysetup
diff options
context:
space:
mode:
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2017-12-09 19:23:26 +0100
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2017-12-15 16:47:11 +0100
commitfb883e759d3c35a9085b1ab25d6176c2979d29d6 (patch)
tree786470ac24c1dd19c02410fce4eff0bdbc9af802 /src/veritysetup
parente09fc88440f399766f7f4fbe8e56b68ba18a131d (diff)
downloadsystemd-fb883e759d3c35a9085b1ab25d6176c2979d29d6.tar.gz
systemd-fb883e759d3c35a9085b1ab25d6176c2979d29d6.tar.bz2
systemd-fb883e759d3c35a9085b1ab25d6176c2979d29d6.zip
generator: add helper function for writing unit files
It doesn't save too much, but it's a common pattern so I think it's worth to factor this out.
Diffstat (limited to 'src/veritysetup')
-rw-r--r--src/veritysetup/veritysetup-generator.c20
1 files changed, 10 insertions, 10 deletions
diff --git a/src/veritysetup/veritysetup-generator.c b/src/veritysetup/veritysetup-generator.c
index 5919b1380e..c29c6f0bb3 100644
--- a/src/veritysetup/veritysetup-generator.c
+++ b/src/veritysetup/veritysetup-generator.c
@@ -27,6 +27,7 @@
#include "fd-util.h"
#include "fileio.h"
#include "fstab-util.h"
+#include "generator.h"
#include "hexdecoct.h"
#include "id128-util.h"
#include "mkdir.h"
@@ -36,6 +37,8 @@
#include "string-util.h"
#include "unit-name.h"
+#define SYSTEMD_VERITYSETUP_SERVICE "systemd-veritysetup@root.service"
+
static char *arg_dest = NULL;
static bool arg_enabled = true;
static char *arg_root_hash = NULL;
@@ -45,7 +48,7 @@ static char *arg_hash_what = NULL;
static int create_device(void) {
_cleanup_free_ char *u = NULL, *v = NULL, *d = NULL, *e = NULL, *u_escaped = NULL, *v_escaped = NULL, *root_hash_escaped = NULL;
_cleanup_fclose_ FILE *f = NULL;
- const char *p, *to;
+ const char *to;
int r;
/* If all three pieces of information are missing, then verity is turned off */
@@ -67,8 +70,6 @@ static int create_device(void) {
" hash device %s,\n"
" and root hash %s.", arg_data_what, arg_hash_what, arg_root_hash);
- p = strjoina(arg_dest, "/systemd-veritysetup@root.service");
-
u = fstab_node_to_udev_node(arg_data_what);
if (!u)
return log_oom();
@@ -94,12 +95,11 @@ static int create_device(void) {
if (!root_hash_escaped)
return log_oom();
- f = fopen(p, "wxe");
- if (!f)
- return log_error_errno(errno, "Failed to create unit file %s: %m", p);
+ r = generator_open_unit_file(arg_dest, NULL, SYSTEMD_VERITYSETUP_SERVICE, &f);
+ if (r < 0)
+ return r;
fprintf(f,
- "# Automatically generated by systemd-veritysetup-generator\n\n"
"[Unit]\n"
"Description=Integrity Protection Setup for %%I\n"
"Documentation=man:systemd-veritysetup-generator(8) man:systemd-veritysetup@.service(8)\n"
@@ -121,12 +121,12 @@ static int create_device(void) {
r = fflush_and_check(f);
if (r < 0)
- return log_error_errno(r, "Failed to write file %s: %m", p);
+ return log_error_errno(r, "Failed to write file unit "SYSTEMD_VERITYSETUP_SERVICE": %m");
- to = strjoina(arg_dest, "/cryptsetup.target.requires/systemd-veritysetup@root.service");
+ to = strjoina(arg_dest, "/cryptsetup.target.requires/" SYSTEMD_VERITYSETUP_SERVICE);
(void) mkdir_parents(to, 0755);
- if (symlink("../systemd-veritysetup@root.service", to) < 0)
+ if (symlink("../" SYSTEMD_VERITYSETUP_SERVICE, to) < 0)
return log_error_errno(errno, "Failed to create symlink %s: %m", to);
return 0;