| [6326f5e6] | 1 | /*
|
|---|
| [df4ed85] | 2 | * Copyright (c) 2006 Jakub Vana
|
|---|
| [196c253] | 3 | * Copyright (c) 2012 Martin Decky
|
|---|
| [6326f5e6] | 4 | * All rights reserved.
|
|---|
| 5 | *
|
|---|
| 6 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 7 | * modification, are permitted provided that the following conditions
|
|---|
| 8 | * are met:
|
|---|
| 9 | *
|
|---|
| 10 | * - Redistributions of source code must retain the above copyright
|
|---|
| 11 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 12 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 13 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 14 | * documentation and/or other materials provided with the distribution.
|
|---|
| 15 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 16 | * derived from this software without specific prior written permission.
|
|---|
| 17 | *
|
|---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 28 | */
|
|---|
| 29 |
|
|---|
| [a71c158] | 30 | /** @addtogroup generic
|
|---|
| [b45c443] | 31 | * @{
|
|---|
| 32 | */
|
|---|
| 33 | /** @file
|
|---|
| 34 | */
|
|---|
| 35 |
|
|---|
| [2666daa] | 36 | #include <sysinfo/sysinfo.h>
|
|---|
| 37 | #include <mm/slab.h>
|
|---|
| 38 | #include <print.h>
|
|---|
| [35a96cf] | 39 | #include <syscall/copy.h>
|
|---|
| [6b6626d3] | 40 | #include <synch/mutex.h>
|
|---|
| [a80687e5] | 41 | #include <arch/asm.h>
|
|---|
| [d9fae235] | 42 | #include <errno.h>
|
|---|
| [311bc25] | 43 | #include <macros.h>
|
|---|
| [d9fae235] | 44 |
|
|---|
| [80bfb601] | 45 | /** Maximal sysinfo path length */
|
|---|
| [d9fae235] | 46 | #define SYSINFO_MAX_PATH 2048
|
|---|
| [2666daa] | 47 |
|
|---|
| [a71c158] | 48 | bool fb_exported = false;
|
|---|
| [2666daa] | 49 |
|
|---|
| [80bfb601] | 50 | /** Global sysinfo tree root item */
|
|---|
| [d9fae235] | 51 | static sysinfo_item_t *global_root = NULL;
|
|---|
| [80bfb601] | 52 |
|
|---|
| 53 | /** Sysinfo SLAB cache */
|
|---|
| [d9fae235] | 54 | static slab_cache_t *sysinfo_item_slab;
|
|---|
| 55 |
|
|---|
| [6b6626d3] | 56 | /** Sysinfo lock */
|
|---|
| 57 | static mutex_t sysinfo_lock;
|
|---|
| [9dae191e] | 58 |
|
|---|
| [80bfb601] | 59 | /** Sysinfo item constructor
|
|---|
| 60 | *
|
|---|
| 61 | */
|
|---|
| [7a0359b] | 62 | NO_TRACE static int sysinfo_item_constructor(void *obj, unsigned int kmflag)
|
|---|
| [d9fae235] | 63 | {
|
|---|
| 64 | sysinfo_item_t *item = (sysinfo_item_t *) obj;
|
|---|
| 65 |
|
|---|
| 66 | item->name = NULL;
|
|---|
| 67 | item->val_type = SYSINFO_VAL_UNDEFINED;
|
|---|
| 68 | item->subtree_type = SYSINFO_SUBTREE_NONE;
|
|---|
| [b658c5d] | 69 | item->subtree.table = NULL;
|
|---|
| [d9fae235] | 70 | item->next = NULL;
|
|---|
| 71 |
|
|---|
| 72 | return 0;
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| [80bfb601] | 75 | /** Sysinfo item destructor
|
|---|
| 76 | *
|
|---|
| 77 | * Note that the return value is not perfectly correct
|
|---|
| 78 | * since more space might get actually freed thanks
|
|---|
| 79 | * to the disposal of item->name
|
|---|
| 80 | *
|
|---|
| 81 | */
|
|---|
| [7a0359b] | 82 | NO_TRACE static size_t sysinfo_item_destructor(void *obj)
|
|---|
| [d9fae235] | 83 | {
|
|---|
| 84 | sysinfo_item_t *item = (sysinfo_item_t *) obj;
|
|---|
| 85 |
|
|---|
| 86 | if (item->name != NULL)
|
|---|
| 87 | free(item->name);
|
|---|
| 88 |
|
|---|
| 89 | return 0;
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| [80bfb601] | 92 | /** Initialize sysinfo subsystem
|
|---|
| 93 | *
|
|---|
| 94 | * Create SLAB cache for sysinfo items.
|
|---|
| 95 | *
|
|---|
| 96 | */
|
|---|
| [d9fae235] | 97 | void sysinfo_init(void)
|
|---|
| 98 | {
|
|---|
| [f97f1e51] | 99 | sysinfo_item_slab = slab_cache_create("sysinfo_item_t",
|
|---|
| [d9fae235] | 100 | sizeof(sysinfo_item_t), 0, sysinfo_item_constructor,
|
|---|
| 101 | sysinfo_item_destructor, SLAB_CACHE_MAGDEFERRED);
|
|---|
| [3d23553] | 102 |
|
|---|
| [6b6626d3] | 103 | mutex_initialize(&sysinfo_lock, MUTEX_ACTIVE);
|
|---|
| [d9fae235] | 104 | }
|
|---|
| 105 |
|
|---|
| [80bfb601] | 106 | /** Recursively find an item in sysinfo tree
|
|---|
| [9dae191e] | 107 | *
|
|---|
| [c6218327] | 108 | * Should be called with sysinfo_lock held.
|
|---|
| [9dae191e] | 109 | *
|
|---|
| [80bfb601] | 110 | * @param name Current sysinfo path suffix.
|
|---|
| 111 | * @param subtree Current sysinfo (sub)tree root item.
|
|---|
| 112 | * @param ret If the return value is NULL, this argument
|
|---|
| [0030eef] | 113 | * can be set either to NULL (i.e. no item was
|
|---|
| [80bfb601] | 114 | * found and no data was generated) or the
|
|---|
| 115 | * original pointer is used to store the value
|
|---|
| 116 | * generated by a generated subtree function.
|
|---|
| [e1b6742] | 117 | * @param dry_run Do not actually get any generated
|
|---|
| 118 | * binary data, just calculate the size.
|
|---|
| [80bfb601] | 119 | *
|
|---|
| 120 | * @return Found item or NULL if no item in the fixed tree
|
|---|
| 121 | * was found (N.B. ret).
|
|---|
| 122 | *
|
|---|
| [9dae191e] | 123 | */
|
|---|
| [7a0359b] | 124 | NO_TRACE static sysinfo_item_t *sysinfo_find_item(const char *name,
|
|---|
| [e1b6742] | 125 | sysinfo_item_t *subtree, sysinfo_return_t **ret, bool dry_run)
|
|---|
| [2666daa] | 126 | {
|
|---|
| [9dae191e] | 127 | ASSERT(subtree != NULL);
|
|---|
| 128 |
|
|---|
| [d9fae235] | 129 | sysinfo_item_t *cur = subtree;
|
|---|
| [749122b] | 130 |
|
|---|
| [80bfb601] | 131 | /* Walk all siblings */
|
|---|
| [d9fae235] | 132 | while (cur != NULL) {
|
|---|
| 133 | size_t i = 0;
|
|---|
| [749122b] | 134 |
|
|---|
| [d9fae235] | 135 | /* Compare name with path */
|
|---|
| 136 | while ((cur->name[i] != 0) && (name[i] == cur->name[i]))
|
|---|
| [749122b] | 137 | i++;
|
|---|
| 138 |
|
|---|
| [d9fae235] | 139 | /* Check for perfect name and path match */
|
|---|
| 140 | if ((name[i] == 0) && (cur->name[i] == 0))
|
|---|
| 141 | return cur;
|
|---|
| [749122b] | 142 |
|
|---|
| [d9fae235] | 143 | /* Partial match up to the delimiter */
|
|---|
| 144 | if ((name[i] == '.') && (cur->name[i] == 0)) {
|
|---|
| 145 | /* Look into the subtree */
|
|---|
| 146 | switch (cur->subtree_type) {
|
|---|
| 147 | case SYSINFO_SUBTREE_TABLE:
|
|---|
| 148 | /* Recursively find in subtree */
|
|---|
| [9dae191e] | 149 | return sysinfo_find_item(name + i + 1,
|
|---|
| [e1b6742] | 150 | cur->subtree.table, ret, dry_run);
|
|---|
| [d9fae235] | 151 | case SYSINFO_SUBTREE_FUNCTION:
|
|---|
| [9dae191e] | 152 | /* Get generated data */
|
|---|
| [0030eef] | 153 | if (ret != NULL)
|
|---|
| 154 | **ret = cur->subtree.generator.fn(name + i + 1,
|
|---|
| 155 | dry_run, cur->subtree.generator.data);
|
|---|
| 156 |
|
|---|
| [9dae191e] | 157 | return NULL;
|
|---|
| [d9fae235] | 158 | default:
|
|---|
| [80bfb601] | 159 | /* Not found, no data generated */
|
|---|
| [0030eef] | 160 | if (ret != NULL)
|
|---|
| 161 | *ret = NULL;
|
|---|
| 162 |
|
|---|
| [d9fae235] | 163 | return NULL;
|
|---|
| 164 | }
|
|---|
| [2666daa] | 165 | }
|
|---|
| [d9fae235] | 166 |
|
|---|
| 167 | cur = cur->next;
|
|---|
| [2666daa] | 168 | }
|
|---|
| [d9fae235] | 169 |
|
|---|
| [80bfb601] | 170 | /* Not found, no data generated */
|
|---|
| [0030eef] | 171 | if (ret != NULL)
|
|---|
| 172 | *ret = NULL;
|
|---|
| 173 |
|
|---|
| [2666daa] | 174 | return NULL;
|
|---|
| 175 | }
|
|---|
| 176 |
|
|---|
| [9dae191e] | 177 | /** Recursively create items in sysinfo tree
|
|---|
| 178 | *
|
|---|
| [c6218327] | 179 | * Should be called with sysinfo_lock held.
|
|---|
| [9dae191e] | 180 | *
|
|---|
| [80bfb601] | 181 | * @param name Current sysinfo path suffix.
|
|---|
| 182 | * @param psubtree Pointer to an already existing (sub)tree root
|
|---|
| 183 | * item or where to store a new tree root item.
|
|---|
| 184 | *
|
|---|
| 185 | * @return Existing or newly allocated sysinfo item or NULL
|
|---|
| 186 | * if the current tree configuration does not allow to
|
|---|
| 187 | * create a new item.
|
|---|
| 188 | *
|
|---|
| [9dae191e] | 189 | */
|
|---|
| [7a0359b] | 190 | NO_TRACE static sysinfo_item_t *sysinfo_create_path(const char *name,
|
|---|
| [d9fae235] | 191 | sysinfo_item_t **psubtree)
|
|---|
| [2666daa] | 192 | {
|
|---|
| [b658c5d] | 193 | ASSERT(psubtree != NULL);
|
|---|
| 194 |
|
|---|
| [d9fae235] | 195 | if (*psubtree == NULL) {
|
|---|
| 196 | /* No parent */
|
|---|
| 197 |
|
|---|
| 198 | size_t i = 0;
|
|---|
| 199 |
|
|---|
| 200 | /* Find the first delimiter in name */
|
|---|
| 201 | while ((name[i] != 0) && (name[i] != '.'))
|
|---|
| [7bb6b06] | 202 | i++;
|
|---|
| [d9fae235] | 203 |
|
|---|
| 204 | *psubtree =
|
|---|
| 205 | (sysinfo_item_t *) slab_alloc(sysinfo_item_slab, 0);
|
|---|
| 206 | ASSERT(*psubtree);
|
|---|
| 207 |
|
|---|
| 208 | /* Fill in item name up to the delimiter */
|
|---|
| 209 | (*psubtree)->name = str_ndup(name, i);
|
|---|
| 210 | ASSERT((*psubtree)->name);
|
|---|
| 211 |
|
|---|
| 212 | /* Create subtree items */
|
|---|
| 213 | if (name[i] == '.') {
|
|---|
| 214 | (*psubtree)->subtree_type = SYSINFO_SUBTREE_TABLE;
|
|---|
| 215 | return sysinfo_create_path(name + i + 1,
|
|---|
| 216 | &((*psubtree)->subtree.table));
|
|---|
| [7bb6b06] | 217 | }
|
|---|
| [d9fae235] | 218 |
|
|---|
| 219 | /* No subtree needs to be created */
|
|---|
| 220 | return *psubtree;
|
|---|
| [2666daa] | 221 | }
|
|---|
| [d9fae235] | 222 |
|
|---|
| 223 | sysinfo_item_t *cur = *psubtree;
|
|---|
| 224 |
|
|---|
| [80bfb601] | 225 | /* Walk all siblings */
|
|---|
| [d9fae235] | 226 | while (cur != NULL) {
|
|---|
| 227 | size_t i = 0;
|
|---|
| [749122b] | 228 |
|
|---|
| [d9fae235] | 229 | /* Compare name with path */
|
|---|
| 230 | while ((cur->name[i] != 0) && (name[i] == cur->name[i]))
|
|---|
| [749122b] | 231 | i++;
|
|---|
| 232 |
|
|---|
| [d9fae235] | 233 | /* Check for perfect name and path match
|
|---|
| 234 | * -> item is already present.
|
|---|
| 235 | */
|
|---|
| 236 | if ((name[i] == 0) && (cur->name[i] == 0))
|
|---|
| 237 | return cur;
|
|---|
| [749122b] | 238 |
|
|---|
| [d9fae235] | 239 | /* Partial match up to the delimiter */
|
|---|
| 240 | if ((name[i] == '.') && (cur->name[i] == 0)) {
|
|---|
| 241 | switch (cur->subtree_type) {
|
|---|
| 242 | case SYSINFO_SUBTREE_NONE:
|
|---|
| 243 | /* No subtree yet, create one */
|
|---|
| 244 | cur->subtree_type = SYSINFO_SUBTREE_TABLE;
|
|---|
| 245 | return sysinfo_create_path(name + i + 1,
|
|---|
| 246 | &(cur->subtree.table));
|
|---|
| 247 | case SYSINFO_SUBTREE_TABLE:
|
|---|
| 248 | /* Subtree already created, add new sibling */
|
|---|
| 249 | return sysinfo_create_path(name + i + 1,
|
|---|
| 250 | &(cur->subtree.table));
|
|---|
| 251 | default:
|
|---|
| 252 | /* Subtree items handled by a function, this
|
|---|
| [80bfb601] | 253 | * cannot be overriden by a constant item.
|
|---|
| [d9fae235] | 254 | */
|
|---|
| 255 | return NULL;
|
|---|
| [749122b] | 256 | }
|
|---|
| [2666daa] | 257 | }
|
|---|
| [d9fae235] | 258 |
|
|---|
| 259 | /* No match and no more siblings to check
|
|---|
| 260 | * -> create a new sibling item.
|
|---|
| 261 | */
|
|---|
| 262 | if (cur->next == NULL) {
|
|---|
| 263 | /* Find the first delimiter in name */
|
|---|
| [749122b] | 264 | i = 0;
|
|---|
| [d9fae235] | 265 | while ((name[i] != 0) && (name[i] != '.'))
|
|---|
| [749122b] | 266 | i++;
|
|---|
| 267 |
|
|---|
| [d9fae235] | 268 | sysinfo_item_t *item =
|
|---|
| 269 | (sysinfo_item_t *) slab_alloc(sysinfo_item_slab, 0);
|
|---|
| 270 | ASSERT(item);
|
|---|
| [749122b] | 271 |
|
|---|
| [d9fae235] | 272 | cur->next = item;
|
|---|
| 273 |
|
|---|
| 274 | /* Fill in item name up to the delimiter */
|
|---|
| 275 | item->name = str_ndup(name, i);
|
|---|
| 276 | ASSERT(item->name);
|
|---|
| 277 |
|
|---|
| 278 | /* Create subtree items */
|
|---|
| 279 | if (name[i] == '.') {
|
|---|
| 280 | item->subtree_type = SYSINFO_SUBTREE_TABLE;
|
|---|
| 281 | return sysinfo_create_path(name + i + 1,
|
|---|
| 282 | &(item->subtree.table));
|
|---|
| [2666daa] | 283 | }
|
|---|
| [d9fae235] | 284 |
|
|---|
| 285 | /* No subtree needs to be created */
|
|---|
| [2666daa] | 286 | return item;
|
|---|
| [d9fae235] | 287 | }
|
|---|
| 288 |
|
|---|
| 289 | cur = cur->next;
|
|---|
| [2666daa] | 290 | }
|
|---|
| [d9fae235] | 291 |
|
|---|
| 292 | /* Unreachable */
|
|---|
| 293 | ASSERT(false);
|
|---|
| [2666daa] | 294 | return NULL;
|
|---|
| 295 | }
|
|---|
| 296 |
|
|---|
| [80bfb601] | 297 | /** Set sysinfo item with a constant numeric value
|
|---|
| 298 | *
|
|---|
| 299 | * @param name Sysinfo path.
|
|---|
| 300 | * @param root Pointer to the root item or where to store
|
|---|
| 301 | * a new root item (NULL for global sysinfo root).
|
|---|
| 302 | * @param val Value to store in the item.
|
|---|
| 303 | *
|
|---|
| 304 | */
|
|---|
| [d9fae235] | 305 | void sysinfo_set_item_val(const char *name, sysinfo_item_t **root,
|
|---|
| [96b02eb9] | 306 | sysarg_t val)
|
|---|
| [2666daa] | 307 | {
|
|---|
| [80bfb601] | 308 | /* Protect sysinfo tree consistency */
|
|---|
| [6b6626d3] | 309 | mutex_lock(&sysinfo_lock);
|
|---|
| [9dae191e] | 310 |
|
|---|
| [749122b] | 311 | if (root == NULL)
|
|---|
| [d9fae235] | 312 | root = &global_root;
|
|---|
| [749122b] | 313 |
|
|---|
| 314 | sysinfo_item_t *item = sysinfo_create_path(name, root);
|
|---|
| [d9fae235] | 315 | if (item != NULL) {
|
|---|
| [749122b] | 316 | item->val_type = SYSINFO_VAL_VAL;
|
|---|
| [d9fae235] | 317 | item->val.val = val;
|
|---|
| [749122b] | 318 | }
|
|---|
| [9dae191e] | 319 |
|
|---|
| [6b6626d3] | 320 | mutex_unlock(&sysinfo_lock);
|
|---|
| [2666daa] | 321 | }
|
|---|
| 322 |
|
|---|
| [80bfb601] | 323 | /** Set sysinfo item with a constant binary data
|
|---|
| 324 | *
|
|---|
| 325 | * Note that sysinfo only stores the pointer to the
|
|---|
| 326 | * binary data and does not touch it in any way. The
|
|---|
| 327 | * data should be static and immortal.
|
|---|
| 328 | *
|
|---|
| 329 | * @param name Sysinfo path.
|
|---|
| 330 | * @param root Pointer to the root item or where to store
|
|---|
| 331 | * a new root item (NULL for global sysinfo root).
|
|---|
| 332 | * @param data Binary data.
|
|---|
| 333 | * @param size Size of the binary data.
|
|---|
| 334 | *
|
|---|
| 335 | */
|
|---|
| [d9fae235] | 336 | void sysinfo_set_item_data(const char *name, sysinfo_item_t **root,
|
|---|
| 337 | void *data, size_t size)
|
|---|
| [2666daa] | 338 | {
|
|---|
| [80bfb601] | 339 | /* Protect sysinfo tree consistency */
|
|---|
| [6b6626d3] | 340 | mutex_lock(&sysinfo_lock);
|
|---|
| [9dae191e] | 341 |
|
|---|
| [749122b] | 342 | if (root == NULL)
|
|---|
| [d9fae235] | 343 | root = &global_root;
|
|---|
| [749122b] | 344 |
|
|---|
| 345 | sysinfo_item_t *item = sysinfo_create_path(name, root);
|
|---|
| [d9fae235] | 346 | if (item != NULL) {
|
|---|
| 347 | item->val_type = SYSINFO_VAL_DATA;
|
|---|
| 348 | item->val.data.data = data;
|
|---|
| 349 | item->val.data.size = size;
|
|---|
| 350 | }
|
|---|
| [9dae191e] | 351 |
|
|---|
| [6b6626d3] | 352 | mutex_unlock(&sysinfo_lock);
|
|---|
| [d9fae235] | 353 | }
|
|---|
| 354 |
|
|---|
| [80bfb601] | 355 | /** Set sysinfo item with a generated numeric value
|
|---|
| 356 | *
|
|---|
| 357 | * @param name Sysinfo path.
|
|---|
| 358 | * @param root Pointer to the root item or where to store
|
|---|
| 359 | * a new root item (NULL for global sysinfo root).
|
|---|
| 360 | * @param fn Numeric value generator function.
|
|---|
| [196c253] | 361 | * @param data Private data.
|
|---|
| [80bfb601] | 362 | *
|
|---|
| 363 | */
|
|---|
| [196c253] | 364 | void sysinfo_set_item_gen_val(const char *name, sysinfo_item_t **root,
|
|---|
| 365 | sysinfo_fn_val_t fn, void *data)
|
|---|
| [d9fae235] | 366 | {
|
|---|
| [80bfb601] | 367 | /* Protect sysinfo tree consistency */
|
|---|
| [6b6626d3] | 368 | mutex_lock(&sysinfo_lock);
|
|---|
| [9dae191e] | 369 |
|
|---|
| [d9fae235] | 370 | if (root == NULL)
|
|---|
| 371 | root = &global_root;
|
|---|
| [749122b] | 372 |
|
|---|
| [d9fae235] | 373 | sysinfo_item_t *item = sysinfo_create_path(name, root);
|
|---|
| 374 | if (item != NULL) {
|
|---|
| 375 | item->val_type = SYSINFO_VAL_FUNCTION_VAL;
|
|---|
| [196c253] | 376 | item->val.gen_val.fn = fn;
|
|---|
| 377 | item->val.gen_val.data = data;
|
|---|
| [749122b] | 378 | }
|
|---|
| [9dae191e] | 379 |
|
|---|
| [6b6626d3] | 380 | mutex_unlock(&sysinfo_lock);
|
|---|
| [2666daa] | 381 | }
|
|---|
| 382 |
|
|---|
| [80bfb601] | 383 | /** Set sysinfo item with a generated binary data
|
|---|
| 384 | *
|
|---|
| 385 | * Note that each time the generator function is called
|
|---|
| 386 | * it is supposed to return a new dynamically allocated
|
|---|
| 387 | * data. This data is then freed by sysinfo in the context
|
|---|
| 388 | * of the current sysinfo request.
|
|---|
| 389 | *
|
|---|
| 390 | * @param name Sysinfo path.
|
|---|
| 391 | * @param root Pointer to the root item or where to store
|
|---|
| 392 | * a new root item (NULL for global sysinfo root).
|
|---|
| 393 | * @param fn Binary data generator function.
|
|---|
| [196c253] | 394 | * @param data Private data.
|
|---|
| [80bfb601] | 395 | *
|
|---|
| 396 | */
|
|---|
| [196c253] | 397 | void sysinfo_set_item_gen_data(const char *name, sysinfo_item_t **root,
|
|---|
| 398 | sysinfo_fn_data_t fn, void *data)
|
|---|
| [d9fae235] | 399 | {
|
|---|
| [80bfb601] | 400 | /* Protect sysinfo tree consistency */
|
|---|
| [6b6626d3] | 401 | mutex_lock(&sysinfo_lock);
|
|---|
| [9dae191e] | 402 |
|
|---|
| [d9fae235] | 403 | if (root == NULL)
|
|---|
| 404 | root = &global_root;
|
|---|
| 405 |
|
|---|
| 406 | sysinfo_item_t *item = sysinfo_create_path(name, root);
|
|---|
| 407 | if (item != NULL) {
|
|---|
| 408 | item->val_type = SYSINFO_VAL_FUNCTION_DATA;
|
|---|
| [196c253] | 409 | item->val.gen_data.fn = fn;
|
|---|
| 410 | item->val.gen_data.data = data;
|
|---|
| [d9fae235] | 411 | }
|
|---|
| [9dae191e] | 412 |
|
|---|
| [6b6626d3] | 413 | mutex_unlock(&sysinfo_lock);
|
|---|
| [d9fae235] | 414 | }
|
|---|
| [2666daa] | 415 |
|
|---|
| [80bfb601] | 416 | /** Set sysinfo item with an undefined value
|
|---|
| 417 | *
|
|---|
| 418 | * @param name Sysinfo path.
|
|---|
| 419 | * @param root Pointer to the root item or where to store
|
|---|
| 420 | * a new root item (NULL for global sysinfo root).
|
|---|
| 421 | *
|
|---|
| 422 | */
|
|---|
| [749122b] | 423 | void sysinfo_set_item_undefined(const char *name, sysinfo_item_t **root)
|
|---|
| [2666daa] | 424 | {
|
|---|
| [80bfb601] | 425 | /* Protect sysinfo tree consistency */
|
|---|
| [6b6626d3] | 426 | mutex_lock(&sysinfo_lock);
|
|---|
| [9dae191e] | 427 |
|
|---|
| [749122b] | 428 | if (root == NULL)
|
|---|
| [d9fae235] | 429 | root = &global_root;
|
|---|
| [749122b] | 430 |
|
|---|
| 431 | sysinfo_item_t *item = sysinfo_create_path(name, root);
|
|---|
| 432 | if (item != NULL)
|
|---|
| 433 | item->val_type = SYSINFO_VAL_UNDEFINED;
|
|---|
| [9dae191e] | 434 |
|
|---|
| [6b6626d3] | 435 | mutex_unlock(&sysinfo_lock);
|
|---|
| [2666daa] | 436 | }
|
|---|
| 437 |
|
|---|
| [80bfb601] | 438 | /** Set sysinfo item with a generated subtree
|
|---|
| 439 | *
|
|---|
| 440 | * @param name Sysinfo path.
|
|---|
| 441 | * @param root Pointer to the root item or where to store
|
|---|
| 442 | * a new root item (NULL for global sysinfo root).
|
|---|
| 443 | * @param fn Subtree generator function.
|
|---|
| [5869ce0] | 444 | * @param data Private data to be passed to the generator.
|
|---|
| [80bfb601] | 445 | *
|
|---|
| 446 | */
|
|---|
| [9dae191e] | 447 | void sysinfo_set_subtree_fn(const char *name, sysinfo_item_t **root,
|
|---|
| [5869ce0] | 448 | sysinfo_fn_subtree_t fn, void *data)
|
|---|
| [9dae191e] | 449 | {
|
|---|
| [80bfb601] | 450 | /* Protect sysinfo tree consistency */
|
|---|
| [6b6626d3] | 451 | mutex_lock(&sysinfo_lock);
|
|---|
| [9dae191e] | 452 |
|
|---|
| 453 | if (root == NULL)
|
|---|
| 454 | root = &global_root;
|
|---|
| 455 |
|
|---|
| 456 | sysinfo_item_t *item = sysinfo_create_path(name, root);
|
|---|
| [80bfb601] | 457 |
|
|---|
| 458 | /* Change the type of the subtree only if it is not already
|
|---|
| 459 | a fixed subtree */
|
|---|
| [9dae191e] | 460 | if ((item != NULL) && (item->subtree_type != SYSINFO_SUBTREE_TABLE)) {
|
|---|
| 461 | item->subtree_type = SYSINFO_SUBTREE_FUNCTION;
|
|---|
| [5869ce0] | 462 | item->subtree.generator.fn = fn;
|
|---|
| 463 | item->subtree.generator.data = data;
|
|---|
| [9dae191e] | 464 | }
|
|---|
| 465 |
|
|---|
| [6b6626d3] | 466 | mutex_unlock(&sysinfo_lock);
|
|---|
| [9dae191e] | 467 | }
|
|---|
| 468 |
|
|---|
| 469 | /** Sysinfo dump indentation helper routine
|
|---|
| [80bfb601] | 470 | *
|
|---|
| [efb8d15] | 471 | * @param depth Number of spaces to print.
|
|---|
| [9dae191e] | 472 | *
|
|---|
| 473 | */
|
|---|
| [efb8d15] | 474 | NO_TRACE static void sysinfo_indent(size_t spaces)
|
|---|
| [d9fae235] | 475 | {
|
|---|
| [efb8d15] | 476 | for (size_t i = 0; i < spaces; i++)
|
|---|
| 477 | printf(" ");
|
|---|
| [d9fae235] | 478 | }
|
|---|
| [2666daa] | 479 |
|
|---|
| [9dae191e] | 480 | /** Dump the structure of sysinfo tree
|
|---|
| 481 | *
|
|---|
| [c6218327] | 482 | * Should be called with sysinfo_lock held.
|
|---|
| [9dae191e] | 483 | *
|
|---|
| [efb8d15] | 484 | * @param root Root item of the current (sub)tree.
|
|---|
| 485 | * @param spaces Current indentation level.
|
|---|
| [80bfb601] | 486 | *
|
|---|
| [9dae191e] | 487 | */
|
|---|
| [efb8d15] | 488 | NO_TRACE static void sysinfo_dump_internal(sysinfo_item_t *root, size_t spaces)
|
|---|
| [2666daa] | 489 | {
|
|---|
| [80bfb601] | 490 | /* Walk all siblings */
|
|---|
| [efb8d15] | 491 | for (sysinfo_item_t *cur = root; cur; cur = cur->next) {
|
|---|
| 492 | size_t length;
|
|---|
| 493 |
|
|---|
| 494 | if (spaces == 0) {
|
|---|
| 495 | printf("%s", cur->name);
|
|---|
| 496 | length = str_length(cur->name);
|
|---|
| 497 | } else {
|
|---|
| 498 | sysinfo_indent(spaces);
|
|---|
| 499 | printf(".%s", cur->name);
|
|---|
| 500 | length = str_length(cur->name) + 1;
|
|---|
| 501 | }
|
|---|
| [2666daa] | 502 |
|
|---|
| [96b02eb9] | 503 | sysarg_t val;
|
|---|
| [d9fae235] | 504 | size_t size;
|
|---|
| [2666daa] | 505 |
|
|---|
| [80bfb601] | 506 | /* Display node value and type */
|
|---|
| [d9fae235] | 507 | switch (cur->val_type) {
|
|---|
| [f8ddd17] | 508 | case SYSINFO_VAL_UNDEFINED:
|
|---|
| [efb8d15] | 509 | printf(" [undefined]\n");
|
|---|
| [f8ddd17] | 510 | break;
|
|---|
| 511 | case SYSINFO_VAL_VAL:
|
|---|
| [efb8d15] | 512 | printf(" -> %" PRIun" (%#" PRIxn ")\n", cur->val.val,
|
|---|
| 513 | cur->val.val);
|
|---|
| [f8ddd17] | 514 | break;
|
|---|
| [d9fae235] | 515 | case SYSINFO_VAL_DATA:
|
|---|
| [efb8d15] | 516 | printf(" (%zu bytes)\n", cur->val.data.size);
|
|---|
| [f8ddd17] | 517 | break;
|
|---|
| [d9fae235] | 518 | case SYSINFO_VAL_FUNCTION_VAL:
|
|---|
| [196c253] | 519 | val = cur->val.gen_val.fn(cur, cur->val.gen_val.data);
|
|---|
| [efb8d15] | 520 | printf(" -> %" PRIun" (%#" PRIxn ") [generated]\n", val,
|
|---|
| 521 | val);
|
|---|
| [d9fae235] | 522 | break;
|
|---|
| 523 | case SYSINFO_VAL_FUNCTION_DATA:
|
|---|
| [70e2b2d] | 524 | /* N.B.: No data was actually returned (only a dry run) */
|
|---|
| [196c253] | 525 | (void) cur->val.gen_data.fn(cur, &size, true,
|
|---|
| 526 | cur->val.gen_data.data);
|
|---|
| [efb8d15] | 527 | printf(" (%zu bytes) [generated]\n", size);
|
|---|
| [d9fae235] | 528 | break;
|
|---|
| 529 | default:
|
|---|
| 530 | printf("+ %s [unknown]\n", cur->name);
|
|---|
| [749122b] | 531 | }
|
|---|
| [2666daa] | 532 |
|
|---|
| [80bfb601] | 533 | /* Recursivelly nest into the subtree */
|
|---|
| [d9fae235] | 534 | switch (cur->subtree_type) {
|
|---|
| 535 | case SYSINFO_SUBTREE_NONE:
|
|---|
| 536 | break;
|
|---|
| 537 | case SYSINFO_SUBTREE_TABLE:
|
|---|
| [efb8d15] | 538 | sysinfo_dump_internal(cur->subtree.table, spaces + length);
|
|---|
| [d9fae235] | 539 | break;
|
|---|
| 540 | case SYSINFO_SUBTREE_FUNCTION:
|
|---|
| [efb8d15] | 541 | sysinfo_indent(spaces + length);
|
|---|
| 542 | printf("<generated subtree>\n");
|
|---|
| [d9fae235] | 543 | break;
|
|---|
| 544 | default:
|
|---|
| [efb8d15] | 545 | sysinfo_indent(spaces + length);
|
|---|
| 546 | printf("<unknown subtree>\n");
|
|---|
| [d9fae235] | 547 | }
|
|---|
| [749122b] | 548 | }
|
|---|
| [2666daa] | 549 | }
|
|---|
| 550 |
|
|---|
| [80bfb601] | 551 | /** Dump the structure of sysinfo tree
|
|---|
| 552 | *
|
|---|
| 553 | * @param root Root item of the sysinfo (sub)tree.
|
|---|
| 554 | * If it is NULL then consider the global
|
|---|
| 555 | * sysinfo tree.
|
|---|
| 556 | *
|
|---|
| 557 | */
|
|---|
| [9dae191e] | 558 | void sysinfo_dump(sysinfo_item_t *root)
|
|---|
| 559 | {
|
|---|
| [80bfb601] | 560 | /* Avoid other functions to mess with sysinfo
|
|---|
| 561 | while we are dumping it */
|
|---|
| [6b6626d3] | 562 | mutex_lock(&sysinfo_lock);
|
|---|
| [9dae191e] | 563 |
|
|---|
| 564 | if (root == NULL)
|
|---|
| 565 | sysinfo_dump_internal(global_root, 0);
|
|---|
| 566 | else
|
|---|
| 567 | sysinfo_dump_internal(root, 0);
|
|---|
| 568 |
|
|---|
| [6b6626d3] | 569 | mutex_unlock(&sysinfo_lock);
|
|---|
| [9dae191e] | 570 | }
|
|---|
| 571 |
|
|---|
| [80bfb601] | 572 | /** Return sysinfo item value determined by name
|
|---|
| [9dae191e] | 573 | *
|
|---|
| [c6218327] | 574 | * Should be called with sysinfo_lock held.
|
|---|
| [9dae191e] | 575 | *
|
|---|
| [70e2b2d] | 576 | * @param name Sysinfo path.
|
|---|
| 577 | * @param root Root item of the sysinfo (sub)tree.
|
|---|
| 578 | * If it is NULL then consider the global
|
|---|
| 579 | * sysinfo tree.
|
|---|
| 580 | * @param dry_run Do not actually get any generated
|
|---|
| 581 | * binary data, just calculate the size.
|
|---|
| [80bfb601] | 582 | *
|
|---|
| 583 | * @return Item value (constant or generated).
|
|---|
| 584 | *
|
|---|
| [9dae191e] | 585 | */
|
|---|
| [7a0359b] | 586 | NO_TRACE static sysinfo_return_t sysinfo_get_item(const char *name,
|
|---|
| [70e2b2d] | 587 | sysinfo_item_t **root, bool dry_run)
|
|---|
| [2666daa] | 588 | {
|
|---|
| [749122b] | 589 | if (root == NULL)
|
|---|
| [d9fae235] | 590 | root = &global_root;
|
|---|
| [749122b] | 591 |
|
|---|
| [80bfb601] | 592 | /* Try to find the item or generate data */
|
|---|
| [d9fae235] | 593 | sysinfo_return_t ret;
|
|---|
| [9dae191e] | 594 | sysinfo_return_t *ret_ptr = &ret;
|
|---|
| [e1b6742] | 595 | sysinfo_item_t *item = sysinfo_find_item(name, *root, &ret_ptr,
|
|---|
| 596 | dry_run);
|
|---|
| [749122b] | 597 |
|
|---|
| 598 | if (item != NULL) {
|
|---|
| [80bfb601] | 599 | /* Item found in the fixed sysinfo tree */
|
|---|
| 600 |
|
|---|
| [9dae191e] | 601 | ret.tag = item->val_type;
|
|---|
| [d9fae235] | 602 | switch (item->val_type) {
|
|---|
| 603 | case SYSINFO_VAL_UNDEFINED:
|
|---|
| 604 | break;
|
|---|
| 605 | case SYSINFO_VAL_VAL:
|
|---|
| [749122b] | 606 | ret.val = item->val.val;
|
|---|
| [d9fae235] | 607 | break;
|
|---|
| 608 | case SYSINFO_VAL_DATA:
|
|---|
| 609 | ret.data = item->val.data;
|
|---|
| 610 | break;
|
|---|
| 611 | case SYSINFO_VAL_FUNCTION_VAL:
|
|---|
| [196c253] | 612 | ret.val = item->val.gen_val.fn(item, item->val.gen_val.data);
|
|---|
| [d9fae235] | 613 | break;
|
|---|
| 614 | case SYSINFO_VAL_FUNCTION_DATA:
|
|---|
| [196c253] | 615 | ret.data.data = item->val.gen_data.fn(item, &ret.data.size,
|
|---|
| 616 | dry_run, item->val.gen_data.data);
|
|---|
| [d9fae235] | 617 | break;
|
|---|
| 618 | }
|
|---|
| [9dae191e] | 619 | } else {
|
|---|
| [80bfb601] | 620 | /* No item in the fixed sysinfo tree */
|
|---|
| 621 | if (ret_ptr == NULL) {
|
|---|
| 622 | /* Even no data was generated */
|
|---|
| [9dae191e] | 623 | ret.tag = SYSINFO_VAL_UNDEFINED;
|
|---|
| [80bfb601] | 624 | }
|
|---|
| [9dae191e] | 625 | }
|
|---|
| [d9fae235] | 626 |
|
|---|
| 627 | return ret;
|
|---|
| 628 | }
|
|---|
| 629 |
|
|---|
| [9dae191e] | 630 | /** Return sysinfo item determined by name from user space
|
|---|
| 631 | *
|
|---|
| [b4ad39f] | 632 | * The path string passed from the user space has to be properly null-terminated
|
|---|
| [80bfb601] | 633 | * (the last passed character must be null).
|
|---|
| 634 | *
|
|---|
| [70e2b2d] | 635 | * @param ptr Sysinfo path in the user address space.
|
|---|
| 636 | * @param size Size of the path string.
|
|---|
| 637 | * @param dry_run Do not actually get any generated
|
|---|
| 638 | * binary data, just calculate the size.
|
|---|
| [9dae191e] | 639 | *
|
|---|
| 640 | */
|
|---|
| [7a0359b] | 641 | NO_TRACE static sysinfo_return_t sysinfo_get_item_uspace(void *ptr, size_t size,
|
|---|
| [70e2b2d] | 642 | bool dry_run)
|
|---|
| [d9fae235] | 643 | {
|
|---|
| 644 | sysinfo_return_t ret;
|
|---|
| 645 | ret.tag = SYSINFO_VAL_UNDEFINED;
|
|---|
| 646 |
|
|---|
| 647 | if (size > SYSINFO_MAX_PATH)
|
|---|
| 648 | return ret;
|
|---|
| 649 |
|
|---|
| 650 | char *path = (char *) malloc(size + 1, 0);
|
|---|
| 651 | ASSERT(path);
|
|---|
| 652 |
|
|---|
| [3d23553] | 653 | if ((copy_from_uspace(path, ptr, size + 1) == 0) &&
|
|---|
| 654 | (path[size] == 0)) {
|
|---|
| [b4ad39f] | 655 | /*
|
|---|
| 656 | * Prevent other functions from messing with sysinfo while we
|
|---|
| 657 | * are reading it.
|
|---|
| 658 | */
|
|---|
| [6b6626d3] | 659 | mutex_lock(&sysinfo_lock);
|
|---|
| [70e2b2d] | 660 | ret = sysinfo_get_item(path, NULL, dry_run);
|
|---|
| [6b6626d3] | 661 | mutex_unlock(&sysinfo_lock);
|
|---|
| [b4ad39f] | 662 | }
|
|---|
| [3d23553] | 663 |
|
|---|
| [9dae191e] | 664 | free(path);
|
|---|
| [2666daa] | 665 | return ret;
|
|---|
| 666 | }
|
|---|
| [35a96cf] | 667 |
|
|---|
| [0030eef] | 668 | /** Return sysinfo keys determined by name
|
|---|
| 669 | *
|
|---|
| 670 | * Should be called with sysinfo_lock held.
|
|---|
| 671 | *
|
|---|
| 672 | * @param name Sysinfo path.
|
|---|
| 673 | * @param root Root item of the sysinfo (sub)tree.
|
|---|
| 674 | * If it is NULL then consider the global
|
|---|
| 675 | * sysinfo tree.
|
|---|
| 676 | * @param dry_run Do not actually get any generated
|
|---|
| 677 | * binary data, just calculate the size.
|
|---|
| 678 | *
|
|---|
| 679 | * @return Item value (constant or generated).
|
|---|
| 680 | *
|
|---|
| 681 | */
|
|---|
| 682 | NO_TRACE static sysinfo_return_t sysinfo_get_keys(const char *name,
|
|---|
| 683 | sysinfo_item_t **root, bool dry_run)
|
|---|
| 684 | {
|
|---|
| 685 | if (root == NULL)
|
|---|
| 686 | root = &global_root;
|
|---|
| 687 |
|
|---|
| [76f382b] | 688 | sysinfo_item_t *subtree = NULL;
|
|---|
| 689 |
|
|---|
| 690 | if (name[0] != 0) {
|
|---|
| 691 | /* Try to find the item */
|
|---|
| 692 | sysinfo_item_t *item =
|
|---|
| 693 | sysinfo_find_item(name, *root, NULL, dry_run);
|
|---|
| 694 | if ((item != NULL) &&
|
|---|
| 695 | (item->subtree_type == SYSINFO_SUBTREE_TABLE))
|
|---|
| 696 | subtree = item->subtree.table;
|
|---|
| 697 | } else
|
|---|
| 698 | subtree = *root;
|
|---|
| [0030eef] | 699 |
|
|---|
| 700 | sysinfo_return_t ret;
|
|---|
| 701 |
|
|---|
| [76f382b] | 702 | if (subtree != NULL) {
|
|---|
| [0030eef] | 703 | /*
|
|---|
| 704 | * Calculate the size of subkeys.
|
|---|
| 705 | */
|
|---|
| 706 | size_t size = 0;
|
|---|
| [76f382b] | 707 | for (sysinfo_item_t *cur = subtree; cur; cur = cur->next)
|
|---|
| [0030eef] | 708 | size += str_size(cur->name) + 1;
|
|---|
| 709 |
|
|---|
| 710 | if (dry_run) {
|
|---|
| [76f382b] | 711 | ret.tag = SYSINFO_VAL_DATA;
|
|---|
| [0030eef] | 712 | ret.data.data = NULL;
|
|---|
| 713 | ret.data.size = size;
|
|---|
| 714 | } else {
|
|---|
| 715 | /* Allocate buffer for subkeys */
|
|---|
| 716 | char *names = (char *) malloc(size, FRAME_ATOMIC);
|
|---|
| 717 | if (names == NULL)
|
|---|
| 718 | return ret;
|
|---|
| 719 |
|
|---|
| 720 | size_t pos = 0;
|
|---|
| [76f382b] | 721 | for (sysinfo_item_t *cur = subtree; cur; cur = cur->next) {
|
|---|
| [0030eef] | 722 | str_cpy(names + pos, size - pos, cur->name);
|
|---|
| 723 | pos += str_size(cur->name) + 1;
|
|---|
| 724 | }
|
|---|
| 725 |
|
|---|
| 726 | /* Correct return value */
|
|---|
| [76f382b] | 727 | ret.tag = SYSINFO_VAL_DATA;
|
|---|
| [0030eef] | 728 | ret.data.data = (void *) names;
|
|---|
| 729 | ret.data.size = size;
|
|---|
| 730 | }
|
|---|
| 731 | } else {
|
|---|
| 732 | /* No item in the fixed sysinfo tree */
|
|---|
| 733 | ret.tag = SYSINFO_VAL_UNDEFINED;
|
|---|
| 734 | }
|
|---|
| 735 |
|
|---|
| 736 | return ret;
|
|---|
| 737 | }
|
|---|
| 738 |
|
|---|
| 739 | /** Return sysinfo keys determined by name from user space
|
|---|
| 740 | *
|
|---|
| 741 | * The path string passed from the user space has to be properly
|
|---|
| 742 | * null-terminated (the last passed character must be null).
|
|---|
| 743 | *
|
|---|
| 744 | * @param ptr Sysinfo path in the user address space.
|
|---|
| 745 | * @param size Size of the path string.
|
|---|
| 746 | * @param dry_run Do not actually get any generated
|
|---|
| 747 | * binary data, just calculate the size.
|
|---|
| 748 | *
|
|---|
| 749 | */
|
|---|
| 750 | NO_TRACE static sysinfo_return_t sysinfo_get_keys_uspace(void *ptr, size_t size,
|
|---|
| 751 | bool dry_run)
|
|---|
| 752 | {
|
|---|
| 753 | sysinfo_return_t ret;
|
|---|
| 754 | ret.tag = SYSINFO_VAL_UNDEFINED;
|
|---|
| [2a08005] | 755 | ret.data.data = NULL;
|
|---|
| 756 | ret.data.size = 0;
|
|---|
| [0030eef] | 757 |
|
|---|
| 758 | if (size > SYSINFO_MAX_PATH)
|
|---|
| 759 | return ret;
|
|---|
| 760 |
|
|---|
| 761 | char *path = (char *) malloc(size + 1, 0);
|
|---|
| 762 | ASSERT(path);
|
|---|
| 763 |
|
|---|
| 764 | if ((copy_from_uspace(path, ptr, size + 1) == 0) &&
|
|---|
| 765 | (path[size] == 0)) {
|
|---|
| 766 | /*
|
|---|
| 767 | * Prevent other functions from messing with sysinfo while we
|
|---|
| 768 | * are reading it.
|
|---|
| 769 | */
|
|---|
| 770 | mutex_lock(&sysinfo_lock);
|
|---|
| 771 | ret = sysinfo_get_keys(path, NULL, dry_run);
|
|---|
| 772 | mutex_unlock(&sysinfo_lock);
|
|---|
| 773 | }
|
|---|
| 774 |
|
|---|
| 775 | free(path);
|
|---|
| 776 | return ret;
|
|---|
| 777 | }
|
|---|
| 778 |
|
|---|
| 779 | /** Get the sysinfo keys size (syscall)
|
|---|
| 780 | *
|
|---|
| 781 | * The path string passed from the user space has
|
|---|
| 782 | * to be properly null-terminated (the last passed
|
|---|
| 783 | * character must be null).
|
|---|
| 784 | *
|
|---|
| 785 | * @param path_ptr Sysinfo path in the user address space.
|
|---|
| 786 | * @param path_size Size of the path string.
|
|---|
| 787 | * @param size_ptr User space pointer where to store the
|
|---|
| 788 | * keys size.
|
|---|
| 789 | *
|
|---|
| 790 | * @return Error code (EOK in case of no error).
|
|---|
| 791 | *
|
|---|
| 792 | */
|
|---|
| 793 | sysarg_t sys_sysinfo_get_keys_size(void *path_ptr, size_t path_size,
|
|---|
| 794 | void *size_ptr)
|
|---|
| 795 | {
|
|---|
| 796 | int rc;
|
|---|
| 797 |
|
|---|
| 798 | /*
|
|---|
| 799 | * Get the keys.
|
|---|
| 800 | *
|
|---|
| 801 | * N.B.: There is no need to free any potential keys
|
|---|
| 802 | * since we request a dry run.
|
|---|
| 803 | */
|
|---|
| 804 | sysinfo_return_t ret =
|
|---|
| 805 | sysinfo_get_keys_uspace(path_ptr, path_size, true);
|
|---|
| 806 |
|
|---|
| 807 | /* Check return data tag */
|
|---|
| 808 | if (ret.tag == SYSINFO_VAL_DATA)
|
|---|
| 809 | rc = copy_to_uspace(size_ptr, &ret.data.size,
|
|---|
| 810 | sizeof(ret.data.size));
|
|---|
| 811 | else
|
|---|
| 812 | rc = EINVAL;
|
|---|
| 813 |
|
|---|
| 814 | return (sysarg_t) rc;
|
|---|
| 815 | }
|
|---|
| 816 |
|
|---|
| 817 | /** Get the sysinfo keys (syscall)
|
|---|
| 818 | *
|
|---|
| 819 | * The path string passed from the user space has
|
|---|
| 820 | * to be properly null-terminated (the last passed
|
|---|
| 821 | * character must be null).
|
|---|
| 822 | *
|
|---|
| 823 | * If the user space buffer size does not equal
|
|---|
| 824 | * the actual size of the returned data, the data
|
|---|
| 825 | * is truncated.
|
|---|
| 826 | *
|
|---|
| 827 | * The actual size of data returned is stored to
|
|---|
| 828 | * size_ptr.
|
|---|
| 829 | *
|
|---|
| 830 | * @param path_ptr Sysinfo path in the user address space.
|
|---|
| 831 | * @param path_size Size of the path string.
|
|---|
| 832 | * @param buffer_ptr User space pointer to the buffer where
|
|---|
| 833 | * to store the binary data.
|
|---|
| 834 | * @param buffer_size User space buffer size.
|
|---|
| 835 | * @param size_ptr User space pointer where to store the
|
|---|
| 836 | * binary data size.
|
|---|
| 837 | *
|
|---|
| 838 | * @return Error code (EOK in case of no error).
|
|---|
| 839 | *
|
|---|
| 840 | */
|
|---|
| 841 | sysarg_t sys_sysinfo_get_keys(void *path_ptr, size_t path_size,
|
|---|
| 842 | void *buffer_ptr, size_t buffer_size, size_t *size_ptr)
|
|---|
| 843 | {
|
|---|
| 844 | int rc;
|
|---|
| 845 |
|
|---|
| 846 | /* Get the keys */
|
|---|
| 847 | sysinfo_return_t ret = sysinfo_get_keys_uspace(path_ptr, path_size,
|
|---|
| 848 | false);
|
|---|
| 849 |
|
|---|
| 850 | /* Check return data tag */
|
|---|
| 851 | if (ret.tag == SYSINFO_VAL_DATA) {
|
|---|
| 852 | size_t size = min(ret.data.size, buffer_size);
|
|---|
| 853 | rc = copy_to_uspace(buffer_ptr, ret.data.data, size);
|
|---|
| 854 | if (rc == EOK)
|
|---|
| 855 | rc = copy_to_uspace(size_ptr, &size, sizeof(size));
|
|---|
| 856 |
|
|---|
| 857 | free(ret.data.data);
|
|---|
| 858 | } else
|
|---|
| 859 | rc = EINVAL;
|
|---|
| 860 |
|
|---|
| 861 | return (sysarg_t) rc;
|
|---|
| 862 | }
|
|---|
| 863 |
|
|---|
| [80bfb601] | 864 | /** Get the sysinfo value type (syscall)
|
|---|
| 865 | *
|
|---|
| 866 | * The path string passed from the user space has
|
|---|
| 867 | * to be properly null-terminated (the last passed
|
|---|
| 868 | * character must be null).
|
|---|
| 869 | *
|
|---|
| 870 | * @param path_ptr Sysinfo path in the user address space.
|
|---|
| 871 | * @param path_size Size of the path string.
|
|---|
| 872 | *
|
|---|
| 873 | * @return Item value type.
|
|---|
| 874 | *
|
|---|
| 875 | */
|
|---|
| [9a426d1f] | 876 | sysarg_t sys_sysinfo_get_val_type(void *path_ptr, size_t path_size)
|
|---|
| [d9fae235] | 877 | {
|
|---|
| [b4ad39f] | 878 | /*
|
|---|
| 879 | * Get the item.
|
|---|
| 880 | *
|
|---|
| 881 | * N.B.: There is no need to free any potential generated
|
|---|
| 882 | * binary data since we request a dry run.
|
|---|
| 883 | */
|
|---|
| [70e2b2d] | 884 | sysinfo_return_t ret = sysinfo_get_item_uspace(path_ptr, path_size, true);
|
|---|
| [9dae191e] | 885 |
|
|---|
| [b4ad39f] | 886 | /*
|
|---|
| [3d23553] | 887 | * Map generated value types to constant types (user space does
|
|---|
| 888 | * not care whether the value is constant or generated).
|
|---|
| [b4ad39f] | 889 | */
|
|---|
| [9dae191e] | 890 | if (ret.tag == SYSINFO_VAL_FUNCTION_VAL)
|
|---|
| 891 | ret.tag = SYSINFO_VAL_VAL;
|
|---|
| 892 | else if (ret.tag == SYSINFO_VAL_FUNCTION_DATA)
|
|---|
| 893 | ret.tag = SYSINFO_VAL_DATA;
|
|---|
| 894 |
|
|---|
| [96b02eb9] | 895 | return (sysarg_t) ret.tag;
|
|---|
| [d9fae235] | 896 | }
|
|---|
| [5719f6d] | 897 |
|
|---|
| [80bfb601] | 898 | /** Get the sysinfo numerical value (syscall)
|
|---|
| 899 | *
|
|---|
| 900 | * The path string passed from the user space has
|
|---|
| 901 | * to be properly null-terminated (the last passed
|
|---|
| 902 | * character must be null).
|
|---|
| 903 | *
|
|---|
| 904 | * @param path_ptr Sysinfo path in the user address space.
|
|---|
| 905 | * @param path_size Size of the path string.
|
|---|
| 906 | * @param value_ptr User space pointer where to store the
|
|---|
| 907 | * numberical value.
|
|---|
| 908 | *
|
|---|
| 909 | * @return Error code (EOK in case of no error).
|
|---|
| 910 | *
|
|---|
| 911 | */
|
|---|
| [96b02eb9] | 912 | sysarg_t sys_sysinfo_get_value(void *path_ptr, size_t path_size,
|
|---|
| [d9fae235] | 913 | void *value_ptr)
|
|---|
| [35a96cf] | 914 | {
|
|---|
| [9dae191e] | 915 | int rc;
|
|---|
| [3d23553] | 916 |
|
|---|
| [b4ad39f] | 917 | /*
|
|---|
| 918 | * Get the item.
|
|---|
| 919 | *
|
|---|
| [3d23553] | 920 | * N.B.: There is no need to free any potential generated binary
|
|---|
| 921 | * data since we request a dry run.
|
|---|
| [b4ad39f] | 922 | */
|
|---|
| 923 | sysinfo_return_t ret = sysinfo_get_item_uspace(path_ptr, path_size, true);
|
|---|
| [d9fae235] | 924 |
|
|---|
| [80bfb601] | 925 | /* Only constant or generated numerical value is returned */
|
|---|
| [9dae191e] | 926 | if ((ret.tag == SYSINFO_VAL_VAL) || (ret.tag == SYSINFO_VAL_FUNCTION_VAL))
|
|---|
| 927 | rc = copy_to_uspace(value_ptr, &ret.val, sizeof(ret.val));
|
|---|
| 928 | else
|
|---|
| 929 | rc = EINVAL;
|
|---|
| [d9fae235] | 930 |
|
|---|
| [96b02eb9] | 931 | return (sysarg_t) rc;
|
|---|
| [d9fae235] | 932 | }
|
|---|
| [5719f6d] | 933 |
|
|---|
| [80bfb601] | 934 | /** Get the sysinfo binary data size (syscall)
|
|---|
| 935 | *
|
|---|
| 936 | * The path string passed from the user space has
|
|---|
| 937 | * to be properly null-terminated (the last passed
|
|---|
| 938 | * character must be null).
|
|---|
| 939 | *
|
|---|
| 940 | * @param path_ptr Sysinfo path in the user address space.
|
|---|
| 941 | * @param path_size Size of the path string.
|
|---|
| 942 | * @param size_ptr User space pointer where to store the
|
|---|
| 943 | * binary data size.
|
|---|
| 944 | *
|
|---|
| 945 | * @return Error code (EOK in case of no error).
|
|---|
| 946 | *
|
|---|
| 947 | */
|
|---|
| [96b02eb9] | 948 | sysarg_t sys_sysinfo_get_data_size(void *path_ptr, size_t path_size,
|
|---|
| [d9fae235] | 949 | void *size_ptr)
|
|---|
| 950 | {
|
|---|
| [b4ad39f] | 951 | int rc;
|
|---|
| [70e2b2d] | 952 |
|
|---|
| [b4ad39f] | 953 | /*
|
|---|
| 954 | * Get the item.
|
|---|
| 955 | *
|
|---|
| [3d23553] | 956 | * N.B.: There is no need to free any potential generated binary
|
|---|
| 957 | * data since we request a dry run.
|
|---|
| [b4ad39f] | 958 | */
|
|---|
| [70e2b2d] | 959 | sysinfo_return_t ret = sysinfo_get_item_uspace(path_ptr, path_size, true);
|
|---|
| [9dae191e] | 960 |
|
|---|
| [80bfb601] | 961 | /* Only the size of constant or generated binary data is considered */
|
|---|
| [9dae191e] | 962 | if ((ret.tag == SYSINFO_VAL_DATA) || (ret.tag == SYSINFO_VAL_FUNCTION_DATA))
|
|---|
| 963 | rc = copy_to_uspace(size_ptr, &ret.data.size,
|
|---|
| 964 | sizeof(ret.data.size));
|
|---|
| 965 | else
|
|---|
| 966 | rc = EINVAL;
|
|---|
| [749122b] | 967 |
|
|---|
| [96b02eb9] | 968 | return (sysarg_t) rc;
|
|---|
| [35a96cf] | 969 | }
|
|---|
| 970 |
|
|---|
| [80bfb601] | 971 | /** Get the sysinfo binary data (syscall)
|
|---|
| 972 | *
|
|---|
| 973 | * The path string passed from the user space has
|
|---|
| 974 | * to be properly null-terminated (the last passed
|
|---|
| 975 | * character must be null).
|
|---|
| 976 | *
|
|---|
| [311bc25] | 977 | * If the user space buffer size does not equal
|
|---|
| 978 | * the actual size of the returned data, the data
|
|---|
| 979 | * is truncated. Whether this is actually a fatal
|
|---|
| 980 | * error or the data can be still interpreted as valid
|
|---|
| 981 | * depends on the nature of the data and has to be
|
|---|
| 982 | * decided by the user space.
|
|---|
| 983 | *
|
|---|
| 984 | * The actual size of data returned is stored to
|
|---|
| 985 | * size_ptr.
|
|---|
| [80bfb601] | 986 | *
|
|---|
| 987 | * @param path_ptr Sysinfo path in the user address space.
|
|---|
| 988 | * @param path_size Size of the path string.
|
|---|
| 989 | * @param buffer_ptr User space pointer to the buffer where
|
|---|
| 990 | * to store the binary data.
|
|---|
| 991 | * @param buffer_size User space buffer size.
|
|---|
| [311bc25] | 992 | * @param size_ptr User space pointer where to store the
|
|---|
| 993 | * binary data size.
|
|---|
| [80bfb601] | 994 | *
|
|---|
| 995 | * @return Error code (EOK in case of no error).
|
|---|
| 996 | *
|
|---|
| 997 | */
|
|---|
| [96b02eb9] | 998 | sysarg_t sys_sysinfo_get_data(void *path_ptr, size_t path_size,
|
|---|
| [311bc25] | 999 | void *buffer_ptr, size_t buffer_size, size_t *size_ptr)
|
|---|
| [35a96cf] | 1000 | {
|
|---|
| [b4ad39f] | 1001 | int rc;
|
|---|
| [9dae191e] | 1002 |
|
|---|
| [80bfb601] | 1003 | /* Get the item */
|
|---|
| [311bc25] | 1004 | sysinfo_return_t ret = sysinfo_get_item_uspace(path_ptr, path_size,
|
|---|
| 1005 | false);
|
|---|
| 1006 |
|
|---|
| [80bfb601] | 1007 | /* Only constant or generated binary data is considered */
|
|---|
| [311bc25] | 1008 | if ((ret.tag == SYSINFO_VAL_DATA) ||
|
|---|
| 1009 | (ret.tag == SYSINFO_VAL_FUNCTION_DATA)) {
|
|---|
| 1010 | size_t size = min(ret.data.size, buffer_size);
|
|---|
| 1011 | rc = copy_to_uspace(buffer_ptr, ret.data.data, size);
|
|---|
| 1012 | if (rc == EOK)
|
|---|
| 1013 | rc = copy_to_uspace(size_ptr, &size, sizeof(size));
|
|---|
| [9dae191e] | 1014 | } else
|
|---|
| 1015 | rc = EINVAL;
|
|---|
| [5719f6d] | 1016 |
|
|---|
| [80bfb601] | 1017 | /* N.B.: The generated binary data should be freed */
|
|---|
| [9dae191e] | 1018 | if ((ret.tag == SYSINFO_VAL_FUNCTION_DATA) && (ret.data.data != NULL))
|
|---|
| 1019 | free(ret.data.data);
|
|---|
| [749122b] | 1020 |
|
|---|
| [96b02eb9] | 1021 | return (sysarg_t) rc;
|
|---|
| [35a96cf] | 1022 | }
|
|---|
| [b45c443] | 1023 |
|
|---|
| [cc73a8a1] | 1024 | /** @}
|
|---|
| [b45c443] | 1025 | */
|
|---|