And keep the output format of the former python script.

Signed-off-by: Ralf Ramsauer <[email protected]>
---
 tools/Makefile            |   1 -
 tools/jailhouse-cell-list |  71 ---------------------------
 tools/jailhouse.c         | 121 +++++++++++++++++++++++++++++++++++++++++++++-
 3 files changed, 120 insertions(+), 73 deletions(-)
 delete mode 100755 tools/jailhouse-cell-list

diff --git a/tools/Makefile b/tools/Makefile
index 6975a76..9b6e176 100644
--- a/tools/Makefile
+++ b/tools/Makefile
@@ -21,7 +21,6 @@ TARGETS := jailhouse
 INST_TARGETS := $(TARGETS)
 HELPERS := \
        jailhouse-cell-linux \
-       jailhouse-cell-list \
        jailhouse-cell-stats \
        jailhouse-config-create \
        jailhouse-hardware-check
diff --git a/tools/jailhouse-cell-list b/tools/jailhouse-cell-list
deleted file mode 100755
index d4123f6..0000000
--- a/tools/jailhouse-cell-list
+++ /dev/null
@@ -1,71 +0,0 @@
-#!/usr/bin/env python
-
-# Jailhouse, a Linux-based partitioning hypervisor
-#
-# Copyright (c) Siemens AG, 2014
-#
-# Authors:
-#  Jan Kiszka <[email protected]>
-#
-# This work is licensed under the terms of the GNU GPL, version 2.  See
-# the COPYING file in the top-level directory.
-
-import glob
-import os
-import sys
-
-
-def read_cpus(path):
-    return int(open(path).readline().strip().replace(",", ""), 16)
-
-
-def finish_cpu_list(last_cpu, cpu):
-    if last_cpu is not None:
-        if cpu-last_cpu > 1:
-            return "%s%d," % ("-" if cpu-last_cpu > 2 else ",", cpu-1)
-        else:
-            return ","
-    else:
-        return ""
-
-
-def print_cpus(mask):
-    last_cpu = None
-    cpu = 0
-    output = ""
-    while mask > 0:
-        if mask & 1:
-            if last_cpu is None:
-                last_cpu = cpu
-                output += str(cpu)
-        else:
-            output += finish_cpu_list(last_cpu, cpu)
-            last_cpu = None
-        mask >>= 1
-        cpu += 1
-    output += finish_cpu_list(last_cpu, cpu)
-    return output.strip(",")
-
-
-if len(sys.argv) > 1:
-    print("usage: %s" % os.path.basename(sys.argv[0]).replace("-", " "))
-    exit(0 if sys.argv[1] in ("--help", "-h") else 1)
-
-cells = []
-for cell_path in glob.glob('/sys/devices/jailhouse/cells/*'):
-    cells.append({
-        'id': os.path.basename(cell_path),
-        'name': open(cell_path + "/name").readline().strip(),
-        'state': open(cell_path + "/state").readline().strip(),
-        'cpus_assigned': read_cpus(cell_path + "/cpus_assigned"),
-        'cpus_failed': read_cpus(cell_path + "/cpus_failed")
-        })
-
-line_format = "%-8s%-24s%-16s%-24s%-24s"
-if not cells == []:
-    print(line_format % ("ID", "Name", "State",
-                         "Assigned CPUs", "Failed CPUs"))
-for cell in sorted(cells, key=lambda cell: cell['id']):
-    print(line_format % (cell['id'], cell['name'], cell['state'],
-                         print_cpus(cell['cpus_assigned']),
-                         print_cpus(cell['cpus_failed'])))
diff --git a/tools/jailhouse.c b/tools/jailhouse.c
index ae2ed3f..84e5fe3 100644
--- a/tools/jailhouse.c
+++ b/tools/jailhouse.c
@@ -14,6 +14,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <dirent.h>
 #include <unistd.h>
 #include <fcntl.h>
 #include <errno.h>
@@ -27,6 +28,7 @@
 
 #define JAILHOUSE_EXEC_DIR     LIBEXECDIR "/jailhouse"
 #define JAILHOUSE_DEVICE       "/dev/jailhouse"
+#define JAILHOUSE_CELLS                "/sys/devices/jailhouse/cells/"
 
 enum shutdown_load_mode {LOAD, SHUTDOWN};
 
@@ -34,11 +36,17 @@ struct extension {
        char *cmd, *subcmd, *help;
 };
 
+struct jailhouse_cell_info {
+       struct jailhouse_cell_id id;
+       char *state;
+       char *cpus_assigned_list;
+       char *cpus_failed_list;
+};
+
 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]\n"
@@ -57,6 +65,7 @@ static void __attribute__((noreturn)) help(char *prog, int 
exit_status)
               "   enable SYSCONFIG\n"
               "   disable\n"
               "   cell create CELLCONFIG\n"
