## 解决的根问题
装了真 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 照常可用。
353 lines
9.2 KiB
Diff
353 lines
9.2 KiB
Diff
--- a/lib/dpkg/doubao-prefix.c 2026-10-14 12:41:34.595602235 +0800
|
||
+++ b/lib/dpkg/doubao-prefix.c 2026-10-14 12:41:34.600860227 +0800
|
||
@@ -0,0 +1,349 @@
|
||
+/*
|
||
+ * doubao-prefix.c - 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.
|
||
+ *
|
||
+ * Upstream dpkg bakes its configuration directory in at configure time
|
||
+ * (-DCONFIGDIR), and Termux builds it as $PREFIX/etc/dpkg where $PREFIX is a
|
||
+ * fixed absolute path chosen by the Termux application. Android gives every
|
||
+ * application its own private data directory, so a dpkg built for
|
||
+ * com.termux will always look inside /data/data/com.termux -- a directory
|
||
+ * another application is not allowed to traverse. The resulting EACCES makes
|
||
+ * dpkg abort before it has even parsed its command line, which is why the two
|
||
+ * applications cannot coexist.
|
||
+ *
|
||
+ * We instead derive the prefix from the path of the running executable, which
|
||
+ * is exactly where this binary happens to live. That also lets us repair
|
||
+ * PATH, LD_LIBRARY_PATH and the interpreter lines of maintainer scripts,
|
||
+ * all of which upstream assumes are already correct.
|
||
+ */
|
||
+
|
||
+#include <config.h>
|
||
+#include <compat.h>
|
||
+
|
||
+#include <fcntl.h>
|
||
+#include <limits.h>
|
||
+#include <stdlib.h>
|
||
+#include <string.h>
|
||
+#include <sys/stat.h>
|
||
+#include <unistd.h>
|
||
+
|
||
+#include <dpkg/i18n.h>
|
||
+#include <dpkg/debug.h>
|
||
+#include <dpkg/dpkg.h>
|
||
+#include <dpkg/doubao-prefix.h>
|
||
+
|
||
+/* How many distinct strings we keep alive so returned pointers stay valid. */
|
||
+#define DOUBAO_CACHE_SLOTS 4
|
||
+
|
||
+static char prefix_buf[PATH_MAX];
|
||
+static const char *prefix_cached;
|
||
+static char config_buf[PATH_MAX];
|
||
+
|
||
+static void
|
||
+doubao_prefix_resolve(void)
|
||
+{
|
||
+ char self[PATH_MAX];
|
||
+ const char *override;
|
||
+ char *slash;
|
||
+ ssize_t n;
|
||
+
|
||
+ if (prefix_cached != NULL)
|
||
+ return;
|
||
+
|
||
+ prefix_cached = DOUBAO_DEFAULT_PREFIX;
|
||
+
|
||
+ override = getenv("DOUBAO_DPKG_PREFIX");
|
||
+ if (override != NULL) {
|
||
+ if (*override != '\0' && strlen(override) < sizeof(prefix_buf)) {
|
||
+ strcpy(prefix_buf, override);
|
||
+ prefix_cached = prefix_buf;
|
||
+ }
|
||
+ return;
|
||
+ }
|
||
+
|
||
+ n = readlink("/proc/self/exe", self, sizeof(self) - 1);
|
||
+ if (n <= 0 || (size_t)n >= sizeof(self) - 1)
|
||
+ return;
|
||
+ self[n] = '\0';
|
||
+
|
||
+ /* .../bin/dpkg -> .../bin -> ... */
|
||
+ slash = strrchr(self, '/');
|
||
+ if (slash == NULL)
|
||
+ return;
|
||
+ *slash = '\0';
|
||
+
|
||
+ slash = strrchr(self, '/');
|
||
+ if (slash == NULL)
|
||
+ return;
|
||
+ *slash = '\0';
|
||
+
|
||
+ if (strlen(self) < sizeof(prefix_buf)) {
|
||
+ strcpy(prefix_buf, self);
|
||
+ prefix_cached = prefix_buf;
|
||
+ }
|
||
+}
|
||
+
|
||
+const char *
|
||
+doubao_prefix(void)
|
||
+{
|
||
+ doubao_prefix_resolve();
|
||
+ return prefix_cached;
|
||
+}
|
||
+
|
||
+const char *
|
||
+doubao_config_dir(void)
|
||
+{
|
||
+ const char *prefix = doubao_prefix();
|
||
+
|
||
+ if (snprintf(config_buf, sizeof(config_buf), "%s/etc/dpkg", prefix)
|
||
+ >= (int)sizeof(config_buf))
|
||
+ return CONFIGDIR;
|
||
+
|
||
+ return config_buf;
|
||
+}
|
||
+
|
||
+/*
|
||
+ * Prepend our own bin directories to PATH and make sure the loader can find
|
||
+ * our shared libraries, so that dpkg and the maintainer scripts it runs work
|
||
+ * no matter what environment our parent handed us.
|
||
+ */
|
||
+void
|
||
+doubao_fix_environment(void)
|
||
+{
|
||
+ static bool done = false;
|
||
+ static char pathbuf[4 * PATH_MAX];
|
||
+ static char libbuf[PATH_MAX + 64];
|
||
+
|
||
+ const char *prefix, *old;
|
||
+ size_t len;
|
||
+
|
||
+ if (done)
|
||
+ return;
|
||
+ done = true;
|
||
+
|
||
+ prefix = doubao_prefix();
|
||
+
|
||
+ old = getenv("PATH");
|
||
+ /* Only prepend when our prefix is not already the first entry, so that
|
||
+ * a re-exec does not keep growing PATH. */
|
||
+ len = strlen(prefix);
|
||
+ if (old == NULL || strncmp(old, prefix, len) != 0 ||
|
||
+ strncmp(old + len, "/bin:", 5) != 0) {
|
||
+ if (snprintf(pathbuf, sizeof(pathbuf), "%s/bin:%s/bin/applets:%s",
|
||
+ prefix, prefix, old != NULL ? old : "/system/bin")
|
||
+ < (int)sizeof(pathbuf))
|
||
+ setenv("PATH", pathbuf, 1);
|
||
+ }
|
||
+
|
||
+ if (snprintf(libbuf, sizeof(libbuf), "%s/lib",
|
||
+ prefix) < (int)sizeof(libbuf))
|
||
+ setenv("LD_LIBRARY_PATH", libbuf, 1);
|
||
+}
|
||
+
|
||
+/*
|
||
+ * Maintainer scripts inside a Termux package are written in terms of the
|
||
+ * Termux prefix:
|
||
+ *
|
||
+ * #!/data/data/com.termux/files/usr/bin/sh
|
||
+ * mkdir -p "/data/data/com.termux/files/usr/var/empty"
|
||
+ * KEYFILE="/data/data/com.termux/files/usr/etc/ssh/ssh_host_rsa_key"
|
||
+ *
|
||
+ * Neither form works for us. The interpreter line is resolved by the kernel
|
||
+ * and lands in another application's private directory (EACCES, which dpkg
|
||
+ * then reports as though the script itself were not executable), and the
|
||
+ * absolute paths are absolute -- our sandbox symlinks live under our own
|
||
+ * prefix and cannot intercept a path that starts at "/".
|
||
+ *
|
||
+ * The scripts are plain text, so rewrite both before executing them. This is
|
||
+ * a local edit of the dpkg database rather than a change to the package, so
|
||
+ * it also covers packages installed long after this code was written.
|
||
+ */
|
||
+#define DOUBAO_OLD_PREFIX "/data/data/com.termux/files/usr"
|
||
+
|
||
+static void
|
||
+doubao_rewrite_script(const char *path)
|
||
+{
|
||
+ char *buf;
|
||
+ char *out;
|
||
+ const char *prefix;
|
||
+ size_t oldlen = sizeof(DOUBAO_OLD_PREFIX) - 1;
|
||
+ size_t newlen, len, cap;
|
||
+ size_t i, o;
|
||
+ int fd;
|
||
+ ssize_t n;
|
||
+ struct stat st;
|
||
+
|
||
+ if (path == NULL)
|
||
+ return;
|
||
+
|
||
+ fd = open(path, O_RDONLY);
|
||
+ if (fd < 0)
|
||
+ return;
|
||
+ if (fstat(fd, &st) != 0 || !S_ISREG(st.st_mode) || st.st_size <= 0 ||
|
||
+ st.st_size > 1024 * 1024) {
|
||
+ close(fd);
|
||
+ return;
|
||
+ }
|
||
+
|
||
+ len = (size_t)st.st_size;
|
||
+ buf = malloc(len + 1);
|
||
+ if (buf == NULL) {
|
||
+ close(fd);
|
||
+ return;
|
||
+ }
|
||
+ n = read(fd, buf, len);
|
||
+ close(fd);
|
||
+ if (n <= 0) {
|
||
+ free(buf);
|
||
+ return;
|
||
+ }
|
||
+ buf[n] = '\0';
|
||
+ len = (size_t)n;
|
||
+
|
||
+ prefix = doubao_prefix();
|
||
+ newlen = strlen(prefix);
|
||
+
|
||
+ /* 先看有没有要换的东西,没有就别写盘 */
|
||
+ for (i = 0; i + oldlen <= len; i++) {
|
||
+ if (memcmp(buf + i, DOUBAO_OLD_PREFIX, oldlen) == 0)
|
||
+ break;
|
||
+ }
|
||
+ if (i + oldlen > len) {
|
||
+ free(buf);
|
||
+ return;
|
||
+ }
|
||
+
|
||
+ /* 每一步替换都会变长(我们的前缀比 Termux 的长),所以要放大缓冲 */
|
||
+ cap = len + 64;
|
||
+ out = malloc(cap);
|
||
+ if (out == NULL) {
|
||
+ free(buf);
|
||
+ return;
|
||
+ }
|
||
+
|
||
+ for (i = 0, o = 0; i < len; ) {
|
||
+ if (i + oldlen <= len &&
|
||
+ memcmp(buf + i, DOUBAO_OLD_PREFIX, oldlen) == 0) {
|
||
+ if (o + newlen + (len - i) + 2 > cap) {
|
||
+ cap = (o + newlen + (len - i) + 2) * 2;
|
||
+ out = realloc(out, cap);
|
||
+ if (out == NULL) {
|
||
+ free(buf);
|
||
+ return;
|
||
+ }
|
||
+ }
|
||
+ memcpy(out + o, prefix, newlen);
|
||
+ o += newlen;
|
||
+ i += oldlen;
|
||
+ } else {
|
||
+ out[o++] = buf[i++];
|
||
+ }
|
||
+ }
|
||
+ out[o] = '\0';
|
||
+ free(buf);
|
||
+
|
||
+ fd = open(path, O_WRONLY | O_TRUNC);
|
||
+ if (fd < 0) {
|
||
+ free(out);
|
||
+ return;
|
||
+ }
|
||
+ if (write(fd, out, o) != (ssize_t)o) {
|
||
+ close(fd);
|
||
+ free(out);
|
||
+ ohshite(_("unable to rewrite maintainer script '%.250s'"), path);
|
||
+ }
|
||
+ close(fd);
|
||
+ free(out);
|
||
+
|
||
+ debug(dbg_scripts, "rewrote prefix in maintainer script '%s'", path);
|
||
+}
|
||
+
|
||
+/*
|
||
+ * 把维护脚本的 shebang 换成本安装自己的 shell。
|
||
+ *
|
||
+ * 保留原来用的是哪个 shell(`sh` 还是 `bash`)—— 换错解释器会毁掉脚本,
|
||
+ * 这一条真踩过:把 `bin/pip`(Python 脚本)的 shebang 改成 bash,
|
||
+ * 于是报 `bin/pip: line 2: import: command not found`。
|
||
+ * 规则是"只换路径,不动解释器"。
|
||
+ */
|
||
+void
|
||
+doubao_fix_script_shebang(const char *path)
|
||
+{
|
||
+ static const char old_shebang[] = "#!/data/data/com.termux/files/usr/";
|
||
+ /* Android 上 /data/user/0/<pkg> 和 /data/data/<pkg> 是同一个目录的两种
|
||
+ * 写法,包里两种都出现过,所以两个都要认。 */
|
||
+ static const char old_shebang_alt[] = "#!/data/user/0/com.termux/files/usr/";
|
||
+
|
||
+ char buf[PATH_MAX + 256];
|
||
+ const char *prefix;
|
||
+ char *nl;
|
||
+ int fd;
|
||
+ ssize_t n;
|
||
+ ssize_t skip = -1;
|
||
+ size_t head, rest, total;
|
||
+ char *fixed;
|
||
+
|
||
+ if (path == NULL)
|
||
+ return;
|
||
+
|
||
+ /* 先把正文里的绝对路径改掉(`#!/...` 那行也会一起被改到) */
|
||
+ doubao_rewrite_script(path);
|
||
+
|
||
+ fd = open(path, O_RDONLY);
|
||
+ if (fd < 0)
|
||
+ return;
|
||
+
|
||
+ n = read(fd, buf, sizeof(buf) - 1);
|
||
+ close(fd);
|
||
+ if (n <= 0)
|
||
+ return;
|
||
+ buf[n] = '\0';
|
||
+
|
||
+ if (strncmp(buf, old_shebang, sizeof(old_shebang) - 1) == 0)
|
||
+ skip = (ssize_t)(sizeof(old_shebang) - 1);
|
||
+ else if (strncmp(buf, old_shebang_alt, sizeof(old_shebang_alt) - 1) == 0)
|
||
+ skip = (ssize_t)(sizeof(old_shebang_alt) - 1);
|
||
+
|
||
+ if (skip < 0)
|
||
+ return;
|
||
+
|
||
+ nl = strchr(buf, '\n');
|
||
+ if (nl == NULL)
|
||
+ return;
|
||
+
|
||
+ prefix = doubao_prefix();
|
||
+ head = 2 + strlen(prefix) + 1; /* "#!/" + prefix + "/" */
|
||
+ rest = (size_t)n - (size_t)skip; /* 旧前缀之后的一直到文件尾 */
|
||
+ total = head + rest;
|
||
+ if (total + 1 > sizeof(buf))
|
||
+ return;
|
||
+
|
||
+ fixed = malloc(total + 1);
|
||
+ if (fixed == NULL)
|
||
+ return;
|
||
+
|
||
+ fixed[0] = '#';
|
||
+ fixed[1] = '!';
|
||
+ memcpy(fixed + 2, prefix, strlen(prefix));
|
||
+ fixed[2 + strlen(prefix)] = '/';
|
||
+ memcpy(fixed + head, buf + skip, rest);
|
||
+ fixed[total] = '\0';
|
||
+
|
||
+ fd = open(path, O_WRONLY | O_TRUNC);
|
||
+ if (fd < 0) {
|
||
+ free(fixed);
|
||
+ return;
|
||
+ }
|
||
+ if (write(fd, fixed, total) != (ssize_t)total)
|
||
+ ohshite(_("unable to rewrite maintainer script '%.250s'"), path);
|
||
+ close(fd);
|
||
+ free(fixed);
|
||
+
|
||
+ debug(dbg_scripts, "rewrote interpreter line of '%s'", path);
|
||
+}
|