Changeset 76f382b in mainline


Ignore:
Timestamp:
2012-03-02T14:50:42Z (12 years ago)
Author:
Martin Decky <martin@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
8b9a443
Parents:
560d79f
Message:

support for listing sysinfo from uspace

Files:
4 edited

Legend:

Unmodified
Added
Removed
  • kernel/generic/src/sysinfo/sysinfo.c

    r560d79f r76f382b  
    678678                root = &global_root;
    679679       
    680         /* Try to find the item */
    681         sysinfo_item_t *item = sysinfo_find_item(name, *root, NULL,
    682             dry_run);
     680        sysinfo_item_t *subtree = NULL;
     681       
     682        if (name[0] != 0) {
     683                /* Try to find the item */
     684                sysinfo_item_t *item =
     685                    sysinfo_find_item(name, *root, NULL, dry_run);
     686                if ((item != NULL) &&
     687                    (item->subtree_type == SYSINFO_SUBTREE_TABLE))
     688                        subtree = item->subtree.table;
     689        } else
     690                subtree = *root;
    683691       
    684692        sysinfo_return_t ret;
    685693       
    686         if ((item != NULL) && (item->subtree_type == SYSINFO_SUBTREE_TABLE)) {
     694        if (subtree != NULL) {
    687695                /*
    688                  * Subtree found in the fixed sysinfo tree.
    689696                 * Calculate the size of subkeys.
    690697                 */
    691698                size_t size = 0;
    692                 for (sysinfo_item_t *cur = item->subtree.table; cur;
    693                     cur = cur->next)
     699                for (sysinfo_item_t *cur = subtree; cur; cur = cur->next)
    694700                        size += str_size(cur->name) + 1;
    695701               
    696702                if (dry_run) {
    697                         ret.tag = SYSINFO_VAL_FUNCTION_DATA;
     703                        ret.tag = SYSINFO_VAL_DATA;
    698704                        ret.data.data = NULL;
    699705                        ret.data.size = size;
     
    705711                       
    706712                        size_t pos = 0;
    707                         for (sysinfo_item_t *cur = item->subtree.table; cur;
    708                             cur = cur->next) {
     713                        for (sysinfo_item_t *cur = subtree; cur; cur = cur->next) {
    709714                                str_cpy(names + pos, size - pos, cur->name);
    710715                                pos += str_size(cur->name) + 1;
     
    712717                       
    713718                        /* Correct return value */
    714                         ret.tag = SYSINFO_VAL_FUNCTION_DATA;
     719                        ret.tag = SYSINFO_VAL_DATA;
    715720                        ret.data.data = (void *) names;
    716721                        ret.data.size = size;
  • uspace/app/sysinfo/sysinfo.c

    r560d79f r76f382b  
    3737#include <stdio.h>
    3838#include <sysinfo.h>
     39#include <malloc.h>
    3940#include <sys/types.h>
    4041
    41 static int print_item_val(char *ipath);
    42 static int print_item_data(char *ipath);
     42static void dump_bytes_hex(char *data, size_t size)
     43{
     44        for (size_t i = 0; i < size; i++) {
     45                if (i > 0)
     46                        putchar(' ');
     47                printf("0x%02x", (uint8_t) data[i]);
     48        }
     49}
    4350
    44 static void dump_bytes_hex(char *data, size_t size);
    45 static void dump_bytes_text(char *data, size_t size);
     51static void dump_bytes_text(char *data, size_t size)
     52{
     53        size_t offset = 0;
     54       
     55        while (offset < size) {
     56                wchar_t c = str_decode(data, &offset, size);
     57                printf("%lc", (wint_t) c);
     58        }
     59}
    4660
    47 static void print_syntax(void);
     61static int print_item_val(char *ipath)
     62{
     63        sysarg_t value;
     64        int rc = sysinfo_get_value(ipath, &value);
     65        if (rc != EOK) {
     66                printf("Error reading item '%s'.\n", ipath);
     67                return rc;
     68        }
     69       
     70        printf("%s -> %" PRIu64 " (0x%" PRIx64 ")\n", ipath,
     71            (uint64_t) value, (uint64_t) value);
     72       
     73        return EOK;
     74}
     75
     76static int print_item_data(char *ipath)
     77{
     78        size_t size;
     79        void *data = sysinfo_get_data(ipath, &size);
     80        if (data == NULL) {
     81                printf("Error reading item '%s'.\n", ipath);
     82                return -1;
     83        }
     84       
     85        printf("%s -> ", ipath);
     86        dump_bytes_hex(data, size);
     87        fputs(" ('", stdout);
     88        dump_bytes_text(data, size);
     89        fputs("')\n", stdout);
     90       
     91        return EOK;
     92}
     93
     94static void print_keys(const char *path)
     95{
     96        size_t size;
     97        char *keys = sysinfo_get_keys(path, &size);
     98        if ((keys == NULL) || (size == 0))
     99                return;
     100       
     101        size_t pos = 0;
     102        while (pos < size) {
     103                /* Process each key with sanity checks */
     104                size_t cur_size = str_nsize(keys + pos, size - pos);
     105                size_t path_size = str_size(path) + cur_size + 2;
     106                char *cur_path = (char *) malloc(path_size);
     107                if (cur_path == NULL)
     108                        break;
     109               
     110                if (path[0] != 0)
     111                        snprintf(cur_path, path_size, "%s.%s", path, keys + pos);
     112                else
     113                        snprintf(cur_path, path_size, "%s", keys + pos);
     114               
     115                printf("%s\n", cur_path);
     116                print_keys(cur_path);
     117               
     118                free(cur_path);
     119                pos += cur_size + 1;
     120        }
     121       
     122        free(keys);
     123}
    48124
    49125int main(int argc, char *argv[])
    50126{
    51         int rc;
    52         char *ipath;
    53         sysinfo_item_val_type_t tag;
    54 
    55127        if (argc != 2) {
    56                 print_syntax();
    57                 return 1;
     128                /* Print keys */
     129                print_keys("");
     130                return 0;
    58131        }
    59 
    60         ipath = argv[1];
    61 
    62         tag = sysinfo_get_val_type(ipath);
    63 
     132       
     133        char *ipath = argv[1];
     134        sysinfo_item_val_type_t tag = sysinfo_get_val_type(ipath);
     135       
    64136        /* Silence warning */
    65         rc = EOK;
    66 
     137        int rc = 0;
     138       
    67139        switch (tag) {
    68140        case SYSINFO_VAL_UNDEFINED:
     
    82154                break;
    83155        }
    84 
     156       
    85157        return rc;
    86 }
    87 
    88 static int print_item_val(char *ipath)
    89 {
    90         sysarg_t value;
    91         int rc;
    92 
    93         rc = sysinfo_get_value(ipath, &value);
    94         if (rc != EOK) {
    95                 printf("Error reading item '%s'.\n", ipath);
    96                 return rc;
    97         }
    98 
    99         printf("%s -> %" PRIu64 " (0x%" PRIx64 ")\n", ipath,
    100             (uint64_t) value, (uint64_t) value);
    101 
    102         return EOK;
    103 }
    104 
    105 static int print_item_data(char *ipath)
    106 {
    107         void *data;
    108         size_t size;
    109 
    110         data = sysinfo_get_data(ipath, &size);
    111         if (data == NULL) {
    112                 printf("Error reading item '%s'.\n", ipath);
    113                 return -1;
    114         }
    115 
    116         printf("%s -> ", ipath);
    117         dump_bytes_hex(data, size);
    118         fputs(" ('", stdout);
    119         dump_bytes_text(data, size);
    120         fputs("')\n", stdout);
    121 
    122         return EOK;
    123 }
    124 
    125 static void dump_bytes_hex(char *data, size_t size)
    126 {
    127         size_t i;
    128 
    129         for (i = 0; i < size; ++i) {
    130                 if (i > 0) putchar(' ');
    131                 printf("0x%02x", (uint8_t) data[i]);
    132         }
    133 }
    134 
    135 static void dump_bytes_text(char *data, size_t size)
    136 {
    137         wchar_t c;
    138         size_t offset;
    139 
    140         offset = 0;
    141 
    142         while (offset < size) {
    143                 c = str_decode(data, &offset, size);
    144                 printf("%lc", (wint_t) c);
    145         }
    146 }
    147 
    148 
    149 static void print_syntax(void)
    150 {
    151         printf("Syntax: sysinfo <item_path>\n");
    152158}
    153159
  • uspace/lib/c/generic/sysinfo.c

    r560d79f r76f382b  
    4040#include <bool.h>
    4141
     42/** Get sysinfo keys size
     43 *
     44 * @param path  Sysinfo path.
     45 * @param value Pointer to store the keys size.
     46 *
     47 * @return EOK if the keys were successfully read.
     48 *
     49 */
     50static int sysinfo_get_keys_size(const char *path, size_t *size)
     51{
     52        return (int) __SYSCALL3(SYS_SYSINFO_GET_KEYS_SIZE, (sysarg_t) path,
     53            (sysarg_t) str_size(path), (sysarg_t) size);
     54}
     55
     56/** Get sysinfo keys
     57 *
     58 * @param path  Sysinfo path.
     59 * @param value Pointer to store the keys size.
     60 *
     61 * @return Keys read from sysinfo or NULL if the
     62 *         sysinfo item has no subkeys.
     63 *         The returned non-NULL pointer should be
     64 *         freed by free().
     65 *
     66 */
     67char *sysinfo_get_keys(const char *path, size_t *size)
     68{
     69        /*
     70         * The size of the keys might change during time.
     71         * Unfortunatelly we cannot allocate the buffer
     72         * and transfer the keys as a single atomic operation.
     73         */
     74       
     75        /* Get the keys size */
     76        int ret = sysinfo_get_keys_size(path, size);
     77        if ((ret != EOK) || (size == 0)) {
     78                /*
     79                 * Item with no subkeys.
     80                 */
     81                *size = 0;
     82                return NULL;
     83        }
     84       
     85        char *data = malloc(*size);
     86        if (data == NULL) {
     87                *size = 0;
     88                return NULL;
     89        }
     90       
     91        /* Get the data */
     92        size_t sz;
     93        ret = __SYSCALL5(SYS_SYSINFO_GET_KEYS, (sysarg_t) path,
     94            (sysarg_t) str_size(path), (sysarg_t) data, (sysarg_t) *size,
     95            (sysarg_t) &sz);
     96        if (ret == EOK) {
     97                *size = sz;
     98                return data;
     99        }
     100       
     101        free(data);
     102        *size = 0;
     103        return NULL;
     104}
     105
    42106/** Get sysinfo item type
    43107 *
  • uspace/lib/c/include/sysinfo.h

    r560d79f r76f382b  
    4040#include <abi/sysinfo.h>
    4141
     42extern char *sysinfo_get_keys(const char *, size_t *);
    4243extern sysinfo_item_val_type_t sysinfo_get_val_type(const char *);
    4344extern int sysinfo_get_value(const char *, sysarg_t *);
Note: See TracChangeset for help on using the changeset viewer.