-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshm-launch.c
124 lines (93 loc) · 2.01 KB
/
shm-launch.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <string.h>
#include <error.h>
#include <errno.h>
#include <sys/types.h>
#include <unistd.h>
#include <ashm.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <fcntl.h>
char* oldldpreload;
char* setpreload(char* cmd)
{
char* fincmd = malloc(strlen(cmd));
char* ldpreload = malloc(strlen(cmd));
sscanf(cmd, "LD_PRELOAD=%s %s", ldpreload, fincmd);
setenv("LD_PRELOAD", ldpreload, 1);
strcpy(fincmd, cmd);
fincmd = strchr(fincmd, ' ') + 1;
return fincmd;
}
int launch(char* cmd)
{
printf("launching: %s\n", cmd);
size_t cmdlen = strlen(cmd);
char* newcmd = malloc(sizeof(char) * cmdlen);
strcpy(newcmd, cmd);
char* endcmd;
if (strncmp(newcmd, "LD_PRELOAD", 10) == 0)
{
endcmd = setpreload(newcmd);
} else
{
endcmd = newcmd;
}
int j = 0;
for (int i = 0; i < cmdlen; i++)
{
if (endcmd[i] == ' ') j++;
}
j++;
char** cargv = (char**) malloc(sizeof(char*) * j + 1);
j = 0;
char* tok = strtok(endcmd, " ");
cargv[j] = tok;
j++;
while((tok = strtok(NULL, " ")) != NULL)
{
cargv[j] = tok;
j++;
}
cargv[j] = NULL;
pid_t pid = fork();
if (pid == 0)
{
if (execvp(cargv[0], cargv))
return -1;
} else
{
if (pid == -1) return -1;
return pid;
}
return 0;
}
int main(int argc, char** argv)
{
char* oldpreload;
if (shm_init() != 0)
error(errno, errno, "shm_init() error");
for (int i = 1; i < argc; i++)
{
char* tmp = getenv("LD_PRELOAD");
if (tmp == NULL)
oldldpreload = "";
else
{
oldpreload = malloc(strlen(tmp));
strcpy(oldpreload, tmp);
}
int pid;
char* safecmd = malloc(strlen(argv[i]));
safecmd = strcpy(safecmd, argv[i]);
if ((pid = launch(safecmd)) == -1)
error(errno, errno, "error launch");
printf("pid=%d\n", pid);
setenv("LD_PRELOAD", oldldpreload, 1);
sleep(3);
}
while(wait(NULL) != -1) sleep(2);
return 0;
}