aboutsummaryrefslogtreecommitdiffstats
path: root/src/dumm/main.c
blob: aca5967c26bc86be0b83f959ab469911c67a75eb (plain)
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
/*
 * Copyright (C) 2007 Martin Willi
 * Hochschule fuer Technik Rapperswil
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

#define _GNU_SOURCE

#include <stdio.h>
#include <unistd.h>
#include <getopt.h>
#include <library.h>
#include <readline/readline.h>
#include <readline/history.h>

#include "dumm.h"

/**
 * global set of UMLs guests
 */
dumm_t *dumm;

/**
 * show usage information (program arguments)
 */
static void usage()
{
	printf("Usage:\n");
	printf("  --dir|-d <path>            set working dir to <path>\n");
	printf("  --help|-h                  show this help\n");
}

/**
 * help for dumm root shell
 */
static void help()
{
	printf("create name=<name>            start a guest named <name>\n");
	printf("       [master=<dir>]         read only master root filesystem\n");
	printf("       [memory=<MB>]          guest main memory in megabyte\n");
	printf("list                          list running guests\n");
	printf("guest <name>                  open guest menu for <name>\n");
	printf("help                          show this help\n");
	printf("quit                          kill quests and exit\n");
}

/**
 * help for guest shell
 */
static void help_guest()
{
	printf("start [kernel=<uml-kernel>]   start a stopped guest\n");
	printf("stop                          stop a started guest\n");
	printf("addif <name>                  add an interface to the guest\n");
	printf("delif <name>                  remove the interface\n");
	printf("listif                        list guests interfaces\n");
	printf("help                          show this help\n");
	printf("quit                          quit the guest menu\n");
}

/**
 * add an iface to a guest
 */
static void add_if(guest_t *guest, char *name)
{
	iface_t *iface;
	
	iface = guest->create_iface(guest, name);
	if (iface)
	{
		printf("created guest interface '%s' connected to '%s'\n",
			   iface->get_guestif(iface), iface->get_hostif(iface));
	}
	else
	{
		printf("failed to create guest interface\n");
	}
}

/**
 * delete an iface from a guest
 */
static void del_if(guest_t *guest, char *name)
{
	iface_t *iface;
	iterator_t *iterator;
	bool found = FALSE;
	
	iterator = guest->create_iface_iterator(guest);
	while (iterator->iterate(iterator, (void**)&iface))
	{
		if (streq(name, iface->get_guestif(iface)))
		{
			iterator->remove(iterator);
			printf("removing interface '%s' ('%s') from %s\n",
				   iface->get_guestif(iface), iface->get_hostif(iface),
				   guest->get_name(guest));
			iface->destroy(iface);
			found = TRUE;
			break;
		}
	}
	iterator->destroy(iterator);
	if (!found)
	{
		printf("guest '%s' has no interface named '%s'\n",
			   guest->get_name(guest), name);
	}
}

/**
 * list interfaces on a guest
 */
static void list_if(guest_t *guest)
{
	iface_t *iface;
	iterator_t *iterator;
	
	iterator = guest->create_iface_iterator(guest);
	while (iterator->iterate(iterator, (void**)&iface))
	{
		printf("'%s' => '%s'\n", iface->get_guestif(iface), iface->get_hostif(iface));

	}
	iterator->destroy(iterator);
}

/**
 * start an UML guest
 */
static void start_guest(guest_t *guest, char *line)
{
	enum {
		KERNEL = 0,
	};
	char *const opts[] = {
		[KERNEL] = "kernel",
		NULL
	};
	char *value;
	char *kernel = NULL;
	
	while (TRUE)
	{
		switch (getsubopt(&line, opts, &value))
		{
			case KERNEL:
				kernel = value;
				continue;
			default:
				break;
		}
		break;
	}
	if (kernel == NULL)
	{
		kernel = "./linux";
	}
	
	printf("starting guest '%s'... \n", guest->get_name(guest));
	if (guest->start(guest, kernel))
	{
		printf("guest '%s' is up\n", guest->get_name(guest));
	}
	else
	{
		printf("failed to start guest '%s'!\n", guest->get_name(guest));
	}
}

/**
 * stop (kill) an UML guest
 */
static void stop_guest(guest_t *guest, char *line)
{	
	printf("stopping guest '%s'...\n", guest->get_name(guest));
	guest->stop(guest);
	printf("guest '%s' is down\n", guest->get_name(guest));
}

/**
 * subshell for guests
 */
static void guest(char *name)
{
	char *line = NULL;
	char prompt[32];
	int len;
	iterator_t *iterator;
	guest_t *guest;
	bool found = FALSE;
	
	iterator = dumm->create_guest_iterator(dumm);
	while (iterator->iterate(iterator, (void**)&guest))
	{
		if (streq(name, guest->get_name(guest)))
		{
			found = TRUE;
			break;
		}
	}
	iterator->destroy(iterator);
	if (!found)
	{
		printf("guest '%s' not found\n", name);
		return;
	}
	
	len = snprintf(prompt, sizeof(prompt), "dumm@%s# ", name);
	if (len < 0 || len >= sizeof(prompt))
	{
		return;
	}

	while (TRUE)
	{
		enum {
			QUIT = 0,
			HELP,
			START,
			STOP,
			ADDIF,
			DELIF,
			LISTIF,
		};
		char *const opts[] = {
			[QUIT] = "quit",
			[HELP] = "help",
			[START] = "start",
			[STOP] = "stop",
			[ADDIF] = "addif",
			[DELIF] = "delif",
			[LISTIF] = "listif",
			NULL
		};
		char *pos, *value;
		
		free(line);
		line = readline(prompt);
		if (line == NULL || *line == '\0')
		{
			continue;
		}
		add_history(line);
		pos = line;
		while (*pos != '\0')
		{
			if (*pos == ' ')
			{
				*pos = ',';
			}
			pos++;
		}
		pos = line;
		switch (getsubopt(&pos, opts, &value))
		{
			case QUIT:
				free(line);
				break;
			case HELP:
				help_guest();
				continue;
			case START:
				start_guest(guest, pos);
				continue;
			case STOP:
				stop_guest(guest, pos);
				continue;
			case ADDIF:
				add_if(guest, pos);
				continue;
			case DELIF:
				del_if(guest, pos);
				continue;
			case LISTIF:
				list_if(guest);
				continue;
			default:
				printf("command unknown: '%s'\n", line);
				continue;
		}
		break;
	}
}


