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.
20 #include <sys/types.h>
21 #include <sys/ioctl.h>
24 #include <jailhouse.h>
26 enum shutdown_load_mode {LOAD, SHUTDOWN};
28 static void __attribute__((noreturn))
29 help(const char *progname, int exit_status)
31 printf("%s { COMMAND | --help }\n"
32 "\nAvailable commands:\n"
35 " cell create CELLCONFIG\n"
36 " cell load { ID | [--name] NAME } IMAGE "
37 "[ -a | --address ADDRESS] ...\n"
38 " cell start { ID | [--name] NAME }\n"
39 " cell shutdown { ID | [--name] NAME }\n"
40 " cell destroy { ID | [--name] NAME }\n",
49 fd = open("/dev/jailhouse", O_RDWR);
51 perror("opening /dev/jailhouse");
57 static void *read_file(const char *name, size_t *size)
63 fd = open(name, O_RDONLY);
65 fprintf(stderr, "opening %s: %s\n", name, strerror(errno));
69 if (fstat(fd, &stat) < 0) {
74 buffer = malloc(stat.st_size);
76 fprintf(stderr, "insufficient memory\n");
80 if (read(fd, buffer, stat.st_size) < stat.st_size) {
81 fprintf(stderr, "reading %s: %s\n", name, strerror(errno));
93 static int enable(int argc, char *argv[])
101 config = read_file(argv[2], NULL);
105 err = ioctl(fd, JAILHOUSE_ENABLE, config);
107 perror("JAILHOUSE_ENABLE");
115 static int cell_create(int argc, char *argv[])
117 struct jailhouse_cell_create cell_create;
124 cell_create.config_address = (unsigned long)read_file(argv[3], &size);
125 cell_create.config_size = size;
129 err = ioctl(fd, JAILHOUSE_CELL_CREATE, &cell_create);
131 perror("JAILHOUSE_CELL_CREATE");
138 static int parse_cell_id(struct jailhouse_cell_id *cell_id, int argc,
141 bool use_name = false;
148 if (strcmp(argv[0], "--name") == 0) {
155 cell_id->id = strtoll(argv[0], &endp, 0);
156 if (errno != 0 || *endp != 0 || cell_id->id < 0)
161 cell_id->id = JAILHOUSE_CELL_ID_UNUSED;
162 strncpy(cell_id->name, argv[arg_pos], sizeof(cell_id->name));
163 cell_id->name[sizeof(cell_id->name) - 1] = 0;
169 static bool match_opt(const char *argv, const char *short_opt,
170 const char *long_opt)
172 return strcmp(argv, short_opt) == 0 ||
173 strcmp(argv, long_opt) == 0;
176 static int cell_shutdown_load(int argc, char *argv[],
177 enum shutdown_load_mode mode)
179 unsigned int images, id_args, arg_num, n;
180 struct jailhouse_preload_image *image;
181 struct jailhouse_cell_load *cell_load;
182 struct jailhouse_cell_id cell_id;
187 id_args = parse_cell_id(&cell_id, argc - 3, &argv[3]);
188 arg_num = 3 + id_args;
189 if (id_args == 0 || (mode == SHUTDOWN && arg_num != argc) ||
190 (mode == LOAD && arg_num == argc))
194 while (arg_num < argc) {
198 if (arg_num < argc &&
199 match_opt(argv[arg_num], "-a", "--address")) {
200 if (arg_num + 1 >= argc)
206 cell_load = malloc(sizeof(*cell_load) + sizeof(*image) * images);
208 fprintf(stderr, "insufficient memory\n");
211 cell_load->cell_id = cell_id;
212 cell_load->num_preload_images = images;
214 arg_num = 3 + id_args;
216 for (n = 0, image = cell_load->image; n < images; n++, image++) {
217 image->source_address =
218 (unsigned long)read_file(argv[arg_num++], &size);
220 image->target_address = 0;
222 if (arg_num < argc &&
223 match_opt(argv[arg_num], "-a", "--address")) {
225 image->target_address =
226 strtoll(argv[arg_num + 1], &endp, 0);
227 if (errno != 0 || *endp != 0)
236 err = ioctl(fd, JAILHOUSE_CELL_LOAD, cell_load);
238 perror("JAILHOUSE_CELL_LOAD");
241 for (n = 0, image = cell_load->image; n < images; n++, image++)
242 free((void *)(unsigned long)image->source_address);
248 static int cell_simple_cmd(int argc, char *argv[], unsigned int command)
250 struct jailhouse_cell_id cell_id;
251 int id_args, err, fd;
253 id_args = parse_cell_id(&cell_id, argc - 3, &argv[3]);
254 if (id_args == 0 || 3 + id_args != argc)
259 err = ioctl(fd, command, &cell_id);
261 perror(command == JAILHOUSE_CELL_START ?
262 "JAILHOUSE_CELL_START" :
263 command == JAILHOUSE_CELL_DESTROY ?
264 "JAILHOUSE_CELL_DESTROY" :
265 "<unknown command>");
272 static int cell_management(int argc, char *argv[])
279 if (strcmp(argv[2], "create") == 0)
280 err = cell_create(argc, argv);
281 else if (strcmp(argv[2], "load") == 0)
282 err = cell_shutdown_load(argc, argv, LOAD);
283 else if (strcmp(argv[2], "start") == 0)
284 err = cell_simple_cmd(argc, argv, JAILHOUSE_CELL_START);
285 else if (strcmp(argv[2], "shutdown") == 0)
286 err = cell_shutdown_load(argc, argv, SHUTDOWN);
287 else if (strcmp(argv[2], "destroy") == 0)
288 err = cell_simple_cmd(argc, argv, JAILHOUSE_CELL_DESTROY);
295 int main(int argc, char *argv[])
303 if (strcmp(argv[1], "enable") == 0) {
304 err = enable(argc, argv);
305 } else if (strcmp(argv[1], "disable") == 0) {
307 err = ioctl(fd, JAILHOUSE_DISABLE);
309 perror("JAILHOUSE_DISABLE");
311 } else if (strcmp(argv[1], "cell") == 0) {
312 err = cell_management(argc, argv);
313 } else if (strcmp(argv[1], "--help") == 0) {