]> rtime.felk.cvut.cz Git - jailhouse.git/blobdiff - tools/jailhouse.c
Merge remote-tracking branch 'kiszka/master'
[jailhouse.git] / tools / jailhouse.c
index fbd3cd157fe56e1b024a1bf148c38f1939f161a8..ba2af66a666338b4b96da88cee58dc597ebe5062 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * Jailhouse, a Linux-based partitioning hypervisor
  *
- * Copyright (c) Siemens AG, 2013
+ * Copyright (c) Siemens AG, 2013-2016
  *
  * Authors:
  *  Jan Kiszka <jan.kiszka@siemens.com>
@@ -25,6 +25,8 @@
 
 #include <jailhouse.h>
 
+#define JAILHOUSE_EXEC_DIR     LIBEXECDIR "/jailhouse"
+
 enum shutdown_load_mode {LOAD, SHUTDOWN};
 
 struct extension {
@@ -32,11 +34,16 @@ struct extension {
 };
 
 static const struct extension extensions[] = {
+       { "cell", "linux", "CELLCONFIG KERNEL [-i | --initrd FILE]\n"
+         "              [-c | --cmdline \"STRING\"] "
+                                       "[-w | --write-params FILE]" },
        { "cell", "list", "" },
        { "cell", "stats", "{ ID | [--name] NAME }" },
        { "config", "create", "[-h] [-g] [-r ROOT] "
-         "[--mem-inmates MEM_INMATES] [--mem-hv MEM_HV] FILE" },
+         "[--mem-inmates MEM_INMATES]\n"
+         "                 [--mem-hv MEM_HV] FILE" },
        { "config", "collect", "FILE.TAR" },
+       { "hardware", "check", "SYSCONFIG" },
        { NULL }
 };
 
@@ -44,13 +51,14 @@ static void __attribute__((noreturn)) help(char *prog, int exit_status)
 {
        const struct extension *ext;
 
-       printf("Usage: %s { COMMAND | --help }\n"
+       printf("Usage: %s { COMMAND | --help || --version }\n"
               "\nAvailable commands:\n"
               "   enable SYSCONFIG\n"
               "   disable\n"
               "   cell create CELLCONFIG\n"
-              "   cell load { ID | [--name] NAME } IMAGE "
-                       "[ -a | --address ADDRESS] ...\n"
+              "   cell load { ID | [--name] NAME } "
+                               "{ IMAGE | { -s | --string } \"STRING\" }\n"
+              "             [-a | --address ADDRESS] ...\n"
               "   cell start { ID | [--name] NAME }\n"
               "   cell shutdown { ID | [--name] NAME }\n"
               "   cell destroy { ID | [--name] NAME }\n",
@@ -76,7 +84,7 @@ static void call_extension_script(const char *cmd, int argc, char *argv[])
                        continue;
 
                snprintf(new_path, sizeof(new_path), "PATH=%s:%s:%s",
-                       dirname(argv[0]), "/usr/lib/jailhouse",
+                       dirname(argv[0]), JAILHOUSE_EXEC_DIR,
                        getenv("PATH") ? : "");
                putenv(new_path);
 
@@ -101,6 +109,21 @@ static int open_dev()
        return fd;
 }
 
+static void *read_string(const char *string, size_t *size)
+{
+       void *buffer;
+
+       *size = strlen(string) + 1;
+
+       buffer = strdup(string);
+       if (!buffer) {
+               fprintf(stderr, "insufficient memory\n");
+               exit(1);
+       }
+
+       return buffer;
+}
+
 static void *read_file(const char *name, size_t *size)
 {
        struct stat stat;
@@ -178,6 +201,7 @@ static int cell_create(int argc, char *argv[])
                perror("JAILHOUSE_CELL_CREATE");
 
        close(fd);
+       free((void *)(unsigned long)cell_create.config_address);
 
        return err;
 }
@@ -189,6 +213,8 @@ static int parse_cell_id(struct jailhouse_cell_id *cell_id, int argc,
        int arg_pos = 0;
        char *endp;
 
+       memset(cell_id, 0, sizeof(*cell_id));
+
        if (argc < 1)
                return 0;
 
@@ -206,8 +232,10 @@ static int parse_cell_id(struct jailhouse_cell_id *cell_id, int argc,
 
        if (use_name) {
                cell_id->id = JAILHOUSE_CELL_ID_UNUSED;
-               strncpy(cell_id->name, argv[arg_pos], sizeof(cell_id->name));
-               cell_id->name[sizeof(cell_id->name) - 1] = 0;
+               /* cell_id is initialized with zeros, so leaving out the last
+                * byte ensures that the string is always terminated. */
+               strncpy(cell_id->name, argv[arg_pos],
+                       sizeof(cell_id->name) - 1);
        }
 
        return arg_pos + 1;
@@ -223,12 +251,12 @@ static bool match_opt(const char *argv, const char *short_opt,
 static int cell_shutdown_load(int argc, char *argv[],
                              enum shutdown_load_mode mode)
 {
-       unsigned int images, id_args, arg_num, n;
        struct jailhouse_preload_image *image;
        struct jailhouse_cell_load *cell_load;
        struct jailhouse_cell_id cell_id;
+       int err, fd, id_args, arg_num;
+       unsigned int images, n;
        size_t size;
-       int err, fd;
        char *endp;
 
        id_args = parse_cell_id(&cell_id, argc - 3, &argv[3]);
@@ -239,6 +267,12 @@ static int cell_shutdown_load(int argc, char *argv[],
 
        images = 0;
        while (arg_num < argc) {
+               if (match_opt(argv[arg_num], "-s", "--string")) {
+                       if (arg_num + 1 >= argc)
+                               help(argv[0], 1);
+                       arg_num++;
+               }
+
                images++;
                arg_num++;
 
@@ -261,8 +295,16 @@ static int cell_shutdown_load(int argc, char *argv[],
        arg_num = 3 + id_args;
 
        for (n = 0, image = cell_load->image; n < images; n++, image++) {
-               image->source_address =
-                       (unsigned long)read_file(argv[arg_num++], &size);
+               if (match_opt(argv[arg_num], "-s", "--string")) {
+                       arg_num++;
+                       image->source_address =
+                               (unsigned long)read_string(argv[arg_num++],
+                                                          &size);
+               } else {
+                       image->source_address =
+                               (unsigned long)read_file(argv[arg_num++],
+                                                        &size);
+               }
                image->size = size;
                image->target_address = 0;
 
@@ -358,13 +400,18 @@ int main(int argc, char *argv[])
                close(fd);
        } else if (strcmp(argv[1], "cell") == 0) {
                err = cell_management(argc, argv);
-       } else if (strcmp(argv[1], "config") == 0) {
+       } else if (strcmp(argv[1], "config") == 0 ||
+                  strcmp(argv[1], "hardware") == 0) {
                call_extension_script(argv[1], argc, argv);
                help(argv[0], 1);
+       } else if (strcmp(argv[1], "--version") == 0) {
+               printf("Jailhouse management tool %s\n", JAILHOUSE_VERSION);
+               return 0;
        } else if (strcmp(argv[1], "--help") == 0) {
                help(argv[0], 0);
-       } else
+       } else {
                help(argv[0], 1);
+       }
 
        return err ? 1 : 0;
 }