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