| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <unistd.h> |
| 4 |
|
| 5 |
#include "alloc-util.h" |
| 6 |
#include "fd-util.h" |
| 7 |
#include "fileio.h" |
| 8 |
#include "generator.h" |
| 9 |
#include "log.h" |
| 10 |
#include "proc-cmdline.h" |
| 11 |
#include "special.h" |
| 12 |
#include "specifier.h" |
| 13 |
#include "string-util.h" |
| 14 |
#include "strv.h" |
| 15 |
|
| 16 |
static const char *arg_dest = NULL; |
| 17 |
static char **arg_commands = NULL; |
| 18 |
static char *arg_success_action = NULL; |
| 19 |
static char *arg_failure_action = NULL; |
| 20 |
|
| 21 |
STATIC_DESTRUCTOR_REGISTER(arg_commands, strv_freep); |
| 22 |
STATIC_DESTRUCTOR_REGISTER(arg_success_action, freep); |
| 23 |
STATIC_DESTRUCTOR_REGISTER(arg_failure_action, freep); |
| 24 |
|
| 25 |
static int parse(const char *key, const char *value, void *data) { |
| 26 |
int r; |
| 27 |
|
| 28 |
assert(key); |
| 29 |
|
| 30 |
if (streq(key, "systemd.run")) { |
| 31 |
|
| 32 |
if (proc_cmdline_value_missing(key, value)) |
| 33 |
return 0; |
| 34 |
|
| 35 |
r = strv_extend(&arg_commands, value); |
| 36 |
if (r < 0) |
| 37 |
return log_oom(); |
| 38 |
|
| 39 |
} else if (proc_cmdline_key_streq(key, "systemd.run_success_action")) { |
| 40 |
|
| 41 |
if (proc_cmdline_value_missing(key, value)) |
| 42 |
return 0; |
| 43 |
|
| 44 |
return free_and_strdup_warn(&arg_success_action, value); |
| 45 |
|
| 46 |
} else if (proc_cmdline_key_streq(key, "systemd.run_failure_action")) { |
| 47 |
|
| 48 |
if (proc_cmdline_value_missing(key, value)) |
| 49 |
return 0; |
| 50 |
|
| 51 |
return free_and_strdup_warn(&arg_failure_action, value); |
| 52 |
} |
| 53 |
|
| 54 |
return 0; |
| 55 |
} |
| 56 |
|
| 57 |
static int generate(void) { |
| 58 |
_cleanup_fclose_ FILE *f = NULL; |
| 59 |
const char *p; |
| 60 |
int r; |
| 61 |
|
| 62 |
if (strv_isempty(arg_commands) && !arg_success_action) |
| 63 |
return 0; |
| 64 |
|
| 65 |
r = generator_open_unit_file(arg_dest, /* source= */ NULL, "kernel-command-line.service", &f); |
| 66 |
if (r < 0) |
| 67 |
return r; |
| 68 |
|
| 69 |
fputs("[Unit]\n" |
| 70 |
"Description=Command from Kernel Command Line\n" |
| 71 |
"Documentation=man:systemd-run-generator(8)\n" |
| 72 |
"SourcePath=/proc/cmdline\n", f); |
| 73 |
|
| 74 |
if (!streq_ptr(arg_success_action, "none")) |
| 75 |
fprintf(f, "SuccessAction=%s\n", |
| 76 |
arg_success_action ?: "exit"); |
| 77 |
|
| 78 |
if (!streq_ptr(arg_failure_action, "none")) |
| 79 |
fprintf(f, "FailureAction=%s\n", |
| 80 |
arg_failure_action ?: "exit"); |
| 81 |
|
| 82 |
fputs("\n" |
| 83 |
"[Service]\n" |
| 84 |
"Type=oneshot\n" |
| 85 |
"StandardOutput=journal+console\n", f); |
| 86 |
|
| 87 |
STRV_FOREACH(c, arg_commands) { |
| 88 |
_cleanup_free_ char *a = NULL; |
| 89 |
|
| 90 |
a = specifier_escape(*c); |
| 91 |
if (!a) |
| 92 |
return log_oom(); |
| 93 |
|
| 94 |
fprintf(f, "ExecStart=%s\n", a); |
| 95 |
} |
| 96 |
|
| 97 |
r = fflush_and_check(f); |
| 98 |
if (r < 0) |
| 99 |
return log_error_errno(r, "Failed to write unit kernel-command-line.service: %m"); |
| 100 |
|
| 101 |
/* Let's create a target we can link "default.target" to */ |
| 102 |
p = strjoina(arg_dest, "/kernel-command-line.target"); |
| 103 |
r = write_string_file( |
| 104 |
p, |
| 105 |
"# Automatically generated by systemd-run-generator\n\n" |
| 106 |
"[Unit]\n" |
| 107 |
"Description=Command from Kernel Command Line\n" |
| 108 |
"Documentation=man:systemd-run-generator(8)\n" |
| 109 |
"SourcePath=/proc/cmdline\n" |
| 110 |
"Requires=kernel-command-line.service\n" |
| 111 |
"After=kernel-command-line.service\n", |
| 112 |
WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_NOFOLLOW); |
| 113 |
if (r < 0) |
| 114 |
return log_error_errno(r, "Failed to create unit file %s: %m", p); |
| 115 |
|
| 116 |
/* And now redirect default.target to our new target */ |
| 117 |
return generator_add_symlink( |
| 118 |
arg_dest, |
| 119 |
SPECIAL_DEFAULT_TARGET, |
| 120 |
/* dep_type= */ NULL, |
| 121 |
"kernel-command-line.target"); |
| 122 |
} |
| 123 |
|
| 124 |
static int run(const char *dest, const char *dest_early, const char *dest_late) { |
| 125 |
int r; |
| 126 |
|
| 127 |
assert_se(arg_dest = dest); |
| 128 |
|
| 129 |
r = proc_cmdline_parse(parse, NULL, PROC_CMDLINE_RD_STRICT|PROC_CMDLINE_STRIP_RD_PREFIX); |
| 130 |
if (r < 0) |
| 131 |
log_warning_errno(r, "Failed to parse kernel command line, ignoring: %m"); |
| 132 |
|
| 133 |
return generate(); |
| 134 |
} |
| 135 |
|
| 136 |
DEFINE_MAIN_GENERATOR_FUNCTION(run); |