summaryrefslogtreecommitdiff
path: root/src
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
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')
-rw-r--r--src/cryptsetup/cryptsetup-generator.c25
-rw-r--r--src/fstab-generator/fstab-generator.c54
-rw-r--r--src/shared/generator.c34
-rw-r--r--src/shared/generator.h6
-rw-r--r--src/veritysetup/veritysetup-generator.c20
5 files changed, 73 insertions, 66 deletions
diff --git a/src/cryptsetup/cryptsetup-generator.c b/src/cryptsetup/cryptsetup-generator.c
index 7e61332e52..531edc6847 100644
--- a/src/cryptsetup/cryptsetup-generator.c
+++ b/src/cryptsetup/cryptsetup-generator.c
@@ -61,7 +61,7 @@ static int create_disk(
const char *password,
const char *options) {
- _cleanup_free_ char *p = NULL, *n = NULL, *d = NULL, *u = NULL, *e = NULL,
+ _cleanup_free_ char *n = NULL, *d = NULL, *u = NULL, *e = NULL,
*filtered = NULL, *u_escaped = NULL, *password_escaped = NULL, *filtered_escaped = NULL, *name_escaped = NULL;
_cleanup_fclose_ FILE *f = NULL;
const char *dmname;
@@ -90,18 +90,14 @@ static int create_disk(
if (!e)
return log_oom();
- r = unit_name_build("systemd-cryptsetup", e, ".service", &n);
- if (r < 0)
- return log_error_errno(r, "Failed to generate unit name: %m");
-
- p = strjoin(arg_dest, "/", n);
- if (!p)
- return log_oom();
-
u = fstab_node_to_udev_node(device);
if (!u)
return log_oom();
+ r = unit_name_build("systemd-cryptsetup", e, ".service", &n);
+ if (r < 0)
+ return log_error_errno(r, "Failed to generate unit name: %m");
+
u_escaped = specifier_escape(u);
if (!u_escaped)
return log_oom();
@@ -114,14 +110,11 @@ static int create_disk(
if (!password_escaped)
return log_oom();
- f = fopen(p, "wxe");
- if (!f)
- return log_error_errno(errno, "Failed to create unit file %s: %m", p);
-
- (void) __fsetlocking(f, FSETLOCKING_BYCALLER);
+ r = generator_open_unit_file(arg_dest, NULL, n, &f);
+ if (r < 0)
+ return r;
fprintf(f,
- "# Automatically generated by systemd-cryptsetup-generator\n\n"
"[Unit]\n"
"Description=Cryptography Setup for %%I\n"
"Documentation=man:crypttab(5) man:systemd-cryptsetup-generator(8) man:systemd-cryptsetup@.service(8)\n"
@@ -210,7 +203,7 @@ static int create_disk(
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 unit file %s: %m", n);
if (!noauto) {
r = generator_add_symlink(arg_dest, d, "wants", n);
diff --git a/src/fstab-generator/fstab-generator.c b/src/fstab-generator/fstab-generator.c
index 22c4ae9861..95bc5e38d5 100644
--- a/src/fstab-generator/fstab-generator.c
+++ b/src/fstab-generator/fstab-generator.c
@@ -102,7 +102,7 @@ static int add_swap(
struct mntent *me,
MountpointFlags flags) {
- _cleanup_free_ char *name = NULL, *unit = NULL;
+ _cleanup_free_ char *name = NULL;
_cleanup_fclose_ FILE *f = NULL;
int r;
@@ -123,19 +123,9 @@ static int add_swap(
if (r < 0)
return log_error_errno(r, "Failed to generate unit name: %m");
- unit = strjoin(arg_dest, "/", name);
- if (!unit)
- return log_oom();
-
- f = fopen(unit, "wxe");
- if (!f)
- return log_error_errno(errno,
- errno == EEXIST ?
- "Failed to create swap unit file %s, as it already exists. Duplicate entry in /etc/fstab?" :
- "Failed to create unit file %s: %m",
- unit);
-
- (void) __fsetlocking(f, FSETLOCKING_BYCALLER);
+ r = generator_open_unit_file(arg_dest, "/etc/fstab", name, &f);
+ if (r < 0)
+ return r;
fputs("# Automatically generated by systemd-fstab-generator\n\n"
"[Unit]\n"
@@ -153,7 +143,7 @@ static int add_swap(
r = fflush_and_check(f);
if (r < 0)
- return log_error_errno(r, "Failed to write unit file %s: %m", unit);
+ return log_error_errno(r, "Failed to write unit file %s: %m", name);
/* use what as where, to have a nicer error message */
r = generator_write_timeouts(arg_dest, what, what, me->mnt_opts, NULL);
@@ -323,10 +313,9 @@ static int add_mount(
_cleanup_free_ char
*name = NULL,
- *automount_name = NULL, *automount_unit = NULL,
+ *automount_name = NULL,
*filtered = NULL,
*where_escaped = NULL;
- const char *unit;
_cleanup_fclose_ FILE *f = NULL;
int r;
@@ -363,20 +352,11 @@ static int add_mount(
if (r < 0)
return log_error_errno(r, "Failed to generate unit name: %m");
- unit = strjoina(dest, "/", name);
-
- f = fopen(unit, "wxe");
- if (!f)
- return log_error_errno(errno,
- errno == EEXIST ?
- "Failed to create mount unit file %s, as it already exists. Duplicate entry in /etc/fstab?" :
- "Failed to create unit file %s: %m",
- unit);
-
- (void) __fsetlocking(f, FSETLOCKING_BYCALLER);
+ r = generator_open_unit_file(dest, "/etc/fstab", name, &f);
+ if (r < 0)
+ return r;
fprintf(f,
- "# Automatically generated by systemd-fstab-generator\n\n"
"[Unit]\n"
"SourcePath=%s\n"
"Documentation=man:fstab(5) man:systemd-fstab-generator(8)\n",
@@ -461,7 +441,7 @@ static int add_mount(
r = fflush_and_check(f);
if (r < 0)
- return log_error_errno(r, "Failed to write unit file %s: %m", unit);
+ return log_error_errno(r, "Failed to write unit file %s: %m", name);
if (flags & MAKEFS) {
r = generator_hook_up_mkfs(dest, what, where, fstype);
@@ -487,19 +467,13 @@ static int add_mount(
if (r < 0)
return log_error_errno(r, "Failed to generate unit name: %m");
- automount_unit = strjoin(dest, "/", automount_name);
- if (!automount_unit)
- return log_oom();
-
fclose(f);
- f = fopen(automount_unit, "wxe");
- if (!f)
- return log_error_errno(errno, "Failed to create unit file %s: %m", automount_unit);
- (void) __fsetlocking(f, FSETLOCKING_BYCALLER);
+ r = generator_open_unit_file(dest, "/etc/fstab", automount_name, &f);
+ if (r < 0)
+ return r;
fprintf(f,
- "# Automatically generated by systemd-fstab-generator\n\n"
"[Unit]\n"
"SourcePath=%s\n"
"Documentation=man:fstab(5) man:systemd-fstab-generator(8)\n",
@@ -534,7 +508,7 @@ static int add_mount(
r = fflush_and_check(f);
if (r < 0)
- return log_error_errno(r, "Failed to write unit file %s: %m", automount_unit);
+ return log_error_errno(r, "Failed to write unit file %s: %m", automount_name);
r = generator_add_symlink(dest, post,
(flags & NOFAIL) ? "wants" : "requires", automount_name);
diff --git a/src/shared/generator.c b/src/shared/generator.c
index 3495a7ef7d..2b0a4ecdc8 100644
--- a/src/shared/generator.c
+++ b/src/shared/generator.c
@@ -19,6 +19,7 @@
***/
#include <errno.h>
+#include <stdio_ext.h>
#include <unistd.h>
#include "alloc-util.h"
@@ -39,6 +40,39 @@
#include "unit-name.h"
#include "util.h"
+int generator_open_unit_file(
+ const char *dest,
+ const char *source,
+ const char *name,
+ FILE **file) {
+
+ const char *unit;
+ FILE *f;
+
+ unit = strjoina(dest, "/", name);
+
+ f = fopen(unit, "wxe");
+ if (!f) {
+ if (source && errno == EEXIST)
+ return log_error_errno(errno,
+ "Failed to create unit file %s, as it already exists. Duplicate entry in %s?",
+ unit, source);
+ else
+ return log_error_errno(errno,
+ "Failed to create unit file %s: %m",
+ unit);
+ }
+
+ (void) __fsetlocking(f, FSETLOCKING_BYCALLER);
+
+ fprintf(f,
+ "# Automatically generated by %s\n\n",
+ program_invocation_short_name);
+
+ *file = f;
+ return 0;
+}
+
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> */
diff --git a/src/shared/generator.h b/src/shared/generator.h
index 32d1ad021c..e1c636a218 100644
--- a/src/shared/generator.h
+++ b/src/shared/generator.h
@@ -22,6 +22,12 @@
#include <stdio.h>
+int generator_open_unit_file(
+ const char *dest,
+ const char *source,
+ const char *name,
+ FILE **file);
+
int generator_add_symlink(const char *root, const char *dst, const char *dep_type, const char *src);
int generator_write_fsck_deps(
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;