source: mainline/kernel/generic/src/sysinfo/sysinfo.c

Last change on this file was 74b92e6, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 3 months ago

Fix sysinfo locking

Turns out sysinfo doesn't like spinlock

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