imsd, engine, packaging: received short messages are kept until an app deletes them — List and Delete on the Messaging interface

A received message went to a bus signal and nowhere else but DUMP_SIP's
opt-in raw dump of the last MESSAGE outside a call's dialog, so with no
app listening it was gone. It is now written
to <state dir>/sms/<id>.msg — mode 0600 in a 0700 directory, whole to a
.tmp, linked to its name and the directory fsynced; the engine's FormatStoredSms/ParseStoredSms
record codec, pinned by tests/Engine — before the signal, and stays until
Delete. List hands over the records it can read, by arrival second, then
within a run by sequence number. The record is named by
the message's id and published with link(2), which refuses a name the
store already holds, so the next number is tried and nothing is
replaced. link(2) refuses only a name the store
still holds: a client that deleted the daemon's copy after importing it
freed the name, and the run's token in the id is what keeps a later run
from giving it to another text, unless that run draws the same token and
meets the same second and number. The two methods sit behind the
Messaging polkit action with Send, and the policy text says so; GetStatus,
the open read, does not count the store, since the count would tell any
local account when a text arrived, and the unit makes the state
directory 0700 (StateDirectoryMode) for the same reason.

For a message the store cannot write the delivery report stays an
RP-ACK: the signal is its only delivery, and the journal says so. The
protocol's refusal —
RP-ERROR "memory capacity exceeded", TS 24.011 cause 22 — is half of a
mechanism: the other half, the UE's memory-available notification
(RP-SMMA, TS 24.341 5.3.2.5), imsd does not send; a refused last segment
would also lose its set. The
refusal was built and benched on KPN — the store made unwritable, the
text refused; it came again five minutes later, the store writable by
then, and a later text came at once — and taken out again; no other core
is measured. It returns
with the notification and a probe of the store. The record's directory is
fsynced after the link, its result unchecked like the directory open's; a
concatenated text's earlier segments are held in memory, not in the
store, until the set is whole (§7.9).

Tested on the dev phone (KPN): a text from the phone to its own number on
2026-10-18 23:30, under this publish (a build of this range ahead of this
history, 735dfda, whose StoreReceivedSms has this commit's code), stored
as
ims-sms-in-1792359045-1, an id of that build's shape; the store directory listed on 2026-10-19 holds
that record and no temporary (the fp6 journal's 2026-10-17-sms-over-ip-bench
capture: r20-tip-smoke-2026-10-18-2329, r22-store-backup-and-delete-proof).
The Exists path has no bench: a run's own ids never repeat. The refusal's bench (store-refused-2026-10-18-0136): the store directory
made immutable at 01:37, a text to the phone's own number not stored
(EPERM on its .tmp), the RP-ERROR answered 202, the text delivered again
at 01:42:56 under its first SC time stamp, stored and acknowledged, a later
text not held. Not on the phone: List's skip of a file that is no record
and four of Delete's answers ran on the workstation, in dev mode on a
private session bus, on this commit's host build (2026-11-19; its sha256
begins 8fdd9192866645d5): the skip, with its journal line; InvalidArgs
for a string that is no id; IOError, the
record still listed, for a record in a store directory made unwritable;
the removal once it was writable; FileNotFound for an id with no record.

The engine's start sweeps the .tmp names a crash left — one it cannot
unlink stays, unreported — so a deleted text's second name goes at the
next engine start that can remove it (Delete leaves .tmp
names alone). A Delete whose removal fails answers IOError with the system's
reason, and a record that is there is kept; FileNotFound is left for a
removal that found no file.
List's order breaks a tie between two texts of one run in one second by
the sequence number (pinned), not by the id as text.
This commit is contained in:
Jorijn van der Graaf 2026-10-17 00:24:50 +02:00
commit d0cfaf712d
Signed by: jorijnvdgraaf
GPG key ID: 2937E59CDCC1BCFB
10 changed files with 475 additions and 77 deletions

View file

