#include <libsyscommon/libgdbus.h>
#include <libsyscommon/libsystemd.h>
-#define DEVICED_CALL_MAX_TRIES 60
-#define DEVICED_CALL_INTERVAL_SECONDS 2
-#define DEVICED_SERVICE_NAME "deviced.service"
+#include <device/power.h>
+
+#define DEVICED_CALL_MAX_SECONDS 120
+#define DEVICED_CALL_INTERVAL_SECONDS 1
+#define DEVICED_SERVICE_NAME "deviced.service"
/* State can be checked manually by calling:
* dbus-send --system --print-reply --dest=org.freedesktop.systemd1 /org/freedesktop/systemd1/unit/deviced_2eservice org.freedesktop.DBus.Properties.Get string:org.freedesktop.systemd1.Unit string:ActiveState
static bool deviced_call_poweroff_once(const char *action, const char *extra_option)
{
+ static bool reboot_triggered = false;
assert(action);
+ if (reboot_triggered)
+ return true;
+
int ret_dbus = extra_option
? gdbus_call_sync_with_reply_int(DEVICED_BUS_NAME, DEVICED_PATH_POWEROFF, DEVICED_INTERFACE_POWEROFF, "PowerOffWithOption", g_variant_new("(ss)", action, extra_option), NULL)
: gdbus_call_sync_with_reply_int(DEVICED_BUS_NAME, DEVICED_PATH_POWEROFF, DEVICED_INTERFACE_POWEROFF, "PowerOff", g_variant_new("(s)", action), NULL)
return false;
} else
CRITICAL_LOG("Succesfully requested shutdown using deviced");
+
+ reboot_triggered = true;
return true;
}
static bool deviced_call_poweroff(const char *action, const char *extra_option)
{
- for (int i = 0; i < DEVICED_CALL_MAX_TRIES; i++) {
+ struct timespec begin, now;
+ device_power_request_lock(POWER_LOCK_CPU, 0);
+ clock_gettime(CLOCK_MONOTONIC, &begin);
+
+ while (true) {
if (service_is_inactive(DEVICED_SERVICE_NAME)) {
CRITICAL_LOG(DEVICED_SERVICE_NAME " is inactive, will not request shutdown to it");
return false;
}
- if (deviced_call_poweroff_once(action, extra_option)) {
- /* Don't exit yet as this will cause "normal" systemd poweroff to happen,
- * likely before deviced poweroff completes
- */
- for (;;)
- pause();
- return true;
- }
-
- CRITICAL_LOG("Failed to request shutdown to deviced (try %d of %d). Will sleep %d seconds.",
- i, DEVICED_CALL_MAX_TRIES, DEVICED_CALL_INTERVAL_SECONDS);
+ deviced_call_poweroff_once(action, extra_option);
+ CRITICAL_LOG("Deviced call poweroff and will sleep %d second.", DEVICED_CALL_INTERVAL_SECONDS);
sleep(DEVICED_CALL_INTERVAL_SECONDS);
+
+ clock_gettime(CLOCK_MONOTONIC, &now);
+ if ((now.tv_sec - begin.tv_sec) >= DEVICED_CALL_MAX_SECONDS)
+ break;
}
CRITICAL_LOG("Failed to shutdown system using deviced - falling back to systemd shutdown");