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.
This commit is contained in:
Luxferre committed 2026-10-28 20:07:42 +02:00
1 parent 99e2280590
commit a4dbf71af9
5 files changed
+119 -33

No files matched your search

@@ -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())