--- 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 +#include + +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +/* 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/ 和 /data/data/ 是同一个目录的两种 + * 写法,包里两种都出现过,所以两个都要认。 */ + 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); +}