On 2016-08-22 11:39, Ralf Ramsauer wrote:
> And keep the output format of the former python script.
> 
> Signed-off-by: Ralf Ramsauer <[email protected]>
> 
> 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 d5a535b..0000000
> --- a/tools/jailhouse-cell-list
> +++ /dev/null
> @@ -1,40 +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
> -
> -
> -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_list':
> -            open(cell_path + "/cpus_assigned_list").readline().strip(),
> -        'cpus_failed_list':
> -            open(cell_path + "/cpus_failed_list").readline().strip()
> -        })
> -
> -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'],
> -                         cell['cpus_assigned_list'], 
> cell['cpus_failed_list']))
> diff --git a/tools/jailhouse.c b/tools/jailhouse.c
> index ae2ed3f..58decf1 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,20 @@ static void *read_file(const char *name, size_t *size)
>       return buffer;
>  }
>  
> +static char *read_sysfs_entry(const char *name)

read_sysfs_string - not everything in sysfs is a string, so this
functions is not as generic as it sounds.

> +{
> +     char *ret;
> +     size_t size;
> +
> +     ret = read_file(name, &size);
> +
> +     /* enforce the string to be null-terminated */
> +     if (size)
> +             ret[size-1] = 0;

What happens if size is 0? And shouldn't this append the 0, not
overwrite the last character? Or is the last one always a linefeed?

> +
> +     return ret;
> +}
> +
>  static int enable(int argc, char *argv[])
>  {
>       void *config;
> @@ -251,6 +274,97 @@ 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 int id)
> +{
> +     char buf[128];
> +     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 */
> +     snprintf(buf, sizeof(buf), JAILHOUSE_CELLS "%d/name", id);
> +     tmp = read_sysfs_entry(buf);

snprintf could be pushed into read_sysfs_string. Just pass the entry
name and the id and build everything there. Would avoid this repeating
pattern.

> +     strncpy(cinfo->id.name, tmp, JAILHOUSE_CELL_ID_NAMELEN);
> +     cinfo->id.name[JAILHOUSE_CELL_ID_NAMELEN] = 0;
> +     free(tmp);
> +
> +     /* get cell state */
> +     snprintf(buf, sizeof(buf), JAILHOUSE_CELLS "%d/state", id);
> +     cinfo->state = read_sysfs_entry(buf);
> +
> +     /* get assigned cpu list */
> +     snprintf(buf, sizeof(buf), JAILHOUSE_CELLS "%d/cpus_assigned_list", id);
> +     cinfo->cpus_assigned_list = read_sysfs_entry(buf);
> +
> +     /* get failed cpu list */
> +     snprintf(buf, sizeof(buf), JAILHOUSE_CELLS "%d/cpus_failed_list", id);
> +     cinfo->cpus_failed_list = read_sysfs_entry(buf);
> +
> +     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;
> +     int id, 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)
> +             /* Silently return if jailhouse is not enabled */
> +             goto namelist_free;

...or:

if (num_entries > 0)
        printf(banner)

But that's just a matter of taste.

> +
> +     printf("%-8s%-24s%-16s%-24s%-24s\n",
> +             "ID", "Name", "State", "Assigned CPUs", "Failed CPUs");
> +     for (i = 0; i < num_entries; i++) {
> +             id = (int)strtol(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]);
> +     }
> +
> +namelist_free:
> +     free(namelist);
> +     return 0;
> +}
> +
>  static int cell_shutdown_load(int argc, char *argv[],
>                             enum shutdown_load_mode mode)
>  {
> @@ -369,6 +483,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) {
> 

Jan

-- 
Siemens AG, Corporate Technology, CT RDA ITP SES-DE
Corporate Competence Center Embedded Linux

-- 
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