mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-19 04:46:08 +00:00
307 lines
12 KiB
C
307 lines
12 KiB
C
// Part of libddnatv-hook.so, included by ddnatv-hook.c (one translation unit, so the
|
|
// parts share their static functions and are built with the same command as before).
|
|
//
|
|
// Start-up (DDNATV_ROOT, certificate and time zone defaults), /proc/self/exe, rewrite_path() and
|
|
// the short names of Unix sockets.
|
|
|
|
#ifndef DDNATV_HOST_TEST
|
|
|
|
static char g_real_exe[PATH_MAX] = {0};
|
|
// DDNATV_PRELOAD: guest paths of libraries every program loads before the hook (what
|
|
// /etc/ld.so.preload does for programs run by the rootfs's own ld.so), and the same as host paths.
|
|
static char *g_preload_guest = NULL;
|
|
static char *g_preload_host = NULL;
|
|
static int g_initialized = 0;
|
|
|
|
static void init_ddnatv_hook(void) {
|
|
if (g_initialized) return;
|
|
const char *root = getenv("DDNATV_ROOT");
|
|
if (root && strlen(root) > 0) {
|
|
size_t len = strlen(root);
|
|
if (len > sizeof(g_ddnatv_root) - 1) len = sizeof(g_ddnatv_root) - 1;
|
|
if (root[len - 1] == '/') len--;
|
|
ddnatv_set_root(root, len);
|
|
} else {
|
|
Dl_info info;
|
|
if (dladdr((void *)init_ddnatv_hook, &info) && info.dli_fname) {
|
|
const char *p = strstr(info.dli_fname, "/usr/lib/libddnatv-hook");
|
|
if (p && p > info.dli_fname) {
|
|
size_t len = p - info.dli_fname;
|
|
if (len < sizeof(g_ddnatv_root)) {
|
|
ddnatv_set_root(info.dli_fname, len);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
ddnatv_load_binds(getenv("DDNATV_BINDS"));
|
|
const char *preload = getenv("DDNATV_PRELOAD");
|
|
if (preload && preload[0] && g_ddnatv_root_len > 0) {
|
|
size_t cap = strlen(preload) + (g_ddnatv_root_len + 1) * 16 + 1;
|
|
char *host = malloc(cap);
|
|
if (host) {
|
|
size_t used = 0;
|
|
host[0] = ' |