Skip to content

Commit 0fcc022

Browse files
committed
env_reexec: clean up portable prism variables
The portable version of PrismLauncher modifies various colon-delimited environment variables to point to paths within the portable Prism install. This needs to be cleaned up when re-executing waywall with Prism's environment.
1 parent 10d7943 commit 0fcc022

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

waywall/env_reexec.c

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,34 @@ static struct list_envvar passthrough_env;
3232
static char **passthrough_envlist;
3333
static bool used_passthrough = false;
3434

35+
static bool
36+
colon_var_contains(char *orig_env, char *needle) {
37+
char *env = strdup(orig_env);
38+
check_alloc(env);
39+
40+
char *env_orig = env;
41+
42+
for (;;) {
43+
char *next = strchr(env, ':');
44+
if (next) {
45+
*next = '\0';
46+
}
47+
48+
if (strcmp(needle, env) == 0) {
49+
free(env_orig);
50+
return true;
51+
}
52+
53+
if (!next) {
54+
break;
55+
}
56+
env = next + 1;
57+
}
58+
59+
free(env_orig);
60+
return false;
61+
}
62+
3563
static void
3664
envlist_destroy(char **envlist) {
3765
for (char **var = envlist; *var; var++) {
@@ -78,6 +106,12 @@ penv_find(struct list_envvar *penv, const char *name) {
78106
return -1;
79107
}
80108

109+
static char *
110+
penv_get(struct list_envvar *penv, const char *name) {
111+
ssize_t idx = penv_find(penv, name);
112+
return (idx >= 0) ? penv->data[idx].value : NULL;
113+
}
114+
81115
static void
82116
penv_set(struct list_envvar *penv, const char *name, const char *value) {
83117
char *dup_value = strdup(value);
@@ -309,6 +343,48 @@ env_reexec(char **argv) {
309343
snprintf(fd_env, STATIC_ARRLEN(fd_env), "%d", passthrough_fd);
310344
penv_set(&penv, PASSTHROUGH_FD_ENV, fd_env);
311345

346+
// HACK: Portable PrismLauncher modifies several environment variables (each using the
347+
// colon-delimited format) which must be cleaned up.
348+
static const char *CLEAN_PORTABLE_VARS[] = {"LD_LIBRARY_PATH", "LD_PRELOAD", "QT_PLUGIN_PATH",
349+
"QT_FONTPATH"};
350+
static const char *PORTABLE_VARS[] = {"LAUNCHER_LD_LIBRARY_PATH", "LAUNCHER_LD_PRELOAD",
351+
"LAUNCHER_QT_PLUGIN_PATH", "LAUNCHER_QT_FONTPATH"};
352+
static_assert(STATIC_ARRLEN(CLEAN_PORTABLE_VARS) == STATIC_ARRLEN(PORTABLE_VARS));
353+
for (size_t i = 0; i < STATIC_ARRLEN(PORTABLE_VARS); i++) {
354+
char *portable_var = penv_get(&penv, PORTABLE_VARS[i]);
355+
char *var = penv_get(&penv, CLEAN_PORTABLE_VARS[i]);
356+
if (!portable_var || !var) {
357+
continue;
358+
}
359+
360+
char *var_orig = var = strdup(var);
361+
check_alloc(var_orig);
362+
363+
str cleaned = str_new();
364+
for (;;) {
365+
char *next = strchr(var, ':');
366+
if (next) {
367+
*next = '\0';
368+
}
369+
370+
if (!colon_var_contains(portable_var, var)) {
371+
str_append(&cleaned, var);
372+
str_append(&cleaned, ":");
373+
}
374+
375+
if (!next) {
376+
break;
377+
}
378+
var = next + 1;
379+
}
380+
penv_set(&penv, CLEAN_PORTABLE_VARS[i], cleaned);
381+
382+
ww_log(LOG_INFO, "cleaned portable variable %s=%s", CLEAN_PORTABLE_VARS[i], cleaned);
383+
384+
str_free(cleaned);
385+
free(var_orig);
386+
}
387+
312388
char **envlist = penv_to_envlist(&penv);
313389

314390
ww_log(LOG_INFO, "set passthrough environment fd to %d, restarting", passthrough_fd);

0 commit comments

Comments
 (0)