内置 Termux: 自编 dpkg,和真 Termux 共存 (v2.0.33)
## 解决的根问题
装了真 Termux 的手机上 `pkg install` 必死:
dpkg: error: error opening configuration directory
'/data/data/com.termux/files/usr/etc/dpkg/dpkg.cfg.d': Permission denied
dpkg 的配置目录是**编译期常量**(Termux 把 $PREFIX 固化成了
/data/data/com.termux/files/usr),而且它**在解析命令行之前**就去读,
所以 --admindir / --root / DPKG_ROOT 全都救不了。那个目录属于另一个
App(0700),我们连穿都穿不进去 —— ENOENT 会静默跳过,EACCES 直接致命。
试过并全部失败的路子:--root / DPKG_ROOT / LD_PRELOAD=libtermux-exec.so /
prefix 内软链 / 在 /data/data 下建软链 / unshare -m / 直接改二进制
(21 字节 → 40 字节,长度不等)。
## 做法:自己编一份 dpkg
用 NDK 交叉编译 dpkg 1.22.6(上游源码 + Termux 的 7 个 Android 补丁 +
我们自己的 9 个补丁),一共修了四处:
1. **配置目录改成运行时解析** —— 从 /proc/self/exe 反推前缀,
同一个二进制在任何包名下都指向自己所在的前缀。
2. **架构名改用 Termux 的写法**(x86_64 / aarch64)。用 Debian 的
amd64/arm64 会让 dpkg 把数据库里**每一个包**都判成非法架构;
x86_64 带下划线,C 侧和 Perl 侧的架构名校验都要放开。
3. **修好 PATH / LD_LIBRARY_PATH** —— apt 派生 dpkg 时会塞给它一个
指向真 Termux 的 PATH,dpkg 于是报 "5 expected programs not found"。
4. **改写维护脚本里的前缀** —— Termux 包里整篇是
`#!/data/data/com.termux/files/usr/bin/sh` 和
`mkdir -p "/data/data/com.termux/files/usr/var/empty"`;
前者被内核解析(EACCES,dpkg 却显示成"脚本没有执行权限",指错方向),
后者是根下绝对路径、沙箱软链拦不住。三条执行路径都要挂上
(只挂一处的话 --configure 走的 vmaintscript_installed 不会有任何效果)。
构建配方、补丁、校验脚本都在 android/termux-dpkg/,从源码可复现。
二进制以 tar 包形式放进 lib/<abi>/libdoubao-dpkg.so(跟
libtermux-bootstrap.so 一个套路,Android 只对 lib/ 免压缩解包)。
## 实测(Android 14 x86_64,真 Termux 同时装着,全程读不到它)
$ pkg install -y openssh
Setting up openssh (10.6p1-2) ...
Generating public/private rsa/ecdsa/ed25519 key pair.
→ 未配置 = 0,半装 = 0,ssh -V → OpenSSH_10.6p1
还验过 nmap、git 2.56.0 全新安装(含 lua54/libpcap 这些带 postinst
的依赖链),全部 未配置 = 0,维护脚本里 com.termux 残留 = 0。
## 顺带
- 去掉终端页那条"检测到真 Termux,请卸载"的红色横幅 —— 现在不需要了。
- 补上 files/home 沙箱链:bash 找 $HOME/.bashrc 走的是
/data/data/com.termux/files/home,少了这条链每开一次终端都会撞真 Termux
的权限。建链顺序有讲究(files 接成软链之前必须先建 files/home)。
- bin/dpkg 不再是 wrapper 脚本,bin/dpkg.real 也不需要了 ——
wrapper 本来就是在绕"配置目录编译死"这个已经修掉的问题。
## 已知残留(不影响功能)
bin/bash 里还有 13 处编译死的 com.termux 路径(rewritePrefixes 用
"有 NUL 就跳过"避开二进制,这对长度不等的替换是对的),表现是每开一个
终端打印一次 `bash: .../bash.bashrc: Permission denied`。
读不到就跳过,终端和 pkg install 照常可用。
This commit is contained in:
55
android/termux-dpkg/patches/doubao/doubao-prefix-h.patch
Normal file
55
android/termux-dpkg/patches/doubao/doubao-prefix-h.patch
Normal file
@@ -0,0 +1,55 @@
|
||||
--- a/lib/dpkg/doubao-prefix.h 2026-10-14 12:41:34.579902172 +0800
|
||||
+++ b/lib/dpkg/doubao-prefix.h 2026-10-14 12:41:34.580926465 +0800
|
||||
@@ -0,0 +1,52 @@
|
||||
+/*
|
||||
+ * doubao-prefix.h - resolve the installation prefix at runtime
|
||||
+ *
|
||||
+ * Copyright (C) 2026 doubao-clone
|
||||
+ *
|
||||
+ * This is free software; you can redistribute it and/or modify
|
||||
+ * it under the terms of the GNU General Public License as published by
|
||||
+ * the Free Software Foundation; either version 2 of the License, or
|
||||
+ * (at your option) any later version.
|
||||
+ */
|
||||
+
|
||||
+#ifndef DOUBAO_PREFIX_H
|
||||
+#define DOUBAO_PREFIX_H
|
||||
+
|
||||
+#include <dpkg/dpkg.h>
|
||||
+
|
||||
+DPKG_BEGIN_DECLS
|
||||
+
|
||||
+/**
|
||||
+ * Return the prefix this dpkg was installed under.
|
||||
+ *
|
||||
+ * The prefix is derived from the path of the running executable, because
|
||||
+ * Android hands every application its own private data directory and the
|
||||
+ * same binary has to work under any of them. Falls back to the compile-time
|
||||
+ * DOUBAO_DEFAULT_PREFIX when the executable path cannot be determined.
|
||||
+ */
|
||||
+const char *doubao_prefix(void);
|
||||
+
|
||||
+/**
|
||||
+ * Return the configuration directory ($prefix/etc/dpkg).
|
||||
+ */
|
||||
+const char *doubao_config_dir(void);
|
||||
+
|
||||
+/**
|
||||
+ * Prepend our own bin directories to PATH and point LD_LIBRARY_PATH at our
|
||||
+ * own lib directory.
|
||||
+ *
|
||||
+ * Needed because apt sanitises the environment before it execs dpkg, and the
|
||||
+ * PATH it hands over names the *other* application's prefix; dpkg would then
|
||||
+ * fail its own sanity check with "expected programs not found in PATH".
|
||||
+ */
|
||||
+void doubao_fix_environment(void);
|
||||
+
|
||||
+/**
|
||||
+ * Rewrite a maintainer script's interpreter line so it points at this
|
||||
+ * installation instead of the application it was built for.
|
||||
+ */
|
||||
+void doubao_fix_script_shebang(const char *path);
|
||||
+
|
||||
+DPKG_END_DECLS
|
||||
+
|
||||
+#endif /* DOUBAO_PREFIX_H */
|
||||
Reference in New Issue
Block a user