aboutsummaryrefslogtreecommitdiffstats
path: root/src/dumm/main.c
blob: 26896c2df0016b6ddfebfd7c64142dd85ee74ccd (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
#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"

/**
 * 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");
}

/**
 * show usage information (commandline arguments)
 */
static void help()
{
	printf("start name=<name> [options]   start a guest named <name>\n");
	printf("                              additional options:\n");
	printf("                                kernel=<uml-kernel>\n");
	printf("                                master=<read-only root files>\n");
	printf("                                memory=<guest memory in MB>\n");
	printf("guests                        list running guests\n");
	printf("help                          show this help\n");
	printf("quit                          kill quests and exit\n");
}

/**
 * start an UML guest
 */
static void start(umli_t *umli, char *line)
{
	enum {
		NAME = 0,
		MASTER,
		KERNEL,
		MEMORY,
	};
	char *const opts[] = {
		[NAME] = "name",
		[MASTER] = "master",
		[KERNEL] = "kernel",
		[MEMORY] = "memory",
		NULL
	};
	char *value;
	char *name = NULL;
	char *kernel = NULL;
	char *master = NULL;
	int mem = 0;
	
	while (TRUE)
	{
		switch (getsubopt(&line, opts, &value))
		{
			case NAME:
				name = value;
				continue;
			case MASTER:
				master = value;
				continue;
			case KERNEL:
				kernel = value;
				continue;
			case MEMORY:
				if (value)
				{
					mem = atoi(value);
				}
				continue;
			default:
				break;
		}
		break;
	}
	if (name == NULL)
	{
		printf("option 'name' is required.\n");
		help();
		return;
	}
	if (kernel == NULL)
	{
		kernel = "./linux";
	}
	if (master == NULL)
	{
		master = "master";
	}
	if (mem == 0)
	{
		mem = 128;
	}
	
	if (umli->start_guest(umli, name, kernel, master, mem))
	{
		printf("starting guest '%s'\n", name);
	}
	else
	{
		printf("starting guest '%s' failed\n", name);
	}
}

/**
 * list running UML guests
 */
static void guests(umli_t *umli)
{
	iterator_t *iterator;
	guest_t *guest;
	
	iterator = umli->create_guest_iterator(umli);
	while (iterator->iterate(iterator, (void**)&guest))
	{
		printf("%s\n", guest->get_name(guest));
	}
	iterator->destroy(iterator);
}

/**
 * main routine, parses args and reads from console
 */
int main(int argc, char *argv[])
{
	umli_t *umli;
	char *line = NULL;

	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':
				if (chdir(optarg))
				{
					printf("changing to directory '%s' failed.\n", optarg);
					return 1;
				}
				continue;
			case 'h':
				usage();
				return 0;
			default:
				usage();
				return 1;
		}
		break;
	}
	
	umli = umli_create();

	while (TRUE)
	{
		enum {
			QUIT = 0,
			HELP,
			START,
			GUESTS,
		};
		char *const opts[] = {
			[QUIT] = "quit",
			[HELP] = "help",
			[START] = "start",
			[GUESTS] = "guests",
			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 START:
				start(umli, pos);
				continue;
			case GUESTS:
				guests(umli);
				continue;
			default:
				printf("command unknown: '%s'\n", line);
				continue;
		}
		break;
	}
	umli->destroy(umli);
	clear_history();
	return 0;
}