From 1207e920255335d84efa48b72cbaf21fafb77077 Mon Sep 17 00:00:00 2001 From: LucaLin <78164141+LucaLin233@users.noreply.github.com> Date: Wed, 2 Dec 2026 05:53:20 +0800 Subject: [PATCH] fix(motd): detect terminal width outside command substitution motd_columns ran inside $(...), where stdout is a pipe, so [[ -t 1 ]] was always false and a real 32-column terminal fell back to 40 columns. Capture the stdout TTY state before the substitution and pass it to the function, keeping explicit COLUMNS priority and the PAM 40-column fallback. Cover the real PTY path with a regression test whose payload stderr is redirected inside the script child, so a fd 2 based implementation cannot pass. --- modules/system-customize.sh | 8 +++++--- tests/test-motd.sh | 19 ++++++++++++++++++- tools/setup-motd.sh | 8 +++++--- 3 files changed, 28 insertions(+), 7 deletions(-) diff --git a/modules/system-customize.sh b/modules/system-customize.sh index d70db19..5b36028 100644 --- a/modules/system-customize.sh +++ b/modules/system-customize.sh @@ -38,7 +38,7 @@ fi readonly MOTD_ETC_ROOT MOTD_STATE_DIR MOTD_LOCK_FILE MOTD_TRANSACTION_PARENT MOTD_TRUSTED_UID MOTD_TRUSTED_GID readonly MOTD_SCRIPT="$MOTD_ETC_ROOT/update-motd.d/00-custom-welcome" readonly MOTD_SNAPSHOT_VERSION=1 -readonly MOTD_PAYLOAD_SHA256="04152b5c08fd321c7d64046d82c3d1e8a90a287ace7a2ca505d0245b4de07ce1" +readonly MOTD_PAYLOAD_SHA256="1e1a2987751cbaa3fca0697e20f5bb9c16dad13fb28a61bf1540807fc67ac2b8" readonly -a MOTD_TARGET_IDS=(motd issue issue_net custom_welcome uname motd_news) readonly -a MOTD_TARGET_PATHS=( "$MOTD_ETC_ROOT/motd" "$MOTD_ETC_ROOT/issue" "$MOTD_ETC_ROOT/issue.net" @@ -2083,11 +2083,13 @@ motd_write_payload(){ local destination="$1"; install -m 0755 /dev/stdin "$desti export LC_ALL=C +motd_stdout_is_tty=false +if [[ -t 1 ]]; then motd_stdout_is_tty=true; fi motd_columns() { local value="${COLUMNS:-}" digits # PAM/run-parts 用 env -i 执行并重定向输出,既没有 COLUMNS 也不是 TTY; - # 只有直接运行在交互终端里才尝试 tput 取列宽。 - if [[ ! "$value" =~ ^[0-9]+$ ]] && [[ -t 1 ]] && command -v tput >/dev/null 2>&1; then + # 命令替换里的 stdout 是管道,TTY 状态必须在替换外判定后再交给本函数。 + if [[ ! "$value" =~ ^[0-9]+$ ]] && [[ "$motd_stdout_is_tty" == true ]] && command -v tput >/dev/null 2>&1; then value=$(tput cols 2>/dev/null) || value="" fi # 先去掉前导零再校验位数,避免超长数字进入 10# 运算后回绕;08 等写法按十进制处理。 diff --git a/tests/test-motd.sh b/tests/test-motd.sh index b516a2a..56902d9 100644 --- a/tests/test-motd.sh +++ b/tests/test-motd.sh @@ -251,7 +251,7 @@ extract_payload "$ROOT_DIR/tools/setup-motd.sh">"$TEST_DIR/t"; extract_payload "$ROOT_DIR/modules/system-customize.sh">"$TEST_DIR/m"; cmp -s "$TEST_DIR/t" "$TEST_DIR/m"||fail payload; pass "MOTD payloads stay identical"; -assert_eq 04152b5c08fd321c7d64046d82c3d1e8a90a287ace7a2ca505d0245b4de07ce1 "$(sha256sum "$TEST_DIR/t"|awk '{print $1}')" "payload hash" +assert_eq 1e1a2987751cbaa3fca0697e20f5bb9c16dad13fb28a61bf1540807fc67ac2b8 "$(sha256sum "$TEST_DIR/t"|awk '{print $1}')" "payload hash" # 固定版式断言按显式 80 列跑,避免宿主长主机名/云内核在 40 列兜底里自然折行后误失败。 payload_output=$(COLUMNS=80 bash "$TEST_DIR/t" | sed -E $'s/\033\\[[0-9;]*m//g' | sed 's/[[:space:]]*$//'); payload_lines=$(printf '%s\n' "$payload_output" | sed '/^$/d'); @@ -410,6 +410,23 @@ physical_out=$(COLUMNS=32 PATH="$TEST_DIR/bin-physical-wide:$PATH" bash "$TEST_D [[ "$(join_text "$physical_out")" == *"usnc-prod-edge-node-hkg-01.internal.example.com"* ]] || fail "long hostname lost at 32 columns"; pass "payload keeps physical core and thread semantics at 32 columns" +# 真实 32 列 PTY 回归:没有 COLUMNS,payload 的 stdout 是终端。 +# 宽度必须在命令替换外判定,否则 motd_columns 在 $(...) 里只看到管道, +# 会错误退回 40 列;旧版在这里输出 38 列,32 列终端会折行。 +# payload 自己的 stderr 在 script 子命令内重定向到文件;外层 2> 只抓 wrapper +# 的 stderr。两者分开断言,确保只看 fd 1 的实现能通过、改用 fd 2 的变异会失败。 +pty_out="$TEST_DIR/pty-out" pty_child_err="$TEST_DIR/pty-child-err" pty_wrapper_err="$TEST_DIR/pty-wrapper-err" +env -u COLUMNS TERM=xterm-256color script -qec "stty cols 32 rows 24; exec bash '$TEST_DIR/t' 2>'$pty_child_err'" /dev/null >"$pty_out" 2>"$pty_wrapper_err" || fail "32-column PTY run failed"; +[[ ! -s "$pty_child_err" ]] || fail "32-column PTY payload wrote to stderr: $(cat "$pty_child_err")"; +[[ ! -s "$pty_wrapper_err" ]] || fail "32-column PTY wrapper wrote to stderr: $(cat "$pty_wrapper_err")"; +pty_text=$(tr -d '\r' <"$pty_out"); +pty_width=$(widest_line "$pty_text"); +(( pty_width > 0 && pty_width <= 32 )) || fail "32-column PTY output is $pty_width columns wide"; +pty_joined=$(join_text "$pty_text"); +[[ "$pty_joined" == *"欢迎登录"* ]] || fail "32-column PTY lost the welcome line"; +[[ "$pty_joined" == *"CPU"* ]] || fail "32-column PTY lost the CPU line"; +pass "payload autodetects 32 columns from a real PTY without COLUMNS ($pty_width)" + # NO_COLOR 断言必须比较同一份输入:运行时间、负载、内存和根盘每次都会变, # 直接对两次实时取样做整体字符串相等会随宿主波动误报。这里用一个最薄的包装脚本, # 只把 /proc/uptime、/proc/loadavg、/proc/meminfo 和 df 换成固定值,其余命令照常, diff --git a/tools/setup-motd.sh b/tools/setup-motd.sh index ad64210..5fed709 100755 --- a/tools/setup-motd.sh +++ b/tools/setup-motd.sh @@ -24,7 +24,7 @@ fi readonly MOTD_ETC_ROOT MOTD_STATE_DIR MOTD_LOCK_FILE MOTD_TRANSACTION_PARENT MOTD_TRUSTED_UID MOTD_TRUSTED_GID readonly MOTD_SCRIPT="$MOTD_ETC_ROOT/update-motd.d/00-custom-welcome" readonly MOTD_SNAPSHOT_VERSION=1 -readonly MOTD_PAYLOAD_SHA256="04152b5c08fd321c7d64046d82c3d1e8a90a287ace7a2ca505d0245b4de07ce1" +readonly MOTD_PAYLOAD_SHA256="1e1a2987751cbaa3fca0697e20f5bb9c16dad13fb28a61bf1540807fc67ac2b8" readonly -a MOTD_TARGET_IDS=(motd issue issue_net custom_welcome uname motd_news) readonly -a MOTD_TARGET_PATHS=( "$MOTD_ETC_ROOT/motd" "$MOTD_ETC_ROOT/issue" "$MOTD_ETC_ROOT/issue.net" @@ -2069,11 +2069,13 @@ motd_write_payload(){ local destination="$1"; install -m 0755 /dev/stdin "$desti export LC_ALL=C +motd_stdout_is_tty=false +if [[ -t 1 ]]; then motd_stdout_is_tty=true; fi motd_columns() { local value="${COLUMNS:-}" digits # PAM/run-parts 用 env -i 执行并重定向输出,既没有 COLUMNS 也不是 TTY; - # 只有直接运行在交互终端里才尝试 tput 取列宽。 - if [[ ! "$value" =~ ^[0-9]+$ ]] && [[ -t 1 ]] && command -v tput >/dev/null 2>&1; then + # 命令替换里的 stdout 是管道,TTY 状态必须在替换外判定后再交给本函数。 + if [[ ! "$value" =~ ^[0-9]+$ ]] && [[ "$motd_stdout_is_tty" == true ]] && command -v tput >/dev/null 2>&1; then value=$(tput cols 2>/dev/null) || value="" fi # 先去掉前导零再校验位数,避免超长数字进入 10# 运算后回绕;08 等写法按十进制处理。