]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
systemctl: emit notice about some kernel commandline options
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Fri, 13 Mar 2020 14:50:37 +0000 (15:50 +0100)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Fri, 13 Mar 2020 16:52:19 +0000 (17:52 +0100)
Things can be pretty confusing when there's a commandline option overrding the
configured default... Let's be nice to the user and emit a warning.

src/systemctl/systemctl.c

index 07f060e95346a8c7338f1692d8b12b8636e31458..2cefcac693e07554bd4e380fa37c15ff2b21ba63 100644 (file)
@@ -2065,6 +2065,41 @@ static int list_machines(int argc, char *argv[], void *userdata) {
         return rc;
 }
 
+static int parse_proc_cmdline_item(const char *key, const char *value, void *data) {
+        char **ret = data;
+
+        if (streq(key, "systemd.unit")) {
+                if (proc_cmdline_value_missing(key, value))
+                        return 0;
+                if (!unit_name_is_valid(value, UNIT_NAME_PLAIN|UNIT_NAME_INSTANCE))
+                        return log_warning("Unit name specified on %s= is not valid, ignoring: %s", key, value);
+
+                return free_and_strdup_warn(ret, key);
+
+        } else if (!value) {
+                if (runlevel_to_target(key))
+                        return free_and_strdup_warn(ret, key);
+        }
+
+        return 0;
+}
+
+static void emit_cmdline_warning(void) {
+        if (arg_quiet || arg_root)
+                /* don't bother checking the commandline if we're operating on a container */
+                return;
+
+        _cleanup_free_ char *override = NULL;
+        int r;
+
+        r = proc_cmdline_parse(parse_proc_cmdline_item, &override, 0);
+        if (r < 0)
+                log_debug_errno(r, "Failed to parse kernel command line, ignoring: %m");
+        if (override)
+                log_notice("Note: found \"%s\" on the kernel commandline, which overrides the default unit.",
+                           override);
+}
+
 static int get_default(int argc, char *argv[], void *userdata) {
         _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
         _cleanup_free_ char *_path = NULL;
@@ -2077,7 +2112,6 @@ static int get_default(int argc, char *argv[], void *userdata) {
                         return log_error_errno(r, "Failed to get default target: %m");
                 path = _path;
 
-                r = 0;
         } else {
                 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
                 sd_bus *bus;
@@ -2106,6 +2140,8 @@ static int get_default(int argc, char *argv[], void *userdata) {
         if (path)
                 printf("%s\n", path);
 
+        emit_cmdline_warning();
+
         return 0;
 }
 
@@ -2164,6 +2200,8 @@ static int set_default(int argc, char *argv[], void *userdata) {
                         r = 0;
         }
 
+        emit_cmdline_warning();
+
 finish:
         unit_file_changes_free(changes, n_changes);