chiark / gitweb /
systemctl: warn if user enables unit with no installation instructions
[elogind.git] / src / systemctl.c
index 918dcbe8b542aa9dcae08a4049fdaa61db5c11a6..671745b7d001500ac5bfcf45d126960b88cdbee2 100644 (file)
@@ -297,7 +297,8 @@ static void output_units_list(const struct unit_info *unit_infos, unsigned c) {
                 if (!output_show_job(u))
                         continue;
 
-                if (!streq(u->load_state, "loaded")) {
+                if (!streq(u->load_state, "loaded") &&
+                    !streq(u->load_state, "banned")) {
                         on_loaded = ansi_highlight(true);
                         off_loaded = ansi_highlight(false);
                 } else
@@ -1567,6 +1568,7 @@ typedef struct UnitStatusInfo {
         const char *sub_state;
 
         const char *description;
+        const char *following;
 
         const char *path;
         const char *default_control_group;
@@ -1628,7 +1630,11 @@ static void print_status_info(UnitStatusInfo *i) {
 
         printf("\n");
 
-        if (streq_ptr(i->load_state, "failed")) {
+        if (i->following)
+                printf("\t  Follow: unit currently follows state of %s\n", i->following);
+
+        if (streq_ptr(i->load_state, "failed") ||
+            streq_ptr(i->load_state, "banned")) {
                 on = ansi_highlight(true);
                 off = ansi_highlight(false);
         } else
@@ -1673,9 +1679,9 @@ static void print_status_info(UnitStatusInfo *i) {
         s2 = format_timestamp(since2, sizeof(since2), timestamp);
 
         if (s1)
-                printf(" since [%s; %s]\n", s2, s1);
+                printf(" since %s; %s\n", s2, s1);
         else if (s2)
-                printf(" since [%s]\n", s2);
+                printf(" since %s\n", s2);
         else
                 printf("\n");
 
@@ -1839,6 +1845,8 @@ static int status_property(const char *name, DBusMessageIter *iter, UnitStatusIn
                                 i->where = s;
                         else if (streq(name, "What"))
                                 i->what = s;
+                        else if (streq(name, "Following"))
+                                i->following = s;
                 }
 
                 break;
@@ -3219,6 +3227,7 @@ typedef struct {
 
 static Hashmap *will_install = NULL, *have_installed = NULL;
 static Set *remove_symlinks_to = NULL;
+static unsigned n_symlinks = 0;
 
 static void install_info_free(InstallInfo *i) {
         assert(i);
@@ -3245,7 +3254,7 @@ static int install_info_add(const char *name) {
 
         assert(will_install);
 
-        if (!unit_name_is_valid_no_type(name)) {
+        if (!unit_name_is_valid_no_type(name, true)) {
                 log_warning("Unit name %s is not a valid unit name.", name);
                 return -EINVAL;
         }
@@ -3631,12 +3640,6 @@ static int install_info_symlink_alias(const char *verb, InstallInfo *i, const ch
 
         STRV_FOREACH(s, i->aliases) {
 
-                if (!unit_name_is_valid_no_type(*s)) {
-                        log_error("Invalid name %s.", *s);
-                        r = -EINVAL;
-                        goto finish;
-                }
-
                 free(alias_path);
                 if (!(alias_path = path_make_absolute(*s, config_path))) {
                         log_error("Out of memory");
@@ -3668,7 +3671,7 @@ static int install_info_symlink_wants(const char *verb, InstallInfo *i, const ch
         assert(config_path);
 
         STRV_FOREACH(s, i->wanted_by) {
-                if (!unit_name_is_valid_no_type(*s)) {
+                if (!unit_name_is_valid_no_type(*s, true)) {
                         log_error("Invalid name %s.", *s);
                         r = -EINVAL;
                         goto finish;
@@ -3757,6 +3760,9 @@ static int install_info_apply(const char *verb, LookupPaths *paths, InstallInfo
                 return r;
         }
 
+        n_symlinks += strv_length(i->aliases);
+        n_symlinks += strv_length(i->wanted_by);
+
         fclose(f);
 
         if ((r = install_info_symlink_alias(verb, i, config_path)) != 0)
@@ -3857,19 +3863,24 @@ static int enable_unit(DBusConnection *bus, char **args, unsigned n) {
 
         if (streq(verb, "is-enabled"))
                 r = r > 0 ? 0 : -ENOENT;
-        else if (bus &&
-                 /* Don't try to reload anything if the user asked us to not do this */
-                 !arg_no_reload &&
-                 /* Don't try to reload anything when updating a unit globally */
-                 !arg_global &&
-                 /* Don't try to reload anything if we are called for system changes but the system wasn't booted with systemd */
-                 (arg_session || sd_booted() > 0) &&
-                 /* Don't try to reload anything if we are running in a chroot environment */
-                 (arg_session || running_in_chroot() <= 0) ) {
-                int q;
+        else {
+                if (n_symlinks <= 0)
+                        log_warning("Unit files contain no applicable installation information. Ignoring.");
+
+                if (bus &&
+                    /* Don't try to reload anything if the user asked us to not do this */
+                    !arg_no_reload &&
+                    /* Don't try to reload anything when updating a unit globally */
+                    !arg_global &&
+                    /* Don't try to reload anything if we are called for system changes but the system wasn't booted with systemd */
+                    (arg_session || sd_booted() > 0) &&
+                    /* Don't try to reload anything if we are running in a chroot environment */
+                    (arg_session || running_in_chroot() <= 0) ) {
+                        int q;
 
-                if ((q = daemon_reload(bus, args, n)) < 0)
-                        r = q;
+                        if ((q = daemon_reload(bus, args, n)) < 0)
+                                r = q;
+                }
         }
 
 finish: