packaging, ims-pdn-up, imsd, README: imsd runs without /etc/imsd.env and idles without a SIM

With the P-CSCF coming from the PDN's PCO, the configuration file has no
required line left, so the apk's ConditionPathExists=/etc/imsd.env drop-in
goes: the unit starts on every phone with the package, and VoLTE is on by
default. Installing the package and inserting a SIM is the opt-in; the file
holds overrides only, and its existence changes nothing. By the versioning
rule (AGENTS §11, the first commit of this series) that is what moves the
major number: the release is 1.0.0, and §11 now records it. What used to be the
file's job — keeping a phone nobody configured out of a failing unit — is now
a rule on facts, the same for every phone: a modem in the failed state with
a SIM reason makes the script write IMSD_IDLE=no-sim to /run/imsd.env and
exit 0; a profile-indexed connect series the network refuses ten times in a
row — each a persistent 3GPP cause in ModemManager's typed error (not
subscribed, unknown APN, barred, not authorised in the PLMN, authentication
failed, the service option not supported, the wrong PDN type) with packet
service at the time; counted only while the modem reads registered,
connecting or connected with packet service not detached — stricter than
the daemon's PacketServiceUp; any other read, a timeout or a transient cause
starts the count again — writes IMSD_IDLE=no-ims-pdn; and a bring-up that
ends with no P-CSCF — none configured, none discovered: no IPv6 P-CSCF read
from the PCO (PCSCF_DISCOVERY=none in the hand-off, the reader's exit 2: the
list absent, empty, unreadable or IPv4/FQDN only), discovery skipped (no
rmnet mux id) or no hand-off at all (a manual run) — and no registration of
imsd's own to resume is the daemon's own idle, decided before it takes the bus name (SaPeer and the
holds' old_gone say what can be resumed; a pair the holds wrote off is taken
out of the kernel there) and at the no-target bring-up, where a pair nothing
can resume is written off and taken out of the kernel first. imsd reads the key, logs idle: … and exits 0, which Restart=on-failure
leaves alone, so the unit stays inactive until the next boot, a systemctl
start or a start of imsd-ofonod where that runs (its Wants starts an idle
imsd again). What is not a fact keeps the retry: no coverage, a SIM still
waiting for its PIN, a modem without an ims profile (the APN-string connect
is tried), an APN-string series (the modem's own wall), a resumable pair
whose attempt failed (the fatal), and a P-CSCF reader that did not answer
(PCSCF_DISCOVERY=failed: the daemon exits 1 and the restart runs discovery
again). A phone that never had the file keeps its stopped unit through the
upgrade and starts at its next boot, registering where the rule above lets
it — at the upgrade itself where imsd-ofonod runs, whose restart pulls imsd
in through its Wants (systemd queues the marked unit's try-restart, collapses
it to a restart and starts what the unit Wants in the same transaction:
job_type_collapse, transaction_add_job_and_dependencies, 262~rc3); the
opt-out is systemctl mask --now imsd, since a plain disable is undone by
that Wants. imsd-ofonod.service loses its After=imsd.service: with imsd
starting on every phone, GNOME Calls' backend would otherwise wait behind
the ims PDN bring-up at every boot and at its marked restart at an upgrade,
for the whole bring-up (up to its TimeoutStartSec=900); it starts at once and republishes when imsd's name
appears (OnImsdAppeared).
The configuration file's path is the IMSD_ENV seam the mock bench uses.
AGENTS §6's lint figures are this tree's (109 findings in 16 of 20 files,
2026-10-13; the 72 of 2026-10-06 predate the Qmi files).

Tested: crafter-build test, 8 passed; on the host build the daemon's idle
exit for the script's keys and, with no pair, for every discovery verdict
but failed (none, skipped, no hand-off) — exit 0 — and the exit 1 for
failed; the script's arms on the mock bench; on the dev phone (KPN), in
builds of this series: the daemon's idle at the no-target bring-up with the
dead pair removed and before the bus name for a pair the holds had written
off, the discovery-failed exit 1 and its restart, the start without the file, the pull-in by an imsd-ofonod restart
and the mask holding against it and a reboot, and imsd-ofonod starting at
once with imsd stopped and republishing when imsd's name appeared.
This commit is contained in:
Jorijn van der Graaf 2026-10-11 16:20:07 +02:00
commit 7a3dcc8951
Signed by: jorijnvdgraaf
GPG key ID: 2937E59CDCC1BCFB
7 changed files with 476 additions and 149 deletions

View file

@ -60,18 +60,17 @@ package() {
# account created at install — imsd-dialerd owns those session bus names
install -Dm644 "$srcdir"/org.kde.modem.daemon.desktop \
"$pkgdir"/etc/skel/.config/autostart/org.kde.modem.daemon.desktop
# enabled by preset: the unit is a no-op until /etc/imsd.env exists, and
# VoLTE surviving reboots must not depend on a manual systemctl enable
# enabled by preset: VoLTE surviving reboots must not depend on a manual
# systemctl enable. Since 1.0.0 the unit needs no configuration file (the
# P-CSCF comes from the PDN's PCO) and starts on every phone with the
# package; without a SIM it exits cleanly and stays inactive (IMSD_IDLE),
# and `systemctl mask --now imsd` opts out (a disable is undone by
# imsd-ofonod's Wants=imsd.service)
install -Dm644 "$srcdir"/80-imsd.preset \
"$pkgdir"/usr/lib/systemd/system-preset/80-imsd.preset
mkdir -p "$pkgdir"/etc/systemd/system/multi-user.target.wants
ln -s /usr/lib/systemd/system/imsd.service \
"$pkgdir"/etc/systemd/system/multi-user.target.wants/imsd.service
# ...but only actually start once the carrier config exists, so
# unconfigured systems don't boot into a failing unit
mkdir -p "$pkgdir"/usr/lib/systemd/system/imsd.service.d
printf '[Unit]\nConditionPathExists=/etc/imsd.env\n' \
> "$pkgdir"/usr/lib/systemd/system/imsd.service.d/10-require-config.conf
install -Dm755 imsd-ofonod "$pkgdir"/usr/bin/imsd-ofonod
install -Dm755 imsd-ofono-restart-calls.sh \
"$pkgdir"/usr/libexec/imsd-ofono-restart-calls.sh

View file

@ -10,8 +10,10 @@
# the first discovered address (the daemon's fresh target) a host route —
# never a default route (F15 below). Idempotent — safe to run when the PDN
# is already up. Runs as
# ExecStartPre of imsd.service, so imsd only starts once the PDN exists; a
# nonzero exit fails the unit and systemd retries per Restart/RestartSec.
# ExecStartPre of imsd.service, so imsd starts once the PDN exists — or once
# this script has written IMSD_IDLE= and exited 0 with nothing to do, when
# imsd logs the idle and exits cleanly (idle_exit below); a nonzero exit
# fails the unit and systemd retries per Restart/RestartSec.
#
# Configuration, via the unit's EnvironmentFile /etc/imsd.env (also read
# directly so manual runs behave the same):
@ -32,6 +34,9 @@
# /run/imsd.env, the first of them routed
# IMSD_PCSCF the reader's path (default /usr/libexec/imsd-pcscf) — a
# bench seam, from the environment only (not from the file)
# IMSD_ENV the configuration file's path (default /etc/imsd.env) —
# a bench seam. The file is optional (overrides only); its
# existence changes nothing
log() { echo "ims-pdn-up: $*"; }
@ -52,9 +57,15 @@ bearer_paths() {
# `enabled` with the modem still in low power; switching the radio back on
# then no longer brings the modem up (dev phone 2026-10-06).
radio_off() { [ "$(modem_state)" = disabled ]; }
# A locked SIM (a PIN not entered yet) is the same kind of state the user
# controls: the modem registers once the PIN is in, so like a disabled radio
# it exits non-zero and the unit's restart tries again.
sim_locked() { [ "$(modem_state)" = locked ]; }
unlock_required() { kv -m "$MODEM" | sed -n 's/^modem\.generic\.unlock-required *: *//p'; }
IMSD_ENV=${IMSD_ENV:-/etc/imsd.env}
envval() { # $1 = key — for manual runs; under systemd the vars are inherited
sed -n "s/^$1=//p" /etc/imsd.env 2>/dev/null | tail -n1 | tr -d '"'
sed -n "s/^$1=//p" "$IMSD_ENV" 2>/dev/null | tail -n1 | tr -d '"'
}
IP_TYPE=${IMS_IP_TYPE:-$(envval IMS_IP_TYPE)}
IP_TYPE=${IP_TYPE:-ipv6}
@ -75,6 +86,44 @@ while :; do
done
log "modem $MODEM"
# ---- nothing to do: a clean exit, not a retry loop. imsd reads IMSD_IDLE=
# from /run/imsd.env, logs it and exits 0, which Restart=on-failure leaves
# alone — the unit stays inactive until the next boot, a `systemctl start
# imsd` or a start of imsd-ofonod (its Wants=imsd.service, the Phosh image).
# Installing the package and inserting a SIM is the opt-in; no configuration
# file changes what follows. The dead ends are facts: no SIM (the modem's
# `failed` state with a SIM reason; final until ModemManager re-probes;
# checked here and while waiting for registration below), a profile-indexed
# connect series refused ten times in a row — each a persistent 3GPP cause
# in ModemManager's typed error (not subscribed, unknown APN, barred, not
# authorised in this PLMN, authentication failed, the service option not
# supported, the wrong PDN type), with packet service; counted only while the
# modem reads registered, connecting or connected with packet service not
# detached — any other read, a timeout or a transient cause starts the count
# again — and — the daemon's own, with the same clean exit — a
# bring-up that ends with no P-CSCF (none configured, the PCO names none)
# and no registration of imsd's own to resume. One attempt per boot (or per
# such imsd-ofonod start). What is not a fact exits 1 like a disabled radio
# and the restart retries: ten failures without coverage, a SIM still waiting
# for its PIN, a modem without an ims profile (the APN-string connect is
# tried), an APN-string series (the modem's own wall under an IPv4 attach,
# F6), a P-CSCF reader that did not answer (PCSCF_DISCOVERY=failed, below).
idle_exit() { # $1 = key, $2 = log text
log "$2"
printf 'IMSD_IDLE=%s\n' "$1" > /run/imsd.env
log "wrote /run/imsd.env: IMSD_IDLE=$1"
exit 0
}
sim_check() {
[ "$(modem_state)" = failed ] || return 0
REASON=$(kv -m "$MODEM" | sed -n 's/^modem\.generic\.state-failed-reason *: *//p')
case "$REASON" in
sim-missing|esim-without-profiles|sim-error)
idle_exit no-sim "no SIM ($REASON): not bringing the ims PDN up — systemctl start imsd once one is in" ;;
esac
}
sim_check
# ---- the ims profile index (WDS profile list), unless configured. The
# lookup itself can fail while ModemManager is still enabling the modem; a
# failed lookup is not "no profile" (the APN-string request it would fall
@ -175,7 +224,7 @@ if [ -z "$BEARER" ]; then
waited=0 last=
while :; do
STATE=$(modem_state)
case "$STATE" in registered|connecting|connected) break ;; esac
case "$STATE" in registered|connecting|connected) break ;; failed) sim_check ;; esac
[ "$STATE" != "$last" ] && log "waiting for registration (state: ${STATE:-unknown})"
last=$STATE
if [ "$waited" -ge "$REG_TIMEOUT" ]; then
@ -183,6 +232,10 @@ if [ -z "$BEARER" ]; then
log "modem still disabled after $REG_TIMEOUT s (radio off): not connecting"
exit 1
fi
if [ "$STATE" = locked ]; then
log "SIM locked ($(unlock_required)) after $REG_TIMEOUT s: not connecting until the PIN is entered"
exit 1
fi
log "not registered after $REG_TIMEOUT s — attempting anyway"
break
fi
@ -208,10 +261,31 @@ if [ -z "$BEARER" ]; then
fi
n=0
REFUSED=0 # connects refused in a row, each a persistent 3GPP cause with the modem registered/connecting/connected and packet service not detached (stricter than the daemon's PacketServiceUp)
while [ -z "$BEARER" ]; do
n=$((n + 1))
[ "$n" -gt 10 ] && { log "bearer connect failed after 10 attempts"; exit 1; }
if [ "$n" -gt 10 ]; then
# only a profile-indexed series the network refused says anything
# about it: an APN-string series (a lookup that kept failing, a late
# registration) meets the modem's own wall under an IPv4 attach (F6),
# which the restart's fresh lookup gets past, and a failure without
# packet service is a coverage gap, not a refusal — counted only while
# the modem reads registered, connecting or connected with packet
# service not `detached` (a CS-registered modem still attaching) and
# only for a persistent 3GPP cause in ModemManager's typed error; any
# other read, `disconnecting`, `unknown` or an unanswered mmcli among
# them, and a timeout or a transient cause, reset it — stricter than
# the daemon's PacketServiceUp, which counts an unknown state as
# service — so the
# count is of refusals in a row, each with packet service at the time,
# never one state read after the series: a series that failed without
# coverage and found the network at its end exits 1 like any other,
# and the restart retries on the attached modem
[ -z "$PROFILE_ID" ] || [ "$REFUSED" -lt 10 ] || idle_exit no-ims-pdn "bearer connect refused 10 times in a row by profile index with packet service and a persistent cause (last: $(squash "$OUT")): no ims PDN for this SIM here — systemctl start imsd to try again"
log "bearer connect failed after 10 attempts (modem state: $(modem_state), last: $(squash "$OUT"))"; exit 1
fi
radio_off && { log "modem disabled (radio off): not connecting"; exit 1; }
sim_locked && { log "SIM locked ($(unlock_required)): not connecting until the PIN is entered"; exit 1; }
B=$(find_ims_bearer no) # reuse a stale disconnected ims bearer of this run's shape
if [ -z "$B" ]; then
if [ -n "$PROFILE_ID" ]; then
@ -225,6 +299,7 @@ while [ -z "$BEARER" ]; do
log "created bearer $B ($SPEC)"
else
log "create-bearer attempt $n failed: $(squash "$OUT"); retrying in 10 s"
REFUSED=0 # not the network's answer to a connect
sleep 10
continue
fi
@ -232,7 +307,24 @@ while [ -z "$BEARER" ]; do
if OUT=$(mmcli -b "$B" --connect 2>&1); then
BEARER=$B
else
log "connect attempt $n failed (state: $(modem_state)): $(squash "$OUT"); retrying in 10 s"
ST=$(modem_state); PS=$(packet_state)
log "connect attempt $n failed (state: ${ST:-unknown}, packet service: ${PS:-unknown}): $(squash "$OUT"); retrying in 10 s"
# a refusal is the network's answer, as ModemManager types it: a
# persistent 3GPP cause — not subscribed, unknown APN, barred, not
# authorised in this PLMN, authentication failed, the service option
# not supported, the wrong PDN type for this attach — with the modem
# attached. A timeout (D-Bus, esm-proc-timeout), a transient cause
# (insufficient resources, out of order, a network failure) or any
# other error starts the count again, as a read without packet service
# does: an outage must not idle VoLTE until the next boot
PERSISTENT=
case "$OUT" in
*MobileEquipment.ServiceOptionNotSubscribed:*|*MobileEquipment.MissingOrUnknownApn:*|*MobileEquipment.OperatorDeterminedBarring:*|*MobileEquipment.ServiceOptionNotAuthorizedInPlmn:*|*MobileEquipment.UserAuthenticationFailed:*|*MobileEquipment.ServiceOptionNotSupported:*|*MobileEquipment.Ipv4OnlyAllowed:*|*MobileEquipment.Ipv6OnlyAllowed:*) PERSISTENT=1 ;;
esac
case "$ST" in
registered|connecting|connected) if [ "$PS" = detached ] || [ -z "$PERSISTENT" ]; then REFUSED=0; else REFUSED=$((REFUSED + 1)); fi ;;
*) REFUSED=0 ;;
esac
case "$OUT" in *call-already-present*)
# the PDN is up on a bearer this run did not recognise (ip-type or
# APN spelling); use it rather than ask the modem for a second one
@ -309,9 +401,15 @@ fi
# pass for the admin's PCSCF=, and it is never read from the environment
# here). The PDN is brought up IPv6-only, so only IPv6 addresses are usable;
# IPv4 ones and names are logged and dropped. Nothing usable is not this
# script's failure: imsd resumes a registration of its own when it can; with
# nothing to resume it says so and exits, and the unit's restart runs
# discovery again.
# script's failure, and PCSCF_DISCOVERY= in the hand-off tells the daemon
# which kind it was: `none` — the PCO carried no IPv6 P-CSCF (the reader's
# exit 2), a fact: a registration of imsd's own that can be resumed is
# resumed and stays up, else the daemon idles (its `idle:` exit; after a
# resumable pair's failed attempt the fatal and the restart) — or `failed` —
# the reader did not answer (an error, a timeout, nothing printed), no fact:
# a daemon with nothing to resume exits 1 and the unit's restart runs
# discovery again. `found` and `skipped` (PCSCF= set, an IPv4-only PDN, no
# rmnet mux id) are the other two values.
# The P-CSCF is reachable through the ims PDN only: a host route stands in
# for the default — `dev` without a gateway (a raw-IP netdev without
# header_ops, so the kernel resolves no neighbours), the shape imsd uses for
@ -331,6 +429,7 @@ route_pcscf() { # $1 = address
}
PCSCF=${PCSCF:-$(envval PCSCF)}
DISCOVERED=
DISCOVERY=skipped
case "$PCSCF" in
'')
if [ "$IP_TYPE" = ipv4 ]; then
@ -340,6 +439,7 @@ case "$PCSCF" in
if [ -z "$MUX" ]; then
log "P-CSCF discovery: no rmnet mux id on $IFACE (ip -d link); set PCSCF="
else
DISCOVERY=failed
OUT=$("${IMSD_PCSCF:-/usr/libexec/imsd-pcscf}" --mux "$MUX" ${PROFILE_ID:+--profile "$PROFILE_ID"} --timeout 10 2>&1); rc=$?
V6=; SHOWN=; OTHER=; FLAGS=; ERR=; n=0
while IFS= read -r line; do
@ -356,13 +456,14 @@ case "$PCSCF" in
$OUT
EOF
if [ -n "$V6" ]; then
DISCOVERY=found
log "P-CSCF discovered: $SHOWN${FLAGS:+ ($FLAGS)}"
[ -n "$OTHER" ] && log "P-CSCF discovery also gave $OTHER (not usable on the IPv6-only ims PDN)"
route_pcscf "${V6%% *}" # the first: the daemon's fresh target; imsd routes the P-CSCF it uses itself as well (the fresh target, the resumed peer)
else
case $rc in
0) log "P-CSCF discovery: nothing usable (imsd-pcscf printed no address${ERR:+: $ERR})" ;;
2) log "P-CSCF discovery: nothing usable (the PCO carried no IPv6 P-CSCF${OTHER:+; it gave $OTHER}${FLAGS:+; $FLAGS})" ;;
2) DISCOVERY=none; log "P-CSCF discovery: nothing usable (the PCO carried no IPv6 P-CSCF${OTHER:+; it gave $OTHER}${FLAGS:+; $FLAGS})" ;;
*) log "P-CSCF discovery: nothing usable (imsd-pcscf rc $rc${ERR:+: $ERR})" ;;
esac
fi
@ -381,9 +482,17 @@ esac
# the RA, which the netdev lists first and which is deprecated after a day
# and removed after two (ims ledger F18; the SAs and the Contact would go
# with it). PCSCF_DISCOVERED= is written on every run, empty when nothing
# was discovered or PCSCF= is set.
printf 'DEV=%s\nLOCAL=%s\nPCSCF_DISCOVERED=%s\n' "$IFACE" "$ADDR" "$DISCOVERED" > /run/imsd.env
log "wrote /run/imsd.env: DEV=$IFACE LOCAL=$ADDR PCSCF_DISCOVERED=$DISCOVERED"
# was discovered or PCSCF= is set; PCSCF_DISCOVERY= says how it came to be
# (found, none, failed, skipped — above).
# The dead end after the PDN is up — nothing to register to (no PCSCF=,
# nothing discovered, no registration of imsd's own in the kernel to resume)
# — is the daemon's to decide (its `idle:` exit before it takes the bus name,
# §7.3): it knows the fresh target, the kernel's SA pair and the holds, and
# PCSCF_DISCOVERY= tells it whether an empty list is the PCO's answer (none:
# the idle) or the reader's failure (failed: exit 1, the restart runs
# discovery again). The hand-off is written on every run.
printf 'DEV=%s\nLOCAL=%s\nPCSCF_DISCOVERED=%s\nPCSCF_DISCOVERY=%s\n' "$IFACE" "$ADDR" "$DISCOVERED" "$DISCOVERY" > /run/imsd.env
log "wrote /run/imsd.env: DEV=$IFACE LOCAL=$ADDR PCSCF_DISCOVERED=$DISCOVERED PCSCF_DISCOVERY=$DISCOVERY"
# ---- open the IMS protected ports in the firewall (rung 5b root cause,
# journal/ims.md s56): pmOS's default nftables INPUT chain is policy-drop and

