imsd: logind's block inhibitor for sleep while a call is in the calls table

Nothing of imsd runs while the phone is suspended: a suspend under a
call stops the daemon and the call's media leg for as long as the phone
sleeps. On the dev phone (Plasma Mobile, the sleep targets unmasked for
the test, a call placed on the bus) logind listed one block inhibitor
for sleep under a call, imsd's, and PowerDevil's policy agent none of
its own.

While the calls table holds a call the main thread holds logind's block
inhibitor for sleep (Inhibit with "sleep", "imsd", "a call", "block"):
from the call's addition to the table, which is an incoming call's
announcement and for a Dial follows the recovery's try on a dead flow
and the refresh of the P-Access-Network-Info, to its removal. Under it
logind refused a plain "systemctl suspend" on the dev phone (below); a
request can tell logind to ignore inhibitors (systemctl suspend -i). The
engine starts before the bus connection is there: a lock wanted by then
is asked for when the connection comes.

On the dev phone (systemd 262.2) logind answered Inhibit for this lock
OperationInProgress in a sleep hook before a suspend, in one after it,
and to imsd 0.1 to 0.3 s after the resume, and gave it once it had
logged the operation's "finished". So a lock that was not given and is
still wanted is asked for again when logind sends PrepareForSleep with
false. Where logind gives none at all (the session bus of --session has
no logind) calls go on without one, and the journal says so once until a
call's lock is next given.

Tested: the suites (none reaches the change, which is the main
thread's). On the workstation the daemon of this commit and 1.2.4's ran
with a stand-in logind that prints every Inhibit and every lock's
release and refuses every Inhibit, with logind's OperationInProgress
error, while a file says a sleep's operation runs (the fp6 journal's
2026-11-25-through-sleep capture, bench/bench-sleep.sh): one lock per
call, taken within 1.5 s of the Dial and released within 1.5 s after the
call's end, for a call answered and hung up, a call the network refused
and two calls in a row; a call placed while the stand-in refused had its
lock within 0.5 s of the stand-in's PrepareForSleep(false); with no
logind on the bus the call ran as with one; 1.2.4 asked for no lock. On
the dev phone (KPN, the daemon of this commit, the sleep targets
unmasked for the test; the capture's phone/), under a call to the
carrier's voicemail: the lock in systemd-inhibit's list, "systemctl
suspend" answered "Operation denied due to active block inhibitor" and
no suspend counted, the lock gone after the hang-up; and for a call
placed from a sleep hook at a resume, with logind's operation still
running: the refusal's line, and the lock when logind had said the sleep
was over. Not run: a suspend under a call without the lock; an incoming
call that wakes a sleeping phone; a call announced before the bus
connection is there; a request at a second row's addition or removal (a
Dial refused under a call that has no lock); a second refusal before a
lock is given, which prints no line; an answer of logind's without a
descriptor; a PrepareForSleep whose argument is not one boolean; the
lock wanted where it is held, which asks nothing (a suspend told to
ignore inhibitors under it; a Dial refused under a call that has it); a
desktop's own request for a suspend under the lock.
This commit is contained in:
Jorijn van der Graaf 2026-11-29 14:30:43 +01:00
commit 52d4b4f217
Signed by: jorijnvdgraaf
GPG key ID: 2937E59CDCC1BCFB
2 changed files with 154 additions and 2 deletions

View file

