Skip to content

Commit d16f0c9

Browse files
authored
mptcpize: do not override existing LD_PRELOAD value (#315)
If any. Before, previously set LD_PRELOAD were overridden. According to 'man ld.so', there can be more than one library to load before other objects as this env var accepts a list separated by spaces or colons: A list of additional, user-specified, ELF shared objects to be loaded before all others. This feature can be used to selectively override functions in other shared objects. The items of the list can be separated by spaces or colons, and there is no support for escaping either separator. So let's do that: if a previous LD_PRELOAD is detected, it will no longer be dropped, but used with the MPTCP Wrap library added at the end, e.g. LD_PRELOAD=dummy.so:libmptcpwrap.so.0.0.1 Signed-off-by: Matthieu Baerts (NGI0) <[email protected]>
1 parent 07d4ec0 commit d16f0c9

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

src/mptcpize.c

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
#define SYSTEMD_SERVICE_TAG "[Service]"
3535
#define SYSTEMCTL_SHOW "systemctl show -p FragmentPath "
3636
#define PRELOAD_VAR "LD_PRELOAD="
37-
#define MPTCPWRAP_ENV "LD_PRELOAD="PKGLIBDIR"/libmptcpwrap.so.0.0."LIBREVISION
37+
#define MPTCPWRAP_ENV PKGLIBDIR"/libmptcpwrap.so.0.0."LIBREVISION
3838

3939
/* Program documentation. */
4040
static char args_doc[] = "CMD";
@@ -62,8 +62,9 @@ static void help(void)
6262

6363
static int run(int argc, char *av[])
6464
{
65-
int i, nr = 0, debug = 0;
66-
char **envp, **argv;
65+
int i, nr = 0, ld, debug = 0;
66+
char **envp, **argv, *env;
67+
size_t len;
6768

6869
if (argc > 0 && strcmp(av[0], "-d") == 0) {
6970
debug = 1;
@@ -87,17 +88,27 @@ static int run(int argc, char *av[])
8788
// ... filtering out any 'LD_PRELOAD' ...
8889
nr = 0;
8990
i = 0;
91+
ld = -1;
9092
while (environ[nr]) {
9193
if (strncmp(environ[nr], PRELOAD_VAR,
92-
strlen(PRELOAD_VAR)) != 0) {
94+
strlen(PRELOAD_VAR)) == 0) {
95+
ld = nr;
96+
} else {
9397
envp[i] = environ[nr];
9498
i++;
9599
}
96100
nr++;
97101
}
98102

99103
// ... appending the mptcpwrap preload...
100-
envp[i++] = MPTCPWRAP_ENV;
104+
if (ld >= 0) {
105+
len = strlen(environ[ld]) + strlen(MPTCPWRAP_ENV) + 2;
106+
env = alloca(len);
107+
snprintf(env, len, "%s:%s", environ[ld], MPTCPWRAP_ENV);
108+
} else {
109+
env = PRELOAD_VAR MPTCPWRAP_ENV;
110+
}
111+
envp[i++] = env;
101112

102113
// ... and enable dbg if needed
103114
if (debug)
@@ -204,7 +215,7 @@ static int unit_update(int argc, char *argv[], int enable)
204215

205216
if (append_env &&
206217
(is_env || strncmp(line, SYSTEMD_SERVICE_TAG, strlen(SYSTEMD_SERVICE_TAG)) == 0)) {
207-
if (dprintf(dst, "%s%s\n", SYSTEMD_ENV_VAR, MPTCPWRAP_ENV) < 0)
218+
if (dprintf(dst, "%s%s\n", SYSTEMD_ENV_VAR, PRELOAD_VAR MPTCPWRAP_ENV) < 0)
208219
error(1, errno, "can't write to env string into %s", dst_path);
209220
append_env = 0;
210221
}

0 commit comments

Comments
 (0)