View file

@ -11,7 +11,9 @@ Documentation=https://forgejo.catcrafts.net/Catcrafts/imsd
# — Plasma Mobile uses imsd-dialerd instead. Both may run at once (different
# buses, different consumers), but a real ofonod must not.
Conflicts=ofono.service
After=imsd.service
# Wants, not After: imsd starts on every phone since 1.0.0 and its start runs
# the ims PDN bring-up (the whole ExecStartPre, up to TimeoutStartSec=900);
# this backend starts at once and republishes when imsd's name appears (OnImsdAppeared)
Wants=imsd.service
[Service]

View file

@ -21,7 +21,9 @@ ExecStartPre=-/usr/libexec/imsd-handover.sh
TimeoutStartSec=900
# /run/imsd.env: written by ims-pdn-up.sh (DEV= the connected ims netdev,
# LOCAL= the PDN address ModemManager assigned to it, PCSCF_DISCOVERED= the
# P-CSCF list the network put in the PDN's PCO, read from the modem).
# P-CSCF list the network put in the PDN's PCO, read from the modem,
# PCSCF_DISCOVERY= how that list came to be: found, none, failed, skipped;
# or IMSD_IDLE=<reason> alone when the bring-up found nothing to do).
# /etc/imsd.env: admin configuration, none required where discovery finds a
# P-CSCF (AGENTS.md §10, the README); it wins over the runtime-derived values
# — PCSCF= overrides discovery.