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
27 changes: 27 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,33 @@ jobs:
working-directory: enterprise
run: cargo check --workspace --all-targets

# **同是 unix,不等于和 macOS 一样。**`getifaddrs` 给的标志、sysfs 里有
# 没有 `device`、pid 的上限、`stat` 的参数,这些在 macOS 上测过不说明
# 任何事 —— 而桌面版要在 22.04 上跑,所以就在 22.04 上测。
#
# 不重复跑 `cargo fmt`:格式和平台无关。
linux:
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
with:
components: clippy
- uses: Swatinem/rust-cache@v2
with:
# 理由见下面 windows 那一段
cache-on-failure: true

- name: clippy
run: cargo clippy --workspace --all-targets -- -D warnings

- name: Test
run: cargo test --workspace

# 真二进制、真 socket、真数据面,和 macOS 那一步是同一个脚本
- name: Smoke (real binary, real socket, real data plane)
run: ./scripts/smoke.sh

# **这台机器上没有 unix socket,也没有信号。**core 在这两件事上各有一套
# 平台实现,而在这个任务出现之前,Windows 那一半从来没有被编译过 ——
# 一个 `#[cfg(windows)]` 模块「检查通过」和「根本没人看过它」长得一模一样。
Expand Down
97 changes: 97 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,100 @@ jobs:
dist/twcore-aarch64-pc-windows-msvc.exe
dist/twcore-aarch64-pc-windows-msvc.exe.sha256
generate_release_notes: true

twcore-linux:
name: twcore (${{ matrix.target }})
# **两个架构都原生编**,各在自己架构的 runner 上 —— 交叉编 aarch64 要
# 一套 aarch64 的 glibc sysroot 和链接器,而 GitHub 给公开仓库提供了
# arm64 的 Linux runner(`ubuntu-22.04-arm`),原生编还能顺手把产物
# 跑一遍,交叉编的跑不了。
#
# **两个都是 22.04,不是 24.04。**glibc 只向后兼容:在 2.39(24.04)上
# 链出来的二进制,拿到 2.35(22.04)上会报 `GLIBC_2.38 not found` 起不来。
# 桌面版在 Linux 上的底线就是 22.04(WebKitGTK 4.1),core 跟它对齐。
#
# **不用 musl 静态编。**它能摆脱 glibc 版本,但桌面版本体反正要 glibc
# 2.35+,core 单独摆脱它换不来什么;而 musl 的内存分配器在多线程下慢得
# 多、DNS 解析的行为和 glibc 不同 —— 用户机器上出了问题,排查的是一个
# 和系统里其他程序都不一样的运行时。同一个 glibc 底线,排查也简单。
strategy:
fail-fast: false
matrix:
include:
- target: x86_64-unknown-linux-gnu
runner: ubuntu-22.04
file_arch: x86-64
- target: aarch64-unknown-linux-gnu
runner: ubuntu-22.04-arm
file_arch: ARM aarch64
runs-on: ${{ matrix.runner }}
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
with:
targets: ${{ matrix.target }}
- uses: Swatinem/rust-cache@v2
with:
key: ${{ matrix.target }}

- name: Build
run: cargo build --release -p twcore --target ${{ matrix.target }}

# **产物自检。**一个编得过但起不来的二进制,从文件列表上看不出任何
# 问题 —— 而它会一路发到用户手里。
#
# 比 macOS 那段多一条 glibc 的上限:runner 的镜像哪天换了、或者有人把
# 上面的 22.04 改成了 latest,产物就会悄悄要求一个更新的 glibc,而在
# 构建机上它照样跑得起来 —— 只有用户的 22.04 上起不来。
- name: It runs, it is the version on the tag, and it needs glibc 2.35 at most
env:
FILE_ARCH: ${{ matrix.file_arch }}
run: |
set -euo pipefail
BIN=target/${{ matrix.target }}/release/twcore
file "$BIN"
file "$BIN" | grep -q "$FILE_ARCH"

MAX_GLIBC=2.35
NEED=$(objdump -T "$BIN" | grep -o 'GLIBC_[0-9][0-9.]*' | sed 's/GLIBC_//' | sort -uV | tail -n 1)
if [ -z "$NEED" ]; then
echo "objdump found no GLIBC_ symbol versions in $BIN" >&2
exit 1
fi
echo "needs glibc $NEED (limit $MAX_GLIBC)"
if [ "$(printf '%s\n%s\n' "$NEED" "$MAX_GLIBC" | sort -V | tail -n 1)" != "$MAX_GLIBC" ]; then
echo "twcore needs glibc $NEED, newer than $MAX_GLIBC (Ubuntu 22.04)" >&2
exit 1
fi

GOT=$("$BIN" --version | awk '{print $2}')
# 排练时没有 tag 可对,只把版本打出来
if [ "$GITHUB_REF_TYPE" = tag ]; then
WANT="${GITHUB_REF_NAME#v}"
if [ "$GOT" != "$WANT" ]; then
echo "the tag says $WANT but the binary reports $GOT -- Cargo.toml did not keep up" >&2
exit 1
fi
else
echo "rehearsal: the binary reports $GOT"
fi
"$BIN" --help > /dev/null

