|
| 1 | +/* |
| 2 | + * Statically-linked wrapper for QtWebEngineProcess. |
| 3 | + * |
| 4 | + * Qt WebEngine launches this as a subprocess. Because the parent sets |
| 5 | + * LD_LIBRARY_PATH to the AppImage's bundled libraries, a shell-script |
| 6 | + * wrapper would crash: the host's /bin/bash gets loaded by the host's |
| 7 | + * dynamic linker, which then pulls in the *bundled* glibc — an ABI |
| 8 | + * mismatch that segfaults on distros whose glibc differs from the build |
| 9 | + * host (e.g. Ubuntu vs Arch). |
| 10 | + * |
| 11 | + * A static binary sidesteps the problem entirely: it has no shared |
| 12 | + * library dependencies, so LD_LIBRARY_PATH is irrelevant. It simply |
| 13 | + * re-execs the real QtWebEngineProcess binary through the bundled |
| 14 | + * ld-linux with an explicit --library-path. |
| 15 | + */ |
| 16 | + |
| 17 | +#include <limits.h> |
| 18 | +#include <stdio.h> |
| 19 | +#include <stdlib.h> |
| 20 | +#include <string.h> |
| 21 | +#include <unistd.h> |
| 22 | + |
| 23 | +int main(int argc, char *argv[]) |
| 24 | +{ |
| 25 | + char self[PATH_MAX]; |
| 26 | + ssize_t len = readlink("/proc/self/exe", self, sizeof(self) - 1); |
| 27 | + if (len < 0) |
| 28 | + _exit(127); |
| 29 | + self[len] = '\0'; |
| 30 | + |
| 31 | + /* Derive APPDIR: this binary lives under $APPDIR/usr/lib/..., |
| 32 | + so the first /usr/lib/ in the path marks the boundary. */ |
| 33 | + char *p = strstr(self, "/usr/lib/"); |
| 34 | + if (!p) |
| 35 | + _exit(127); |
| 36 | + |
| 37 | + int appdir_len = (int)(p - self); |
| 38 | + |
| 39 | + char ldlinux[PATH_MAX]; |
| 40 | + snprintf(ldlinux, sizeof(ldlinux), |
| 41 | + "%.*s/usr/lib/ld-linux-x86-64.so.2", appdir_len, self); |
| 42 | + |
| 43 | + char real_bin[PATH_MAX + 8]; |
| 44 | + snprintf(real_bin, sizeof(real_bin), "%s.real", self); |
| 45 | + |
| 46 | + char lib_path[PATH_MAX * 2]; |
| 47 | + snprintf(lib_path, sizeof(lib_path), |
| 48 | + "%.*s/usr/lib" |
| 49 | + ":/usr/lib64:/usr/lib:/lib64:/lib" |
| 50 | + ":/usr/lib/x86_64-linux-gnu:/lib/x86_64-linux-gnu", |
| 51 | + appdir_len, self); |
| 52 | + |
| 53 | + /* argv: ld-linux --inhibit-cache --library-path <path> <real> [original args…] */ |
| 54 | + char **new_argv = calloc((size_t)(argc + 5), sizeof(char *)); |
| 55 | + if (!new_argv) |
| 56 | + _exit(127); |
| 57 | + |
| 58 | + new_argv[0] = ldlinux; |
| 59 | + new_argv[1] = "--inhibit-cache"; |
| 60 | + new_argv[2] = "--library-path"; |
| 61 | + new_argv[3] = lib_path; |
| 62 | + new_argv[4] = real_bin; |
| 63 | + for (int i = 1; i < argc; i++) |
| 64 | + new_argv[i + 4] = argv[i]; |
| 65 | + |
| 66 | + execv(ldlinux, new_argv); |
| 67 | + _exit(127); |
| 68 | +} |
0 commit comments