+/*** Path rules ***/
+
+struct path_rule {
+ char *path;
+ enum action action;
+ struct path_rule *next;
+};
+
+static struct path_rule default_path_rules[] = {
+ { "/etc/", A_YES },
+ { "/lib/", A_YES },
+ { "/usr/lib/", A_YES },
+ { "/opt/lib/", A_YES },
+ { "/usr/share/zoneinfo/", A_YES },
+ { "/usr/share/locale/", A_YES },
+ { "/dev/null", A_YES },
+ { "/dev/zero", A_YES },
+ { "/proc/meminfo", A_YES },
+ { "/proc/self/stat", A_YES },
+ { "/proc/self/exe", A_YES }, // Needed by FPC 2.0.x runtime
+};
+
+static struct path_rule *user_path_rules;
+static struct path_rule **last_path_rule = &user_path_rules;
+
+static int
+set_path_action(char *a)
+{
+ char *sep = strchr(a, '=');
+ enum action act = A_YES;
+ if (sep)
+ {
+ *sep++ = 0;
+ if (!strcmp(sep, "yes"))
+ act = A_YES;
+ else if (!strcmp(sep, "no"))
+ act = A_NO;
+ else
+ return 0;
+ }
+
+ struct path_rule *r = xmalloc(sizeof(*r) + strlen(a) + 1);
+ r->path = (char *)(r+1);
+ strcpy(r->path, a);
+ r->action = act;
+ r->next = NULL;
+ *last_path_rule = r;
+ last_path_rule = &r->next;
+ return 1;
+}
+
+static enum action
+match_path_rule(struct path_rule *r, char *path)
+{
+ char *rr = r->path;
+ while (*rr)
+ if (*rr++ != *path++)
+ {
+ if (rr[-1] == '/' && !path[-1])
+ break;
+ return A_DEFAULT;
+ }
+ if (rr > r->path && rr[-1] != '/' && *path)
+ return A_DEFAULT;
+ return r->action;
+}
+
+/*** Environment rules ***/
+
+struct env_rule {
+ char *var; // Variable to match
+ char *val; // ""=clear, NULL=inherit
+ int var_len;
+ struct env_rule *next;
+};
+
+static struct env_rule *first_env_rule;
+static struct env_rule **last_env_rule = &first_env_rule;
+
+static struct env_rule default_env_rules[] = {
+ { "LIBC_FATAL_STDERR_", "1" }
+};
+
+static int
+set_env_action(char *a0)
+{
+ struct env_rule *r = xmalloc(sizeof(*r) + strlen(a0) + 1);
+ char *a = (char *)(r+1);
+ strcpy(a, a0);
+
+ char *sep = strchr(a, '=');
+ if (sep == a)
+ return 0;
+ r->var = a;
+ if (sep)
+ {
+ *sep++ = 0;
+ r->val = sep;
+ }
+ else
+ r->val = NULL;
+ *last_env_rule = r;
+ last_env_rule = &r->next;
+ r->next = NULL;
+ return 1;
+}
+
+static int
+match_env_var(char *env_entry, struct env_rule *r)
+{
+ if (strncmp(env_entry, r->var, r->var_len))
+ return 0;
+ return (env_entry[r->var_len] == '=');
+}
+
+static void
+apply_env_rule(char **env, int *env_sizep, struct env_rule *r)
+{
+ // First remove the variable if already set
+ int pos = 0;
+ while (pos < *env_sizep && !match_env_var(env[pos], r))
+ pos++;
+ if (pos < *env_sizep)
+ {
+ (*env_sizep)--;
+ env[pos] = env[*env_sizep];
+ env[*env_sizep] = NULL;
+ }
+
+ // What is the new value?
+ char *new;
+ if (r->val)
+ {
+ if (!r->val[0])
+ return;
+ new = xmalloc(r->var_len + 1 + strlen(r->val) + 1);
+ sprintf(new, "%s=%s", r->var, r->val);
+ }
+ else
+ {
+ pos = 0;
+ while (environ[pos] && !match_env_var(environ[pos], r))
+ pos++;
+ if (!(new = environ[pos]))
+ return;
+ }
+
+ // Add it at the end of the array
+ env[(*env_sizep)++] = new;
+ env[*env_sizep] = NULL;
+}
+
+static char **
+setup_environment(void)
+{
+ // Link built-in rules with user rules
+ for (int i=ARRAY_SIZE(default_env_rules)-1; i >= 0; i--)
+ {
+ default_env_rules[i].next = first_env_rule;
+ first_env_rule = &default_env_rules[i];
+ }
+
+ // Scan the original environment
+ char **orig_env = environ;
+ int orig_size = 0;
+ while (orig_env[orig_size])
+ orig_size++;
+
+ // For each rule, reserve one more slot and calculate length
+ int num_rules = 0;
+ for (struct env_rule *r = first_env_rule; r; r=r->next)
+ {
+ num_rules++;
+ r->var_len = strlen(r->var);
+ }
+
+ // Create a new environment
+ char **env = xmalloc((orig_size + num_rules + 1) * sizeof(char *));
+ int size;
+ if (pass_environ)
+ {
+ memcpy(env, environ, orig_size * sizeof(char *));
+ size = orig_size;
+ }
+ else
+ size = 0;
+ env[size] = NULL;
+
+ // Apply the rules one by one
+ for (struct env_rule *r = first_env_rule; r; r=r->next)
+ apply_env_rule(env, &size, r);
+
+ // Return the new env and pass some gossip
+ if (verbose > 1)
+ {
+ fprintf(stderr, "Passing environment:\n");
+ for (int i=0; env[i]; i++)
+ fprintf(stderr, "\t%s\n", env[i]);
+ }
+ return env;
+}
+
+/*** Syscall checks ***/
+