chiark / gitweb /
unit: check for unneeded dependencies even when unit stop was expected
[elogind.git] / src / unit.c
index 5006742d9da83f65922c38694c74e2dc226e93c3..03c90f5874908335b448f398e04b6d5e79339403 100644 (file)
@@ -564,8 +564,8 @@ int unit_add_exec_dependencies(Unit *u, ExecContext *c) {
             c->std_output != EXEC_OUTPUT_KMSG_AND_CONSOLE &&
             c->std_output != EXEC_OUTPUT_SYSLOG_AND_CONSOLE &&
             c->std_error != EXEC_OUTPUT_KMSG &&
-            c->std_error != EXEC_OUTPUT_SYSLOG_AND_CONSOLE &&
-            c->std_error != EXEC_OUTPUT_KMSG &&
+            c->std_error != EXEC_OUTPUT_SYSLOG &&
+            c->std_error != EXEC_OUTPUT_KMSG_AND_CONSOLE &&
             c->std_error != EXEC_OUTPUT_SYSLOG_AND_CONSOLE)
                 return 0;
 
@@ -573,7 +573,7 @@ int unit_add_exec_dependencies(Unit *u, ExecContext *c) {
          * logging daemon is run first. */
 
         if (u->meta.manager->running_as == MANAGER_SYSTEM)
-                if ((r = unit_add_two_dependencies_by_name(u, UNIT_REQUIRES, UNIT_AFTER, SPECIAL_LOGGER_SOCKET, NULL, true)) < 0)
+                if ((r = unit_add_two_dependencies_by_name(u, UNIT_REQUIRES, UNIT_AFTER, SPECIAL_STDOUT_SYSLOG_BRIDGE_SOCKET, NULL, true)) < 0)
                         return r;
 
         return 0;
@@ -686,9 +686,17 @@ void unit_dump(Unit *u, FILE *f, const char *prefix) {
                         fprintf(f, "%s\tControlGroup: %s:%s\n",
                                 prefix, b->controller, b->path);
 
-                LIST_FOREACH(by_unit, a, u->meta.cgroup_attributes)
+                LIST_FOREACH(by_unit, a, u->meta.cgroup_attributes) {
+                        char *v = NULL;
+
+                        if (a->map_callback)
+                                a->map_callback(a->controller, a->name, a->value, &v);
+
                         fprintf(f, "%s\tControlGroupAttribute: %s %s \"%s\"\n",
-                                prefix, a->controller, a->name, a->value);
+                                prefix, a->controller, a->name, v ? v : a->value);
+
+                        free(v);
+                }
 
                 if (UNIT_VTABLE(u)->dump)
                         UNIT_VTABLE(u)->dump(u, f, prefix2);
@@ -766,7 +774,7 @@ int unit_add_default_target_dependency(Unit *u, Unit *target) {
         /* If either side wants no automatic dependencies, then let's
          * skip this */
         if (!u->meta.default_dependencies ||
-            target->meta.default_dependencies)
+            !target->meta.default_dependencies)
                 return 0;
 
         /* Don't create loops */
@@ -850,6 +858,7 @@ fail:
         u->meta.load_state = UNIT_ERROR;
         u->meta.load_error = r;
         unit_add_to_dbus_queue(u);
+        unit_add_to_gc_queue(u);
 
         log_debug("Failed to load configuration for %s: %s", u->meta.id, strerror(-r));
 
@@ -880,16 +889,20 @@ int unit_start(Unit *u) {
         if (u->meta.load_state != UNIT_LOADED)
                 return -EINVAL;
 
-        /* If this is already (being) started, then this will
-         * succeed. Note that this will even succeed if this unit is
-         * not startable by the user. This is relied on to detect when
-         * we need to wait for units and when waiting is finished. */
+        /* If this is already started, then this will succeed. Note
+         * that this will even succeed if this unit is not startable
+         * by the user. This is relied on to detect when we need to
+         * wait for units and when waiting is finished. */
         state = unit_active_state(u);
         if (UNIT_IS_ACTIVE_OR_RELOADING(state))
                 return -EALREADY;
 
-        /* If the conditions failed, don't do anything at all */
-        if (!unit_condition_test(u)) {
+        /* If the conditions failed, don't do anything at all. If we
+         * already are activating this call might still be useful to
+         * speed up activation in case there is some hold-off time,
+         * but we don't want to recheck the condition in that case. */
+        if (state != UNIT_ACTIVATING &&
+            !unit_condition_test(u)) {
                 log_debug("Starting of %s requested but condition failed. Ignoring.", u->meta.id);
                 return -EALREADY;
         }
@@ -912,7 +925,7 @@ int unit_start(Unit *u) {
 
         unit_add_to_dbus_queue(u);
 
-        unit_status_printf(u, "Starting %s...\n", unit_description(u));
+        unit_status_printf(u, NULL, "Starting %s...", unit_description(u));
         return UNIT_VTABLE(u)->start(u);
 }
 
@@ -954,7 +967,7 @@ int unit_stop(Unit *u) {
 
         unit_add_to_dbus_queue(u);
 
-        unit_status_printf(u, "Stopping %s...\n", unit_description(u));
+        unit_status_printf(u, NULL, "Stopping %s...", unit_description(u));
         return UNIT_VTABLE(u)->stop(u);
 }
 
@@ -1019,19 +1032,19 @@ static void unit_check_unneeded(Unit *u) {
                 return;
 
         SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRED_BY], i)
-                if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
+                if (unit_pending_active(other))
                         return;
 
         SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRED_BY_OVERRIDABLE], i)
