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)
238 err = ioctl(fd, JAILHOUSE_CELL_LOAD, cell_load);
240 perror("JAILHOUSE_CELL_LOAD");
243 for (n = 0, image = cell_load->image; n < images; n++, image++)
244 free((void *)(unsigned long)image->source_address);
250 static int cell_simple_cmd(int argc, char *argv[], unsigned int command)
252 struct jailhouse_cell_id cell_id;
253 int id_args, err, fd;
255 id_args = parse_cell_id(&cell_id, argc - 3, &argv[3]);
256 if (id_args == 0 || 3 + id_args != argc)
261 err = ioctl(fd, command, &cell_id);
263 perror(command == JAILHOUSE_CELL_START ?
264 "JAILHOUSE_CELL_START" :
265 command == JAILHOUSE_CELL_DESTROY ?
266 "JAILHOUSE_CELL_DESTROY" :
267 "<unknown command>");
274 static int cell_list(int argc, char *argv[])
276 static const char *cell_list_cmd = "jailhouse-cell-list";
277 char new_path[PATH_MAX];
283 snprintf(new_path, sizeof(new_path), "PATH=%s:%s",
284 getenv("PATH") ? : "", dirname(argv[0]));
287 err = execvp(cell_list_cmd, argv);
289 perror(cell_list_cmd);
293 static int cell_management(int argc, char *argv[])
300 if (strcmp(argv[2], "create") == 0)
301 err = cell_create(argc, argv);
302 else if (strcmp(argv[2], "load") == 0)
303 err = cell_shutdown_load(argc, argv, LOAD);
304 else if (strcmp(argv[2], "start") == 0)
305 err = cell_simple_cmd(argc, argv, JAILHOUSE_CELL_START);
306 else if (strcmp(argv[2], "shutdown") == 0)
307 err = cell_shutdown_load(argc, argv, SHUTDOWN);
308 else if (strcmp(argv[2], "destroy") == 0)
309 err = cell_simple_cmd(argc, argv, JAILHOUSE_CELL_DESTROY);
310 else if (strcmp(argv[2], "list") == 0)
311 err = cell_list(argc, argv);
318 int main(int argc, char *argv[])
326 if (strcmp(argv[1], "enable") == 0) {
327 err = enable(argc, argv);
328 } else if (strcmp(argv[1], "disable") == 0) {
330 err = ioctl(fd, JAILHOUSE_DISABLE);
332 perror("JAILHOUSE_DISABLE");
334 } else if (strcmp(argv[1], "cell") == 0) {
335 err = cell_management(argc, argv);
336 } else if (strcmp(argv[1], "--help") == 0) {