1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 #include "alloc-util.h"
9 #include "proc-cmdline.h"
10 #include "specifier.h"
13 static const char *arg_dest
= NULL
;
14 static char **arg_commands
= NULL
;
15 static char *arg_success_action
= NULL
;
16 static char *arg_failure_action
= NULL
;
18 STATIC_DESTRUCTOR_REGISTER(arg_commands
, strv_freep
);
19 STATIC_DESTRUCTOR_REGISTER(arg_success_action
, freep
);
20 STATIC_DESTRUCTOR_REGISTER(arg_failure_action
, freep
);
22 static int parse(const char *key
, const char *value
, void *data
) {
25 if (proc_cmdline_key_streq(key
, "systemd.run")) {
27 if (proc_cmdline_value_missing(key
, value
))
30 r
= strv_extend(&arg_commands
, value
);
34 } else if (proc_cmdline_key_streq(key
, "systemd.run_success_action")) {
36 if (proc_cmdline_value_missing(key
, value
))
39 if (free_and_strdup(&arg_success_action
, value
) < 0)
42 } else if (proc_cmdline_key_streq(key
, "systemd.run_failure_action")) {
44 if (proc_cmdline_value_missing(key
, value
))
47 if (free_and_strdup(&arg_failure_action
, value
) < 0)
54 static int generate(void) {
55 _cleanup_fclose_
FILE *f
= NULL
;
60 if (strv_isempty(arg_commands
) && !arg_success_action
)
63 p
= strjoina(arg_dest
, "/kernel-command-line.service");
66 return log_error_errno(errno
, "Failed to create unit file %s: %m", p
);
68 fputs("# Automatically generated by systemd-run-generator\n\n"
70 "Description=Command from Kernel Command Line\n"
71 "Documentation=man:systemd-run-generator(8)\n"
72 "SourcePath=/proc/cmdline\n", f
);
74 if (!streq_ptr(arg_success_action
, "none"))
75 fprintf(f
, "SuccessAction=%s\n",
76 arg_success_action
?: "exit");
78 if (!streq_ptr(arg_failure_action
, "none"))
79 fprintf(f
, "FailureAction=%s\n",
80 arg_failure_action
?: "exit");
85 "StandardOutput=journal+console\n", f
);
87 STRV_FOREACH(c
, arg_commands
) {
88 _cleanup_free_
char *a
= NULL
;
90 a
= specifier_escape(*c
);
94 fprintf(f
, "ExecStart=%s\n", a
);
97 r
= fflush_and_check(f
);
99 return log_error_errno(r
, "Failed to write unit file %s: %m", p
);
101 /* Let's create a a target we can link "default.target" to */
102 p
= strjoina(arg_dest
, "/kernel-command-line.target");
103 r
= write_string_file(
105 "# Automatically generated by systemd-run-generator\n\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
);
114 return log_error_errno(r
, "Failed to create unit file %s: %m", p
);
116 /* And now redirect default.target to our new target */
117 p
= strjoina(arg_dest
, "/default.target");
118 if (symlink("kernel-command-line.target", p
) < 0)
119 return log_error_errno(errno
, "Failed to link unit file kernel-command-line.target → %s: %m", p
);
124 static int run(const char *dest
, const char *dest_early
, const char *dest_late
) {
127 assert_se(arg_dest
= dest
);
129 r
= proc_cmdline_parse(parse
, NULL
, PROC_CMDLINE_RD_STRICT
|PROC_CMDLINE_STRIP_RD_PREFIX
);
131 log_warning_errno(r
, "Failed to parse kernel command line, ignoring: %m");
136 DEFINE_MAIN_GENERATOR_FUNCTION(run
);