/**
 * create an bridge
 */
static void create_bridge(char *name)
{
	dumm->create_bridge(dumm, name);

}

/**
 * create an UML guest
 */
static void create_guest(char *line)
{
	enum {
		NAME = 0,
		KERNEL,
		MASTER,
		MEMORY,
	};
	char *const opts[] = {
		[NAME] = "name",
		[KERNEL] = "kernel",
		[MASTER] = "master",
		[MEMORY] = "memory",
		NULL
	};
	char *value;
	char *name = NULL;
	char *master = NULL;
	char *kernel = NULL;
	int mem = 128;
	
	while (TRUE)
	{
		switch (getsubopt(&line, opts, &value))
		{
			case NAME:
				name = value;
				continue;
			case KERNEL:
				kernel = value;
				continue;
			case MASTER:
				master = value;
				continue;
			case MEMORY:
				if (value)
				{
					mem = atoi(value);
				}
				continue;
			default:
				break;
		}
		break;
	}
	if (name == NULL || master == NULL || kernel == NULL)
	{
		printf("too few arguments!\n");
		help();
		return;
	}
	if (mem == 0)
	{
		mem = 128;
	}
	if (dumm->create_guest(dumm, name, kernel, master, mem))
	{
		printf("guest '%s' created\n", name);
		guest(name);
	}
	else
	{
		printf("failed to create guest '%s'!\n", name);
	}
}

/**
 * list running UML guests
 */
static void list()
{
	iterator_t *guests, *ifaces;
	guest_t *guest;
	iface_t *iface;
	
	guests = dumm->create_guest_iterator(dumm);
	while (guests->iterate(guests, (void**)&guest))
	{
		printf("%s (%N)\n", guest->get_name(guest),
			   guest_state_names, guest->get_state(guest));
		ifaces = guest->create_iface_iterator(guest);
		while (ifaces->iterate(ifaces, (void**)&iface))
		{
			printf("  '%s' => '%s'\n",
				   iface->get_guestif(iface), iface->get_hostif(iface));
		}
		ifaces->destroy(ifaces);
	}
	guests->destroy(guests);
}

/**
 * Signal handler 
 */
void signal_action(int sig, siginfo_t *info, void *ucontext)
{
	printf("\nuse 'quit'\ndumm# ");
}

/**
 * main routine, parses args and reads from console
 */
int main(int argc, char *argv[])
{
	char *line = NULL;
	struct sigaction action;
	char *dir = ".";

	while (TRUE)
	{
		struct option options[] = {
			{"dir", 1, 0, 0},
			{"help", 0, 0, 0},
			{0, 0, 0, 0}
		};
		
		switch (getopt_long(argc, argv, "d:h", options, NULL)) 
		{
			case -1:
				break;
			case 'd':
				dir = optarg;
				continue;
			case 'h':
				usage();
				return 0;
			default:
				usage();
				return 1;
		}
		break;
	}
	
	dumm = dumm_create(dir);
	
	memset(&action, 0, sizeof(action));
	action.sa_sigaction = signal_action;
	action.sa_flags = SA_SIGINFO;
	if (sigaction(SIGINT, &action, NULL) != 0 ||
		sigaction(SIGQUIT, &action, NULL) != 0 ||
		sigaction(SIGTERM, &action, NULL) != 0)
	{
		printf("signal handler setup failed: %m.\n");
		return 1;
	}

	while (TRUE)
	{
		enum {
			QUIT = 0,
			HELP,
			CREATE,
			BRIDGE,
			LIST,
			GUEST,
		};
		char *const opts[] = {
			[QUIT] = "quit",
			[HELP] = "help",
			[CREATE] = "create",
			[BRIDGE] = "bridge",
			[LIST] = "list",
			[GUEST] = "guest",
			NULL
		};
		char *pos, *value;
		
		free(line);
		line = readline("dumm# ");
		if (line == NULL || *line == '\0')
		{
			continue;
		}
		
		add_history(line);
		pos = line;
		while (*pos != '\0')
		{
			if (*pos == ' ')
			{
				*pos = ',';
			}
			pos++;
		}
		pos = line;
		switch (getsubopt(&pos, opts, &value))
		{
			case QUIT:
				free(line);
				break;
			case HELP:
				help();
				continue;
			case CREATE:
				create_guest(pos);
				continue;
			case BRIDGE:
				create_bridge(pos);
				continue;
			case LIST:
				list();
				continue;
			case GUEST:
				guest(pos);
				continue;
			default:
				printf("command unknown: '%s'\n", line);
				continue;
		}
		break;
	}
	dumm->destroy(dumm);
	clear_history();
	return 0;
}