session->create_message = sd_bus_message_ref(message);
- /* Now, let's wait until the slice unit and stuff got
- * created. We send the reply back from
- * session_send_create_reply(). */
-
- /* Elogind note: replying directly, since we're not actually
- starting slices and thus we aren't waiting on systemd. */
+ /* Here upstream systemd starts cgroups and the user systemd,
+ and arranges to reply asynchronously. We reply
+ directly. */
r = session_send_create_reply(session, NULL);
if (r < 0)
static int bus_manager_log_shutdown(
Manager *m,
InhibitWhat w,
- const char *unit_name) {
+ HandleAction action) {
const char *p, *q;
assert(m);
- assert(unit_name);
if (w != INHIBIT_SHUTDOWN)
return 0;
- if (streq(unit_name, SPECIAL_POWEROFF_TARGET)) {
+ switch (action) {
+ case HANDLE_POWEROFF:
p = "MESSAGE=System is powering down.";
q = "SHUTDOWN=power-off";
- } else if (streq(unit_name, SPECIAL_HALT_TARGET)) {
+ break;
+ case HANDLE_HALT:
p = "MESSAGE=System is halting.";
q = "SHUTDOWN=halt";
- } else if (streq(unit_name, SPECIAL_REBOOT_TARGET)) {
+ break;
+ case HANDLE_REBOOT:
p = "MESSAGE=System is rebooting.";
q = "SHUTDOWN=reboot";
- } else if (streq(unit_name, SPECIAL_KEXEC_TARGET)) {
+ break;
+ case HANDLE_KEXEC:
p = "MESSAGE=System is rebooting with kexec.";
q = "SHUTDOWN=kexec";
- } else {
+ break;
+ default:
p = "MESSAGE=System is shutting down.";
q = NULL;
+ break;
}
return log_struct(LOG_NOTICE,
static int execute_shutdown_or_sleep(
Manager *m,
InhibitWhat w,
- const char *unit_name,
+ HandleAction action,
sd_bus_error *error) {
-
- _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
- const char *p;
- char *c;
int r;
assert(m);
assert(w >= 0);
assert(w < _INHIBIT_WHAT_MAX);
- assert(unit_name);
-
- bus_manager_log_shutdown(m, w, unit_name);
- r = sd_bus_call_method(
- m->bus,
- "org.freedesktop.systemd1",
- "/org/freedesktop/systemd1",
- "org.freedesktop.systemd1.Manager",
- "StartUnit",
- error,
- &reply,
- "ss", unit_name, "replace-irreversibly");
- if (r < 0)
- return r;
+ bus_manager_log_shutdown(m, w, action);
- r = sd_bus_message_read(reply, "o", &p);
+ r = shutdown_or_sleep(m, action);
if (r < 0)
return r;
- c = strdup(p);
- if (!c)
- return -ENOMEM;
-
- m->action_unit = unit_name;
- free(m->action_job);
- m->action_job = c;
- m->action_what = w;
-
- /* Make sure the lid switch is ignored for a while */
+ /* Make sure the lid switch is ignored for a while (?) */
manager_set_lid_switch_ignore(m, now(CLOCK_MONOTONIC) + m->holdoff_timeout_usec);
return 0;
static int delay_shutdown_or_sleep(
Manager *m,
InhibitWhat w,
- const char *unit_name) {
+ HandleAction action) {
assert(m);
assert(w >= 0);
assert(w < _INHIBIT_WHAT_MAX);
- assert(unit_name);
m->action_timestamp = now(CLOCK_MONOTONIC);
- m->action_unit = unit_name;
+ m->pending_action = action;
m->action_what = w;
return 0;
int bus_manager_shutdown_or_sleep_now_or_later(
Manager *m,
- const char *unit_name,
+ HandleAction action,
InhibitWhat w,
sd_bus_error *error) {
int r;
assert(m);
- assert(unit_name);
assert(w >= 0);
assert(w <= _INHIBIT_WHAT_MAX);
- assert(!m->action_job);
/* Tell everybody to prepare for shutdown/sleep */
send_prepare_for(m, w, true);
if (delayed)
/* Shutdown is delayed, keep in mind what we
* want to do, and start a timeout */
- r = delay_shutdown_or_sleep(m, w, unit_name);
+ r = delay_shutdown_or_sleep(m, w, action);
else
/* Shutdown is not delayed, execute it
* immediately */
- r = execute_shutdown_or_sleep(m, w, unit_name, error);
+ r = execute_shutdown_or_sleep(m, w, action, error);
return r;
}
static int method_do_shutdown_or_sleep(
Manager *m,
sd_bus_message *message,
- const char *unit_name,
+ HandleAction sleep_action,
InhibitWhat w,
const char *action,
const char *action_multiple_sessions,
assert(m);
assert(message);
- assert(unit_name);
assert(w >= 0);
assert(w <= _INHIBIT_WHAT_MAX);
assert(action);
return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
}
- r = bus_manager_shutdown_or_sleep_now_or_later(m, unit_name, w, error);
+ r = bus_manager_shutdown_or_sleep_now_or_later(m, sleep_action, w, error);
if (r < 0)
return r;
return method_do_shutdown_or_sleep(
m, message,
- SPECIAL_POWEROFF_TARGET,
+ HANDLE_POWEROFF,
INHIBIT_SHUTDOWN,
"org.freedesktop.login1.power-off",
"org.freedesktop.login1.power-off-multiple-sessions",
return method_do_shutdown_or_sleep(
m, message,
- SPECIAL_REBOOT_TARGET,
+ HANDLE_REBOOT,
INHIBIT_SHUTDOWN,
"org.freedesktop.login1.reboot",
"org.freedesktop.login1.reboot-multiple-sessions",
return method_do_shutdown_or_sleep(
m, message,
- SPECIAL_SUSPEND_TARGET,
+ HANDLE_SUSPEND,
INHIBIT_SLEEP,
"org.freedesktop.login1.suspend",
"org.freedesktop.login1.suspend-multiple-sessions",
return method_do_shutdown_or_sleep(
m, message,
- SPECIAL_HIBERNATE_TARGET,
+ HANDLE_HIBERNATE,
INHIBIT_SLEEP,
"org.freedesktop.login1.hibernate",
"org.freedesktop.login1.hibernate-multiple-sessions",
return method_do_shutdown_or_sleep(
m, message,
- SPECIAL_HYBRID_SLEEP_TARGET,
+ HANDLE_HYBRID_SLEEP,
INHIBIT_SLEEP,
"org.freedesktop.login1.hibernate",
"org.freedesktop.login1.hibernate-multiple-sessions",
SD_BUS_VTABLE_END
};
-static int session_jobs_reply(Session *s, const char *unit, const char *result) {
- int r = 0;
-
- assert(s);
- assert(unit);
-
- if (!s->started)
- return r;
-
- if (streq(result, "done"))
- r = session_send_create_reply(s, NULL);
- else {
- _cleanup_bus_error_free_ sd_bus_error e = SD_BUS_ERROR_NULL;
-
- sd_bus_error_setf(&e, BUS_ERROR_JOB_FAILED, "Start job for unit %s failed with '%s'", unit, result);
- r = session_send_create_reply(s, &e);
- }
-
- return r;
-}
-
-int match_job_removed(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
- const char *path, *result, *unit;
- Manager *m = userdata;
- Session *session;
- uint32_t id;
- User *user;
- int r;
-
- assert(bus);
- assert(message);
- assert(m);
-
- r = sd_bus_message_read(message, "uoss", &id, &path, &unit, &result);
- if (r < 0) {
- bus_log_parse_error(r);
- return r;
- }
-
- if (m->action_job && streq(m->action_job, path)) {
- log_info("Operation finished.");
-
- /* Tell people that they now may take a lock again */
- send_prepare_for(m, m->action_what, false);
-
- free(m->action_job);
- m->action_job = NULL;
- m->action_unit = NULL;
- m->action_what = 0;
- return 0;
- }
-
- session = hashmap_get(m->session_units, unit);
- if (session) {
-
- if (streq_ptr(path, session->scope_job)) {
- free(session->scope_job);
- session->scope_job = NULL;
- }
-
- session_jobs_reply(session, unit, result);
-
- session_save(session);
- session_add_to_gc_queue(session);
- }
-
- user = hashmap_get(m->user_units, unit);
- if (user) {
-
- if (streq_ptr(path, user->service_job)) {
- free(user->service_job);
- user->service_job = NULL;
- }
-
- if (streq_ptr(path, user->slice_job)) {
- free(user->slice_job);
- user->slice_job = NULL;
- }
-
- LIST_FOREACH(sessions_by_user, session, user->sessions) {
- session_jobs_reply(session, unit, result);
- }
-
- user_save(user);
- user_add_to_gc_queue(user);
- }
-
- return 0;
-}
-
-int match_unit_removed(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
- const char *path, *unit;
- Manager *m = userdata;
- Session *session;
- User *user;
- int r;
-
- assert(bus);
- assert(message);
- assert(m);
-
- r = sd_bus_message_read(message, "so", &unit, &path);
- if (r < 0) {
- bus_log_parse_error(r);
- return r;
- }
-
- session = hashmap_get(m->session_units, unit);
- if (session)
- session_add_to_gc_queue(session);
-
- user = hashmap_get(m->user_units, unit);
- if (user)
- user_add_to_gc_queue(user);
-
- return 0;
-}
-
-int match_properties_changed(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
- _cleanup_free_ char *unit = NULL;
- Manager *m = userdata;
- const char *path;
- Session *session;
- User *user;
- int r;
-
- assert(bus);
- assert(message);
- assert(m);
-
- path = sd_bus_message_get_path(message);
- if (!path)
- return 0;
-
- r = unit_name_from_dbus_path(path, &unit);
- if (r == -EINVAL) /* not a unit */
- return 0;
- if (r < 0)
- return r;
-
- session = hashmap_get(m->session_units, unit);
- if (session)
- session_add_to_gc_queue(session);
-
- user = hashmap_get(m->user_units, unit);
- if (user)
- user_add_to_gc_queue(user);
-
- return 0;
-}
-
-int match_reloading(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
- Manager *m = userdata;
- Session *session;
- Iterator i;
- int b, r;
-
- assert(bus);
-
- r = sd_bus_message_read(message, "b", &b);
- if (r < 0) {
- bus_log_parse_error(r);
- return r;
- }
-
- if (b)
- return 0;
-
- /* systemd finished reloading, let's recheck all our sessions */
- log_debug("System manager has been reloaded, rechecking sessions...");
-
- HASHMAP_FOREACH(session, m->sessions, i)
- session_add_to_gc_queue(session);
-
- return 0;
-}
-
-int match_name_owner_changed(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
- const char *name, *old, *new;
- Manager *m = userdata;
- Session *session;
- Iterator i;
- int r;
-
-
- char *key;
-
- r = sd_bus_message_read(message, "sss", &name, &old, &new);
- if (r < 0) {
- bus_log_parse_error(r);
- return r;
- }
-
- if (isempty(old) || !isempty(new))
- return 0;
-
- key = set_remove(m->busnames, (char*) old);
- if (!key)
- return 0;
-
- /* Drop all controllers owned by this name */
-
- free(key);
-
- HASHMAP_FOREACH(session, m->sessions, i)
- if (session_is_controller(session, old))
- session_drop_controller(session);
-
- return 0;
-}
-
int manager_send_changed(Manager *manager, const char *property, ...) {
char **l;
assert(manager);
- if (manager->action_what == 0 || manager->action_job)
+ if (manager->action_what == 0)
return 0;
/* Continue delay? */
}
/* Actually do the operation */
- r = execute_shutdown_or_sleep(manager, manager->action_what, manager->action_unit, &error);
+ r = execute_shutdown_or_sleep(manager, manager->action_what, manager->pending_action, &error);
if (r < 0) {
log_warning("Failed to send delayed message: %s", bus_error_message(&error, r));
- manager->action_unit = NULL;
+ manager->pending_action = HANDLE_IGNORE;
manager->action_what = 0;
return r;
}
return 1;
}
-
-int manager_start_scope(
- Manager *manager,
- const char *scope,
- pid_t pid,
- const char *slice,
- const char *description,
- const char *after, const char *after2,
- sd_bus_error *error,
- char **job) {
-
- _cleanup_bus_message_unref_ sd_bus_message *m = NULL, *reply = NULL;
- int r;
-
- assert(manager);
- assert(scope);
- assert(pid > 1);
-
- r = sd_bus_message_new_method_call(
- manager->bus,
- &m,
- "org.freedesktop.systemd1",
- "/org/freedesktop/systemd1",
- "org.freedesktop.systemd1.Manager",
- "StartTransientUnit");
- if (r < 0)
- return r;
-
- r = sd_bus_message_append(m, "ss", strempty(scope), "fail");
- if (r < 0)
- return r;
-
- r = sd_bus_message_open_container(m, 'a', "(sv)");
- if (r < 0)
- return r;
-
- if (!isempty(slice)) {
- r = sd_bus_message_append(m, "(sv)", "Slice", "s", slice);
- if (r < 0)
- return r;
- }
-
- if (!isempty(description)) {
- r = sd_bus_message_append(m, "(sv)", "Description", "s", description);
- if (r < 0)
- return r;
- }
-
- if (!isempty(after)) {
- r = sd_bus_message_append(m, "(sv)", "After", "as", 1, after);
- if (r < 0)
- return r;
- }
-
- if (!isempty(after2)) {
- r = sd_bus_message_append(m, "(sv)", "After", "as", 1, after2);
- if (r < 0)
- return r;
- }
-
- /* cgroup empty notification is not available in containers
- * currently. To make this less problematic, let's shorten the
- * stop timeout for sessions, so that we don't wait
- * forever. */
-
- /* Make sure that the session shells are terminated with
- * SIGHUP since bash and friends tend to ignore SIGTERM */
- r = sd_bus_message_append(m, "(sv)", "SendSIGHUP", "b", true);
- if (r < 0)
- return r;
-
- r = sd_bus_message_append(m, "(sv)", "PIDs", "au", 1, pid);
- if (r < 0)
- return r;
-
- r = sd_bus_message_close_container(m);
- if (r < 0)
- return r;
-
- r = sd_bus_message_append(m, "a(sa(sv))", 0);
- if (r < 0)
- return r;
-
- r = sd_bus_call(manager->bus, m, 0, error, &reply);
- if (r < 0)
- return r;
-
- if (job) {
- const char *j;
- char *copy;
-
- r = sd_bus_message_read(reply, "o", &j);
- if (r < 0)
- return r;
-
- copy = strdup(j);
- if (!copy)
- return -ENOMEM;
-
- *job = copy;
- }
-
- return 1;
-}
-
-int manager_start_unit(Manager *manager, const char *unit, sd_bus_error *error, char **job) {
- _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
- int r;
-
- assert(manager);
- assert(unit);
-
- r = sd_bus_call_method(
- manager->bus,
- "org.freedesktop.systemd1",
- "/org/freedesktop/systemd1",
- "org.freedesktop.systemd1.Manager",
- "StartUnit",
- error,
- &reply,
- "ss", unit, "fail");
- if (r < 0)
- return r;
-
- if (job) {
- const char *j;
- char *copy;
-
- r = sd_bus_message_read(reply, "o", &j);
- if (r < 0)
- return r;
-
- copy = strdup(j);
- if (!copy)
- return -ENOMEM;
-
- *job = copy;
- }
-
- return 1;
-}
-
-int manager_stop_unit(Manager *manager, const char *unit, sd_bus_error *error, char **job) {
- _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
- int r;
-
- assert(manager);
- assert(unit);
-
- r = sd_bus_call_method(
- manager->bus,
- "org.freedesktop.systemd1",
- "/org/freedesktop/systemd1",
- "org.freedesktop.systemd1.Manager",
- "StopUnit",
- error,
- &reply,
- "ss", unit, "fail");
- if (r < 0) {
- if (sd_bus_error_has_name(error, BUS_ERROR_NO_SUCH_UNIT) ||
- sd_bus_error_has_name(error, BUS_ERROR_LOAD_FAILED)) {
-
- if (job)
- *job = NULL;
-
- sd_bus_error_free(error);
- return 0;
- }
-
- return r;
- }
-
- if (job) {
- const char *j;
- char *copy;
-
- r = sd_bus_message_read(reply, "o", &j);
- if (r < 0)
- return r;
-
- copy = strdup(j);
- if (!copy)
- return -ENOMEM;
-
- *job = copy;
- }
-
- return 1;
-}
-
-int manager_abandon_scope(Manager *manager, const char *scope, sd_bus_error *error) {
- _cleanup_free_ char *path = NULL;
- int r;
-
- assert(manager);
- assert(scope);
-
- path = unit_dbus_path_from_name(scope);
- if (!path)
- return -ENOMEM;
-
- r = sd_bus_call_method(
- manager->bus,
- "org.freedesktop.systemd1",
- path,
- "org.freedesktop.systemd1.Scope",
- "Abandon",
- error,
- NULL,
- NULL);
- if (r < 0) {
- if (sd_bus_error_has_name(error, BUS_ERROR_NO_SUCH_UNIT) ||
- sd_bus_error_has_name(error, BUS_ERROR_LOAD_FAILED) ||
- sd_bus_error_has_name(error, BUS_ERROR_SCOPE_NOT_RUNNING)) {
- sd_bus_error_free(error);
- return 0;
- }
-
- return r;
- }
-
- return 1;
-}
-
-int manager_kill_unit(Manager *manager, const char *unit, KillWho who, int signo, sd_bus_error *error) {
- assert(manager);
- assert(unit);
-
- return sd_bus_call_method(
- manager->bus,
- "org.freedesktop.systemd1",
- "/org/freedesktop/systemd1",
- "org.freedesktop.systemd1.Manager",
- "KillUnit",
- error,
- NULL,
- "ssi", unit, who == KILL_LEADER ? "main" : "all", signo);
-}
-
-int manager_unit_is_active(Manager *manager, const char *unit) {
- _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
- _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
- _cleanup_free_ char *path = NULL;
- const char *state;
- int r;
-
- assert(manager);
- assert(unit);
-
- path = unit_dbus_path_from_name(unit);
- if (!path)
- return -ENOMEM;
-
- r = sd_bus_get_property(
- manager->bus,
- "org.freedesktop.systemd1",
- path,
- "org.freedesktop.systemd1.Unit",
- "ActiveState",
- &error,
- &reply,
- "s");
- if (r < 0) {
- /* systemd might have droppped off momentarily, let's
- * not make this an error */
- if (sd_bus_error_has_name(&error, SD_BUS_ERROR_NO_REPLY) ||
- sd_bus_error_has_name(&error, SD_BUS_ERROR_DISCONNECTED))
- return true;
-
- /* If the unit is already unloaded then it's not
- * active */
- if (sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_UNIT) ||
- sd_bus_error_has_name(&error, BUS_ERROR_LOAD_FAILED))
- return false;
-
- return r;
- }
-
- r = sd_bus_message_read(reply, "s", &state);
- if (r < 0)
- return -EINVAL;
-
- return !streq(state, "inactive") && !streq(state, "failed");
-}
-
-int manager_job_is_active(Manager *manager, const char *path) {
- _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
- _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
- int r;
-
- assert(manager);
- assert(path);
-
- r = sd_bus_get_property(
- manager->bus,
- "org.freedesktop.systemd1",
- path,
- "org.freedesktop.systemd1.Job",
- "State",
- &error,
- &reply,
- "s");
- if (r < 0) {
- if (sd_bus_error_has_name(&error, SD_BUS_ERROR_NO_REPLY) ||
- sd_bus_error_has_name(&error, SD_BUS_ERROR_DISCONNECTED))
- return true;
-
- if (sd_bus_error_has_name(&error, SD_BUS_ERROR_UNKNOWN_OBJECT))
- return false;
-
- return r;
- }
-
- /* We don't actually care about the state really. The fact
- * that we could read the job state is enough for us */
-
- return true;
-}