source: mainline/kernel/generic/src/sysinfo/sysinfo.c@ 70e2b2d

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 70e2b2d was 70e2b2d, checked in by Martin Decky <martin@…>, 15 years ago

avoid costly allocation and generation of data when it is actually not necessary

  • Property mode set to 100644
File size: 22.0 KB
Line 
1/*
2 * Copyright (c) 2006 Jakub Vana
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
29/** @addtogroup generic
30 * @{
31 */
32/** @file
33 */
34
35#include <sysinfo/sysinfo.h>
36#include <mm/slab.h>
37#include <print.h>
38#include <syscall/copy.h>
39#include <synch/spinlock.h>
40#include <arch/asm.h>
41#include <errno.h>
42
43/** Maximal sysinfo path length */
44#define SYSINFO_MAX_PATH 2048
45
46bool fb_exported = false;
47
48/** Global sysinfo tree root item */
49static sysinfo_item_t *global_root = NULL;
50
51/** Sysinfo SLAB cache */
52static slab_cache_t *sysinfo_item_slab;
53
54/** Sysinfo spinlock */
55SPINLOCK_STATIC_INITIALIZE_NAME(sysinfo_lock, "sysinfo_lock");
56
57/** Sysinfo item constructor
58 *
59 */
60static int sysinfo_item_constructor(void *obj, int kmflag)
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;
67 item->subtree.table = NULL;
68 item->next = NULL;
69
70 return 0;
71}
72
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 */
80static int sysinfo_item_destructor(void *obj)
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
90/** Initialize sysinfo subsystem
91 *
92 * Create SLAB cache for sysinfo items.
93 *
94 */
95void 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);
100}
101
102/** Recursively find an item in sysinfo tree
103 *
104 * Should be called with interrupts disabled
105 * and sysinfo_lock held.
106 *
107 * @param name Current sysinfo path suffix.
108 * @param subtree Current sysinfo (sub)tree root item.
109 * @param ret If the return value is NULL, this argument
110 * can be either also NULL (i.e. no item was
111 * found and no data was generated) or the
112 * original pointer is used to store the value
113 * generated by a generated subtree function.
114 *
115 * @return Found item or NULL if no item in the fixed tree
116 * was found (N.B. ret).
117 *
118 */
119static sysinfo_item_t *sysinfo_find_item(const char *name,
120 sysinfo_item_t *subtree, sysinfo_return_t **ret)
121{
122 ASSERT(subtree != NULL);
123 ASSERT(ret != NULL);
124
125 sysinfo_item_t *cur = subtree;
126
127 /* Walk all siblings */
128 while (cur != NULL) {
129 size_t i = 0;
130
131 /* Compare name with path */
132 while ((cur->name[i] != 0) && (name[i] == cur->name[i]))
133 i++;
134
135 /* Check for perfect name and path match */
136 if ((name[i] == 0) && (cur->name[i] == 0))
137 return cur;
138
139 /* Partial match up to the delimiter */
140 if ((name[i] == '.') && (cur->name[i] == 0)) {
141 /* Look into the subtree */
142 switch (cur->subtree_type) {
143 case SYSINFO_SUBTREE_TABLE:
144 /* Recursively find in subtree */
145 return sysinfo_find_item(name + i + 1,
146 cur->subtree.table, ret);
147 case SYSINFO_SUBTREE_FUNCTION:
148 /* Get generated data */
149 **ret = cur->subtree.get_data(name + i + 1);
150 return NULL;
151 default:
152 /* Not found, no data generated */
153 *ret = NULL;
154 return NULL;
155 }
156 }
157
158 cur = cur->next;
159 }
160
161 /* Not found, no data generated */
162 *ret = NULL;
163 return NULL;
164}
165
166/** Recursively create items in sysinfo tree
167 *
168 * Should be called with interrupts disabled
169 * and sysinfo_lock held.
170 *
171 * @param name Current sysinfo path suffix.
172 * @param psubtree Pointer to an already existing (sub)tree root
173 * item or where to store a new tree root item.
174 *
175 * @return Existing or newly allocated sysinfo item or NULL
176 * if the current tree configuration does not allow to
177 * create a new item.
178 *
179 */
180static sysinfo_item_t *sysinfo_create_path(const char *name,
181 sysinfo_item_t **psubtree)
182{
183 ASSERT(psubtree != NULL);
184
185 if (*psubtree == NULL) {
186 /* No parent */
187
188 size_t i = 0;
189
190 /* Find the first delimiter in name */
191 while ((name[i] != 0) && (name[i] != '.'))
192 i++;
193
194 *psubtree =
195 (sysinfo_item_t *) slab_alloc(sysinfo_item_slab, 0);
196 ASSERT(*psubtree);
197
198 /* Fill in item name up to the delimiter */
199 (*psubtree)->name = str_ndup(name, i);
200 ASSERT((*psubtree)->name);
201
202 /* Create subtree items */
203 if (name[i] == '.') {
204 (*psubtree)->subtree_type = SYSINFO_SUBTREE_TABLE;
205 return sysinfo_create_path(name + i + 1,
206 &((*psubtree)->subtree.table));
207 }
208
209 /* No subtree needs to be created */
210 return *psubtree;
211 }
212
213 sysinfo_item_t *cur = *psubtree;
214
215 /* Walk all siblings */
216 while (cur != NULL) {
217 size_t i = 0;
218
219 /* Compare name with path */
220 while ((cur->name[i] != 0) && (name[i] == cur->name[i]))
221 i++;
222
223 /* Check for perfect name and path match
224 * -> item is already present.
225 */
226 if ((name[i] == 0) && (cur->name[i] == 0))
227 return cur;
228
229 /* Partial match up to the delimiter */
230 if ((name[i] == '.') && (cur->name[i] == 0)) {
231 switch (cur->subtree_type) {
232 case SYSINFO_SUBTREE_NONE:
233 /* No subtree yet, create one */
234 cur->subtree_type = SYSINFO_SUBTREE_TABLE;
235 return sysinfo_create_path(name + i + 1,
236 &(cur->subtree.table));
237 case SYSINFO_SUBTREE_TABLE:
238 /* Subtree already created, add new sibling */
239 return sysinfo_create_path(name + i + 1,
240 &(cur->subtree.table));
241 default:
242 /* Subtree items handled by a function, this
243 * cannot be overriden by a constant item.
244 */
245 return NULL;
246 }
247 }
248
249 /* No match and no more siblings to check
250 * -> create a new sibling item.
251 */
252 if (cur->next == NULL) {
253 /* Find the first delimiter in name */
254 i = 0;
255 while ((name[i] != 0) && (name[i] != '.'))
256 i++;
257
258 sysinfo_item_t *item =
259 (sysinfo_item_t *) slab_alloc(sysinfo_item_slab, 0);
260 ASSERT(item);
261
262 cur->next = item;
263
264 /* Fill in item name up to the delimiter */
265 item->name = str_ndup(name, i);
266 ASSERT(item->name);
267
268 /* Create subtree items */
269 if (name[i] == '.') {
270 item->subtree_type = SYSINFO_SUBTREE_TABLE;
271 return sysinfo_create_path(name + i + 1,
272 &(item->subtree.table));
273 }
274
275 /* No subtree needs to be created */
276 return item;
277 }
278
279 cur = cur->next;
280 }
281
282 /* Unreachable */
283 ASSERT(false);
284 return NULL;
285}
286
287/** Set sysinfo item with a constant numeric value
288 *
289 * @param name Sysinfo path.
290 * @param root Pointer to the root item or where to store
291 * a new root item (NULL for global sysinfo root).
292 * @param val Value to store in the item.
293 *
294 */
295void sysinfo_set_item_val(const char *name, sysinfo_item_t **root,
296 unative_t val)
297{
298 /* Protect sysinfo tree consistency */
299 ipl_t ipl = interrupts_disable();
300 spinlock_lock(&sysinfo_lock);
301
302 if (root == NULL)
303 root = &global_root;
304
305 sysinfo_item_t *item = sysinfo_create_path(name, root);
306 if (item != NULL) {
307 item->val_type = SYSINFO_VAL_VAL;
308 item->val.val = val;
309 }
310
311 spinlock_unlock(&sysinfo_lock);
312 interrupts_restore(ipl);
313}
314
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 */
328void sysinfo_set_item_data(const char *name, sysinfo_item_t **root,
329 void *data, size_t size)
330{
331 /* Protect sysinfo tree consistency */
332 ipl_t ipl = interrupts_disable();
333 spinlock_lock(&sysinfo_lock);
334
335 if (root == NULL)
336 root = &global_root;
337
338 sysinfo_item_t *item = sysinfo_create_path(name, root);
339 if (item != NULL) {
340 item->val_type = SYSINFO_VAL_DATA;
341 item->val.data.data = data;
342 item->val.data.size = size;
343 }
344
345 spinlock_unlock(&sysinfo_lock);
346 interrupts_restore(ipl);
347}
348
349/** Set sysinfo item with a generated numeric value
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 fn Numeric value generator function.
355 *
356 */
357void sysinfo_set_item_fn_val(const char *name, sysinfo_item_t **root,
358 sysinfo_fn_val_t fn)
359{
360 /* Protect sysinfo tree consistency */
361 ipl_t ipl = interrupts_disable();
362 spinlock_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_FUNCTION_VAL;
370 item->val.fn_val = fn;
371 }
372
373 spinlock_unlock(&sysinfo_lock);
374 interrupts_restore(ipl);
375}
376
377/** Set sysinfo item with a generated binary data
378 *
379 * Note that each time the generator function is called
380 * it is supposed to return a new dynamically allocated
381 * data. This data is then freed by sysinfo in the context
382 * of the current sysinfo request.
383 *
384 * @param name Sysinfo path.
385 * @param root Pointer to the root item or where to store
386 * a new root item (NULL for global sysinfo root).
387 * @param fn Binary data generator function.
388 *
389 */
390void sysinfo_set_item_fn_data(const char *name, sysinfo_item_t **root,
391 sysinfo_fn_data_t fn)
392{
393 /* Protect sysinfo tree consistency */
394 ipl_t ipl = interrupts_disable();
395 spinlock_lock(&sysinfo_lock);
396
397 if (root == NULL)
398 root = &global_root;
399
400 sysinfo_item_t *item = sysinfo_create_path(name, root);
401 if (item != NULL) {
402 item->val_type = SYSINFO_VAL_FUNCTION_DATA;
403 item->val.fn_data = fn;
404 }
405
406 spinlock_unlock(&sysinfo_lock);
407 interrupts_restore(ipl);
408}
409
410/** Set sysinfo item with an undefined value
411 *
412 * @param name Sysinfo path.
413 * @param root Pointer to the root item or where to store
414 * a new root item (NULL for global sysinfo root).
415 *
416 */
417void sysinfo_set_item_undefined(const char *name, sysinfo_item_t **root)
418{
419 /* Protect sysinfo tree consistency */
420 ipl_t ipl = interrupts_disable();
421 spinlock_lock(&sysinfo_lock);
422
423 if (root == NULL)
424 root = &global_root;
425
426 sysinfo_item_t *item = sysinfo_create_path(name, root);
427 if (item != NULL)
428 item->val_type = SYSINFO_VAL_UNDEFINED;
429
430 spinlock_unlock(&sysinfo_lock);
431 interrupts_restore(ipl);
432}
433
434/** Set sysinfo item with a generated subtree
435 *
436 * @param name Sysinfo path.
437 * @param root Pointer to the root item or where to store
438 * a new root item (NULL for global sysinfo root).
439 * @param fn Subtree generator function.
440 *
441 */
442void sysinfo_set_subtree_fn(const char *name, sysinfo_item_t **root,
443 sysinfo_fn_subtree_t fn)
444{
445 /* Protect sysinfo tree consistency */
446 ipl_t ipl = interrupts_disable();
447 spinlock_lock(&sysinfo_lock);
448
449 if (root == NULL)
450 root = &global_root;
451
452 sysinfo_item_t *item = sysinfo_create_path(name, root);
453
454 /* Change the type of the subtree only if it is not already
455 a fixed subtree */
456 if ((item != NULL) && (item->subtree_type != SYSINFO_SUBTREE_TABLE)) {
457 item->subtree_type = SYSINFO_SUBTREE_FUNCTION;
458 item->subtree.get_data = fn;
459 }
460
461 spinlock_unlock(&sysinfo_lock);
462 interrupts_restore(ipl);
463}
464
465/** Sysinfo dump indentation helper routine
466 *
467 * @param depth Number of indentation characters to print.
468 *
469 */
470static void sysinfo_indent(unsigned int depth)
471{
472 unsigned int i;
473 for (i = 0; i < depth; i++)
474 printf(" ");
475}
476
477/** Dump the structure of sysinfo tree
478 *
479 * Should be called with interrupts disabled
480 * and sysinfo_lock held. Because this routine
481 * might take a reasonable long time to proceed,
482 * having the spinlock held is not optimal, but
483 * there is no better simple solution.
484 *
485 * @param root Root item of the current (sub)tree.
486 * @param depth Current depth in the sysinfo tree.
487 *
488 */
489static void sysinfo_dump_internal(sysinfo_item_t *root, unsigned int depth)
490{
491 sysinfo_item_t *cur = root;
492
493 /* Walk all siblings */
494 while (cur != NULL) {
495 sysinfo_indent(depth);
496
497 unative_t val;
498 size_t size;
499
500 /* Display node value and type */
501 switch (cur->val_type) {
502 case SYSINFO_VAL_UNDEFINED:
503 printf("+ %s\n", cur->name);
504 break;
505 case SYSINFO_VAL_VAL:
506 printf("+ %s -> %" PRIun" (%#" PRIxn ")\n", cur->name,
507 cur->val.val, cur->val.val);
508 break;
509 case SYSINFO_VAL_DATA:
510 printf("+ %s (%" PRIs" bytes)\n", cur->name,
511 cur->val.data.size);
512 break;
513 case SYSINFO_VAL_FUNCTION_VAL:
514 val = cur->val.fn_val(cur);
515 printf("+ %s -> %" PRIun" (%#" PRIxn ") [generated]\n",
516 cur->name, val, val);
517 break;
518 case SYSINFO_VAL_FUNCTION_DATA:
519 /* N.B.: No data was actually returned (only a dry run) */
520 (void) cur->val.fn_data(cur, &size, true);
521 printf("+ %s (%" PRIs" bytes) [generated]\n", cur->name,
522 size);
523 break;
524 default:
525 printf("+ %s [unknown]\n", cur->name);
526 }
527
528 /* Recursivelly nest into the subtree */
529 switch (cur->subtree_type) {
530 case SYSINFO_SUBTREE_NONE:
531 break;
532 case SYSINFO_SUBTREE_TABLE:
533 sysinfo_dump_internal(cur->subtree.table, depth + 1);
534 break;
535 case SYSINFO_SUBTREE_FUNCTION:
536 sysinfo_indent(depth + 1);
537 printf("+ [generated subtree]\n");
538 break;
539 default:
540 sysinfo_indent(depth + 1);
541 printf("+ [unknown subtree]\n");
542 }
543
544 cur = cur->next;
545 }
546}
547
548/** Dump the structure of sysinfo tree
549 *
550 * @param root Root item of the sysinfo (sub)tree.
551 * If it is NULL then consider the global
552 * sysinfo tree.
553 *
554 */
555void sysinfo_dump(sysinfo_item_t *root)
556{
557 /* Avoid other functions to mess with sysinfo
558 while we are dumping it */
559 ipl_t ipl = interrupts_disable();
560 spinlock_lock(&sysinfo_lock);
561
562 if (root == NULL)
563 sysinfo_dump_internal(global_root, 0);
564 else
565 sysinfo_dump_internal(root, 0);
566
567 spinlock_unlock(&sysinfo_lock);
568 interrupts_restore(ipl);
569}
570
571/** Return sysinfo item value determined by name
572 *
573 * Should be called with interrupts disabled
574 * and sysinfo_lock held.
575 *
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.
582 *
583 * @return Item value (constant or generated).
584 *
585 */
586static sysinfo_return_t sysinfo_get_item(const char *name,
587 sysinfo_item_t **root, bool dry_run)
588{
589 if (root == NULL)
590 root = &global_root;
591
592 /* Try to find the item or generate data */
593 sysinfo_return_t ret;
594 sysinfo_return_t *ret_ptr = &ret;
595 sysinfo_item_t *item = sysinfo_find_item(name, *root, &ret_ptr);
596
597 if (item != NULL) {
598 /* Item found in the fixed sysinfo tree */
599
600 ret.tag = item->val_type;
601 switch (item->val_type) {
602 case SYSINFO_VAL_UNDEFINED:
603 break;
604 case SYSINFO_VAL_VAL:
605 ret.val = item->val.val;
606 break;
607 case SYSINFO_VAL_DATA:
608 ret.data = item->val.data;
609 break;
610 case SYSINFO_VAL_FUNCTION_VAL:
611 ret.val = item->val.fn_val(item);
612 break;
613 case SYSINFO_VAL_FUNCTION_DATA:
614 ret.data.data = item->val.fn_data(item, &ret.data.size,
615 dry_run);
616 break;
617 }
618 } else {
619 /* No item in the fixed sysinfo tree */
620 if (ret_ptr == NULL) {
621 /* Even no data was generated */
622 ret.tag = SYSINFO_VAL_UNDEFINED;
623 }
624 }
625
626 return ret;
627}
628
629/** Return sysinfo item determined by name from user space
630 *
631 * Should be called with interrupts disabled
632 * and sysinfo_lock held. The path string passed from
633 * the user space has to be properly null-terminated
634 * (the last passed character must be null).
635 *
636 * @param ptr Sysinfo path in the user address space.
637 * @param size Size of the path string.
638 * @param dry_run Do not actually get any generated
639 * binary data, just calculate the size.
640 *
641 */
642static sysinfo_return_t sysinfo_get_item_uspace(void *ptr, size_t size,
643 bool dry_run)
644{
645 sysinfo_return_t ret;
646 ret.tag = SYSINFO_VAL_UNDEFINED;
647
648 if (size > SYSINFO_MAX_PATH)
649 return ret;
650
651 char *path = (char *) malloc(size + 1, 0);
652 ASSERT(path);
653
654 if ((copy_from_uspace(path, ptr, size + 1) == 0)
655 && (path[size] == 0))
656 ret = sysinfo_get_item(path, NULL, dry_run);
657
658 free(path);
659 return ret;
660}
661
662/** Get the sysinfo value type (syscall)
663 *
664 * The path string passed from the user space has
665 * to be properly null-terminated (the last passed
666 * character must be null).
667 *
668 * @param path_ptr Sysinfo path in the user address space.
669 * @param path_size Size of the path string.
670 *
671 * @return Item value type.
672 *
673 */
674unative_t sys_sysinfo_get_tag(void *path_ptr, size_t path_size)
675{
676 /* Avoid other functions to mess with sysinfo
677 while we are reading it */
678 ipl_t ipl = interrupts_disable();
679 spinlock_lock(&sysinfo_lock);
680
681 /* Get the item.
682
683 N.B.: There is no need to free any potential generated
684 binary data since we request a dry run */
685 sysinfo_return_t ret = sysinfo_get_item_uspace(path_ptr, path_size, true);
686
687 /* Map generated value types to constant types
688 (user space does not care whether the
689 value is constant or generated) */
690 if (ret.tag == SYSINFO_VAL_FUNCTION_VAL)
691 ret.tag = SYSINFO_VAL_VAL;
692 else if (ret.tag == SYSINFO_VAL_FUNCTION_DATA)
693 ret.tag = SYSINFO_VAL_DATA;
694
695 spinlock_unlock(&sysinfo_lock);
696 interrupts_restore(ipl);
697
698 return (unative_t) ret.tag;
699}
700
701/** Get the sysinfo numerical value (syscall)
702 *
703 * The path string passed from the user space has
704 * to be properly null-terminated (the last passed
705 * character must be null).
706 *
707 * @param path_ptr Sysinfo path in the user address space.
708 * @param path_size Size of the path string.
709 * @param value_ptr User space pointer where to store the
710 * numberical value.
711 *
712 * @return Error code (EOK in case of no error).
713 *
714 */
715unative_t sys_sysinfo_get_value(void *path_ptr, size_t path_size,
716 void *value_ptr)
717{
718 /* Avoid other functions to mess with sysinfo
719 while we are reading it */
720 ipl_t ipl = interrupts_disable();
721 spinlock_lock(&sysinfo_lock);
722
723 /* Get the item.
724
725 N.B.: There is no need to free any potential generated
726 binary data since we request a dry run */
727 sysinfo_return_t ret = sysinfo_get_item_uspace(path_ptr, path_size, true);
728 int rc;
729
730 /* Only constant or generated numerical value is returned */
731 if ((ret.tag == SYSINFO_VAL_VAL) || (ret.tag == SYSINFO_VAL_FUNCTION_VAL))
732 rc = copy_to_uspace(value_ptr, &ret.val, sizeof(ret.val));
733 else
734 rc = EINVAL;
735
736 spinlock_unlock(&sysinfo_lock);
737 interrupts_restore(ipl);
738
739 return (unative_t) rc;
740}
741
742/** Get the sysinfo binary data size (syscall)
743 *
744 * The path string passed from the user space has
745 * to be properly null-terminated (the last passed
746 * character must be null).
747 *
748 * @param path_ptr Sysinfo path in the user address space.
749 * @param path_size Size of the path string.
750 * @param size_ptr User space pointer where to store the
751 * binary data size.
752 *
753 * @return Error code (EOK in case of no error).
754 *
755 */
756unative_t sys_sysinfo_get_data_size(void *path_ptr, size_t path_size,
757 void *size_ptr)
758{
759 /* Avoid other functions to mess with sysinfo
760 while we are reading it */
761 ipl_t ipl = interrupts_disable();
762 spinlock_lock(&sysinfo_lock);
763
764 /* Get the item.
765
766 N.B.: There is no need to free any potential generated
767 binary data since we request a dry run */
768 sysinfo_return_t ret = sysinfo_get_item_uspace(path_ptr, path_size, true);
769 int rc;
770
771 /* Only the size of constant or generated binary data is considered */
772 if ((ret.tag == SYSINFO_VAL_DATA) || (ret.tag == SYSINFO_VAL_FUNCTION_DATA))
773 rc = copy_to_uspace(size_ptr, &ret.data.size,
774 sizeof(ret.data.size));
775 else
776 rc = EINVAL;
777
778 spinlock_unlock(&sysinfo_lock);
779 interrupts_restore(ipl);
780
781 return (unative_t) rc;
782}
783
784/** Get the sysinfo binary data (syscall)
785 *
786 * The path string passed from the user space has
787 * to be properly null-terminated (the last passed
788 * character must be null).
789 *
790 * The user space buffer must be sized exactly according
791 * to the size of the binary data, otherwise the request
792 * fails.
793 *
794 * @param path_ptr Sysinfo path in the user address space.
795 * @param path_size Size of the path string.
796 * @param buffer_ptr User space pointer to the buffer where
797 * to store the binary data.
798 * @param buffer_size User space buffer size.
799 *
800 * @return Error code (EOK in case of no error).
801 *
802 */
803unative_t sys_sysinfo_get_data(void *path_ptr, size_t path_size,
804 void *buffer_ptr, size_t buffer_size)
805{
806 /* Avoid other functions to mess with sysinfo
807 while we are reading it */
808 ipl_t ipl = interrupts_disable();
809 spinlock_lock(&sysinfo_lock);
810
811 /* Get the item */
812 sysinfo_return_t ret = sysinfo_get_item_uspace(path_ptr, path_size, false);
813 int rc;
814
815 /* Only constant or generated binary data is considered */
816 if ((ret.tag == SYSINFO_VAL_DATA) || (ret.tag == SYSINFO_VAL_FUNCTION_DATA)) {
817 /* Check destination buffer size */
818 if (ret.data.size == buffer_size)
819 rc = copy_to_uspace(buffer_ptr, ret.data.data,
820 ret.data.size);
821 else
822 rc = ENOMEM;
823 } else
824 rc = EINVAL;
825
826 /* N.B.: The generated binary data should be freed */
827 if ((ret.tag == SYSINFO_VAL_FUNCTION_DATA) && (ret.data.data != NULL))
828 free(ret.data.data);
829
830 spinlock_unlock(&sysinfo_lock);
831 interrupts_restore(ipl);
832
833 return (unative_t) rc;
834}
835
836/** @}
837 */
Note: See TracBrowser for help on using the repository browser.