Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,9 @@ notify = "8"
# 请求历史。bundled 是刻意的:零外部依赖,用户不必装 sqlite
rusqlite = { version = "0.40", features = ["bundled"] }
libc = "0.2"
# Windows 上问系统要网卡清单。**纯 Rust 的绑定**,不编译任何 C ——
# 和 unix 那边直接用 libc 是同一个取舍,见 tw-config 的 nics。
# 问 Windows 要它自己才知道的东西:网卡清单(tw-config 的 nics)、
# 一个 pid 在不在(twcore 的 proc)。**纯 Rust 的绑定**,不编译任何 C ——
# 和 unix 那边直接用 libc 是同一个取舍。
windows-sys = "0.61"
# 内置价目表压缩着放(2.3 MB → 110 KB)
flate2 = "1"
Expand Down
11 changes: 10 additions & 1 deletion bin/twcore/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,20 @@ anyhow = { workspace = true }
clap = { workspace = true }
tw-adopt = { workspace = true }
tw-scan = { workspace = true }
libc = "0.2"
serde_yaml_ng = { workspace = true }
tokio = { workspace = true }
tracing = { workspace = true }
tracing-subscriber = { workspace = true }

[target.'cfg(unix)'.dependencies]
# 只剩一处在用:问一个 pid 还在不在(见 src/proc.rs)
libc = { workspace = true }

[target.'cfg(windows)'.dependencies]
windows-sys = { workspace = true, features = [
"Win32_Foundation",
"Win32_System_Threading",
] }

[dev-dependencies]
tempfile = "3"
19 changes: 1 addition & 18 deletions bin/twcore/src/lockfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl LockFile {
if let Ok(text) = std::fs::read_to_string(&path)
&& let Ok(pid) = text.trim().parse::<u32>()
&& pid != std::process::id()
&& process_alive(pid)
&& crate::proc::alive(pid)
{
return Ok(LockOutcome::AlreadyRunning { pid });
}
Expand All @@ -48,23 +48,6 @@ impl Drop for LockFile {
}
}