+              "   cell list\n"
               "   cell load { ID | [--name] NAME } "
                                "{ IMAGE | { -s | --string } \"STRING\" }\n"
               "             [-a | --address ADDRESS] ...\n"
@@ -163,6 +172,30 @@ static void *read_file(const char *name, size_t *size)
        return buffer;
 }
 
+static char *read_sysfs_cell_string(const unsigned int id, const char *entry)
+{
+       char *ret, buffer[128];
+       size_t size;
+
+       snprintf(buffer, sizeof(buffer), JAILHOUSE_CELLS "%u/%s", id, entry);
+       ret = read_file(buffer, &size);
+
+       /* entries in /sys/devices/jailhouse/cells must not be empty */
+       if (size == 0) {
+               snprintf(buffer, sizeof(buffer), "reading " JAILHOUSE_CELLS 
"%u/%s",
+                        id, entry);
+               perror(buffer);
+               exit(1);
+       }
+
+       /* chop trailing linefeeds and enforce the string to be null-terminated 
*/
+       if (ret[size-1] != '\n')
+               ret = realloc(ret, ++size);
+       ret[size-1] = 0;
+
+       return ret;
+}
+
 static int enable(int argc, char *argv[])
 {
        void *config;
@@ -251,6 +284,90 @@ static bool match_opt(const char *argv, const char 
*short_opt,
                strcmp(argv, long_opt) == 0;
 }
 
+static struct jailhouse_cell_info *get_cell_info(const unsigned int id)
+{
+       struct jailhouse_cell_info *cinfo;
+       char *tmp;
+
+       cinfo = malloc(sizeof(struct jailhouse_cell_info));
+       if (cinfo == NULL) {
+               fprintf(stderr, "insufficient memory\n");
+               exit(1);
+       }
+
+       /* set cell id */
+       cinfo->id.id = id;
+
+       /* get cell name */
+       tmp = read_sysfs_cell_string(id, "name");
+       strncpy(cinfo->id.name, tmp, JAILHOUSE_CELL_ID_NAMELEN);
+       cinfo->id.name[JAILHOUSE_CELL_ID_NAMELEN] = 0;
+       free(tmp);
+
+       /* get cell state */
+       cinfo->state = read_sysfs_cell_string(id, "state");
+
+       /* get assigned cpu list */
+       cinfo->cpus_assigned_list =
+               read_sysfs_cell_string(id, "cpus_assigned_list");
+
+       /* get failed cpu list */
+       cinfo->cpus_failed_list = read_sysfs_cell_string(id, 
"cpus_failed_list");
+
+       return cinfo;
+}
+
+static void cell_info_free(struct jailhouse_cell_info *cinfo)
+{
+       free(cinfo->state);
+       free(cinfo->cpus_assigned_list);
+       free(cinfo->cpus_failed_list);
+       free(cinfo);
+}
+
+static int cell_match(const struct dirent *dirent)
+{
+       return dirent->d_name[0] != '.';
+}
+
+static int cell_list(int argc, char *argv[])
+{
+       struct dirent **namelist;
+       struct jailhouse_cell_info *cinfo;
+       unsigned int id;
+       int i, num_entries;
+       (void)argv;
+
+       if (argc != 3)
+               help(argv[0], 1);
+
+       num_entries = scandir(JAILHOUSE_CELLS, &namelist, cell_match, 
alphasort);
+       if (num_entries == -1) {
+               /* Silently return if kernel module is not loaded */
+               if (errno == ENOENT)
+                       return 0;
+
+               perror("scandir");
+               return -1;
+       }
+
+       if (num_entries > 0)
+               printf("%-8s%-24s%-16s%-24s%-24s\n",
+                      "ID", "Name", "State", "Assigned CPUs", "Failed CPUs");
+       for (i = 0; i < num_entries; i++) {
+               id = (unsigned int)strtoul(namelist[i]->d_name, NULL, 10);
+
+               cinfo = get_cell_info(id);
+               printf("%-8d%-24s%-16s%-24s%-24s\n", cinfo->id.id, 
cinfo->id.name,
+                      cinfo->state, cinfo->cpus_assigned_list, 
cinfo->cpus_failed_list);
+               cell_info_free(cinfo);
+               free(namelist[i]);
+       }
+
+       free(namelist);
+       return 0;
+}
+
 static int cell_shutdown_load(int argc, char *argv[],
                              enum shutdown_load_mode mode)
 {
@@ -369,6 +486,8 @@ static int cell_management(int argc, char *argv[])
 
        if (strcmp(argv[2], "create") == 0) {
                err = cell_create(argc, argv);
+       } else if (strcmp(argv[2], "list") == 0) {
+               err = cell_list(argc, argv);
        } else if (strcmp(argv[2], "load") == 0) {
                err = cell_shutdown_load(argc, argv, LOAD);
        } else if (strcmp(argv[2], "start") == 0) {
-- 
2.9.3

-- 
You received this message because you are subscribed to the Google Groups 
"Jailhouse" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to