-                if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
+                if (unit_pending_active(other))
                         return;
 
         SET_FOREACH(other, u->meta.dependencies[UNIT_WANTED_BY], i)
-                if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
+                if (unit_pending_active(other))
                         return;
 
         SET_FOREACH(other, u->meta.dependencies[UNIT_BOUND_BY], i)
-                if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
+                if (unit_pending_active(other))
                         return;
 
         log_info("Service %s is not needed anymore. Stopping.", u->meta.id);
@@ -1092,6 +1105,14 @@ static void retroactively_stop_dependencies(Unit *u) {
         SET_FOREACH(other, u->meta.dependencies[UNIT_BOUND_BY], i)
                 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
                         manager_add_job(u->meta.manager, JOB_STOP, other, JOB_REPLACE, true, NULL, NULL);
+}
+
+static void check_unneeded_dependencies(Unit *u) {
+        Iterator i;
+        Unit *other;
+
+        assert(u);
+        assert(UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(u)));
 
         /* Garbage collect services that might not be needed anymore, if enabled */
         SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRES], i)
@@ -1250,6 +1271,10 @@ void unit_notify(Unit *u, UnitActiveState os, UnitActiveState ns, bool reload_su
                                 retroactively_stop_dependencies(u);
                 }
 
+                /* stop unneeded units regardless if going down was expected or not */
+                if (UNIT_IS_ACTIVE_OR_ACTIVATING(os) && UNIT_IS_INACTIVE_OR_DEACTIVATING(ns))
+                        check_unneeded_dependencies(u);
+
                 if (ns != os && ns == UNIT_FAILED) {
                         log_notice("Unit %s entered failed state.", u->meta.id);
                         unit_trigger_on_failure(u);
@@ -2414,8 +2439,11 @@ int unit_coldplug(Unit *u) {
         return 0;
 }
 
-void unit_status_printf(Unit *u, const char *format, ...) {
+void unit_status_printf(Unit *u, const char *status, const char *format, ...) {
         va_list ap;
+        char *s, *e;
+        int err;
+        const unsigned emax = status ? 80 - (sizeof("[  OK  ]")-1) : 80;
 
         assert(u);
         assert(format);
@@ -2423,21 +2451,28 @@ void unit_status_printf(Unit *u, const char *format, ...) {
         if (!UNIT_VTABLE(u)->show_status)
                 return;
 
-        if (u->meta.manager->running_as != MANAGER_SYSTEM)
-                return;
-
-        /* If Plymouth is running make sure we show the status, so
-         * that there's something nice to see when people press Esc */
-
-        if (!u->meta.manager->show_status && !plymouth_running())
+        if (!manager_get_show_status(u->meta.manager))
                 return;
 
         if (!manager_is_booting_or_shutting_down(u->meta.manager))
                 return;
 
         va_start(ap, format);
-        status_vprintf(format, ap);
+        err = vasprintf(&s, format, ap);
         va_end(ap);
+        if (err < 0)
+                return;
+
+        e = ellipsize(s, emax, 100);
+        free(s);
+        if (!e)
+                return;
+
+        if (status)
+                status_printf("%s%*s[%s]\n", e, emax - strlen(e), "", status);
+        else
+                status_printf("%s\n", e);
+        free(e);
 }
 
 bool unit_need_daemon_reload(Unit *u) {
@@ -2495,7 +2530,7 @@ bool unit_pending_inactive(Unit *u) {
 bool unit_pending_active(Unit *u) {
         assert(u);
 
-        /* Returns true if the unit is inactive or going down */
+        /* Returns true if the unit is active or going up */
 
         if (UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(u)))
                 return true;