#include "escape.h"
#include "fd-util.h"
#include "fileio-label.h"
-#include "formats-util.h"
+#include "format-util.h"
#include "fs-util.h"
#include "logind.h"
#include "mkdir.h"
#include "path-util.h"
#include "process-util.h"
#include "selinux-util.h"
-//#include "sleep-config.h"
+#include "sleep-config.h"
//#include "special.h"
#include "strv.h"
#include "terminal-util.h"
#include "udev-util.h"
#include "unit-name.h"
#include "user-util.h"
-//#include "utmp-wtmp.h"
+#include "utmp-wtmp.h"
+
+/// Additional includes needed by elogind
+#include "elogind-dbus.h"
+//#include "update-utmp.h"
int manager_get_session_from_creds(Manager *m, sd_bus_message *message, const char *name, sd_bus_error *error, Session **ret) {
_cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
return sd_bus_message_append(reply, "t", (uint64_t) hashmap_size(m->sessions));
}
+static int property_get_current_inhibitors(
+ sd_bus *bus,
+ const char *path,
+ const char *interface,
+ const char *property,
+ sd_bus_message *reply,
+ void *userdata,
+ sd_bus_error *error) {
+
+ Manager *m = userdata;
+
+ assert(bus);
+ assert(reply);
+ assert(m);
+
+ return sd_bus_message_append(reply, "t", (uint64_t) hashmap_size(m->inhibitors));
+}
+
static int method_get_session(sd_bus_message *message, void *userdata, sd_bus_error *error) {
_cleanup_free_ char *p = NULL;
Manager *m = userdata;
session->create_message = sd_bus_message_ref(message);
- /* Here upstream systemd starts cgroups and the user systemd,
- and arranges to reply asynchronously. We reply
- directly. */
+#if 0 /// UNNEEDED by elogind
+ /* Now, let's wait until the slice unit and stuff got
+ * created. We send the reply back from
+ * session_send_create_reply(). */
+#else
+ /* We reply directly. */
r = session_send_create_reply(session, NULL);
if (r < 0)
goto fail;
+#endif // 0
return 1;
if (r < 0)
return r;
+#if 1 /// elogind must queue this session
session_add_to_gc_queue(session);
-
+#endif // 1
return sd_bus_reply_method_return(message, NULL);
}
} else {
struct dirent *de;
- while ((de = readdir(d))) {
-
+ FOREACH_DIRENT_ALL(de, d, break) {
if (!dirent_is_file(de))
continue;
return sd_bus_reply_method_return(message, NULL);
}
+#if 0 /// elogind has its own variant in elogind-dbus.c
static int have_multiple_sessions(
Manager *m,
uid_t uid) {
static int bus_manager_log_shutdown(
Manager *m,
InhibitWhat w,
-#if 0 /// elogind does not support systemd units
const char *unit_name) {
const char *p, *q;
p = "MESSAGE=System is shutting down";
q = NULL;
}
-#else
- HandleAction action) {
-
- const char *p, *q;
-
- assert(m);
-
- if (w != INHIBIT_SHUTDOWN)
- return 0;
-
- switch (action) {
- case HANDLE_POWEROFF:
- p = "MESSAGE=System is powering down.";
- q = "SHUTDOWN=power-off";
- break;
- case HANDLE_HALT:
- p = "MESSAGE=System is halting.";
- q = "SHUTDOWN=halt";
- break;
- case HANDLE_REBOOT:
- p = "MESSAGE=System is rebooting.";
- q = "SHUTDOWN=reboot";
- break;
- case HANDLE_KEXEC:
- p = "MESSAGE=System is rebooting with kexec.";
- q = "SHUTDOWN=kexec";
- break;
- default:
- p = "MESSAGE=System is shutting down.";
- q = NULL;
- }
-#endif // 0
+
if (isempty(m->wall_message))
p = strjoina(p, ".");
else
q,
NULL);
}
+#endif // 0
static int lid_switch_ignore_handler(sd_event_source *e, uint64_t usec, void *userdata) {
Manager *m = userdata;
}
}
+#if 0 /// elogind has its own variant in elogind-dbus.c
static int execute_shutdown_or_sleep(
Manager *m,
InhibitWhat w,
- HandleAction action,
+ const char *unit_name,
sd_bus_error *error) {
-#if 0 /// elogind does not need these, we do it ourselves
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
char *c = NULL;
const char *p;
-#endif // 0
int r;
assert(m);
assert(w >= 0);
assert(w < _INHIBIT_WHAT_MAX);
+ assert(unit_name);
- bus_manager_log_shutdown(m, w, action);
+ bus_manager_log_shutdown(m, w, unit_name);
-#if 0 /// elogind does it directly without depending on systemd running the system
if (m->shutdown_dry_run) {
log_info("Running in dry run, suppressing action.");
reset_scheduled_shutdown(m);
} else {
- 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");
-#else
- r = shutdown_or_sleep(m, action);
-
- /* no more pending actions, whether this failed or not */
- m->pending_action = HANDLE_IGNORE;
- m->action_what = 0;
-#endif // 0
- if (r < 0)
- return r;
+ 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;
-#if 0 /// elogind neither needs a dbus reply, nor supports systemd action jobs
- r = sd_bus_message_read(reply, "o", &p);
- if (r < 0)
- return r;
+ r = sd_bus_message_read(reply, "o", &p);
+ if (r < 0)
+ return r;
- c = strdup(p);
- if (!c)
- return -ENOMEM;
+ c = strdup(p);
+ if (!c)
+ return -ENOMEM;
}
m->action_unit = unit_name;
free(m->action_job);
m->action_job = c;
m->action_what = w;
-#endif // 0
/* Make sure the lid switch is ignored for a while */
manager_set_lid_switch_ignore(m, now(CLOCK_MONOTONIC) + m->holdoff_timeout_usec);
assert(manager);
- if ( (0 == manager->action_what) || (HANDLE_IGNORE == manager->pending_action) )
+ if (manager->action_what == 0 || manager->action_job)
return 0;
if (manager_is_inhibited(manager, manager->action_what, INHIBIT_DELAY, NULL, false, false, 0, &offending)) {
}
/* Actually do the operation */
- r = execute_shutdown_or_sleep(manager, manager->action_what, manager->pending_action, &error);
+ r = execute_shutdown_or_sleep(manager, manager->action_what, manager->action_unit, &error);
if (r < 0) {
log_warning("Failed to send delayed message: %s", bus_error_message(&error, r));
- manager->pending_action = HANDLE_IGNORE;
- manager->action_what = 0;
+ manager->action_unit = NULL;
+ manager->action_what = 0;
return r;
}
static int delay_shutdown_or_sleep(
Manager *m,
InhibitWhat w,
- HandleAction action) {
+ const char *unit_name) {
int r;
usec_t timeout_val;
assert(m);
assert(w >= 0);
assert(w < _INHIBIT_WHAT_MAX);
+ assert(unit_name);
timeout_val = now(CLOCK_MONOTONIC) + m->inhibit_delay_max;
return r;
}
- m->pending_action = action;
+ m->action_unit = unit_name;
m->action_what = w;
return 0;
int bus_manager_shutdown_or_sleep_now_or_later(
Manager *m,
- HandleAction action,
+ const char *unit_name,
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);
m->inhibit_delay_max > 0 &&
manager_is_inhibited(m, w, INHIBIT_DELAY, NULL, false, false, 0, NULL);
+ log_debug_elogind("%s called for %s (%sdelayed)", __FUNCTION__,
+ handle_action_to_string(action),
+ delayed ? "" : "NOT ");
if (delayed)
/* Shutdown is delayed, keep in mind what we
* want to do, and start a timeout */
- r = delay_shutdown_or_sleep(m, w, action);
+ r = delay_shutdown_or_sleep(m, w, unit_name);
else
/* Shutdown is not delayed, execute it
* immediately */
- r = execute_shutdown_or_sleep(m, w, action, error);
+ r = execute_shutdown_or_sleep(m, w, unit_name, error);
return r;
}
static int method_do_shutdown_or_sleep(
Manager *m,
sd_bus_message *message,
- HandleAction sleep_action,
+ const char *unit_name,
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);
if (r < 0)
return r;
+ log_debug_elogind("%s called with action '%s', sleep '%s' (%sinteractive)",
+ __FUNCTION__, action, sleep_verb,
+ interactive ? "" : "NOT ");
/* Don't allow multiple jobs being executed at the same time */
if (m->action_what)
return sd_bus_error_setf(error, BUS_ERROR_OPERATION_IN_PROGRESS, "There's already a shutdown or sleep operation in progress");
if (sleep_verb) {
- r = can_sleep(m, sleep_verb);
+ r = can_sleep(sleep_verb);
if (r < 0)
return r;
r = verify_shutdown_creds(m, message, w, interactive, action, action_multiple_sessions,
action_ignore_inhibit, error);
+ log_debug_elogind("verify_shutdown_creds() returned %d", r);
if (r != 0)
return r;
- r = bus_manager_shutdown_or_sleep_now_or_later(m, sleep_action, w, error);
+ r = bus_manager_shutdown_or_sleep_now_or_later(m, unit_name, w, error);
if (r < 0)
return r;
static int method_poweroff(sd_bus_message *message, void *userdata, sd_bus_error *error) {
Manager *m = userdata;
+ log_debug_elogind("%s called", __FUNCTION__);
return method_do_shutdown_or_sleep(
m, message,
- HANDLE_POWEROFF,
+ SPECIAL_POWEROFF_TARGET,
INHIBIT_SHUTDOWN,
"org.freedesktop.login1.power-off",
"org.freedesktop.login1.power-off-multiple-sessions",
static int method_reboot(sd_bus_message *message, void *userdata, sd_bus_error *error) {
Manager *m = userdata;
+ log_debug_elogind("%s called", __FUNCTION__);
return method_do_shutdown_or_sleep(
m, message,
- HANDLE_REBOOT,
+ SPECIAL_REBOOT_TARGET,
INHIBIT_SHUTDOWN,
"org.freedesktop.login1.reboot",
"org.freedesktop.login1.reboot-multiple-sessions",
static int method_suspend(sd_bus_message *message, void *userdata, sd_bus_error *error) {
Manager *m = userdata;
+ log_debug_elogind("%s called", __FUNCTION__);
return method_do_shutdown_or_sleep(
m, message,
- HANDLE_SUSPEND,
+ SPECIAL_SUSPEND_TARGET,
INHIBIT_SLEEP,
"org.freedesktop.login1.suspend",
"org.freedesktop.login1.suspend-multiple-sessions",
"suspend",
error);
}
+#endif // 0
static int nologin_timeout_handler(
sd_event_source *s,
return 0;
fail:
- (void) unlink(temp_path);
- (void) unlink("/run/systemd/shutdown/scheduled");
+ (void) unlink(temp_path);
+ (void) unlink("/run/systemd/shutdown/scheduled");
return log_error_errno(r, "Failed to write information about scheduled shutdowns: %m");
}
+#if 0 /// elogind has its own variant in elogind-dbus.c
static int manager_scheduled_shutdown_handler(
sd_event_source *s,
uint64_t usec,
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
Manager *m = userdata;
- HandleAction action;
+ const char *target;
int r;
assert(m);
return 0;
if (streq(m->scheduled_shutdown_type, "halt"))
- action = HANDLE_HALT;
+ target = SPECIAL_HALT_TARGET;
else if (streq(m->scheduled_shutdown_type, "poweroff"))
- action = HANDLE_POWEROFF;
+ target = SPECIAL_POWEROFF_TARGET;
else
- action = HANDLE_REBOOT;
+ target = SPECIAL_REBOOT_TARGET;
- r = execute_shutdown_or_sleep(m, 0, action, &error);
+ r = execute_shutdown_or_sleep(m, 0, target, &error);
if (r < 0)
- return log_error_errno(r, "Unable to execute transition to %s: %m", m->scheduled_shutdown_type);
+ return log_error_errno(r, "Unable to execute transition to %s: %m", target);
return 0;
}
+#endif // 0
static int method_schedule_shutdown(sd_bus_message *message, void *userdata, sd_bus_error *error) {
Manager *m = userdata;
assert(m);
assert(message);
+ log_debug_elogind("%s called", __FUNCTION__);
r = sd_bus_message_read(message, "st", &type, &elapse);
if (r < 0)
return r;
}
}
-#if 0 /// elogind does not support utmp-wtmp
r = manager_setup_wall_message_timer(m);
if (r < 0)
return r;
-#endif // 0
if (!isempty(type)) {
r = update_schedule_file(m);
static int method_cancel_scheduled_shutdown(sd_bus_message *message, void *userdata, sd_bus_error *error) {
Manager *m = userdata;
bool cancelled;
+#if 1 /// elogind needs to construct the message to allow extra wall messages
+ _cleanup_free_ char *l = NULL;
+#endif // 1
assert(m);
assert(message);
+ log_debug_elogind("%s called", __FUNCTION__);
cancelled = m->scheduled_shutdown_type != NULL;
reset_scheduled_shutdown(m);
-#if 0 /// elogind does not support utmp-wtmp
if (cancelled) {
_cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
const char *tty = NULL;
(void) sd_bus_creds_get_tty(creds, &tty);
}
+#if 0 /// elogind wants to allow extra cancellation messages
utmp_wall("The system shutdown has been cancelled",
uid_to_name(uid), tty, logind_wall_tty_filter, m);
- }
+#else
+ r = asprintf(&l, "%s%sThe system shutdown has been cancelled!",
+ strempty(m->wall_message),
+ isempty(m->wall_message) ? "" : "\n");
+ if (r < 0) {
+ log_oom();
+ return 0;
+ }
+
+ utmp_wall(l, uid_to_name(uid), tty, logind_wall_tty_filter, m);
#endif // 0
+ }
return sd_bus_reply_method_return(message, "b", cancelled);
}
+#if 0 /// elogind has its own variant in elogind-dbus.c
static int method_hibernate(sd_bus_message *message, void *userdata, sd_bus_error *error) {
Manager *m = userdata;
+ log_debug_elogind("%s called", __FUNCTION__);
return method_do_shutdown_or_sleep(
m, message,
- HANDLE_HIBERNATE,
+ SPECIAL_HIBERNATE_TARGET,
INHIBIT_SLEEP,
"org.freedesktop.login1.hibernate",
"org.freedesktop.login1.hibernate-multiple-sessions",
static int method_hybrid_sleep(sd_bus_message *message, void *userdata, sd_bus_error *error) {
Manager *m = userdata;
+ log_debug_elogind("%s called", __FUNCTION__);
return method_do_shutdown_or_sleep(
m, message,
- HANDLE_HYBRID_SLEEP,
+ SPECIAL_HYBRID_SLEEP_TARGET,
INHIBIT_SLEEP,
"org.freedesktop.login1.hibernate",
"org.freedesktop.login1.hibernate-multiple-sessions",
"hybrid-sleep",
error);
}
+#endif // 0
static int method_can_shutdown_or_sleep(
Manager *m,
assert(action_ignore_inhibit);
if (sleep_verb) {
+#if 0 /// elogind needs to have the manager being passed
+ r = can_sleep(sleep_verb);
+#else
r = can_sleep(m, sleep_verb);
+#endif // 0
if (r < 0)
return r;
if (r == 0)
return sd_bus_message_append(reply, "b", r > 0);
#else
- return sd_bus_message_append(reply, "b", -EOPNOTSUPP);
+ return sd_bus_message_append(reply, "b", false);
#endif // 0
}
return sd_bus_reply_method_return(message, "s", result);
#else
- return sd_bus_reply_method_return(message, "s", "na");
+ return sd_bus_reply_method_return(message, "s", "no");
#endif // 0
}
if (r < 0)
return r;
+#if 0 /// elogind only calls this for shutdown/reboot, which already needs authorization.
r = bus_verify_polkit_async(message,
CAP_SYS_ADMIN,
"org.freedesktop.login1.set-wall-message",
return r;
if (r == 0)
return 1; /* Will call us back */
+#endif // 0
if (isempty(wall_message))
m->wall_message = mfree(m->wall_message);
if (r < 0)
return r;
+ if (hashmap_size(m->inhibitors) >= m->inhibitors_max)
+ return sd_bus_error_setf(error, SD_BUS_ERROR_LIMITS_EXCEEDED, "Maximum number of inhibitors (%" PRIu64 ") reached, refusing further inhibitors.", m->inhibitors_max);
+
do {
id = mfree(id);
SD_BUS_WRITABLE_PROPERTY("EnableWallMessages", "b", NULL, NULL, offsetof(Manager, enable_wall_messages), 0),
SD_BUS_WRITABLE_PROPERTY("WallMessage", "s", NULL, NULL, offsetof(Manager, wall_message), 0),
-// SD_BUS_PROPERTY("NAutoVTs", "u", NULL, offsetof(Manager, n_autovts), SD_BUS_VTABLE_PROPERTY_CONST),
+#if 0 /// UNNEEDED by elogind
+ SD_BUS_PROPERTY("NAutoVTs", "u", NULL, offsetof(Manager, n_autovts), SD_BUS_VTABLE_PROPERTY_CONST),
+#endif // 0
SD_BUS_PROPERTY("KillOnlyUsers", "as", NULL, offsetof(Manager, kill_only_users), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("KillExcludeUsers", "as", NULL, offsetof(Manager, kill_exclude_users), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("KillUserProcesses", "b", NULL, offsetof(Manager, kill_user_processes), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("Docked", "b", property_get_docked, 0, 0),
SD_BUS_PROPERTY("RemoveIPC", "b", bus_property_get_bool, offsetof(Manager, remove_ipc), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("RuntimeDirectorySize", "t", bus_property_get_size, offsetof(Manager, runtime_dir_size), SD_BUS_VTABLE_PROPERTY_CONST),
+ SD_BUS_PROPERTY("InhibitorsMax", "t", NULL, offsetof(Manager, inhibitors_max), SD_BUS_VTABLE_PROPERTY_CONST),
+ SD_BUS_PROPERTY("NCurrentInhibitors", "t", property_get_current_inhibitors, 0, 0),
SD_BUS_PROPERTY("SessionsMax", "t", NULL, offsetof(Manager, sessions_max), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("NCurrentSessions", "t", property_get_current_sessions, 0, 0),
SD_BUS_PROPERTY("UserTasksMax", "t", NULL, offsetof(Manager, user_tasks_max), SD_BUS_VTABLE_PROPERTY_CONST),
assert(manager);
assert(slice);
+ assert(job);
r = sd_bus_message_new_method_call(
manager->bus,
if (r < 0)
return r;
- if (job)
- return strdup_job(reply, job);
- return 1;
+ return strdup_job(reply, job);
}
int manager_start_scope(
assert(manager);
assert(scope);
assert(pid > 1);
+ assert(job);
r = sd_bus_message_new_method_call(
manager->bus,
if (r < 0)
return r;
- if (job)
- return strdup_job(reply, job);
- return 1;
+ return strdup_job(reply, job);
}
int manager_start_unit(Manager *manager, const char *unit, sd_bus_error *error, char **job) {
assert(manager);
assert(unit);
+ assert(job);
r = sd_bus_call_method(
manager->bus,
if (r < 0)
return r;
- if (job)
- return strdup_job(reply, job);
- return 1;
+ return strdup_job(reply, job);
}
int manager_stop_unit(Manager *manager, const char *unit, sd_bus_error *error, char **job) {
assert(manager);
assert(unit);
+ assert(job);
r = sd_bus_call_method(
manager->bus,
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;
-
+ *job = NULL;
sd_bus_error_free(error);
return 0;
}
return r;
}
- if (job)
- return strdup_job(reply, job);
- return 1;
+ return strdup_job(reply, job);
}
int manager_abandon_scope(Manager *manager, const char *scope, sd_bus_error *error) {