-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
43 lines (34 loc) · 815 Bytes
/
main.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
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <assert.h>
int main(int argc, char* argv[])
{
if (argc != 4) {
fprintf(stderr, "%s <preloader> <dso> <app> [args]\n", argv[0]);
exit(1);
}
assert(setenv("DLNS", argv[2], 0) == 0);
char* dso = argv[1];
char* ldpreload = realpath(dso, NULL);
assert(ldpreload);
assert(asprintf(&ldpreload, "LD_PRELOAD=%s", ldpreload) > 0);
char* const envs[] = {
"PATH=/bin:/usr/bin",
ldpreload,
NULL,
};
char** args = calloc(argc - 2, sizeof(char*));
args[0] = strdup(argv[3]); // app
int i = 4;
for (; i < argc; i++) {
args[i-3] = strdup(argv[i]);
}
args[i] = NULL;
execvpe(argv[3], args, envs);
perror("execvpe failed");
exit(1);
}