-
Notifications
You must be signed in to change notification settings - Fork 1
/
groot-preload.c
75 lines (62 loc) · 1.68 KB
/
groot-preload.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "utils.h"
#include "groot-ns.h"
/* For some reason the regular unsetenv doesn't seem to work well in an initializer... */
static void
__unsetenv (const char *name)
{
size_t len = strlen (name);
char **ep;
ep = __environ;
while (*ep != NULL)
{
if (strncmp (*ep, name, len) == 0 && (*ep)[len] == '=')
{
char **dp = ep;
do
dp[0] = dp[1];
while (*dp++);
}
else
++ep;
}
}
static void
_groot_init_main (int argc, char *argv[])
{
const char *debug = NULL;
const char *env_wrap = NULL;
const char *disabled = NULL;
char **wrapdirs = NULL;
int num_wrapdirs = 0;
disabled = getenv ("GROOT_DISABLED");
env_wrap = getenv ("GROOT_WRAPFS");
debug = getenv ("GROOT_DEBUG");
/* Don't recursively enable groot */
__unsetenv ("LD_PRELOAD");
if (disabled != NULL)
return;
/* If something re-enables LD_PRELOAD inside, disable it anyway */
if (disabled == NULL)
setenv ("GROOT_DISABLED", "1", 1);
if (env_wrap)
{
autofree char *data = xstrdup (env_wrap);
char *iterator = data;
while (iterator)
{
char *path = strsep (&iterator, ":");
num_wrapdirs++;
wrapdirs = xrealloc (wrapdirs, num_wrapdirs * sizeof (char *));
wrapdirs[num_wrapdirs-1] = xstrdup (path);
}
}
if (debug)
enable_debuglog ();
__debug__(("Enabling grootfs for %s - wrap %s", argv[0], env_wrap));
groot_setup_ns ((const char **)wrapdirs, num_wrapdirs);
strfreev (wrapdirs);
}
__attribute__((section(".init_array"))) void *_groot_init_main_constructor = &_groot_init_main;