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;
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;
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",
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);
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;
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");
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);
}
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;
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"
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);
}
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);
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");
_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 +
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,
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;
}
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();
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();
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"
"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);
}
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();
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"))
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"
"RemainAfterExit=yes\n"
"ExecStart="SYSTEMD_MAKEFS_PATH " %s %s\n"
"TimeoutSec=infinity\n",
- program_invocation_short_name,
fsck_unit,
where_unit,
type,
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);
}
#include "conf-parser.h"
#include "escape.h"
#include "fd-util.h"
+#include "fileio.h"
#include "generator.h"
#include "log.h"
#include "nulstr-util.h"
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;
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"
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);