1 /* SPDX-License-Identifier: LGPL-2.1+ */
5 #include "alloc-util.h"
11 #include "proc-cmdline.h"
12 #include "specifier.h"
15 static const char *arg_dest
= NULL
;
16 static char **arg_commands
= NULL
;
17 static char *arg_success_action
= NULL
;
18 static char *arg_failure_action
= NULL
;
20 STATIC_DESTRUCTOR_REGISTER(arg_commands
, strv_freep
);
21 STATIC_DESTRUCTOR_REGISTER(arg_success_action
, freep
);
22 STATIC_DESTRUCTOR_REGISTER(arg_failure_action
, freep
);
24 static int parse(const char *key
, const char *value
, void *data
) {
27 if (proc_cmdline_key_streq(key
, "systemd.run")) {
29 if (proc_cmdline_value_missing(key
, value
))
32 r
= strv_extend(&arg_commands
, value
);
36 } else if (proc_cmdline_key_streq(key
, "systemd.run_success_action")) {
38 if (proc_cmdline_value_missing(key
, value
))
41 if (free_and_strdup(&arg_success_action
, value
) < 0)
44 } else if (proc_cmdline_key_streq(key
, "systemd.run_failure_action")) {
46 if (proc_cmdline_value_missing(key
, value
))
49 if (free_and_strdup(&arg_failure_action
, value
) < 0)
56 static int generate(void) {
57 _cleanup_fclose_
FILE *f
= NULL
;
62 if (strv_isempty(arg_commands
) && !arg_success_action
)
65 p
= strjoina(arg_dest
, "/kernel-command-line.service");
68 return log_error_errno(errno
, "Failed to create unit file %s: %m", p
);
70 fputs("# Automatically generated by systemd-run-generator\n\n"
72 "Description=Command from Kernel Command Line\n"
73 "Documentation=man:systemd-run-generator(8)\n"
74 "SourcePath=/proc/cmdline\n", f
);
76 if (!streq_ptr(arg_success_action
, "none"))
77 fprintf(f
, "SuccessAction=%s\n",
78 arg_success_action
?: "exit");
80 if (!streq_ptr(arg_failure_action
, "none"))
81 fprintf(f
, "FailureAction=%s\n",
82 arg_failure_action
?: "exit");
87 "StandardOutput=journal+console\n", f
);
89 STRV_FOREACH(c
, arg_commands
) {
90 _cleanup_free_
char *a
= NULL
;
92 a
= specifier_escape(*c
);
96 fprintf(f
, "ExecStart=%s\n", a
);
99 r
= fflush_and_check(f
);
101 return log_error_errno(r
, "Failed to write unit file %s: %m", p
);
103 /* Let's create a a target we can link "default.target" to */
104 p
= strjoina(arg_dest
, "/kernel-command-line.target");
105 r
= write_string_file(
107 "# Automatically generated by systemd-run-generator\n\n"
109 "Description=Command from Kernel Command Line\n"
110 "Documentation=man:systemd-run-generator(8)\n"
111 "SourcePath=/proc/cmdline\n"
112 "Requires=kernel-command-line.service\n"
113 "After=kernel-command-line.service\n",
114 WRITE_STRING_FILE_CREATE
|WRITE_STRING_FILE_NOFOLLOW
);
116 return log_error_errno(r
, "Failed to create unit file %s: %m", p
);
118 /* And now redirect default.target to our new target */
119 p
= strjoina(arg_dest
, "/default.target");
120 if (symlink("kernel-command-line.target", p
) < 0)
121 return log_error_errno(errno
, "Failed to link unit file kernel-command-line.target → %s: %m", p
);
126 static int run(const char *dest
, const char *dest_early
, const char *dest_late
) {
129 assert_se(arg_dest
= dest
);
131 r
= proc_cmdline_parse(parse
, NULL
, PROC_CMDLINE_RD_STRICT
|PROC_CMDLINE_STRIP_RD_PREFIX
);
133 log_warning_errno(r
, "Failed to parse kernel command line, ignoring: %m");
138 DEFINE_MAIN_GENERATOR_FUNCTION(run
);