generators: use generator_open_unit_file where appropriate

This commit is contained in:
Mike Yuan
2023-08-19 01:15:01 +01:00
committed by Luca Boccassi
parent 2c29591c7e
commit 121ce4a687
4 changed files with 60 additions and 91 deletions
+16 -31
View File
@@ -79,7 +79,7 @@ static int add_cryptsetup(
if (r < 0)
return log_error_errno(r, "Failed to generate unit name: %m");
r = generator_open_unit_file(arg_dest, NULL, n, &f);
r = generator_open_unit_file(arg_dest, /* source = */ NULL, n, &f);
if (r < 0)
return r;
@@ -175,7 +175,7 @@ static int add_mount(
const char *description,
const char *post) {
_cleanup_free_ char *unit = NULL, *crypto_what = NULL, *p = NULL;
_cleanup_free_ char *unit = NULL, *crypto_what = NULL;
_cleanup_fclose_ FILE *f = NULL;
int r;
@@ -212,16 +212,11 @@ static int add_mount(
if (r < 0)
return log_error_errno(r, "Failed to generate unit name: %m");
p = path_join(empty_to_root(arg_dest), unit);
if (!p)
return log_oom();
f = fopen(p, "wxe");
if (!f)
return log_error_errno(errno, "Failed to create unit file %s: %m", unit);
r = generator_open_unit_file(arg_dest, /* source = */ NULL, unit, &f);
if (r < 0)
return r;
fprintf(f,
"# Automatically generated by systemd-gpt-auto-generator\n\n"
"[Unit]\n"
"Description=%s\n"
"Documentation=man:systemd-gpt-auto-generator(8)\n",
@@ -253,7 +248,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", p);
return log_error_errno(r, "Failed to write unit %s: %m", unit);
if (growfs) {
r = generator_hook_up_growfs(arg_dest, where, post);
@@ -348,7 +343,7 @@ static int add_partition_mount(
static int add_partition_swap(DissectedPartition *p) {
const char *what;
_cleanup_free_ char *name = NULL, *unit = NULL, *crypto_what = NULL;
_cleanup_free_ char *name = NULL, *crypto_what = NULL;
_cleanup_fclose_ FILE *f = NULL;
int r;
@@ -381,16 +376,11 @@ static int add_partition_swap(DissectedPartition *p) {
if (r < 0)
return log_error_errno(r, "Failed to generate unit name: %m");
unit = path_join(empty_to_root(arg_dest), name);
if (!unit)
return log_oom();
f = fopen(unit, "wxe");
if (!f)
return log_error_errno(errno, "Failed to create unit file %s: %m", unit);
r = generator_open_unit_file(arg_dest, /* source = */ NULL, name, &f);
if (r < 0)
return r;
fprintf(f,
"# Automatically generated by systemd-gpt-auto-generator\n\n"
"[Unit]\n"
"Description=Swap Partition\n"
"Documentation=man:systemd-gpt-auto-generator(8)\n");
@@ -407,7 +397,7 @@ static int add_partition_swap(DissectedPartition *p) {
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 %s: %m", name);
return generator_add_symlink(arg_dest, SPECIAL_SWAP_TARGET, "wants", name);
}
@@ -423,7 +413,7 @@ static int add_automount(
const char *description,
usec_t timeout) {
_cleanup_free_ char *unit = NULL, *p = NULL;
_cleanup_free_ char *unit = NULL;
_cleanup_fclose_ FILE *f = NULL;
int r;
@@ -448,16 +438,11 @@ static int add_automount(
if (r < 0)
return log_error_errno(r, "Failed to generate unit name: %m");
p = path_join(arg_dest, unit);
if (!p)
return log_oom();
f = fopen(p, "wxe");
if (!f)
return log_error_errno(errno, "Failed to create unit file %s: %m", unit);
r = generator_open_unit_file(arg_dest, /* source = */ NULL, unit, &f);
if (r < 0)
return r;
fprintf(f,
"# Automatically generated by systemd-gpt-auto-generator\n\n"
"[Unit]\n"
"Description=%s\n"
"Documentation=man:systemd-gpt-auto-generator(8)\n"
@@ -470,7 +455,7 @@ static int add_automount(
r = fflush_and_check(f);
if (r < 0)
return log_error_errno(r, "Failed to write unit file %s: %m", p);
return log_error_errno(r, "Failed to write unit %s: %m", unit);
return generator_add_symlink(arg_dest, SPECIAL_LOCAL_FS_TARGET, "wants", unit);
}
+5 -7
View File
@@ -63,13 +63,11 @@ static int generate(void) {
if (strv_isempty(arg_commands) && !arg_success_action)
return 0;
p = strjoina(arg_dest, "/kernel-command-line.service");
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, /* source = */ NULL, "kernel-command-line.service", &f);
if (r < 0)
return r;
fputs("# Automatically generated by systemd-run-generator\n\n"
"[Unit]\n"
fputs("[Unit]\n"
"Description=Command from Kernel Command Line\n"
"Documentation=man:systemd-run-generator(8)\n"
"SourcePath=/proc/cmdline\n", f);
@@ -99,7 +97,7 @@ static int generate(void) {
r = fflush_and_check(f);
if (r < 0)
return log_error_errno(r, "Failed to write unit file %s: %m", p);
return log_error_errno(r, "Failed to write unit kernel-command-line.service: %m");
/* Let's create a target we can link "default.target" to */
p = strjoina(arg_dest, "/kernel-command-line.target");
+30 -44
View File
@@ -192,9 +192,12 @@ static int write_fsck_sysroot_service(
_cleanup_free_ char *device = NULL, *escaped = NULL, *escaped2 = NULL;
_cleanup_fclose_ FILE *f = NULL;
const char *fn;
int r;
assert(unit);
assert(dir);
assert(what);
/* Writes out special versions of systemd-fsck-root.service and systemd-fsck-usr.service for use in
* the initrd. The regular statically shipped versions of these unit files use / and /usr for as
* paths, which doesn't match what we need for the initrd (where the dirs are /sysroot +
@@ -208,35 +211,30 @@ static int write_fsck_sysroot_service(
if (!escaped2)
return log_oom();
fn = strjoina(dir, "/", unit);
log_debug("Creating %s", fn);
r = unit_name_from_path(what, ".device", &device);
if (r < 0)
return log_error_errno(r, "Failed to convert device \"%s\" to unit name: %m", what);
f = fopen(fn, "wxe");
if (!f)
return log_error_errno(errno, "Failed to create unit file %s: %m", fn);
r = generator_open_unit_file(dir, /* source = */ NULL, unit, &f);
if (r < 0)
return r;
fprintf(f,
"# Automatically generated by %1$s\n\n"
"[Unit]\n"
"Description=File System Check on %2$s\n"
"Documentation=man:%3$s(8)\n"
"Description=File System Check on %1$s\n"
"Documentation=man:%2$s(8)\n"
"\n"
"DefaultDependencies=no\n"
"BindsTo=%4$s\n"
"BindsTo=%3$s\n"
"Conflicts=shutdown.target\n"
"After=%5$s%6$slocal-fs-pre.target %4$s\n"
"After=%4$s%5$slocal-fs-pre.target %3$s\n"
"Before=shutdown.target\n"
"\n"
"[Service]\n"
"Type=oneshot\n"
"RemainAfterExit=yes\n"
"ExecStart=" SYSTEMD_FSCK_PATH " %7$s\n"
"ExecStart=" SYSTEMD_FSCK_PATH " %6$s\n"
"TimeoutSec=infinity\n",
program_invocation_short_name,
escaped,
unit,
device,
@@ -246,7 +244,7 @@ static int write_fsck_sysroot_service(
r = fflush_and_check(f);
if (r < 0)
return log_error_errno(r, "Failed to write unit file %s: %m", fn);
return log_error_errno(r, "Failed to write unit %s: %m", unit);
return 0;
}
@@ -474,10 +472,12 @@ int generator_hook_up_mkswap(
const char *what) {
_cleanup_free_ char *node = NULL, *unit = NULL, *escaped = NULL, *where_unit = NULL;
_cleanup_free_ char *unit_file = NULL;
_cleanup_fclose_ FILE *f = NULL;
int r;
assert(dir);
assert(what);
node = fstab_node_to_udev_node(what);
if (!node)
return log_oom();
@@ -493,12 +493,6 @@ int generator_hook_up_mkswap(
return log_error_errno(r, "Failed to make unit instance name from path \"%s\": %m",
node);
unit_file = path_join(dir, unit);
if (!unit_file)
return log_oom();
log_debug("Creating %s", unit_file);
escaped = cescape(node);
if (!escaped)
return log_oom();
@@ -508,13 +502,11 @@ int generator_hook_up_mkswap(
return log_error_errno(r, "Failed to make unit name from path \"%s\": %m",
what);
f = fopen(unit_file, "wxe");
if (!f)
return log_error_errno(errno, "Failed to create unit file %s: %m",
unit_file);
r = generator_open_unit_file(dir, /* source = */ NULL, unit, &f);
if (r < 0)
return r;
fprintf(f,
"# Automatically generated by %s\n\n"
"[Unit]\n"
"Description=Make Swap on %%f\n"
"Documentation=man:systemd-mkswap@.service(8)\n"
@@ -531,13 +523,12 @@ int generator_hook_up_mkswap(
"RemainAfterExit=yes\n"
"ExecStart="SYSTEMD_MAKEFS_PATH " swap %s\n"
"TimeoutSec=infinity\n",
program_invocation_short_name,
where_unit,
escaped);
r = fflush_and_check(f);
if (r < 0)
return log_error_errno(r, "Failed to write unit file %s: %m", unit_file);
return log_error_errno(r, "Failed to write unit %s: %m", unit);
return generator_add_symlink(dir, where_unit, "requires", unit);
}
@@ -548,10 +539,15 @@ int generator_hook_up_mkfs(
const char *where,
const char *type) {
_cleanup_free_ char *node = NULL, *unit = NULL, *unit_file = NULL, *escaped = NULL, *where_unit = NULL;
_cleanup_free_ char *node = NULL, *unit = NULL, *escaped = NULL, *where_unit = NULL;
_cleanup_fclose_ FILE *f = NULL;
const char *fsck_unit;
int r;
assert(dir);
assert(what);
assert(where);
node = fstab_node_to_udev_node(what);
if (!node)
return log_oom();
@@ -572,13 +568,6 @@ int generator_hook_up_mkfs(
return log_error_errno(r, "Failed to make unit instance name from path \"%s\": %m",
node);
unit_file = path_join(dir, unit);
if (!unit_file)
return log_oom();
log_debug("Creating %s", unit_file);
const char *fsck_unit;
if (in_initrd() && path_equal(where, "/sysroot"))
fsck_unit = SPECIAL_FSCK_ROOT_SERVICE;
else if (in_initrd() && path_equal(where, "/sysusr/usr"))
@@ -595,13 +584,11 @@ int generator_hook_up_mkfs(
return log_error_errno(r, "Failed to make unit name from path \"%s\": %m",
where);
f = fopen(unit_file, "wxe");
if (!f)
return log_error_errno(errno, "Failed to create unit file %s: %m",
unit_file);
r = generator_open_unit_file(dir, /* source = */ NULL, unit, &f);
if (r < 0)
return r;
fprintf(f,
"# Automatically generated by %s\n\n"
"[Unit]\n"
"Description=Make File System on %%f\n"
"Documentation=man:systemd-makefs@.service(8)\n"
@@ -620,7 +607,6 @@ int generator_hook_up_mkfs(
"RemainAfterExit=yes\n"
"ExecStart="SYSTEMD_MAKEFS_PATH " %s %s\n"
"TimeoutSec=infinity\n",
program_invocation_short_name,
fsck_unit,
where_unit,
type,
@@ -629,7 +615,7 @@ int generator_hook_up_mkfs(
r = fflush_and_check(f);
if (r < 0)
return log_error_errno(r, "Failed to write unit file %s: %m", unit_file);
return log_error_errno(r, "Failed to write unit %s: %m", unit);
return generator_add_symlink(dir, where_unit, "requires", unit);
}
@@ -9,6 +9,7 @@
#include "conf-parser.h"
#include "escape.h"
#include "fd-util.h"
#include "fileio.h"
#include "generator.h"
#include "log.h"
#include "nulstr-util.h"
@@ -516,7 +517,7 @@ int xdg_autostart_service_generate_unit(
const XdgAutostartService *service,
const char *dest) {
_cleanup_free_ char *path_escaped = NULL, *exec_start = NULL, *unit = NULL;
_cleanup_free_ char *path_escaped = NULL, *exec_start = NULL;
_cleanup_fclose_ FILE *f = NULL;
_cleanup_strv_free_ char **only_show_in = NULL, **not_show_in = NULL;
int r;
@@ -603,16 +604,11 @@ int xdg_autostart_service_generate_unit(
if (!path_escaped)
return log_oom();
unit = path_join(dest, service->name);
if (!unit)
return log_oom();
f = fopen(unit, "wxe");
if (!f)
return log_error_errno(errno, "%s: failed to create unit file %s: %m", service->path, unit);
r = generator_open_unit_file(dest, /* source = */ NULL, service->name, &f);
if (r < 0)
return r;
fprintf(f,
"# Automatically generated by systemd-xdg-autostart-generator\n\n"
"[Unit]\n"
"Documentation=man:systemd-xdg-autostart-generator(8)\n"
"SourcePath=%s\n"
@@ -686,6 +682,10 @@ int xdg_autostart_service_generate_unit(
if (r < 0)
return r;
r = fflush_and_check(f);
if (r < 0)
return log_error_errno(r, "Failed to write unit %s: %m", service->name);
log_debug("%s: symlinking %s in xdg-desktop-autostart.target/.wants%s",
service->path, service->name, special_glyph(SPECIAL_GLYPH_ELLIPSIS));
return generator_add_symlink(dest, "xdg-desktop-autostart.target", "wants", service->name);