@ -1396,11 +1396,82 @@ GVariant* SmsVariant(const imsd::engine::SmsReceived& m, std::int64_t receivedAt
return g_variant_builder_end(&b);
}
// ---- logind's inhibitor for sleep (main thread only) -----------------------
// Nothing of imsd runs while the phone is suspended: a suspend under a
// call stops this daemon and the call's media leg for as long as the
// phone sleeps. So imsd takes logind's block inhibitor for sleep while
// the calls table holds a call: from the call's addition (an incoming
// call's ringing; for a Dial after the recovery's try on a dead flow and
// the PANI refresh) to its removal. Under it logind refused a plain
// `systemctl suspend` on the dev phone; a request can tell it to ignore
// inhibitors (`systemctl suspend -i`).
// Where it was asked for during a suspend's operation logind refused this lock (systemd 262 on the
// dev phone: refused in a suspend's hooks before and after the sleep and
// to a call 0.1 to 0.3 s after the resume, given once logind had logged
// the operation's "finished"). So a lock that is wanted and not held is
// asked for again when logind sends PrepareForSleep with false; one
// wanted before the bus connection is there, when it comes.
// Where logind gives no lock at all (the session bus of --session has no
// logind) calls go on without one. The journal says so once until a
// call's lock is next given.
struct SleepLock {
const char* why; // logind's "why": what the lock is held for
bool told; // its taking, its release and a refusal go to the journal
bool want = false;
int fd = -1; // the inhibitor's file descriptor (-1: not held)
};
SleepLock CallLock{"a call", true};
bool NoSleepLockTold = false; // the journal has a refusal's line since a call's lock was last given
void HoldSleepOff(SleepLock& lock, bool want) {
lock.want = want;
if (want == (lock.fd >= 0)) return;
if (!want) {
close(lock.fd);
lock.fd = -1;
if (lock.told) Log("sleep is no longer held off (no call)");
return;
}
if (!DbusConn) return;
GError* err = nullptr;
GUnixFDList* fds = nullptr;
GVariant* r = g_dbus_connection_call_with_unix_fd_list_sync(
DbusConn, "org.freedesktop.login1", "/org/freedesktop/login1", "org.freedesktop.login1.Manager", "Inhibit",
g_variant_new("(ssss)", "sleep", "imsd", lock.why, "block"), G_VARIANT_TYPE("(h)"),
G_DBUS_CALL_FLAGS_NONE, 2000, nullptr, &fds, nullptr, &err);
if (r) {
gint32 handle = -1;
g_variant_get(r, "(h)", &handle);
g_variant_unref(r);
if (fds && handle >= 0) lock.fd = g_unix_fd_list_get(fds, handle, nullptr);
}
if (fds) g_object_unref(fds);
if (lock.fd >= 0) {
if (lock.told) {
Log("sleep is held off while the call lasts (logind's inhibitor)");
NoSleepLockTold = false;
}
} else if (lock.told && !NoSleepLockTold) {
Log(std::format("no sleep inhibitor from logind ({}): asked again when it says a sleep is over", err ? err->message : "no file descriptor in its answer"));
NoSleepLockTold = true;
}
g_clear_error(&err);
}
// logind's PrepareForSleep: at one with false, a lock that is wanted and
// not held is asked for.
void OnPrepareForSleep(GDBusConnection*, const gchar*, const gchar*, const gchar*, const gchar*, GVariant* params, gpointer) {
if (!g_variant_is_of_type(params, G_VARIANT_TYPE("(b)"))) return;
gboolean entering = TRUE;
g_variant_get(params, "(b)", &entering);
if (entering) return;
HoldSleepOff(CallLock, CallLock.want);
}
gboolean OnIdleEvent(gpointer data) {
std::unique_ptr<Event> ev(static_cast<Event*>(data));
switch (ev->type) {
case Event::Type::Added: {
Calls[ev->info.uni] = ev->info;
HoldSleepOff(CallLock, true);
EmitSignal("CallAdded", g_variant_new("(s@a{sv})", ev->info.uni.c_str(), CallVariant(ev->info)));
break;
}
@ -1416,6 +1487,7 @@ gboolean OnIdleEvent(gpointer data) {
}
case Event::Type::Deleted:
Calls.erase(ev->uni);
HoldSleepOff(CallLock, !Calls.empty());
EmitSignal("CallDeleted", g_variant_new("(s)", ev->uni.c_str()));
break;
case Event::Type::Registration:
@ -3857,6 +3929,10 @@ const GDBusInterfaceVTable MessagingVtable = { imsd::bus::Gated<HandleMessagingM
void OnBusAcquired(GDBusConnection* conn, const gchar*, gpointer) {
DbusConn = conn;
g_dbus_connection_signal_subscribe(conn, "org.freedesktop.login1", "org.freedesktop.login1.Manager", "PrepareForSleep",
"/org/freedesktop/login1", nullptr, G_DBUS_SIGNAL_FLAGS_NONE, OnPrepareForSleep, nullptr, nullptr);
// the engine began before the bus was there: what it has asked for by now
HoldSleepOff(CallLock, CallLock.want);
GError* err = nullptr;
GDBusNodeInfo* node = g_dbus_node_info_new_for_xml(IntrospectionXml, &err);
if (!node) { Log(std::format("introspection XML: {}", err ? err->message : "?")); if (err) g_error_free(err); return; }