logind: don't misunderstand UnitRemoved signals during reloading

When PID 1 reloads the units logind/machined will see UnitRemoved
signals for all units. Instead of trusting these immediately, let's
check the actual unit state before considering a unit gone, so that
reloading PID 1 is not mistaken as the end of all sessions.
This commit is contained in:
Lennart Poettering
2013-07-10 23:41:03 +02:00
parent 72673e866a
commit 6797c324a6
4 changed files with 121 additions and 29 deletions
+53 -21
View File
@@ -2427,28 +2427,32 @@ DBusHandlerResult bus_message_filter(
}
session = hashmap_get(m->session_units, unit);
if (session) {
hashmap_remove(m->session_units, session->scope);
free(session->scope);
session->scope = NULL;
session_add_to_gc_queue(session);
}
if (session)
session_add_to_gc_queue(session);
user = hashmap_get(m->user_units, unit);
if (user) {
if (streq_ptr(unit, user->service)) {
hashmap_remove(m->user_units, user->service);
free(user->service);
user->service = NULL;
} else if (streq_ptr(unit, user->slice)) {
hashmap_remove(m->user_units, user->slice);
free(user->slice);
user->slice = NULL;
}
if (user)
user_add_to_gc_queue(user);
} else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "Reloading")) {
dbus_bool_t b;
if (!dbus_message_get_args(message, &error,
DBUS_TYPE_BOOLEAN, &b,
DBUS_TYPE_INVALID)) {
log_error("Failed to parse Reloading message: %s", bus_error_message(&error));
goto finish;
}
/* systemd finished reloading, let's recheck all our sessions */
if (!b) {
Session *session;
Iterator i;
log_debug("System manager has been reloaded, rechecking sessions...");
HASHMAP_FOREACH(session, m->sessions, i)
session_add_to_gc_queue(session);
}
}
@@ -2682,6 +2686,16 @@ int manager_stop_unit(Manager *manager, const char *unit, DBusError *error, char
DBUS_TYPE_STRING, &fail,
DBUS_TYPE_INVALID);
if (r < 0) {
if (dbus_error_has_name(error, BUS_ERROR_NO_SUCH_UNIT) ||
dbus_error_has_name(error, BUS_ERROR_LOAD_FAILED)) {
if (job)
*job = NULL;
dbus_error_free(error);
return 0;
}
log_error("Failed to stop unit %s: %s", unit, bus_error(error, r));
return r;
}
@@ -2704,7 +2718,7 @@ int manager_stop_unit(Manager *manager, const char *unit, DBusError *error, char
*job = copy;
}
return 0;
return 1;
}
int manager_kill_unit(Manager *manager, const char *unit, KillWho who, int signo, DBusError *error) {
@@ -2769,8 +2783,26 @@ int manager_unit_is_active(Manager *manager, const char *unit) {
DBUS_TYPE_STRING, &interface,
DBUS_TYPE_STRING, &property,
DBUS_TYPE_INVALID);
if (r < 0) {
if (dbus_error_has_name(&error, DBUS_ERROR_NO_REPLY) ||
dbus_error_has_name(&error, DBUS_ERROR_DISCONNECTED)) {
/* systemd might have droppped off
* momentarily, let's not make this an
* error */
dbus_error_free(&error);
return true;
}
if (dbus_error_has_name(&error, BUS_ERROR_NO_SUCH_UNIT) ||
dbus_error_has_name(&error, BUS_ERROR_LOAD_FAILED)) {
/* If the unit is already unloaded then it's
* not active */
dbus_error_free(&error);
return false;
}
log_error("Failed to query ActiveState: %s", bus_error(&error, r));
dbus_error_free(&error);
return r;
+12 -1
View File
@@ -1038,12 +1038,23 @@ static int manager_connect_bus(Manager *m) {
"interface='org.freedesktop.DBus.Properties',"
"member='PropertiesChanged'",
&error);
if (dbus_error_is_set(&error)) {
log_error("Failed to add match for PropertiesChanged: %s", bus_error_message(&error));
dbus_error_free(&error);
}
dbus_bus_add_match(m->bus,
"type='signal',"
"sender='org.freedesktop.systemd1',"
"interface='org.freedesktop.systemd1.Manager',"
"member='Reloading',"
"path='/org/freedesktop/systemd1'",
&error);
if (dbus_error_is_set(&error)) {
log_error("Failed to add match for Reloading: %s", bus_error_message(&error));
dbus_error_free(&error);
}
r = bus_method_call_with_reply(
m->bus,
"org.freedesktop.systemd1",
+44 -7
View File
@@ -583,12 +583,28 @@ DBusHandlerResult bus_message_filter(
}
mm = hashmap_get(m->machine_units, unit);
if (mm) {
hashmap_remove(m->machine_units, mm->scope);
free(mm->scope);
mm->scope = NULL;
if (mm)
machine_add_to_gc_queue(mm);
} else if (dbus_message_is_signal(message, "org.freedesktop.systemd1.Manager", "Reloading")) {
dbus_bool_t b;
if (!dbus_message_get_args(message, &error,
DBUS_TYPE_BOOLEAN, &b,
DBUS_TYPE_INVALID)) {
log_error("Failed to parse Reloading message: %s", bus_error_message(&error));
goto finish;
}
/* systemd finished reloading, let's recheck all our machines */
if (!b) {
Machine *mm;
Iterator i;
log_debug("System manager has been reloaded, rechecking machines...");
HASHMAP_FOREACH(mm, m->machines, i)
machine_add_to_gc_queue(mm);
}
}
@@ -727,6 +743,16 @@ int manager_stop_unit(Manager *manager, const char *unit, DBusError *error, char
DBUS_TYPE_STRING, &fail,
DBUS_TYPE_INVALID);
if (r < 0) {
if (dbus_error_has_name(error, BUS_ERROR_NO_SUCH_UNIT) ||
dbus_error_has_name(error, BUS_ERROR_LOAD_FAILED)) {
if (job)
*job = NULL;
dbus_error_free(error);
return 0;
}
log_error("Failed to stop unit %s: %s", unit, bus_error(error, r));
return r;
}
@@ -749,7 +775,7 @@ int manager_stop_unit(Manager *manager, const char *unit, DBusError *error, char
*job = copy;
}
return 0;
return 1;
}
int manager_kill_unit(Manager *manager, const char *unit, KillWho who, int signo, DBusError *error) {
@@ -814,8 +840,19 @@ int manager_unit_is_active(Manager *manager, const char *unit) {
DBUS_TYPE_STRING, &interface,
DBUS_TYPE_STRING, &property,
DBUS_TYPE_INVALID);
if (r < 0) {
if (dbus_error_has_name(&error, DBUS_ERROR_NO_REPLY) ||
dbus_error_has_name(&error, DBUS_ERROR_DISCONNECTED)) {
dbus_error_free(&error);
return true;
}
if (dbus_error_has_name(&error, BUS_ERROR_NO_SUCH_UNIT) ||
dbus_error_has_name(&error, BUS_ERROR_LOAD_FAILED)) {
dbus_error_free(&error);
return false;
}
log_error("Failed to query ActiveState: %s", bus_error(&error, r));
dbus_error_free(&error);
return r;
+12
View File
@@ -233,6 +233,18 @@ static int manager_connect_bus(Manager *m) {
dbus_error_free(&error);
}
dbus_bus_add_match(m->bus,
"type='signal',"
"sender='org.freedesktop.systemd1',"
"interface='org.freedesktop.systemd1.Manager',"
"member='Reloading',"
"path='/org/freedesktop/systemd1'",
&error);
if (dbus_error_is_set(&error)) {
log_error("Failed to add match for Reloading: %s", bus_error_message(&error));
dbus_error_free(&error);
}
r = bus_method_call_with_reply(
m->bus,
"org.freedesktop.systemd1",