/// 这个 pid 还活着吗。
///
/// `kill(pid, 0)` 是 POSIX 上的标准问法:不发信号,只做权限和存在性检查。
fn process_alive(pid: u32) -> bool {
#[cfg(unix)]
unsafe {
// ESRCH 表示进程不存在;EPERM 表示存在但不归我们管(也算活着)。
libc::kill(pid as i32, 0) == 0
|| std::io::Error::last_os_error().raw_os_error() == Some(libc::EPERM)
}
#[cfg(not(unix))]
{
let _ = pid;
true
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
15 changes: 2 additions & 13 deletions bin/twcore/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use anyhow::{Context, Result};
use clap::{Parser, Subcommand};

mod lockfile;
mod proc;

use lockfile::{LockFile, LockOutcome};

Expand Down Expand Up @@ -856,7 +857,7 @@ fn cmd_serve(path: &Path, port: Option<u16>, safe: bool, parent: Option<u32>) ->
tokio::spawn(async move {
loop {
tokio::time::sleep(std::time::Duration::from_secs(2)).await;
if !parent_alive(ppid) {
if !proc::alive(ppid) {
tracing::info!(ppid, "the parent exited; exiting with it");
std::process::exit(0);
}
Expand Down Expand Up @@ -969,18 +970,6 @@ fn build_store(
))
}

fn parent_alive(pid: u32) -> bool {
#[cfg(unix)]
unsafe {
libc::kill(pid as i32, 0) == 0
}
#[cfg(not(unix))]
{
let _ = pid;
true
}
}

async fn shutdown_signal() {
let ctrl_c = async {
tokio::signal::ctrl_c().await.ok();
Expand Down
99 changes: 99 additions & 0 deletions bin/twcore/src/proc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
//! 一个 pid 还在不在。
//!
//! 两个地方问的是同一个问题:单实例锁要判断锁文件里那个 pid 是不是上次没退
//! 干净留下的,`--parent` 要判断拉起我们的那个 UI 是不是已经没了。
//!
//! 以前各写了一份,而且**两份答得不一样** —— 锁那边把「存在但不归我们管」
//! 算活着,父进程那边算死。这里只留一份,取安全的那个答法:**拿不准就算
//! 活着**。反过来(拿不准就说死了)的代价是删掉另一个实例正在用的锁,
//! 或者无缘无故把自己退掉。
//!
//! **pid 会被系统回收**,两个平台都是。所以这个问题严格说来是「现在有没有
//! 一个进程叫这个号」,不是「当初那个进程还在不在」。锁文件靠 `Drop` 尽量
//! 把自己删掉来缩小这个窗口;再往下要对得上身份,就得存别的东西(创建时间、
//! 或者干脆换成一个内核对象),而那是另一件事。

/// 现在有没有一个进程是这个 pid。
#[cfg(unix)]
pub fn alive(pid: u32) -> bool {
// SAFETY: 0 号信号不发信号,只做存在性和权限检查。这是 POSIX 上的标准问法。
unsafe {
// ESRCH 表示进程不存在;EPERM 表示存在但不归我们管(也算活着)。
libc::kill(pid as i32, 0) == 0
|| std::io::Error::last_os_error().raw_os_error() == Some(libc::EPERM)
}
}

#[cfg(windows)]
pub fn alive(pid: u32) -> bool {
use windows_sys::Win32::Foundation::{CloseHandle, ERROR_ACCESS_DENIED, WAIT_TIMEOUT};
use windows_sys::Win32::System::Threading::{
OpenProcess, PROCESS_QUERY_LIMITED_INFORMATION, WaitForSingleObject,
};

/// winnt.h 里的标准访问权限之一。
///
/// **自己写一遍而不是从 windows-sys 里导入**:它只在
/// `Win32::Storage::FileSystem` 下导出(类型还是 `FILE_ACCESS_RIGHTS`),
/// 为一个固定不变的常量把整个文件系统的 feature 拉进来不划算。
const SYNCHRONIZE: u32 = 0x0010_0000;

// SAFETY: 只开一个查询用的句柄,拿到就用、用完就关。
unsafe {
let h = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION | SYNCHRONIZE, 0, pid);
if h.is_null() {
// **打不开不等于不存在。**ACCESS_DENIED 说明它在,只是不归我们管
// —— 和 unix 上的 EPERM 是同一件事,按上面那条算活着。
return std::io::Error::last_os_error().raw_os_error()
== Some(ERROR_ACCESS_DENIED as i32);
}
// **等 0 毫秒,不问退出码。**进程句柄在进程退出的那一刻变为有信号,
// 所以「等不到」就是「还在跑」,这个答案是准的。
//
// 流传更广的写法是 `GetExitCodeProcess` 然后比 `STILL_ACTIVE`,它有
// 一个真实的坑:`STILL_ACTIVE` 就是 259,于是一个正常退出、退出码
// 恰好是 259 的进程会被永远判成活着。core 自己不会那样退,但锁文件里
// 那个 pid 早就可能被系统回收给了任何一个别的程序。
let r = WaitForSingleObject(h, 0);
CloseHandle(h);
r == WAIT_TIMEOUT
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn this_very_process_is_alive() {
assert!(alive(std::process::id()));
}

/// 起一个、等它退干净、再问。**不能随便编一个 pid** —— 编出来的那个
/// 号上完全可能真的有一个进程,那样这个测试会偶发地失败。
#[test]
fn a_process_that_has_exited_is_not() {
let mut child = std::process::Command::new("cmd")
.args(["/C", "exit"])
.spawn()
.or_else(|_| {
std::process::Command::new("/bin/sh")
.args(["-c", "exit 0"])
.spawn()
})
.expect("起一个立刻退出的子进程");
let pid = child.id();
assert!(alive(pid), "刚起来就说它死了");
child.wait().expect("等它退出");
// 收尸之后那个 pid 才真正消失:在此之前它是僵尸,而僵尸是「活着」。
assert!(!alive(pid), "已经退出并收尸了,还说它活着");
}

/// pid 1 存在但不归我们管(unix 上是 init/launchd)。**打不开要算活着**,
/// 否则一个不归我们管的父进程会让 core 自己退掉。
#[cfg(unix)]
#[test]
fn a_process_we_may_not_touch_still_counts_as_alive() {
assert!(alive(1));
}
}
Loading