2 * Jailhouse, a Linux-based partitioning hypervisor
4 * Copyright (c) Siemens AG, 2013
7 * Jan Kiszka <jan.kiszka@siemens.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
22 #include <sys/types.h>
23 #include <sys/ioctl.h>
26 #include <jailhouse.h>
28 enum shutdown_load_mode {LOAD, SHUTDOWN};
30 static void __attribute__((noreturn))
31 help(const char *progname, int exit_status)
33 printf("%s { COMMAND | --help }\n"
34 "\nAvailable commands:\n"
37 " cell create CELLCONFIG\n"
38 " cell load { ID | [--name] NAME } IMAGE "
39 "[ -a | --address ADDRESS] ...\n"
40 " cell start { ID | [--name] NAME }\n"
41 " cell shutdown { ID | [--name] NAME }\n"
42 " cell destroy { ID | [--name] NAME }\n"
52 fd = open("/dev/jailhouse", O_RDWR);
54 perror("opening /dev/jailhouse");
60 static void *read_file(const char *name, size_t *size)
66 fd = open(name, O_RDONLY);
68 fprintf(stderr, "opening %s: %s\n", name, strerror(errno));
72 if (fstat(fd, &stat) < 0) {
77 buffer = malloc(stat.st_size);
79 fprintf(stderr, "insufficient memory\n");
83 if (read(fd, buffer, stat.st_size) < stat.st_size) {
84 fprintf(stderr, "reading %s: %s\n", name, strerror(errno));
96 static int enable(int argc, char *argv[])
104 config = read_file(argv[2], NULL);
108 err = ioctl(fd, JAILHOUSE_ENABLE, config);
110 perror("JAILHOUSE_ENABLE");
118 static int cell_create(int argc, char *argv[])
120 struct jailhouse_cell_create cell_create;
127 cell_create.config_address = (unsigned long)read_file(argv[3], &size);
128 cell_create.config_size = size;
132 err = ioctl(fd, JAILHOUSE_CELL_CREATE, &cell_create);
134 perror("JAILHOUSE_CELL_CREATE");
141 static int parse_cell_id(struct jailhouse_cell_id *cell_id, int argc,
144 bool use_name = false;
151 if (strcmp(argv[0], "--name") == 0) {
158 cell_id->id = strtoll(argv[0], &endp, 0);
159 if (errno != 0 || *endp != 0 || cell_id->id < 0)
164 cell_id->id = JAILHOUSE_CELL_ID_UNUSED;
165 strncpy(cell_id->name, argv[arg_pos], sizeof(cell_id->name));
166 cell_id->name[sizeof(cell_id->name) - 1] = 0;
172 static bool match_opt(const char *argv, const char *short_opt,
173 const char *long_opt)
175 return strcmp(argv, short_opt) == 0 ||
176 strcmp(argv, long_opt) == 0;
179 static int cell_shutdown_load(int argc, char *argv[],
180 enum shutdown_load_mode mode)
182 unsigned int images, id_args, arg_num, n;
183 struct jailhouse_preload_image *image;
184 struct jailhouse_cell_load *cell_load;
185 struct jailhouse_cell_id cell_id;
190 id_args = parse_cell_id(&cell_id, argc - 3, &argv[3]);
191 arg_num = 3 + id_args;
192 if (id_args == 0 || (mode == SHUTDOWN && arg_num != argc) ||
193 (mode == LOAD && arg_num == argc))
197 while (arg_num < argc) {
201 if (arg_num < argc &&
202 match_opt(argv[arg_num], "-a", "--address")) {
203 if (arg_num + 1 >= argc)
209 cell_load = malloc(sizeof(*cell_load) + sizeof(*image) * images);
211 fprintf(stderr, "insufficient memory\n");
214 cell_load->cell_id = cell_id;
215 cell_load->num_preload_images = images;
217 arg_num = 3 + id_args;
219 for (n = 0, image = cell_load->image; n < images; n++, image++) {
220 image->source_address =
221 (unsigned long)read_file(argv[arg_num++], &size);
223 image->target_address = 0;
225 if (arg_num < argc &&
226 match_opt(argv[arg_num], "-a", "--address")) {
228 image->target_address =
229 strtoll(argv[arg_num + 1], &endp, 0);
230 if (errno != 0 || *endp != 0)
239 err = ioctl(fd, JAILHOUSE_CELL_LOAD, cell_load);
241 perror("JAILHOUSE_CELL_LOAD");
244 for (n = 0, image = cell_load->image; n < images; n++, image++)
245 free((void *)(unsigned long)image->source_address);
251 static int cell_simple_cmd(int argc, char *argv[], unsigned int command)
253 struct jailhouse_cell_id cell_id;
254 int id_args, err, fd;
256 id_args = parse_cell_id(&cell_id, argc - 3, &argv[3]);
257 if (id_args == 0 || 3 + id_args != argc)
262 err = ioctl(fd, command, &cell_id);
264 perror(command == JAILHOUSE_CELL_START ?
265 "JAILHOUSE_CELL_START" :
266 command == JAILHOUSE_CELL_DESTROY ?
267 "JAILHOUSE_CELL_DESTROY" :
268 "<unknown command>");
275 static int cell_list(int argc, char *argv[])
277 static const char *cell_list_cmd = "jailhouse-cell-list";
278 char new_path[PATH_MAX];
284 snprintf(new_path, sizeof(new_path), "PATH=%s:%s",
285 getenv("PATH") ? : "", dirname(argv[0]));
288 err = execvp(cell_list_cmd, argv);
290 perror(cell_list_cmd);
294 static int cell_management(int argc, char *argv[])
301 if (strcmp(argv[2], "create") == 0)
302 err = cell_create(argc, argv);
303 else if (strcmp(argv[2], "load") == 0)
304 err = cell_shutdown_load(argc, argv, LOAD);
305 else if (strcmp(argv[2], "start") == 0)
306 err = cell_simple_cmd(argc, argv, JAILHOUSE_CELL_START);
307 else if (strcmp(argv[2], "shutdown") == 0)
308 err = cell_shutdown_load(argc, argv, SHUTDOWN);
309 else if (strcmp(argv[2], "destroy") == 0)
310 err = cell_simple_cmd(argc, argv, JAILHOUSE_CELL_DESTROY);
311 else if (strcmp(argv[2], "list") == 0)
312 err = cell_list(argc, argv);
319 int main(int argc, char *argv[])
327 if (strcmp(argv[1], "enable") == 0) {
328 err = enable(argc, argv);
329 } else if (strcmp(argv[1], "disable") == 0) {
331 err = ioctl(fd, JAILHOUSE_DISABLE);
333 perror("JAILHOUSE_DISABLE");
335 } else if (strcmp(argv[1], "cell") == 0) {
336 err = cell_management(argc, argv);
337 } else if (strcmp(argv[1], "--help") == 0) {