@ -39,9 +39,12 @@
// report routed to the message in flight, 488 for one that is not ours, an
// unusable TPDU acknowledged with an RP-ERROR, a body that is no RP
// message, a page-mode text 200'd and logged, a short message while SMS
// over IP is off refused with 480), and RecentTransactions (a retransmitted
// request gets its final again, for 32 s; no branch, no match). Pure
// reducers, no I/O.
// over IP is off refused with 480), and the store's record codec
// (FormatStoredSms/ParseStoredSms: a full record round trip, defaults
// for absent keys, a value that does not parse or a missing id refused) and order
// (StoredSmsBefore: the arrival second, then within a run the sequence
// number), and RecentTransactions (a retransmitted request gets its final
// again, for 32 s; no branch, no match). Pure reducers, no I/O.
import std;
import Imsd;
@ -1451,11 +1454,19 @@ int main() {
// a status report's recipient against the number a message went to
Check(SameRecipient("+31612345678", "+31612345678") && SameRecipient("0612345678", "+31612345678") && SameRecipient("+31 6 1234 5678", "31612345678") && SameRecipient("0031612345678", "+31612345678") && SameRecipient("6125551234", "+16125551234"), "SameRecipient: equal digits, or a number dialled with a trunk 0, with 00 or without its country code against its international form");
Check(!SameRecipient("+31612345678", "+31687654321") && !SameRecipient("+31612345678", "+49612345678") && !SameRecipient("112", "+31612345112") && !SameRecipient("1266", "+31612341266") && !SameRecipient("", "+31612345678"), "SameRecipient: another number, +31 against +49, a short code as a suffix, an empty one");
// the store's order: two texts in one second by sequence number, not by the id as text
{
StoredSms nine; nine.id = "ims-sms-in-7f3a9c21-1792192663-9"; nine.receivedAt = 1792192663;
StoredSms ten; ten.id = "ims-sms-in-7f3a9c21-1792192663-10"; ten.receivedAt = 1792192663;
StoredSms later; later.id = "ims-sms-in-7f3a9c21-1792192664-1"; later.receivedAt = 1792192664;
Check(StoredSmsBefore(nine, ten) && !StoredSmsBefore(ten, nine) && StoredSmsBefore(ten, later) && !StoredSmsBefore(later, nine), "StoredSmsBefore: arrival second first, then within a run the sequence number");
}
// the ids of the Messaging interface: the run's token first after the prefix, so two runs with different tokens give different ids whatever the clock reads
{
Check(RunToken(0x7f3a9c21u) == "7f3a9c21" && RunToken(0) == "00000000" && RunToken(0xffffffffu) == "ffffffff", "RunToken: eight hex digits, zero-padded");
Check(SentSmsId("7f3a9c21", 1793474321, 3) == "ims-sms-7f3a9c21-1793474321-3" && ReceivedSmsId("7f3a9c21", 1793474321, 1) == "ims-sms-in-7f3a9c21-1793474321-1", "the sent and the received shape");
Check(ReceivedSmsId("7f3a9c21", 42, 1) != ReceivedSmsId("0b1d2e3f", 42, 1) && SentSmsId("7f3a9c21", 42, 1) != SentSmsId("0b1d2e3f", 42, 1), "two runs with different tokens: their ids differ at the same second and counter");
Check(ReceivedSmsId("ffffffff", std::numeric_limits<std::int64_t>::min(), std::numeric_limits<std::uint64_t>::max()).size() <= 64, "the longest received id is no longer than 64 bytes");
}
// inbound MESSAGEs, as the IP-SM-GW sends them
auto inbound = [](std::string_view bodyHex, std::string_view extraHeaders = "") {
@ -1658,6 +1669,36 @@ int main() {
auto r3 = OnSmsMessage(c, rng, plain, re, nullptr, 7.0);
Check(r3.response.starts_with("SIP/2.0 200 OK") && !r3.rp && hasSms(r3.actions, SmsAction::Type::Log) && findSms(r3.actions, SmsAction::Type::Log)->text.contains("hi"), "inbound: a page-mode text is 200'd and logged with its body");
}
// the store's record codec
{
StoredSms r;
r.id = "ims-sms-in-1792186043-7";
r.receivedAt = 1792186043;
r.sms.from = "KPN Service=\n\"x\"";
r.sms.text = "Grüße ❤\nline two=three";
r.sms.scts = "2026-10-16T22:50:00+02:00";
r.sms.sc = "+31653131313";
r.sms.msgClass = 0;
r.sms.pid = 0x40;
r.sms.dcs = 0x08;
r.sms.segments = 2;
r.sms.messageWaiting = true;
r.sms.portDst = 2948;
r.sms.portSrc = 9200;
r.sms.data = {0xDE, 0xAD};
r.sms.tpdu = {0x04, 0x0B, 0x91};
const std::string text = FormatStoredSms(r);
Check(text.starts_with("id=ims-sms-in-1792186043-7\nreceived=1792186043\nfrom=") && text.contains("\nport-dst=2948\nport-src=9200\n") && text.ends_with("\npdu=040b91\n") && !text.contains("Grüße"), "store: the record's shape, the text as hex");
auto back = ParseStoredSms(text);
Check(back && back->id == r.id && back->receivedAt == r.receivedAt && back->sms.from == r.sms.from && back->sms.text == r.sms.text && back->sms.scts == r.sms.scts && back->sms.sc == r.sms.sc && back->sms.msgClass == 0 && back->sms.pid == 0x40 && back->sms.dcs == 0x08 && back->sms.segments == 2 && back->sms.messageWaiting && back->sms.portDst == 2948 && back->sms.portSrc == 9200 && back->sms.data == r.sms.data && back->sms.tpdu == r.sms.tpdu, "store: a full record round-trips");
auto minimal = ParseStoredSms("id=x\n");
Check(minimal && minimal->id == "x" && minimal->receivedAt == 0 && !minimal->sms.msgClass && !minimal->sms.portDst && minimal->sms.segments == 1 && minimal->sms.text.empty(), "store: absent keys read as defaults");
Check(!ParseStoredSms("received=5\nfrom=00\n"), "store: no id, no record");
Check(!ParseStoredSms("id=x\ntext=zz\n"), "store: a malformed hex value, no record");
Check(!ParseStoredSms("id=x\nreceived=soon\n"), "store: a malformed number, no record");
auto noClass = ParseStoredSms("id=x\nclass=-1\nmessage-waiting=0\n");
Check(noClass && !noClass->sms.msgClass && !noClass->sms.messageWaiting, "store: class -1 is none");
}
// a retransmitted MESSAGE: the same final again, nothing else, for 32 s (TimerJ)
{
RecentTransactions recent;