From a4dbf71af91632d8134493931b55bd81c001a1ee Mon Sep 17 00:00:00 2001 From: Luxferre Date: Wed, 28 Oct 2026 20:07:42 +0200 Subject: [PATCH] modem: fix missing call log entries and rejected calls still ringing On a freshly flashed image the root-run dual-SIM daemon created ~/.local/share/sxmo/modem as root. sxmo_modemcall.sh runs under set -e, so its failed modemlog.tsv write aborted hangup() before the call was ended on the network (the caller kept ringing), and no call was ever logged for xcalllog. - dsds-daemon: create the log and call state directories owned by user, append to modemlog.tsv through a helper that keeps it user-owned, and repair root-owned leftovers on start; never create /run/user/10000. - sxmo_modemcall.sh: make log_event non-fatal and hang up on the network before logging and running the hangup/discard hooks. - sxmo_modemmonitor.sh: only fall back to an unprivileged dsds-daemon when doas is unavailable, not whenever the root instance exits. - apply-bananarchy-overlay.sh: pre-create the modem and block log directories in the image. --- CHANGELOG.md | 2 + overlay/rootfs/usr/bin/sxmo_modemcall.sh | 48 ++++++---- overlay/rootfs/usr/bin/sxmo_modemmonitor.sh | 9 +- .../usr/libexec/msm-modem-uim-selection | 89 +++++++++++++++---- scripts/apply-bananarchy-overlay.sh | 4 + 5 files changed, 119 insertions(+), 33 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c6dc506..04f058f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,6 +28,8 @@ Release versions follow the `
` format (where `2026` is `a`, - ReMEdy audio follows the system speaker volume and mute state (the xdrawer **Speaker** slider, `~/.config/sxmo/speaker_vol`, and `SPK DAC Switch`) live while a MIDlet runs. The pause menu **Audio** item changes the system speaker volume with `Left` / `Right` and toggles ReMEdy sound with `OK`. - **Dual-SIM PIN prompt fix**: - Fixed SIM PIN startup prompting (`sxmo_sim_pin_prompt.sh`) so that when two SIM cards are inserted and only one requires PIN entry, only a single PIN prompt is shown (polling live `uim-get-card-status` per slot and deduplicating simultaneous `sxmo_modemmonitor.sh` / `bananarchy-x11-session.sh` invocations via `flock`). +- **Call log and call rejection fix**: + - Fixed incoming and outgoing calls not appearing in the call log (`xcalllog`) and rejected incoming calls continuing to ring for the caller on freshly flashed images: the root-run dual-SIM daemon (`msm-modem-uim-selection dsds-daemon`) created `~/.local/share/sxmo/modem` as `root`, so `sxmo_modemcall.sh` (running under `set -e`) aborted on the failed `modemlog.tsv` write before hanging up. The daemon now keeps the call log, SMS logs, and call state files owned by `user` (and repairs existing root-owned ones), the image pre-creates the log directories, `sxmo_modemcall.sh` ends the call on the network before logging and running hooks, and `sxmo_modemmonitor.sh` no longer replaces a restarted root daemon with an unprivileged one. --- diff --git a/overlay/rootfs/usr/bin/sxmo_modemcall.sh b/overlay/rootfs/usr/bin/sxmo_modemcall.sh index 357e1e7..238e1f9 100755 --- a/overlay/rootfs/usr/bin/sxmo_modemcall.sh +++ b/overlay/rootfs/usr/bin/sxmo_modemcall.sh @@ -34,8 +34,14 @@ log_event() { NUM="$(vid_to_number "$EVT_VID" 2>/dev/null || true)" TIME="$(date +%FT%H:%M:%S%z)" - mkdir -p "$SXMO_LOGDIR" - printf %b "$TIME\t$EVT_HANDLE\t$NUM\n" >> "$SXMO_LOGDIR/modemlog.tsv" + # Logging must never abort call control under `set -e` (for example when + # the log directory is not writable): a failed write would otherwise skip + # the actual accept/hangup that follows. + { + mkdir -p "$SXMO_LOGDIR" && + printf %b "$TIME\t$EVT_HANDLE\t$NUM\n" >> "$SXMO_LOGDIR/modemlog.tsv" + } 2>/dev/null || sxmo_log "Could not write $EVT_HANDLE to $SXMO_LOGDIR/modemlog.tsv" + return 0 } pickup() { @@ -134,33 +140,45 @@ hangup() { ;; esac + # Mark the call as hung up / discarded first (sxmo_modem.sh reads these + # markers when ModemManager reports the call as terminated), then end the + # call on the network right away, and only afterwards log and run hooks so + # a failing log write or hook can never leave the caller ringing. + WAS_PICKED_UP=0 if [ -f "$XDG_RUNTIME_DIR/sxmo_calls/${CALLID}.pickedupcall" ] || \ [ -f "$XDG_RUNTIME_DIR/sxmo_calls/${CALLID}.initiatedcall" ]; then - + WAS_PICKED_UP=1 rm -f "$XDG_RUNTIME_DIR/sxmo_calls/${CALLID}.pickedupcall" \ "$XDG_RUNTIME_DIR/sxmo_calls/${CALLID}.initiatedcall" - touch "$XDG_RUNTIME_DIR/sxmo_calls/${CALLID}.hangedupcall" - log_event "call_hangup" "$CALLID" - - sxmo_log "sxmo_modemcall: Invoking hangup hook" - sxmo_hook_hangup.sh + touch "$XDG_RUNTIME_DIR/sxmo_calls/${CALLID}.hangedupcall" || true else # this call was never picked up and hung up immediately, so it is a discarded call - touch "$XDG_RUNTIME_DIR/sxmo_calls/${CALLID}.discardedcall" - log_event "call_discard" "$CALLID" - - sxmo_log "sxmo_modemcall: Invoking discard hook" - sxmo_hook_discard.sh + touch "$XDG_RUNTIME_DIR/sxmo_calls/${CALLID}.discardedcall" || true fi + HANGUP_FAILED=0 if ! mmcli -m any -o "$CALLID" --hangup 2>/dev/null && \ ! doas -n /usr/libexec/msm-modem-uim-selection call-hangup "$CALLID" >/dev/null 2>&1; then # we ignore already closed calls if list_active_calls | grep -q "/$CALLID "; then - sxmo_notify_user.sh --urgency=critical "We failed to hangup the call" - return 1 + HANGUP_FAILED=1 fi fi + + if [ "$WAS_PICKED_UP" = "1" ]; then + log_event "call_hangup" "$CALLID" + sxmo_log "sxmo_modemcall: Invoking hangup hook" + sxmo_hook_hangup.sh || true + else + log_event "call_discard" "$CALLID" + sxmo_log "sxmo_modemcall: Invoking discard hook" + sxmo_hook_discard.sh || true + fi + + if [ "$HANGUP_FAILED" = "1" ]; then + sxmo_notify_user.sh --urgency=critical "We failed to hangup the call" + return 1 + fi } # We shallow muted/blocked and terminated calls diff --git a/overlay/rootfs/usr/bin/sxmo_modemmonitor.sh b/overlay/rootfs/usr/bin/sxmo_modemmonitor.sh index 34ee04a..6b94915 100755 --- a/overlay/rootfs/usr/bin/sxmo_modemmonitor.sh +++ b/overlay/rootfs/usr/bin/sxmo_modemmonitor.sh @@ -67,8 +67,15 @@ mainloop() { while : ; do uim_st="$(doas -n /usr/libexec/msm-modem-uim-selection status 2>/dev/null || /usr/libexec/msm-modem-uim-selection status 2>/dev/null || true)" if printf "%s\n" "$uim_st" | grep -q "^DUAL_SIM_ACTIVE=1$"; then - doas -n /usr/libexec/msm-modem-uim-selection dsds-daemon 2>/dev/null || \ + # Run the daemon as root whenever doas permits it. Only fall back to + # an unprivileged instance when doas is unavailable, not when the root + # daemon merely exited or was restarted (an unprivileged instance + # cannot keep the user's call log and call state files writable). + if doas -n /usr/libexec/msm-modem-uim-selection status >/dev/null 2>&1; then + doas -n /usr/libexec/msm-modem-uim-selection dsds-daemon 2>/dev/null || true + else /usr/libexec/msm-modem-uim-selection dsds-daemon 2>/dev/null || true + fi fi sleep 5 done diff --git a/overlay/rootfs/usr/libexec/msm-modem-uim-selection b/overlay/rootfs/usr/libexec/msm-modem-uim-selection index e31be47..5b39f3d 100755 --- a/overlay/rootfs/usr/libexec/msm-modem-uim-selection +++ b/overlay/rootfs/usr/libexec/msm-modem-uim-selection @@ -1995,8 +1995,70 @@ QMI_SERVICE_VOICE = 9 CALLS_DIR = "/run/user/10000/sxmo_calls" LOG_DIR = "/home/user/.local/share/sxmo/modem" -os.makedirs(CALLS_DIR, exist_ok=True) -os.makedirs(LOG_DIR, exist_ok=True) +MODEMLOG = os.path.join(LOG_DIR, "modemlog.tsv") + +# This daemon runs as root (via doas), but everything it creates under the +# user's runtime and data directories must stay owned by "user": the sxmo +# scripts that run as the user append to modemlog.tsv and create call state +# files under `set -e`, so a root-owned directory or log file makes them +# abort (no call log entries, and rejected calls never get hung up). +try: + import pwd + _pw = pwd.getpwnam("user") + USER_UID, USER_GID = _pw.pw_uid, _pw.pw_gid +except Exception: + USER_UID, USER_GID = 10000, 10000 + +def chown_user(path): + try: + os.chown(path, USER_UID, USER_GID) + except Exception: + pass + +def ensure_user_dir(path, stop_at): + # Create `path` (and missing parents below `stop_at`) owned by the user, + # and repair ownership of directories a previous root-run instance created. + parts = [] + cur = path + while cur and cur != stop_at and cur != "/": + parts.append(cur) + cur = os.path.dirname(cur) + for d in reversed(parts): + try: + os.mkdir(d, 0o755) + except FileExistsError: + pass + except Exception: + return + try: + if os.stat(d).st_uid == 0: + chown_user(d) + except Exception: + pass + +def append_modemlog(line): + ensure_user_dir(LOG_DIR, "/home/user") + try: + with open(MODEMLOG, "a", encoding="utf-8") as f: + f.write(line) + except Exception: + return + chown_user(MODEMLOG) + +# Only create the call state directory inside an existing user runtime dir; +# never create /run/user/10000 itself as root. +if os.path.isdir("/run/user/10000"): + ensure_user_dir(CALLS_DIR, "/run/user/10000") +ensure_user_dir(LOG_DIR, "/home/user") +# Repair logs and directories left root-owned by earlier daemon versions. +try: + for root_dir, dirs, files in os.walk(LOG_DIR): + for name in dirs + files: + p = os.path.join(root_dir, name) + if os.lstat(p).st_uid == 0: + chown_user(p) +except Exception: + pass def lookup_srv(sid): try: @@ -2219,19 +2281,15 @@ multipart_buf = {} def deliver_sms_to_sxmo(sender, iso_ts, text, sim_slot): num_dir = os.path.join(LOG_DIR, sender) - os.makedirs(num_dir, exist_ok=True) + ensure_user_dir(num_dir, "/home/user") sms_file = os.path.join(num_dir, "sms.txt") now_str = datetime.datetime.now().astimezone().isoformat(timespec="seconds") header = f"Received from {sender} [SIM {sim_slot}] at {now_str}:\n{text}\n\n" with open(sms_file, "a", encoding="utf-8") as f: f.write(header) tsv_text = text.replace("\t", " ").replace("\r", " ").replace("\n", " ") - with open(os.path.join(LOG_DIR, "modemlog.tsv"), "a", encoding="utf-8") as f: - f.write(f"{now_str}\trecv_txt\t{sender}\t[SIM {sim_slot}] {tsv_text}\n") - try: - subprocess.run(["chown", "-R", "user:user", LOG_DIR], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) - except Exception: - pass + append_modemlog(f"{now_str}\trecv_txt\t{sender}\t[SIM {sim_slot}] {tsv_text}\n") + chown_user(sms_file) send_fifo("/tmp/dwm_plugin_sms.fifo", "REFRESH") safe_sender = sender.replace("'", "") safe_text = text.replace("'", "") @@ -2411,6 +2469,7 @@ def sync_sub1_calls(calls, sim_slot): if st != "terminated": try: open(os.path.join(CALLS_DIR, f"{call_key}.{st}"), "w").close() + chown_user(os.path.join(CALLS_DIR, f"{call_key}.{st}")) except Exception: pass else: @@ -2429,27 +2488,23 @@ def sync_sub1_calls(calls, sim_slot): if has_mm_call_files(): del active_calls[cid] continue - with open(os.path.join(LOG_DIR, "modemlog.tsv"), "a", encoding="utf-8") as f: - f.write(f"{now_str}\tcall_ring\t{num}\t[SIM {sim_slot}]\n") + append_modemlog(f"{now_str}\tcall_ring\t{num}\t[SIM {sim_slot}]\n") send_fifo("/tmp/dwm_plugin_callscreen.fifo", f"INCOMING {call_key} {num} SIM{sim_slot}") run_user_cmd(f"sxmo_jobs.sh start --group ringing sxmo_hook_ring.sh '{num}'") elif st in ("dialing", "ringing-out"): set_q6voiced_audio(True) if prev not in ("dialing", "ringing-out"): - with open(os.path.join(LOG_DIR, "modemlog.tsv"), "a", encoding="utf-8") as f: - f.write(f"{now_str}\tcall_dial\t{num}\t[SIM {sim_slot}]\n") + append_modemlog(f"{now_str}\tcall_dial\t{num}\t[SIM {sim_slot}]\n") send_fifo("/tmp/dwm_plugin_callscreen.fifo", f"OUTGOING {call_key} {num} SIM{sim_slot}") elif st == "active": stop_ringing_user() set_q6voiced_audio(True) - with open(os.path.join(LOG_DIR, "modemlog.tsv"), "a", encoding="utf-8") as f: - f.write(f"{now_str}\tcall_start\t{num}\t[SIM {sim_slot}]\n") + append_modemlog(f"{now_str}\tcall_start\t{num}\t[SIM {sim_slot}]\n") send_fifo("/tmp/dwm_plugin_callscreen.fifo", f"ACTIVE {call_key} {num} SIM{sim_slot}") elif st == "terminated": stop_ringing_user() evt = "call_missed" if prev == "ringing-in" else "call_hangup" - with open(os.path.join(LOG_DIR, "modemlog.tsv"), "a", encoding="utf-8") as f: - f.write(f"{now_str}\t{evt}\t{num}\t[SIM {sim_slot}]\n") + append_modemlog(f"{now_str}\t{evt}\t{num}\t[SIM {sim_slot}]\n") send_fifo("/tmp/dwm_plugin_callscreen.fifo", "ENDED CALL ENDED") has_live = any(v["state"] in ("dialing", "ringing-out", "active", "held", "ringing-in", "waiting") for v in active_calls.values()) diff --git a/scripts/apply-bananarchy-overlay.sh b/scripts/apply-bananarchy-overlay.sh index 7ac7b58..da2ee30 100755 --- a/scripts/apply-bananarchy-overlay.sh +++ b/scripts/apply-bananarchy-overlay.sh @@ -45,6 +45,10 @@ cp -af "$BASE_DIR/overlay/rootfs/." "$TARGET_ROOTFS/" if [ -x "$BASE_DIR/scripts/version.sh" ]; then "$BASE_DIR/scripts/version.sh" "$TARGET_ROOTFS" fi +# Pre-create the user's modem/SMS log and blocklist directories so that the +# root-run dual-SIM daemon never creates them root-owned on a fresh image. +mkdir -p "$TARGET_ROOTFS/home/user/.local/share/sxmo/modem" \ + "$TARGET_ROOTFS/home/user/.local/share/sxmo/block" chown -R 10000:10000 "$TARGET_ROOTFS/home/user" 2>/dev/null || true chown 0:0 "$TARGET_ROOTFS/etc/doas.d/50-sxmo.conf" 2>/dev/null || true chmod 640 "$TARGET_ROOTFS/etc/doas.d/50-sxmo.conf" 2>/dev/null || true