内置 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:
doubao
2026-10-14 12:48:47 +08:00
parent 982c64939b
commit 028d67c3ec
33 changed files with 3241 additions and 8 deletions

View File

@@ -0,0 +1,48 @@
--- a/m4/dpkg-arch.m4 2026-10-14 12:41:34.709430018 +0800
+++ b/m4/dpkg-arch.m4 2026-10-14 12:41:34.709459828 +0800
@@ -55,12 +55,40 @@
AC_DEFUN([DPKG_ARCHITECTURE], [
DPKG_CPU_TYPE
DPKG_OS_TYPE
- AC_MSG_CHECKING([dpkg architecture name])
- _DPKG_ARCHITECTURE([DEB_HOST_ARCH], [dpkg_arch])
- AS_IF([test "x$dpkg_arch" = "x"], [
- AC_MSG_ERROR([cannot determine host dpkg architecture])
- ], [
+ dnl Termux/Android: data/cputable, data/ostable and data/tupletable only
+ dnl describe the Debian and GNU worlds -- ostable has no "android" entry at
+ dnl all -- so the generic lookup below can never resolve a *-linux-android
+ dnl host triple and configure dies with "cannot determine host dpkg
+ dnl architecture".
+ dnl
+ dnl Termux names its architectures after the GNU CPU name (the repository
+ dnl serves x86_64, aarch64, arm and i686), and the package database shipped
+ dnl in our bootstrap was written with exactly those names, so we have to
+ dnl report the same thing or dpkg would reject every installed package.
+ dnl
+ dnl Note the host OS arrives as "linux-android", not "android": config.sub
+ dnl normalises --host=x86_64-linux-android into x86_64-pc-linux-android.
+ AS_CASE([$host_os],
+ [*android*], [
+ AS_CASE([$host_cpu],
+ [x86_64|aarch64|i686|arm], [dpkg_arch=$host_cpu],
+ [armv7*], [dpkg_arch=arm],
+ [AC_MSG_ERROR([unsupported Android CPU: $host_cpu])])
+ cpu_type=$host_cpu
+ os_type=android
+ have_dpkg_arch=yes
+ ])
+ AS_IF([test "x$have_dpkg_arch" = "xyes"], [
+ AC_MSG_CHECKING([dpkg architecture name])
AC_MSG_RESULT([$dpkg_arch])
+ ], [
+ AC_MSG_CHECKING([dpkg architecture name])
+ _DPKG_ARCHITECTURE([DEB_HOST_ARCH], [dpkg_arch])
+ AS_IF([test "x$dpkg_arch" = "x"], [
+ AC_MSG_ERROR([cannot determine host dpkg architecture])
+ ], [
+ AC_MSG_RESULT([$dpkg_arch])
+ ])
])
AC_DEFINE_UNQUOTED([ARCHITECTURE], ["${dpkg_arch}"],
[Set this to the canonical dpkg architecture name.])

View File

@@ -0,0 +1,26 @@
--- a/lib/dpkg/arch.c 2026-10-14 12:41:34.729574429 +0800
+++ b/lib/dpkg/arch.c 2026-10-14 12:41:34.729607515 +0800
@@ -62,17 +62,21 @@
internerr("arch name argument is NULL");
if (!*p)
return _("may not be empty string");
+ /* Termux/Android: Termux names its architectures after the GNU CPU
+ * name, and two of the four it serves -- x86_64 and aarch64 -- carry an
+ * underscore. Without this every package in the bootstrap database
+ * would be reported as having an invalid Architecture. */
if (!c_isalnum(*p))
return _("must start with an alphanumeric");
while (*++p != '\0')
- if (!c_isalnum(*p) && *p != '-')
+ if (!c_isalnum(*p) && *p != '-' && *p != '_')
break;
if (*p == '\0')
return NULL;
snprintf(buf, sizeof(buf), _("character '%c' not allowed (only "
"letters, digits and characters '%s')"),
- *p, "-");
+ *p, "-_");
return buf;
}

View File

@@ -0,0 +1,14 @@
--- a/scripts/Dpkg/Arch.pm 2026-10-14 12:41:34.752824615 +0800
+++ b/scripts/Dpkg/Arch.pm 2026-10-14 12:41:34.752929467 +0800
@@ -612,7 +612,10 @@
sub debarch_is_illegal
{
my ($arch, %opts) = @_;
- my $arch_re = qr/[a-zA-Z0-9][a-zA-Z0-9-]*/;
+ # Termux/Android: allow underscores, because Termux names
+ # architectures after the GNU CPU name and x86_64 / aarch64
+ # both contain one.
+ my $arch_re = qr/[a-zA-Z0-9][a-zA-Z0-9_-]*/;
if ($opts{positive}) {
return $arch !~ m/^$arch_re$/;

View File

@@ -0,0 +1,352 @@
--- 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);
+}

View 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 */

View File

@@ -0,0 +1,22 @@
--- a/src/main/main.c 2026-10-14 12:41:34.673058919 +0800
+++ b/src/main/main.c 2026-10-14 12:41:34.673178642 +0800
@@ -53,6 +53,8 @@
#include <dpkg/db-fsys.h>
#include "main.h"
+
+#include <dpkg/doubao-prefix.h>
#include "filters.h"
static int
@@ -736,6 +738,10 @@
dpkg_program_init("dpkg");
set_force_default(FORCE_ALL);
dpkg_options_load(DPKG, cmdinfos);
+
+ /* Another Android application may have handed us a PATH that points at
+ * its own prefix; repair ours before anything looks a program up. */
+ doubao_fix_environment();
dpkg_options_parse(&argv, cmdinfos, printforhelp);
debug(dbg_general, "root=%s admindir=%s", dpkg_fsys_get_dir(), dpkg_db_get_dir());

View File

@@ -0,0 +1,26 @@
--- a/lib/dpkg/Makefile.am 2026-10-14 12:41:34.636534337 +0800
+++ b/lib/dpkg/Makefile.am 2026-10-14 12:41:34.636659433 +0800
@@ -5,6 +5,7 @@
-DCONFIGDIR=\"$(pkgconfdir)\" \
-DADMINDIR=\"$(admindir)\" \
-DDEFAULT_TEXT_DOMAIN=\"@PACKAGE@\" \
+ -DDOUBAO_DEFAULT_PREFIX=\"$(prefix)\" \
-idirafter $(top_srcdir)/lib/compat \
-I$(top_builddir) \
-I$(top_srcdir)/lib \
@@ -132,6 +133,7 @@
triglib.c \
trigdeferred.c \
utils.c \
+ doubao-prefix.c \
varbuf.c \
version.c \
# EOL
@@ -151,6 +153,7 @@
debug.h \
dir.h \
dpkg.h \
+ doubao-prefix.h \
dpkg-db.h \
ehandle.h \
error.h \

View File

@@ -0,0 +1,28 @@
--- a/lib/dpkg/options.c 2026-10-14 12:41:34.656076933 +0800
+++ b/lib/dpkg/options.c 2026-10-14 12:41:34.656194326 +0800
@@ -35,6 +35,7 @@
#include <dpkg/dpkg.h>
#include <dpkg/string.h>
#include <dpkg/options.h>
+#include <dpkg/doubao-prefix.h>
static const char *printforhelp;
@@ -166,7 +167,7 @@
struct dirent **dlist;
int dlist_n, i;
- dirname = str_fmt("%s/%s.cfg.d", CONFIGDIR, prog);
+ dirname = str_fmt("%s/%s.cfg.d", doubao_config_dir(), prog);
dlist_n = scandir(dirname, &dlist, valid_config_filename, alphasort);
if (dlist_n < 0) {
@@ -198,7 +199,7 @@
dpkg_options_load_dir(prog, cmdinfos);
- file = str_fmt("%s/%s.cfg", CONFIGDIR, prog);
+ file = str_fmt("%s/%s.cfg", doubao_config_dir(), prog);
dpkg_options_load_file(file, cmdinfos);
free(file);

View File

@@ -0,0 +1,59 @@
--- a/src/main/script.c 2026-10-14 12:41:34.690665764 +0800
+++ b/src/main/script.c 2026-10-14 12:41:34.690708349 +0800
@@ -47,6 +47,8 @@
#include "main.h"
+#include <dpkg/doubao-prefix.h>
+
void
post_postinst_tasks(struct pkginfo *pkg, enum pkgstatus new_status)
{
@@ -230,6 +232,11 @@
command_add_arg(&cmd, scriptname);
command_add_argv(&cmd, args);
+ /* Termux packages ship maintainer scripts whose interpreter line names
+ * the Termux prefix; on Android that path belongs to another
+ * application and the kernel refuses to exec it. Point it at us. */
+ doubao_fix_script_shebang(scriptpath);
+
if (stat(scriptpath, &stab)) {
command_destroy(&cmd);
@@ -306,6 +313,11 @@
command_add_argv(&cmd, args);
va_end(args);
+ /* Termux packages ship maintainer scripts whose interpreter line names
+ * the Termux prefix; on Android that path belongs to another
+ * application and the kernel refuses to exec it. Point it at us. */
+ doubao_fix_script_shebang(cidir);
+
if (stat(cidir, &stab)) {
command_destroy(&cmd);
@@ -347,6 +359,11 @@
versiondescribe(&pkg->available.version, vdew_nonambig),
NULL);
+ /* Termux packages ship maintainer scripts whose interpreter line names
+ * the Termux prefix; on Android that path belongs to another
+ * application and the kernel refuses to exec it. Point it at us. */
+ doubao_fix_script_shebang(oldscriptpath);
+
if (stat(oldscriptpath, &stab)) {
if (errno == ENOENT) {
debug(dbg_scripts,
@@ -377,6 +394,11 @@
versiondescribe(&pkg->available.version, vdew_nonambig),
NULL);
+ /* Termux packages ship maintainer scripts whose interpreter line names
+ * the Termux prefix; on Android that path belongs to another
+ * application and the kernel refuses to exec it. Point it at us. */
+ doubao_fix_script_shebang(cidir);
+
if (stat(cidir, &stab)) {
command_destroy(&cmd);

View File

@@ -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)) {

View File

@@ -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);
}

View File

@@ -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

View File

@@ -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);
}

View File

@@ -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. */

View 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

View File

@@ -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);