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,7 +39,8 @@ again and is not handled again; one whose first Via line has no
`branch=`, or an empty one, is never taken for a copy), and StatusReportBook with SameRecipient (a status
report's match to the message that asked for one, TP-MR and recipient, and
its word for the whole message), SmsWordOrder (the order of a message's
words) and the ids (RunToken, SentSmsId, ReceivedSmsId). Pure std C++.
words) and the ids (RunToken, SentSmsId, ReceivedSmsId), and the received-message store's record codec
(FormatStoredSms/ParseStoredSms) and order (StoredSmsBefore). Pure std C++.
*/
export module Imsd:Engine;
@ -1297,13 +1298,104 @@ export namespace imsd::engine {
// digits drawn once at the daemon's start — then a second, then a
// counter. The token is what tells one run's ids from another's: the
// wall clock reads 1970 until the first time sync, so a second alone
// repeats across runs.
// repeats across runs, and a client that deleted the daemon's copy of a
// text freed its name.
std::string RunToken(std::uint32_t bits) { return std::format("{:08x}", bits); }
std::string SentSmsId(std::string_view run, std::int64_t start, std::uint64_t n) { return std::format("ims-sms-{}-{}-{}", run, start, n); }
std::string ReceivedSmsId(std::string_view run, std::int64_t at, std::uint64_t n) { return std::format("ims-sms-in-{}-{}-{}", run, at, n); }
// ---- the store of received messages (§7.9) ---------------------------
// A received message as the daemon keeps it until an app takes it
// (`List`/`Delete` on the Messaging interface, §8.4): its id, when it
// arrived (epoch seconds) and the message. One record per file,
// key=value lines; the sender, the text, the data and the PDU as hex
// (a sender or a text may hold any character). Absent keys read as
// their defaults; a record without an id, or with a number or hex
// value that does not parse, is no record.
struct StoredSms {
std::string id;
std::int64_t receivedAt = 0;
SmsReceived sms;
};
std::string FormatStoredSms(const StoredSms& r) {
auto hex = [](std::string_view s) { return imsd::util::ToHex(std::span<const std::uint8_t>(reinterpret_cast<const std::uint8_t*>(s.data()), s.size())); }; // lint-disable-line no-char-pointer
std::string out;
out += std::format("id={}\n", r.id);
out += std::format("received={}\n", r.receivedAt);
out += std::format("from={}\n", hex(r.sms.from));
out += std::format("sc={}\n", r.sms.sc);
out += std::format("timestamp={}\n", r.sms.scts);
out += std::format("class={}\n", r.sms.msgClass.value_or(-1));
out += std::format("pid={}\n", r.sms.pid);
out += std::format("dcs={}\n", r.sms.dcs);
out += std::format("segments={}\n", r.sms.segments);
out += std::format("message-waiting={}\n", r.sms.messageWaiting ? 1 : 0);
if (r.sms.portDst) out += std::format("port-dst={}\n", *r.sms.portDst);
if (r.sms.portSrc) out += std::format("port-src={}\n", *r.sms.portSrc);
out += std::format("text={}\n", hex(r.sms.text));
out += std::format("data={}\n", imsd::util::ToHex(r.sms.data));
out += std::format("pdu={}\n", imsd::util::ToHex(r.sms.tpdu));
return out;
}
// The store's order, oldest first: by the arrival second, then — two
// texts of one run in one second share the id's prefix — by the sequence
// number, which the shorter id carries the smaller of (as text, -10
// would sort before -9).
bool StoredSmsBefore(const StoredSms& a, const StoredSms& b) {
if (a.receivedAt != b.receivedAt) return a.receivedAt < b.receivedAt;
if (a.id.size() != b.id.size()) return a.id.size() < b.id.size();
return a.id < b.id;
}
std::optional<StoredSms> ParseStoredSms(std::string_view text) {
StoredSms r;
bool ok = true;
auto bytesOf = [&](std::string_view v) -> std::vector<std::uint8_t> {
if (v.empty()) return {};
auto b = imsd::util::FromHex(v);
if (!b) { ok = false; return {}; }
return *b;
};
auto stringOf = [&](std::string_view v) { auto b = bytesOf(v); return std::string(b.begin(), b.end()); };
auto intOf = [&](std::string_view v) -> std::int64_t {
std::int64_t n = 0;
auto [p, ec] = std::from_chars(v.data(), v.data() + v.size(), n);
if (ec != std::errc() || p != v.data() + v.size()) ok = false;
return n;
};
for (std::size_t pos = 0; pos < text.size();) {
const std::size_t nl = text.find('\n', pos);
std::string_view line = text.substr(pos, nl == std::string_view::npos ? std::string_view::npos : nl - pos);
pos = nl == std::string_view::npos ? text.size() : nl + 1;
const std::size_t eq = line.find('=');
if (eq == std::string_view::npos) continue;
std::string_view k = line.substr(0, eq);
std::string_view v = line.substr(eq + 1);
if (k == "id") r.id = std::string(v);
else if (k == "received") r.receivedAt = intOf(v);
else if (k == "from") r.sms.from = stringOf(v);
else if (k == "sc") r.sms.sc = std::string(v);
else if (k == "timestamp") r.sms.scts = std::string(v);
else if (k == "class") { const std::int64_t c = intOf(v); if (c >= 0) r.sms.msgClass = static_cast<int>(c); }
else if (k == "pid") r.sms.pid = static_cast<std::uint8_t>(intOf(v));
else if (k == "dcs") r.sms.dcs = static_cast<std::uint8_t>(intOf(v));
else if (k == "segments") r.sms.segments = static_cast<int>(intOf(v));
else if (k == "message-waiting") r.sms.messageWaiting = intOf(v) != 0;
else if (k == "port-dst") r.sms.portDst = static_cast<std::uint16_t>(intOf(v));
else if (k == "port-src") r.sms.portSrc = static_cast<std::uint16_t>(intOf(v));
else if (k == "text") r.sms.text = stringOf(v);
else if (k == "data") r.sms.data = bytesOf(v);
else if (k == "pdu") r.sms.tpdu = bytesOf(v);
}
if (!ok || r.id.empty()) return std::nullopt;
return r;
}
// ---- retransmitted requests (RFC 3261 17.2.2) ------------------------
// A request whose Via branch (the value of the first `branch=` of its

View file

@ -9,12 +9,12 @@ bus side: the polkit gate in front of call control and messaging.
The bus policies let any local account reach both names, so the session
account can dial whatever it is called; whether a caller may place, answer
or end a call is polkit's net.catcrafts.IMS1.Voice, and whether it may send
a short message or subscribe to the message signals
net.catcrafts.IMS1.Messaging
a short message, subscribe to the message signals or list and delete
the stored ones net.catcrafts.IMS1.Messaging
(packaging/net.catcrafts.IMS1.policy), both shaped like ModemManager's
Voice action: the active local session and root, not an ssh login, an
inactive session or a system service account. Reads (status, the call
list) stay open, as ModemManager's are to every local account, and the
list, Messaging's GetStatus) stay open, as ModemManager's are to every local account, and the
call signals reach them all; the message signals go to the
connections Subscribe admitted through the same gate, a submission's
outcome also to the connection that called Send.
@ -74,7 +74,8 @@ void OnCheck(GObject* src, GAsyncResult* res, gpointer data) {
export namespace imsd::bus {
// The polkit actions: call control, and messaging — sending a short
// message over IP and subscribing to the message signals.
// message over IP, subscribing to the message signals, listing and
// deleting the stored ones.
inline constexpr char VoiceAction[] = "net.catcrafts.IMS1.Voice";
inline constexpr char MessagingAction[] = "net.catcrafts.IMS1.Messaging";