From 825f964f7d89f33f7df019f00b3f88ed643c82e7 Mon Sep 17 00:00:00 2001 From: VallierDev <24893073+VallierDev@users.noreply.github.com> Date: Wed, 28 Oct 2026 14:32:37 +0800 Subject: [PATCH] doctor: warn about extra host instances; README: never share config.json (#122) Count only top-level hosts (helpers such as the Wayland capture process and the login window are children) and explain that two instances with one identity kick each other out of the room. Co-Authored-By: Claude Opus 5.5 --- README.en.md | 2 ++ README.md | 2 ++ src/doctor.rs | 38 +++++++++++++++++++++++++++++++------- 3 files changed, 35 insertions(+), 7 deletions(-) diff --git a/README.en.md b/README.en.md index 3ef4b54..dbdc7fb 100644 --- a/README.en.md +++ b/README.en.md @@ -214,6 +214,8 @@ machine, in `~/.config/uuremote/config.json` (mode 0600). A failed status query or the 60-second request timeout does not prove that the QR expired or that the phone confirmed login. The official-client refresh policy is unchanged: after a query error, click Refresh QR (Enter in a terminal; refresh after 60 seconds with neither a window nor a terminal). QR request diagnostics contain only the request phase, error category, elapsed time, HTTP/API status and last known scan state—not tickets, URLs or response content. A `connect` category alone does not identify a DNS, TLS or proxy cause. +The same file holds this machine's device identity (`device_id`, `client_id`, `system_id`), which reinstalls and new QR logins keep. **Do not copy `config.json` between machines or clone a second host from a snapshot/disk that includes the home directory**: two host instances with one identity, on the same or different machines, kick each other out of the room. The log then shows `Cloud room leave without peer identifier` every few seconds with reconnects, and viewers disconnect right after connecting. On a cloned or copied machine, stop the service, remove those three keys plus `token`, `csrf` and `user_id`, and run `uuremote-server login` to register a new device. A leftover instance on the same machine (for example an old `~/.local/bin/uuremote-server` started by hand) has the same effect; `uuremote-server doctor` warns when more than one host instance is running. + ### 4. Run as a user service `~/.config/systemd/user/uuremote-native.service`: diff --git a/README.md b/README.md index d4f815a..35d277a 100644 --- a/README.md +++ b/README.md @@ -197,6 +197,8 @@ cargo build --release --features wayland,portal # + GNOME/KDE Wayland 登录状态查询失败或达到 60 秒请求超时不代表二维码已过期,也不代表手机已经确认。程序保留官方客户端的刷新策略:查询出错后点击「刷新二维码」(终端按回车;无窗口且无终端时 60 秒后刷新)。二维码请求诊断只包含请求阶段、错误类别、耗时、HTTP/API 状态及最后已知扫码状态,不输出票据、URL 或响应内容;`connect` 类别本身不能确定是 DNS、TLS 还是代理问题。 +这个文件里还有本机的设备身份(`device_id`、`client_id`、`system_id`),重装软件和重新扫码登录都会沿用它。**不要在机器之间拷贝 `config.json`,也不要从带着家目录的快照/数据盘克隆出第二台主机**:同一身份的两个主机实例(不论在同一台还是不同机器上)会互相把对方踢出房间,日志表现为每隔几秒出现 `Cloud room leave without peer identifier` 并反复重连,控制端一连就断。克隆或拷贝出来的机器先停掉服务,删掉配置里这三个键和 `token`、`csrf`、`user_id`,再运行 `uuremote-server login` 注册为新设备。同一台机器上残留的旧实例(例如以前手动启动的 `~/.local/bin/uuremote-server`)也会这样,`uuremote-server doctor` 发现多个主机实例时会提示。 + ### 4. 作为用户服务常驻 `~/.config/systemd/user/uuremote-native.service`: diff --git a/src/doctor.rs b/src/doctor.rs index 458492a..dd9c19f 100644 --- a/src/doctor.rs +++ b/src/doctor.rs @@ -147,18 +147,33 @@ pub async fn run() -> Result<(), String> { Err(_) => line("config mode", "missing (run `uuremote-server login`)"), } line("service", command_output("systemctl", &["--user", "is-active", "uuremote-native.service"]).await.unwrap_or_else(|| "systemctl unavailable".into())); - let running = host_processes().map_or_else(|| "unknown".into(), |n| n.to_string()); - line("uuremote-server procs", format!("{running} (including this one; more than 2 means duplicates)")); + let running = host_processes(); + line("uuremote-server procs", format!("{} (including this one; more than 2 means duplicates)", running.map_or_else(|| "unknown".into(), |n| n.to_string()))); + if running.is_some_and(|n| n > 2) { + println!(" ! another host instance is running (check `pgrep -af uuremote`); stop the extra one, e.g. an old copy started by hand"); + println!(" instances sharing one identity, on this or another machine, kick each other out of the room: never copy config.json between machines"); + } Ok(()) } /// Hosts re-exec through /proc/self/exe (comm "exe"), so match the executable's file name, not the process name. +/// Helpers a host spawns (Wayland capture, login window) are its children and are not separate instances. fn host_processes() -> Option { - let count = std::fs::read_dir("/proc").ok()?.flatten() - .filter(|entry| entry.file_name().to_str().is_some_and(|name| name.bytes().all(|c| c.is_ascii_digit()))) - .filter_map(|entry| std::fs::read_link(entry.path().join("exe")).ok()) - .filter(|exe| is_host_executable(exe)).count(); - Some(count) + let hosts: Vec<(u32, u32)> = std::fs::read_dir("/proc").ok()?.flatten() + .filter_map(|entry| Some((entry.file_name().to_str()?.parse::().ok()?, entry.path()))) + .filter(|(_, dir)| std::fs::read_link(dir.join("exe")).is_ok_and(|exe| is_host_executable(&exe))) + .filter_map(|(pid, dir)| Some((pid, parent_pid(&std::fs::read_to_string(dir.join("stat")).ok()?)?))) + .collect(); + Some(top_level(&hosts)) +} + +/// Field 4 of /proc//stat; the command name in field 2 may contain spaces and parentheses. +fn parent_pid(stat: &str) -> Option { + stat.rsplit_once(')')?.1.split_whitespace().nth(1)?.parse().ok() +} + +fn top_level(hosts: &[(u32, u32)]) -> usize { + hosts.iter().filter(|(_, parent)| !hosts.iter().any(|(pid, _)| pid == parent)).count() } fn is_host_executable(exe: &std::path::Path) -> bool { @@ -198,4 +213,13 @@ mod tests { assert!(!super::is_host_executable(Path::new(exe))); } } + + #[test] + fn host_helpers_are_not_counted_as_instances() { + assert_eq!(super::parent_pid("8943 (exe) S 1699 8943 1699 0 -1"), Some(1699)); + assert_eq!(super::parent_pid("12 (a) b (c) R 7 12"), Some(7)); + // service 10 with a login window 11, a stale copy 20, doctor 30 + assert_eq!(super::top_level(&[(10, 1), (11, 10), (20, 5), (30, 9)]), 3); + assert_eq!(super::top_level(&[(10, 1), (11, 10), (30, 9)]), 2); + } }