summaryrefslogtreecommitdiff
path: root/src/fstab-generator/fstab-generator.c
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/fstab-generator/fstab-generator.c
parente09fc88440f399766f7f4fbe8e56b68ba18a131d (diff)
downloadsystemd-fb883e759d3c35a9085b1ab25d6176c2979d29d6.tar.gz
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/fstab-generator/fstab-generator.c')
-rw-r--r--src/fstab-generator/fstab-generator.c54
1 files changed, 14 insertions, 40 deletions
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);