- name: Package
run: |
set -euo pipefail
mkdir -p dist
cp target/${{ matrix.target }}/release/twcore dist/twcore-${{ matrix.target }}
cd dist
# 和另外两个平台同一种校验文件:`<sha> <文件名>`
sha256sum twcore-${{ matrix.target }} > twcore-${{ matrix.target }}.sha256
cat twcore-${{ matrix.target }}.sha256

- uses: softprops/action-gh-release@v2
# 排练不发布。
if: github.ref_type == 'tag'
with:
files: |
dist/twcore-${{ matrix.target }}
dist/twcore-${{ matrix.target }}.sha256
generate_release_notes: true
16 changes: 13 additions & 3 deletions bin/twcore/src/lockfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,19 @@ mod tests {
fn a_stale_lock_from_a_dead_process_is_taken_over() {
// 「上次没退干净」是最常见的情况,不该需要用户手动删文件。
let d = tempfile::tempdir().unwrap();
// 挑一个几乎肯定不存在的。Windows 的 pid 都是 4 的倍数,所以
// 999999 在那里根本不可能是一个 pid;macOS 的 pid_max 是 99998。
std::fs::write(d.path().join("twcore.lock"), "999999").unwrap();
// **不编一个 pid。**以前这里写死 999999,理由是 macOS 的 pid_max
// 是 99998、Windows 的 pid 都是 4 的倍数 —— 而 Linux 上 systemd 把
// pid_max 设成 4194304,999999 完全可能是一个活着的进程。起一个、
// 等它退干净,拿它的号:这个号此刻一定没人用(除非刚好被回收,
// 那个窗口比编一个号小得多)。
let pid = {
let mut c = sleeping_child();
let pid = c.id();
c.kill().unwrap();
c.wait().unwrap();
pid
};
std::fs::write(d.path().join("twcore.lock"), pid.to_string()).unwrap();
assert!(matches!(
LockFile::acquire(d.path()).unwrap(),
LockOutcome::Acquired(_)
Expand Down
7 changes: 5 additions & 2 deletions bin/twcore/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -861,8 +861,11 @@ fn cmd_serve(path: &Path, port: Option<u16>, safe: bool, parent: Option<u32>) ->

if let Some(ppid) = parent {
// 父进程守望:GUI 没了我们跟着退。轮询而不是用 kqueue,是因为
// 这段代码要能在被 launchd 重新 parent 之后仍然正确 —— 那时
// getppid() 会变成 1,而我们要看的是原来那个 pid 还在不在。
// 这段代码要能在被重新 parent 之后仍然正确 —— macOS 上收养我们的
// 是 launchd(getppid() 变成 1),Linux 桌面会话里多半是
// `systemd --user`(它是 subreaper,getppid() 变成它的 pid,不是 1)。
// 所以**不看 getppid()**,只看原来那个 pid 还在不在,哪个平台、
// 谁来收养都一样。
tokio::spawn(async move {
loop {
tokio::time::sleep(std::time::Duration::from_secs(2)).await;
Expand Down
2 changes: 1 addition & 1 deletion crates/tw-api/msg-codes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ gw.internal
gw.listen.addr_unavailable
gw.listen.bind_failed
gw.listen.denied
gw.listen.nic_no_addr
gw.listen.nic_offline
gw.listen.no_such_nic
gw.listen.port_taken
gw.model.no_upstream
Expand Down
12 changes: 8 additions & 4 deletions crates/tw-config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,9 +343,13 @@ pub enum BindError {
name: String,
available: Vec<String>,
},
/// 网卡在,但此刻没有地址:网线拔了、Wi-Fi 没连上、还没拿到 DHCP。
#[error("interface `{name}` currently has no address")]
NicHasNoAddr { name: String },
/// 网卡在,但此刻用不了:网线拔了、Wi-Fi 没连上、还没拿到 DHCP。
///
/// **不说「没有地址」。**没连上的网卡常常还挂着地址(静态配的、或者
/// 一个没有容器接着的 `docker0`),[`nics::list`] 按「连着没有」把它们
/// 筛掉了 —— 这时说「没有地址」是错的,而用户要做的事两种情况一样。
#[error("interface `{name}` is not connected right now")]
NicOffline { name: String },
}

impl Bind {
Expand All @@ -361,7 +365,7 @@ impl Bind {
// 界面上的选单列的也是它,选单上写的就是真要监听的那个
Bind::Nic(name) => match nics::by_name().into_iter().find(|n| n.name == *name) {
Some(n) => Ok(n.addr),
None if nics::exists(name) => Err(BindError::NicHasNoAddr { name: name.clone() }),
None if nics::exists(name) => Err(BindError::NicOffline { name: name.clone() }),
None => Err(BindError::NoSuchNic {
name: name.clone(),
available: nics::by_name().into_iter().map(|n| n.name).collect(),
Expand Down
Loading
Loading