内置 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:
@@ -0,0 +1,14 @@
|
||||
diff -u -r ../dpkg-1.17.6/lib/dpkg/dbmodify.c ./lib/dpkg/dbmodify.c
|
||||
--- ../dpkg-1.17.6/lib/dpkg/dbmodify.c 2013-12-14 06:36:07.000000000 +0100
|
||||
+++ ./lib/dpkg/dbmodify.c 2014-02-25 18:12:15.000000000 +0100
|
||||
@@ -253,8 +253,10 @@
|
||||
switch (readwritereq) {
|
||||
case msdbrw_needsuperuser:
|
||||
case msdbrw_needsuperuserlockonly:
|
||||
+#ifndef __ANDROID__
|
||||
if (getuid() || geteuid())
|
||||
ohshit(_("requested operation requires superuser privilege"));
|
||||
+#endif
|
||||
/* Fall through. */
|
||||
case msdbrw_write: case msdbrw_writeifposs:
|
||||
if (access(dpkg_db_get_dir(), W_OK)) {
|
||||
@@ -0,0 +1,20 @@
|
||||
diff -u -r ../dpkg-1.18.2/lib/dpkg/atomic-file.c ./lib/dpkg/atomic-file.c
|
||||
--- ../dpkg-1.18.2/lib/dpkg/atomic-file.c 2015-07-12 22:38:47.000000000 -0400
|
||||
+++ ./lib/dpkg/atomic-file.c 2015-08-25 18:06:51.689715379 -0400
|
||||
@@ -90,8 +90,14 @@
|
||||
|
||||
if (unlink(name_old) && errno != ENOENT)
|
||||
ohshite(_("error removing old backup file '%s'"), name_old);
|
||||
- if (link(file->name, name_old) && errno != ENOENT)
|
||||
- ohshite(_("error creating new backup file '%s'"), name_old);
|
||||
+#ifdef __ANDROID__
|
||||
+ /* Termux: Use rename(2) since Android does not support hardlinks. */
|
||||
+ if (rename(file->name, name_old) && errno != ENOENT) {
|
||||
+#else
|
||||
+ if (link(file->name, name_old) && errno != ENOENT) {
|
||||
+#endif
|
||||
+ ohshite(_("error creating new backup file '%s'"), name_old);
|
||||
+ }
|
||||
|
||||
free(name_old);
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
Handle EROFS. This is since path_remove_tree() will be called with
|
||||
'/data.dpkg-tmp'
|
||||
'/data/data.dpkg-tmp'
|
||||
'/data/data/com.termux.dpkg-tmp'
|
||||
'/data/data/com.termux/files.dpkg-tmp'
|
||||
'/data/data/com.termux/files/usr.dpkg-tmp'
|
||||
and the first call will get a EROFS, read-only file system error.
|
||||
|
||||
diff -u -r ../dpkg-1.18.2/lib/dpkg/path-remove.c ./lib/dpkg/path-remove.c
|
||||
--- ../dpkg-1.18.2/lib/dpkg/path-remove.c 2015-07-30 00:39:24.000000000 -0400
|
||||
+++ ./lib/dpkg/path-remove.c 2015-08-25 18:04:31.391421421 -0400
|
||||
@@ -126,7 +126,7 @@
|
||||
debug(dbg_eachfile, "%s '%s'", __func__, pathname);
|
||||
if (!rmdir(pathname))
|
||||
return; /* Deleted it OK, it was a directory. */
|
||||
- if (errno == ENOENT || errno == ELOOP)
|
||||
+ if (errno == ENOENT || errno == ELOOP || errno == EROFS)
|
||||
return;
|
||||
if (errno == ENOTDIR) {
|
||||
/* Either it's a file, or one of the path components is. If
|
||||
@@ -0,0 +1,73 @@
|
||||
diff -u -r src-orig/src/main/archives.c src/src/main/archives.c
|
||||
--- src-orig/src/main/archives.c 2024-03-03 01:30:15.000000000 +0000
|
||||
+++ src/src/main/archives.c 2024-05-05 20:36:54.148725216 +0000
|
||||
@@ -394,9 +394,11 @@
|
||||
namenode->statoverride->uid,
|
||||
namenode->statoverride->gid,
|
||||
namenode->statoverride->mode);
|
||||
+#ifndef __ANDROID__
|
||||
rc = fchown(fd, st->uid, st->gid);
|
||||
if (forcible_nonroot_error(rc))
|
||||
ohshite(_("error setting ownership of '%.255s'"), te->name);
|
||||
+#endif
|
||||
rc = fchmod(fd, st->mode & ~S_IFMT);
|
||||
if (forcible_nonroot_error(rc))
|
||||
ohshite(_("error setting permissions of '%.255s'"), te->name);
|
||||
@@ -509,13 +511,17 @@
|
||||
return; /* Already handled using the file descriptor. */
|
||||
|
||||
if (te->type == TAR_FILETYPE_SYMLINK) {
|
||||
+#ifndef __ANDROID__
|
||||
rc = lchown(path, st->uid, st->gid);
|
||||
if (forcible_nonroot_error(rc))
|
||||
ohshite(_("error setting ownership of symlink '%.255s'"), path);
|
||||
+#endif
|
||||
} else {
|
||||
+#ifndef __ANDROID__
|
||||
rc = chown(path, st->uid, st->gid);
|
||||
if (forcible_nonroot_error(rc))
|
||||
ohshite(_("error setting ownership of '%.255s'"), path);
|
||||
+#endif
|
||||
rc = chmod(path, st->mode & ~S_IFMT);
|
||||
if (forcible_nonroot_error(rc))
|
||||
ohshite(_("error setting permissions of '%.255s'"), path);
|
||||
@@ -555,9 +561,11 @@
|
||||
else if (linksize > stab->st_size)
|
||||
ohshit(_("symbolic link '%.250s' size has changed from %jd to %zd"),
|
||||
fn_old, (intmax_t)stab->st_size, linksize);
|
||||
+#ifndef __ANDROID__
|
||||
else if (linksize < stab->st_size)
|
||||
warning(_("symbolic link '%.250s' size has changed from %jd to %zd"),
|
||||
fn_old, (intmax_t)stab->st_size, linksize);
|
||||
+#endif
|
||||
linkmatch = strcmp(linkname.buf, te->linkname) == 0;
|
||||
varbuf_destroy(&linkname);
|
||||
if (linkmatch)
|
||||
@@ -1053,18 +1061,27 @@
|
||||
else if (linksize > stab.st_size)
|
||||
ohshit(_("symbolic link '%.250s' size has changed from %jd to %zd"),
|
||||
fnamevb.buf, (intmax_t)stab.st_size, linksize);
|
||||
+#ifndef __ANDROID__
|
||||
else if (linksize < stab.st_size)
|
||||
warning(_("symbolic link '%.250s' size has changed from %jd to %zd"),
|
||||
fnamevb.buf, (intmax_t)stab.st_size, linksize);
|
||||
+#endif
|
||||
if (symlink(symlinkfn.buf,fnametmpvb.buf))
|
||||
ohshite(_("unable to make backup symlink for '%.255s'"), ti->name);
|
||||
+#ifndef __ANDROID__
|
||||
rc = lchown(fnametmpvb.buf, stab.st_uid, stab.st_gid);
|
||||
if (forcible_nonroot_error(rc))
|
||||
ohshite(_("unable to chown backup symlink for '%.255s'"), ti->name);
|
||||
+#endif
|
||||
tarobject_set_se_context(fnamevb.buf, fnametmpvb.buf, stab.st_mode);
|
||||
} else {
|
||||
debug(dbg_eachfiledetail, "tarobject nondirectory, 'link' backup");
|
||||
+#ifdef __ANDROID__
|
||||
+ /* Android does not support hardlinks. */
|
||||
+ if (rename(fnamevb.buf,fnametmpvb.buf))
|
||||
+#else
|
||||
if (link(fnamevb.buf,fnametmpvb.buf))
|
||||
+#endif
|
||||
ohshite(_("unable to make backup link of '%.255s' before installing new version"),
|
||||
ti->name);
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
diff -u -r ../dpkg-1.18.15/src/configure.c ./src/configure.c
|
||||
--- ../dpkg-1.18.15/src/configure.c 2016-11-11 22:18:40.000000000 -0500
|
||||
+++ ./src/main/configure.c 2016-12-03 16:32:43.719508056 -0500
|
||||
@@ -497,8 +497,10 @@
|
||||
pkg_name(pkg, pnaw_nonambig), cdr2.buf,
|
||||
strerror(errno));
|
||||
if (!(what & CFOF_USER_DEL))
|
||||
- if (link(cdr.buf, cdr2.buf))
|
||||
- warning(_("%s: failed to link '%.250s' to '%.250s': %s"),
|
||||
+ /** Termux modification: Use rename(2) instead of link(2), to avoid hard
|
||||
+ links which does not work on Android 6.0 or later. */
|
||||
+ if (rename(cdr.buf, cdr2.buf))
|
||||
+ warning(_("%s: failed to rename '%.250s' to '%.250s': %s"),
|
||||
pkg_name(pkg, pnaw_nonambig), cdr.buf,
|
||||
cdr2.buf, strerror(errno));
|
||||
/* Fall through. */
|
||||
14
android/termux-dpkg/patches/termux/termux-src-help.c.patch
Normal file
14
android/termux-dpkg/patches/termux/termux-src-help.c.patch
Normal file
@@ -0,0 +1,14 @@
|
||||
diff -u -r ../dpkg-1.19.2/src/help.c ./src/help.c
|
||||
--- ../dpkg-1.19.2/src/help.c 2018-10-07 22:07:52.000000000 +0000
|
||||
+++ ./src/main/help.c 2018-11-11 02:13:05.133548711 +0000
|
||||
@@ -127,8 +127,10 @@
|
||||
"update_dyld_shared_cache",
|
||||
#elif defined(__GLIBC__) || defined(__UCLIBC__) || \
|
||||
defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
|
||||
+#ifndef __ANDROID__
|
||||
"ldconfig",
|
||||
#endif
|
||||
+#endif
|
||||
#if BUILD_START_STOP_DAEMON
|
||||
"start-stop-daemon",
|
||||
#endif
|
||||
@@ -0,0 +1,15 @@
|
||||
diff --color -uNr ../dpkg-1.21.8/src/statoverride/main.c ./src/statoverride/main.c
|
||||
--- ../dpkg-1.21.8/src/statoverride/main.c 2022-05-24 14:36:59.000000000 -0300
|
||||
+++ ./src/statoverride/main.c 2022-05-31 07:39:44.829552700 -0300
|
||||
@@ -188,9 +188,11 @@
|
||||
{
|
||||
int rc;
|
||||
|
||||
+#ifndef __ANDROID__
|
||||
rc = chown(filename, filestat->uid, filestat->gid);
|
||||
if (forcible_nonroot_error(rc) < 0)
|
||||
ohshite(_("error setting ownership of '%.255s'"), filename);
|
||||
+#endif
|
||||
rc = chmod(filename, filestat->mode & ~S_IFMT);
|
||||
if (forcible_nonroot_error(rc) < 0)
|
||||
ohshite(_("error setting permissions of '%.255s'"), filename);
|
||||
Reference in New Issue
Block a user