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

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

sysinfo overhaul

  • cleanup (nicer data structures, use of SLAB allocator)
  • add support for storing arbitrary binary data
  • properly reimplement non-constant values (generated by functions)
  • add support for non-constant subtrees (generated by functions)
  • syscall ABI change, libc API change
  • reflect changes in user code

libc: task_spawn() can now return error code

  • reflect change in user code, print error strings after failed task_spawn()

uspace cleanup

  • more use of string and other constants
  • more use of str_error()
  • unify error reporting in init
  • Property mode set to 100644
File size: 10.7 KB
RevLine 
[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>
[d9fae235]39#include <errno.h>
40
41#define SYSINFO_MAX_PATH 2048
[2666daa]42
[a71c158]43bool fb_exported = false;
[2666daa]44
[d9fae235]45static sysinfo_item_t *global_root = NULL;
46static slab_cache_t *sysinfo_item_slab;
47
48static int sysinfo_item_constructor(void *obj, int kmflag)
49{
50 sysinfo_item_t *item = (sysinfo_item_t *) obj;
51
52 item->name = NULL;
53 item->val_type = SYSINFO_VAL_UNDEFINED;
54 item->subtree_type = SYSINFO_SUBTREE_NONE;
55 item->next = NULL;
56
57 return 0;
58}
59
60static int sysinfo_item_destructor(void *obj)
61{
62 sysinfo_item_t *item = (sysinfo_item_t *) obj;
63
64 if (item->name != NULL)
65 free(item->name);
66
67 return 0;
68}
69
70void sysinfo_init(void)
71{
72 sysinfo_item_slab = slab_cache_create("sysinfo_item_slab",
73 sizeof(sysinfo_item_t), 0, sysinfo_item_constructor,
74 sysinfo_item_destructor, SLAB_CACHE_MAGDEFERRED);
75}
76
77static sysinfo_item_t *sysinfo_find_item(const char *name,
78 sysinfo_item_t *subtree)
[2666daa]79{
[d9fae235]80 sysinfo_item_t *cur = subtree;
[749122b]81
[d9fae235]82 while (cur != NULL) {
83 size_t i = 0;
[749122b]84
[d9fae235]85 /* Compare name with path */
86 while ((cur->name[i] != 0) && (name[i] == cur->name[i]))
[749122b]87 i++;
88
[d9fae235]89 /* Check for perfect name and path match */
90 if ((name[i] == 0) && (cur->name[i] == 0))
91 return cur;
[749122b]92
[d9fae235]93 /* Partial match up to the delimiter */
94 if ((name[i] == '.') && (cur->name[i] == 0)) {
95 /* Look into the subtree */
96 switch (cur->subtree_type) {
97 case SYSINFO_SUBTREE_TABLE:
98 /* Recursively find in subtree */
99 return sysinfo_find_item(name + i + 1, cur->subtree.table);
100 case SYSINFO_SUBTREE_FUNCTION:
101 /* Get generated item */
102 return cur->subtree.find_item(name + i + 1);
103 default:
104 /* Not found */
105 return NULL;
106 }
[2666daa]107 }
[d9fae235]108
109 cur = cur->next;
[2666daa]110 }
[d9fae235]111
[2666daa]112 return NULL;
113}
114
[d9fae235]115static sysinfo_item_t *sysinfo_create_path(const char *name,
116 sysinfo_item_t **psubtree)
[2666daa]117{
[d9fae235]118 if (*psubtree == NULL) {
119 /* No parent */
120
121 size_t i = 0;
122
123 /* Find the first delimiter in name */
124 while ((name[i] != 0) && (name[i] != '.'))
[7bb6b06]125 i++;
[d9fae235]126
127 *psubtree =
128 (sysinfo_item_t *) slab_alloc(sysinfo_item_slab, 0);
129 ASSERT(*psubtree);
130
131 /* Fill in item name up to the delimiter */
132 (*psubtree)->name = str_ndup(name, i);
133 ASSERT((*psubtree)->name);
134
135 /* Create subtree items */
136 if (name[i] == '.') {
137 (*psubtree)->subtree_type = SYSINFO_SUBTREE_TABLE;
138 return sysinfo_create_path(name + i + 1,
139 &((*psubtree)->subtree.table));
[7bb6b06]140 }
[d9fae235]141
142 /* No subtree needs to be created */
143 return *psubtree;
[2666daa]144 }
[d9fae235]145
146 sysinfo_item_t *cur = *psubtree;
147
148 while (cur != NULL) {
149 size_t i = 0;
[749122b]150
[d9fae235]151 /* Compare name with path */
152 while ((cur->name[i] != 0) && (name[i] == cur->name[i]))
[749122b]153 i++;
154
[d9fae235]155 /* Check for perfect name and path match
156 * -> item is already present.
157 */
158 if ((name[i] == 0) && (cur->name[i] == 0))
159 return cur;
[749122b]160
[d9fae235]161 /* Partial match up to the delimiter */
162 if ((name[i] == '.') && (cur->name[i] == 0)) {
163 switch (cur->subtree_type) {
164 case SYSINFO_SUBTREE_NONE:
165 /* No subtree yet, create one */
166 cur->subtree_type = SYSINFO_SUBTREE_TABLE;
167 return sysinfo_create_path(name + i + 1,
168 &(cur->subtree.table));
169 case SYSINFO_SUBTREE_TABLE:
170 /* Subtree already created, add new sibling */
171 return sysinfo_create_path(name + i + 1,
172 &(cur->subtree.table));
173 default:
174 /* Subtree items handled by a function, this
175 * cannot be overriden.
176 */
177 return NULL;
[749122b]178 }
[2666daa]179 }
[d9fae235]180
181 /* No match and no more siblings to check
182 * -> create a new sibling item.
183 */
184 if (cur->next == NULL) {
185 /* Find the first delimiter in name */
[749122b]186 i = 0;
[d9fae235]187 while ((name[i] != 0) && (name[i] != '.'))
[749122b]188 i++;
189
[d9fae235]190 sysinfo_item_t *item =
191 (sysinfo_item_t *) slab_alloc(sysinfo_item_slab, 0);
192 ASSERT(item);
[749122b]193
[d9fae235]194 cur->next = item;
195
196 /* Fill in item name up to the delimiter */
197 item->name = str_ndup(name, i);
198 ASSERT(item->name);
199
200 /* Create subtree items */
201 if (name[i] == '.') {
202 item->subtree_type = SYSINFO_SUBTREE_TABLE;
203 return sysinfo_create_path(name + i + 1,
204 &(item->subtree.table));
[2666daa]205 }
[d9fae235]206
207 /* No subtree needs to be created */
[2666daa]208 return item;
[d9fae235]209 }
210
211 /* Get next sibling */
212 cur = cur->next;
[2666daa]213 }
[d9fae235]214
215 /* Unreachable */
216 ASSERT(false);
[2666daa]217 return NULL;
218}
219
[d9fae235]220void sysinfo_set_item_val(const char *name, sysinfo_item_t **root,
221 unative_t val)
[2666daa]222{
[749122b]223 if (root == NULL)
[d9fae235]224 root = &global_root;
[749122b]225
226 sysinfo_item_t *item = sysinfo_create_path(name, root);
[d9fae235]227 if (item != NULL) {
[749122b]228 item->val_type = SYSINFO_VAL_VAL;
[d9fae235]229 item->val.val = val;
[749122b]230 }
[2666daa]231}
232
[d9fae235]233void sysinfo_set_item_data(const char *name, sysinfo_item_t **root,
234 void *data, size_t size)
[2666daa]235{
[749122b]236 if (root == NULL)
[d9fae235]237 root = &global_root;
[749122b]238
239 sysinfo_item_t *item = sysinfo_create_path(name, root);
[d9fae235]240 if (item != NULL) {
241 item->val_type = SYSINFO_VAL_DATA;
242 item->val.data.data = data;
243 item->val.data.size = size;
244 }
245}
246
247void sysinfo_set_item_val_fn(const char *name, sysinfo_item_t **root,
248 sysinfo_fn_val_t fn)
249{
250 if (root == NULL)
251 root = &global_root;
[749122b]252
[d9fae235]253 sysinfo_item_t *item = sysinfo_create_path(name, root);
254 if (item != NULL) {
255 item->val_type = SYSINFO_VAL_FUNCTION_VAL;
256 item->val.fn_val = fn;
[749122b]257 }
[2666daa]258}
259
[d9fae235]260void sysinfo_set_item_data_fn(const char *name, sysinfo_item_t **root,
261 sysinfo_fn_data_t fn)
262{
263 if (root == NULL)
264 root = &global_root;
265
266 sysinfo_item_t *item = sysinfo_create_path(name, root);
267 if (item != NULL) {
268 item->val_type = SYSINFO_VAL_FUNCTION_DATA;
269 item->val.fn_data = fn;
270 }
271}
[2666daa]272
[749122b]273void sysinfo_set_item_undefined(const char *name, sysinfo_item_t **root)
[2666daa]274{
[749122b]275 if (root == NULL)
[d9fae235]276 root = &global_root;
[749122b]277
278 sysinfo_item_t *item = sysinfo_create_path(name, root);
279 if (item != NULL)
280 item->val_type = SYSINFO_VAL_UNDEFINED;
[2666daa]281}
282
[d9fae235]283static void sysinfo_indent(unsigned int depth)
284{
285 unsigned int i;
286 for (i = 0; i < depth; i++)
287 printf(" ");
288}
[2666daa]289
[d9fae235]290void sysinfo_dump(sysinfo_item_t **proot, unsigned int depth)
[2666daa]291{
[749122b]292 if (proot == NULL)
[d9fae235]293 proot = &global_root;
[749122b]294
[d9fae235]295 sysinfo_item_t *cur = *proot;
[2666daa]296
[d9fae235]297 while (cur != NULL) {
298 sysinfo_indent(depth);
[2666daa]299
[d9fae235]300 unative_t val;
301 size_t size;
[2666daa]302
[d9fae235]303 switch (cur->val_type) {
[f8ddd17]304 case SYSINFO_VAL_UNDEFINED:
[d9fae235]305 printf("+ %s\n", cur->name);
[f8ddd17]306 break;
307 case SYSINFO_VAL_VAL:
[d9fae235]308 printf("+ %s -> %" PRIun" (%#" PRIxn ")\n", cur->name,
309 cur->val.val, cur->val.val);
[f8ddd17]310 break;
[d9fae235]311 case SYSINFO_VAL_DATA:
312 printf("+ %s (%" PRIs" bytes)\n", cur->name,
313 cur->val.data.size);
[f8ddd17]314 break;
[d9fae235]315 case SYSINFO_VAL_FUNCTION_VAL:
316 val = cur->val.fn_val(cur);
317 printf("+ %s -> %" PRIun" (%#" PRIxn ") [generated]\n",
318 cur->name, val, val);
319 break;
320 case SYSINFO_VAL_FUNCTION_DATA:
321 cur->val.fn_data(cur, &size);
322 printf("+ %s (%" PRIs" bytes) [generated]\n", cur->name,
323 size);
324 break;
325 default:
326 printf("+ %s [unknown]\n", cur->name);
[749122b]327 }
[2666daa]328
[d9fae235]329 switch (cur->subtree_type) {
330 case SYSINFO_SUBTREE_NONE:
331 break;
332 case SYSINFO_SUBTREE_TABLE:
333 sysinfo_dump(&(cur->subtree.table), depth + 1);
334 break;
335 case SYSINFO_SUBTREE_FUNCTION:
336 sysinfo_indent(depth + 1);
337 printf(" [generated subtree]\n");
338 break;
339 default:
340 sysinfo_indent(depth + 1);
341 printf(" [unknown subtree]\n");
342 }
[2666daa]343
[d9fae235]344 cur = cur->next;
[749122b]345 }
[2666daa]346}
347
[d9fae235]348sysinfo_return_t sysinfo_get_item(const char *name, sysinfo_item_t **root)
[2666daa]349{
[749122b]350 if (root == NULL)
[d9fae235]351 root = &global_root;
[749122b]352
353 sysinfo_item_t *item = sysinfo_find_item(name, *root);
[d9fae235]354 sysinfo_return_t ret;
[749122b]355
356 if (item != NULL) {
[d9fae235]357 switch (item->val_type) {
358 case SYSINFO_VAL_UNDEFINED:
359 ret.tag = SYSINFO_VAL_UNDEFINED;
360 break;
361 case SYSINFO_VAL_VAL:
362 ret.tag = SYSINFO_VAL_VAL;
[749122b]363 ret.val = item->val.val;
[d9fae235]364 break;
365 case SYSINFO_VAL_DATA:
366 ret.tag = SYSINFO_VAL_DATA;
367 ret.data = item->val.data;
368 break;
369 case SYSINFO_VAL_FUNCTION_VAL:
370 ret.tag = SYSINFO_VAL_VAL;
371 ret.val = item->val.fn_val(item);
372 break;
373 case SYSINFO_VAL_FUNCTION_DATA:
374 ret.tag = SYSINFO_VAL_DATA;
375 ret.data.data = item->val.fn_data(item, &ret.data.size);
376 break;
377 }
378 } else
379 ret.tag = SYSINFO_VAL_UNDEFINED;
380
381 return ret;
382}
383
384static sysinfo_return_t sysinfo_get_item_uspace(void *ptr, size_t size)
385{
386 sysinfo_return_t ret;
387 ret.tag = SYSINFO_VAL_UNDEFINED;
388
389 if (size > SYSINFO_MAX_PATH)
390 return ret;
391
392 char *path = (char *) malloc(size + 1, 0);
393 ASSERT(path);
394
395 if ((copy_from_uspace(path, ptr, size + 1) == 0)
396 && (path[size] == 0)) {
397 ret = sysinfo_get_item(path, NULL);
398 free(path);
[2666daa]399 }
[d9fae235]400
[2666daa]401 return ret;
402}
[35a96cf]403
[d9fae235]404unative_t sys_sysinfo_get_tag(void *path_ptr, size_t path_size)
405{
406 return (unative_t) sysinfo_get_item_uspace(path_ptr, path_size).tag;
407}
[5719f6d]408
[d9fae235]409unative_t sys_sysinfo_get_value(void *path_ptr, size_t path_size,
410 void *value_ptr)
[35a96cf]411{
[d9fae235]412 sysinfo_return_t ret = sysinfo_get_item_uspace(path_ptr, path_size);
413
414 if (ret.tag != SYSINFO_VAL_VAL)
415 return (unative_t) EINVAL;
416
417 return (unative_t) copy_to_uspace(value_ptr, &ret.val,
418 sizeof(ret.val));
419}
[5719f6d]420
[d9fae235]421unative_t sys_sysinfo_get_data_size(void *path_ptr, size_t path_size,
422 void *size_ptr)
423{
424 sysinfo_return_t ret = sysinfo_get_item_uspace(path_ptr, path_size);
[749122b]425
[d9fae235]426 if (ret.tag != SYSINFO_VAL_DATA)
427 return (unative_t) EINVAL;
[749122b]428
[d9fae235]429 return (unative_t) copy_to_uspace(size_ptr, &ret.data.size,
430 sizeof(ret.data.size));
[35a96cf]431}
432
[d9fae235]433unative_t sys_sysinfo_get_data(void *path_ptr, size_t path_size,
434 void *buffer_ptr, size_t buffer_size)
[35a96cf]435{
[d9fae235]436 sysinfo_return_t ret = sysinfo_get_item_uspace(path_ptr, path_size);
[5719f6d]437
[d9fae235]438 if (ret.tag != SYSINFO_VAL_DATA)
439 return (unative_t) EINVAL;
[749122b]440
[d9fae235]441 if (ret.data.size != buffer_size)
442 return ENOMEM;
[749122b]443
[d9fae235]444 return (unative_t) copy_to_uspace(buffer_ptr, ret.data.data,
445 ret.data.size);
[35a96cf]446}
[b45c443]447
[cc73a8a1]448/** @}
[b45c443]449 */
Note: See TracBrowser for help on using the repository browser.