-
Notifications
You must be signed in to change notification settings - Fork 0
/
ctool.c
311 lines (260 loc) · 6.55 KB
/
ctool.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#include "types.h"
#include "stat.h"
#include "user.h"
#include "param.h"
#include "fcntl.h"
#include "continfo.h"
/*
Tests:
ctool create ctest1 -p 4 sh ps echoloop forkbomb
ctool start ctest1 vc0 sh
ctool create ctest2 -m 40000000 sh df free membomb
ctool start ctest2 vc1 sh
ctool create ctest3 -d 100000 sh ps free diskbomb
ctool start ctest3 vc2 sh
ctool create ctest4 sh ps echoloop
ctool start ctest4 vc3 echoloop a b
*/
void
usage(char* usage)
{
printf(1, "usage: ctool %s\n", usage);
exit();
}
// Modified implementation of
// https://stackoverflow.com/questions/33792754/in-c-on-linux-how-would-you-implement-cp
int
cp(char* dst, char* file)
{
char buffer[1024];
int files[2];
int count;
int pathsize = strlen(dst) + strlen(file) + 2; // dst.len + '/' + src.len + \0
char path[pathsize];
memmove(path, dst, strlen(dst));
memmove(path + strlen(dst), "/", 1);
memmove(path + strlen(dst) + 1, file, strlen(file));
memmove(path + strlen(dst) + 1 + strlen(file), "\0", 1);
files[0] = open(file, O_RDONLY);
if (files[0] == -1) // Check if file opened
return -1;
files[1] = open(path, O_WRONLY | O_CREATE);
if (files[1] == -1) { // Check if file opened (permissions problems ...)
printf(1, "failed to create file |%s|\n", path);
close(files[0]);
return -1;
}
while ((count = read(files[0], buffer, sizeof(buffer))) != 0)
write(files[1], buffer, count);
return 1;
}
int
max(int a, int b)
{
if (a > b)
return a;
else
return b;
}
void
create(int argc, char *argv[])
{
int i, k, last_flag = 2, // No flags
mproc = NPROC,
msz = MAX_CONT_MEM,
mdsk = MAX_CONT_DSK;
if (argc < 4)
usage("create <name> [-p <max_processes>] [-m <max_memory>] [-d <max_disk>] prog [prog2.. ]");
for (i = 0; i < argc; i++) {
if (strcmp(argv[i], "-p") == 0) {
last_flag = max(last_flag, i + 1);
mproc = atoi(argv[i + 1]);
}
if (strcmp(argv[i], "-m") == 0) {
last_flag = max(last_flag, i + 1);
msz = atoi(argv[i + 1]);
}
if (strcmp(argv[i], "-d") == 0) {
last_flag = max(last_flag, i + 1);
mdsk = atoi(argv[i + 1]);
}
}
if (mkdir(argv[2]) != 0) {
printf(1, "Error creating directory %s\n", argv[2]);
exit();
}
for (i = last_flag + 1, k = 0; i < argc; i++, k++) {
if (cp(argv[2], argv[i]) != 1)
printf(1, "Failed to copy %s into folder %s. Continuing...\n", argv[i], argv[2]);
}
if (ccreate(argv[2], mproc, msz, mdsk) == 1) {
printf(1, "Created container %s\n", argv[2]);
} else {
printf(1, "Failed to create container %s\n", argv[2]);
exit();
}
}
// ctool start <name> <vc> prog arg1 [arg2 ...]
void
start(int argc, char *argv[])
{
char *args[MAXARG];
int i, k, cid, pid, vc;
if (argc < 5)
usage("ctool start <name> <vc> prog arg1 [arg2 ...]");
if ((vc = open(argv[3], O_RDWR)) == -1) {
printf(1, "Couldn't open virtual console %s\n", argv[3]);
exit();
}
for (i = 4, k = 0; i < argc; i++, k++) {
args[k] = malloc(strlen(argv[i]) + 1);
memmove(args[k], argv[i], strlen(argv[i]));
memmove(args[k] + strlen(argv[i]), "\0", 1);
}
if ((cid = cstart(argv[2])) < 0) {
printf(1, "Failed to start container %s\n", argv[2]);
exit();
}
pid = cfork(cid);
if (pid == 0){
close(0);
close(1);
close(2);
dup(vc);
dup(vc);
dup(vc);
exec(args[0], args);
printf(1, "Failed to start process %s on container\n", args[0]);
cstop(argv[2]);
exit();
} else {
printf(1, "Starting container %s on vc %s\n", argv[2], argv[3]);
}
}
void
pause(int argc, char *argv[])
{
if (argc < 2)
usage("ctool pause <name>");
if (cpause(argv[2]) == 1)
printf(1, "Paused %s\n", argv[2]);
else
printf(1, "ctool: pause failed\n");
}
void
resume(int argc, char *argv[])
{
if (argc < 2)
usage("ctool resume <name>");
if (cresume(argv[2]) == 1)
printf(1, "Resumed %s\n", argv[2]);
else
printf(1, "ctool: resume failed\n");
}
void
stop(int argc, char *argv[])
{
if (argc < 2)
usage("ctool stop <name>");
if (cstop(argv[2]) == 1)
printf(1, "Stopped %s\n", argv[2]);
else
printf(1, "ctool: stop failed\n");
}
void
info()
{
struct continfo *ci;
struct cinfo c;
struct pinfo p;
int i, k, uproc, numstates, totalticks, contticks;
char* state;
static char *states[] = {
[PIUNUSED] "unused",
[PIEMBRYO] "embryo",
[PISLEEPING] "sleep ",
[PIRUNNABLE] "runble",
[PIRUNNING] "run ",
[PIZOMBIE] "zombie"
};
static char *cstates[] = {
[CIUNUSED] "unused",
[CIEMBRYO] "embryo",
[CIREADY] "ready ",
[CIRUNNABLE] "runnable",
[CIRUNNING] "running",
[CIPAUSED] "paused",
[CISTOPPING] "stopping"
};
numstates = 6;
ci = malloc(sizeof(*ci));
if (cinfo(ci) != 1) {
printf(1, "info: failed to get container info\n");
exit();
}
// Count total ticks
for (i = 0; i < NCONT; i++) {
c = ci->conts[i];
if (c.state == CIUNUSED)
continue;
for (k = 0; k < c.mproc; k++) {
p = c.procs[k];
if(p.state == PIUNUSED)
continue;
totalticks += p.ticks;
}
}
for (i = 0; i < NCONT; i++) {
c = ci->conts[i];
if (c.state == CIUNUSED)
continue;
// Count used processes and contticks
for (k = 0, uproc = 0, contticks = 0; k < c.mproc; k++) {
p = c.procs[k];
if(p.state == PIUNUSED)
continue;
uproc++;
contticks += p.ticks;
}
printf(1, "Container %d: %s (/%s) %s\n", c.cid, c.name, c.name, cstates[c.state]);
printf(1, "\tExecuted %d%% (%d/%d) of the time\n", contticks*100/totalticks, contticks, totalticks);
printf(1, "\t\tUsed\tAvailable\n");
printf(1, "\tProc: \t%d\t%d\n\tMem: \t%dkb\t%dmb\n\tDisk: \t%dkb\t%dmb\n",
uproc, c.mproc, c.usz/1024, c.msz/1024/1024, c.udsk/1024, c.mdsk/1024/1024);
printf(1, "\tProcesses:\n");
for (k = 0; k < c.mproc; k++) {
p = c.procs[k];
if(p.state == PIUNUSED)
continue;
if(p.state >= 0 && p.state < numstates && states[p.state])
state = states[p.state];
else
state = "???";
printf(1, "\t\t%d %s %s \t(%d%% CPU time)\n", p.pid, state, p.name, (100*p.ticks/totalticks));
}
printf(1, "\n");
}
}
int
main(int argc, char *argv[])
{
if (argc == 1) {
usage("<tool> <cmd> [<arg> ...]");
exit();
}
if (strcmp(argv[1], "create") == 0)
create(argc, argv);
else if (strcmp(argv[1], "start") == 0)
start(argc, argv);
else if (strcmp(argv[1], "info") == 0)
info();
else if (strcmp(argv[1], "resume") == 0)
resume(argc, argv);
else if (strcmp(argv[1], "stop") == 0)
stop(argc, argv);
else if (strcmp(argv[1], "pause") == 0)
pause(argc, argv);
else
printf(1, "ctool: command not found.\n");
exit();
}