]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
generator: add helper function for writing unit files 7661/head
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Sat, 9 Dec 2017 18:23:26 +0000 (19:23 +0100)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Fri, 15 Dec 2017 15:47:11 +0000 (16:47 +0100)
It doesn't save too much, but it's a common pattern so I think it's worth
to factor this out.

src/cryptsetup/cryptsetup-generator.c
src/fstab-generator/fstab-generator.c
src/shared/generator.c
src/shared/generator.h
src/veritysetup/veritysetup-generator.c

index 7e61332e52c041adf8f59331787bf3689afa3171..531edc6847476eb6826245a9528bbb9dfd2e3836 100644 (file)
@@ -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);
index 22c4ae9861f0f1dde34cdaeb5896b14965cded0c..95bc5e38d5461f228edc86c7b8c0a192de503cdb 100644 (file)
@@ -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);
index 3495a7ef7d58af00caa0e1b37b59947594c65e08..2b0a4ecdc8d4d924c01803b29ecb64fc2628c427 100644 (file)
@@ -19,6 +19,7 @@
 ***/
 
 #include <errno.h>
+#include <stdio_ext.h>
 #include <unistd.h>
 
 #include "alloc-util.h"
 #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> */
 
index 32d1ad021cd5c67a05494a74cfdab2b418c1faf1..e1c636a2181bfd67b049af94b628e63be5389a27 100644 (file)
 
 #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(
index 5919b1380e768b3420d2ac65547feb6368d30eb2..c29c6f0bb3f085b524ceccdd2c4c5badbe6ae17f 100644 (file)
@@ -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;