mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-19 04:46:08 +00:00
287 lines
11 KiB
C
287 lines
11 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).
|
|
//
|
|
// The environment of started programs: Android programs get it without the hook, rootfs
|
|
// programs with it.
|
|
|
|
#ifndef DDNATV_HOST_TEST
|
|
|
|
static char **clean_env_for_system(char *const envp[]) {
|
|
int count = 0;
|
|
while (envp && envp[count]) count++;
|
|
char **new_env = calloc(count + 3, sizeof(char *));
|
|
int dst = 0;
|
|
int has_path = 0;
|
|
for (int i = 0; i < count; i++) {
|
|
char first = envp[i][0];
|
|
if (!((first == 'L' && (strncmp(envp[i], "LD_PRELOAD=", 11) == 0 ||
|
|
strncmp(envp[i], "LD_LIBRARY_PATH=", 16) == 0)) ||
|
|
(first == 'G' && strncmp(envp[i], "GLIBC_TUNABLES=", 15) == 0))) {
|
|
if (first == 'P' && strncmp(envp[i], "PATH=", 5) == 0) {
|
|
has_path = 1;
|
|
size_t plen = strlen(envp[i]);
|
|
char *new_path = malloc(plen + 64);
|
|
if (new_path) {
|
|
snprintf(new_path, plen + 64, "PATH=/system/bin:/system/xbin:/sbin:/vendor/bin:%s", envp[i] + 5);
|
|
new_env[dst++] = new_path;
|
|
} else {
|
|
new_env[dst++] = envp[i];
|
|
}
|
|
} else {
|
|
new_env[dst++] = envp[i];
|
|
}
|
|
}
|
|
}
|
|
if (!has_path) {
|
|
new_env[dst++] = strdup("PATH=/system/bin:/system/xbin:/sbin:/vendor/bin");
|
|
}
|
|
new_env[dst] = NULL;
|
|
return new_env;
|
|
}
|
|
|
|
// LD_PRELOAD with every absolute path that is not in the rootfs yet mapped into it. Entries are
|
|
// separated by ':' or ' '. free() the result.
|
|
static char *map_preload_paths(const char *value) {
|
|
if (!value) return NULL;
|
|
size_t cap = strlen(value) + (g_ddnatv_root_len + 1) * 16 + 16;
|
|
char *out = malloc(cap);
|
|
if (!out) return NULL;
|
|
size_t used = 0;
|
|
out[0] = ' |