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

@ -166,7 +166,11 @@ before any response to its INVITE (1.2.4, §7.6) has its decisions in
and when the call ends instead. The shell's part (`MaybeResendInvite`: the dead flow closed and when the call ends instead. The shell's part (`MaybeResendInvite`: the dead flow closed
without a FIN's exchange, the one connect, the policy's word on whether the without a FIN's exchange, the one connect, the policy's word on whether the
old registration may be reconnected over) ran against a stand-in network on old registration may be reconnected over) ran against a stand-in network on
the workstation and on the dev phone (§5). SMS over IP (1.2.0, §7.9) keeps in the the workstation and on the dev phone (§5). The sleep inhibitor for a call
(1.2.5, §7.6) is all in the shell (`HoldSleepOff` and its callers);
what of it ran against a stand-in logind on the workstation and what on
the dev phone is in §5. SMS over IP
(1.2.0, §7.9) keeps in the
shell, among others, the service centre's source (`SMSC=` over EF_SMSP, `LoadSmsParameters`), shell, among others, the service centre's source (`SMSC=` over EF_SMSP, `LoadSmsParameters`),
the three counters (TP-MR, RP-MR, the concatenation reference), their the three counters (TP-MR, RP-MR, the concatenation reference), their
seeding and the TP-MR file, the routing of an inbound MESSAGE seeding and the TP-MR file, the routing of an inbound MESSAGE
@ -1115,6 +1119,37 @@ Rules:
network makes of a number that skips one, as a REGISTER that was sent network makes of a number that skips one, as a REGISTER that was sent
and never arrived leaves it (the stand-in took one; no phone run tells and never arrived leaves it (the stand-in took one; no phone run tells
whether its network saw one). whether its network saw one).
- The call's sleep inhibitor (§7.6) is the main thread's code, and no suite
reaches it. On the workstation the daemon ran with a stand-in logind
on the bench's bus, which 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, systemd 262.2, Plasma Mobile, the sleep targets unmasked
for the test; `phone/` there), under a
call to the carrier's voicemail placed on the bus: imsd's lock the
only block inhibitor for sleep in `systemd-inhibit --list`, none in
the list of PowerDevil's policy agent, `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.
- `imsd-pcscf` ran on the dev phone (KPN) on 2026-10-11 against the live ims - `imsd-pcscf` ran on the dev phone (KPN) on 2026-10-11 against the live ims
bearer: the `--mux 1 --profile 2 -v` sequence, a plain run, and `OutOfCall` bearer: the `--mux 1 --profile 2 -v` sequence, a plain run, and `OutOfCall`
on an unbound mux (the fp6 journal's `2026-10-11-pcscf-pco-probe` capture, on an unbound mux (the fp6 journal's `2026-10-11-pcscf-pco-probe` capture,
@ -1194,7 +1229,9 @@ the status snapshot live (main thread only). The Messaging interface's
`Subscribe` keeps its subscriber set on the main thread as well, and `Subscribe` keeps its subscriber set on the main thread as well, and
`List` and `Delete` read and unlink the received-message store there while `List` and `Delete` read and unlink the received-message store there while
the engine thread writes it — safe through the `.tmp` + `link(2)` publish the engine thread writes it — safe through the `.tmp` + `link(2)` publish
(§7.9). (§7.9). On the same bus imsd is a client of logind's
(`org.freedesktop.login1`): the main thread takes its sleep inhibitor
there and listens for its `PrepareForSleep` (§7.6).
Per answered call imsd forks `imsd-media` (`IMSD_MEDIA`, default next to the Per answered call imsd forks `imsd-media` (`IMSD_MEDIA`, default next to the
`imsd` binary, else `/usr/libexec/imsd-media`) with the addresses, ports and `imsd` binary, else `/usr/libexec/imsd-media`) with the addresses, ports and
@ -1832,6 +1869,27 @@ sent, emit this D-Bus state change, arm this deadline). States and reasons are t
30 s outside a call, without the ping the flow can have been silent, 30 s outside a call, without the ping the flow can have been silent,
or dropped without a close, long before the call came. Shown on the or dropped without a close, long before the call came. Shown on the
workstation for a call the daemon placed (§5); not met on a network. workstation for a call the daemon placed (§5); not met on a network.
- Sleep: nothing of imsd runs while the phone is suspended, so 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 (§5). While the
calls table holds a call imsd holds logind's block inhibitor for
sleep (`Inhibit` with `sleep`, `imsd`, `a call`, `block`;
`systemd-inhibit --list` shows it): 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 (§7.4) and the refresh of
the `P-Access-Network-Info`, to its removal. Under it logind refused
a plain `systemctl suspend` on the dev phone (§5); a request can tell
logind to ignore inhibitors (`systemctl suspend -i`). Where it was
asked for during a suspend's operation logind refused this lock
(§13): a lock that was not given and is still wanted is asked for
again when logind
sends `PrepareForSleep` with false, and one
wanted before the bus connection was there when the connection
comes. Where logind gives none at all (`--session`: the session bus
has no logind) calls go on without one.
- Emergency: a number `IsEmergency` classifies (112, 911, plus - Emergency: a number `IsEmergency` classifies (112, 911, plus
`EMERGENCY_NUMBERS`; SIM `EF_ECC` is not read yet) is tried as `EMERGENCY_NUMBERS`; SIM `EF_ECC` is not read yet) is tried as
`urn:service:sos` first, with one fallback to a plain INVITE of the digits; `urn:service:sos` first, with one fallback to a plain INVITE of the digits;
@ -2030,6 +2088,12 @@ Lines the operator (and the scripts) look for, all prefixed `imsd: `:
- `call X: the client flow died before any response to the INVITE; reopening it` and, when the connect - `call X: the client flow died before any response to the INVITE; reopening it` and, when the connect
succeeded, `call X: the INVITE again over the reopened flow` (§7.6); `call X ended: error — the client flow succeeded, `call X: the INVITE again over the reopened flow` (§7.6); `call X ended: error — the client flow
died before any response to the INVITE` where the call ends instead. died before any response to the INVITE` where the call ends instead.
- `sleep is held off while the call lasts (logind's inhibitor)` when the
calls table's first call has its lock and `sleep is no longer held off
(no call)` when that lock is released, the table's last call gone
(§7.6): where no lock was held, no such line; `no sleep inhibitor from
logind (…): asked again when it says a sleep is over` where logind gave
none, once until a call's lock is next given.
- `media <codec> pt=N octet=true|false -> [X]:P, telephone-event pt N|none` at a - `media <codec> pt=N octet=true|false -> [X]:P, telephone-event pt N|none` at a
call's media start (`… pt N (theirs M)` when `DTMF_RX_PT` differs from call's media start (`… pt N (theirs M)` when `DTMF_RX_PT` differs from
the one we send with); `call X: DTMF '…' -> media leg (telephone-event pt the one we send with); `call X: DTMF '…' -> media leg (telephone-event pt
@ -3094,6 +3158,18 @@ the related code.
between). Where a network's P-CSCF closes or forgets a flow it hears between). Where a network's P-CSCF closes or forgets a flow it hears
nothing on, `CRLF_KEEPALIVE` is that network's setting (§7.2 step 5 nothing on, `CRLF_KEEPALIVE` is that network's setting (§7.2 step 5
has what imsd does without it). has what imsd does without it).
- **logind refused a block inhibitor for sleep where it was asked for
during a suspend's operation**
(systemd 262.2, the dev phone, 2026-11-25 and 2026-11-28): `Inhibit`
for `sleep` in `block` mode was answered
`org.freedesktop.login1.OperationInProgress` (`The operation
inhibition has been requested for is already running`) in a sleep hook
before the suspend, in one after it, and to imsd 0.1 to 0.3 s after
systemd-sleep's `System returned from sleep operation`;
logind's `Operation 'suspend' finished` came 0.03 s after that line
where the run's sleep hook did nothing after the resume, 0.1 s after
it where the hook made three bus calls then, and 2.1 s after it where
it also waited two seconds; a lock asked for after it was given.
- **pmOS's default nftables** drops inbound on `qmapmux*`; without the - **pmOS's default nftables** drops inbound on `qmapmux*`; without the
protected-port rule every network-initiated request (NOTIFY, MT protected-port rule every network-initiated request (NOTIFY, MT
INVITE) dies after ESP decapsulation and MT calls fall back to CS. The INVITE) dies after ESP decapsulation and MT calls fall back to CS. The

View file

@ -1396,11 +1396,82 @@ GVariant* SmsVariant(const imsd::engine::SmsReceived& m, std::int64_t receivedAt
return g_variant_builder_end(&b); 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) { gboolean OnIdleEvent(gpointer data) {
std::unique_ptr<Event> ev(static_cast<Event*>(data)); std::unique_ptr<Event> ev(static_cast<Event*>(data));
switch (ev->type) { switch (ev->type) {
case Event::Type::Added: { case Event::Type::Added: {
Calls[ev->info.uni] = ev->info; 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))); EmitSignal("CallAdded", g_variant_new("(s@a{sv})", ev->info.uni.c_str(), CallVariant(ev->info)));
break; break;
} }
@ -1416,6 +1487,7 @@ gboolean OnIdleEvent(gpointer data) {
} }
case Event::Type::Deleted: case Event::Type::Deleted:
Calls.erase(ev->uni); Calls.erase(ev->uni);
HoldSleepOff(CallLock, !Calls.empty());
EmitSignal("CallDeleted", g_variant_new("(s)", ev->uni.c_str())); EmitSignal("CallDeleted", g_variant_new("(s)", ev->uni.c_str()));
break; break;
case Event::Type::Registration: case Event::Type::Registration:
@ -3857,6 +3929,10 @@ const GDBusInterfaceVTable MessagingVtable = { imsd::bus::Gated<HandleMessagingM
void OnBusAcquired(GDBusConnection* conn, const gchar*, gpointer) { void OnBusAcquired(GDBusConnection* conn, const gchar*, gpointer) {
DbusConn = conn; 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; GError* err = nullptr;
GDBusNodeInfo* node = g_dbus_node_info_new_for_xml(IntrospectionXml, &err); 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; } if (!node) { Log(std::format("introspection XML: {}", err ? err->message : "?")); if (err) g_error_free(err); return; }