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
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 <errno.h>
40
41#define SYSINFO_MAX_PATH 2048
42
43bool fb_exported = false;
44
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)
79{
80 sysinfo_item_t *cur = subtree;
81
82 while (cur != NULL) {
83 size_t i = 0;
84
85 /* Compare name with path */
86 while ((cur->name[i] != 0) && (name[i] == cur->name[i]))
87 i++;
88
89 /* Check for perfect name and path match */
90 if ((name[i] == 0) && (cur->name[i] == 0))
91 return cur;
92
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 }
107 }
108
109 cur = cur->next;
110 }
111
112 return NULL;
113}
114
115static sysinfo_item_t *sysinfo_create_path(const char *name,
116 sysinfo_item_t **psubtree)
117{
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] != '.'))
125 i++;
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));
140 }
141
142 /* No subtree needs to be created */
143 return *psubtree;
144 }
145
146 sysinfo_item_t *cur = *psubtree;
147
148 while (cur != NULL) {
149 size_t i = 0;
150
151 /* Compare name with path */
152 while ((cur->name[i] != 0) && (name[i] == cur->name[i]))
153 i++;
154
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;
160
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;
178 }
179 }
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 */
186 i = 0;
187 while ((name[i] != 0) && (name[i] != '.'))
188 i++;
189
190 sysinfo_item_t *item =
191 (sysinfo_item_t *) slab_alloc(sysinfo_item_slab, 0);
192 ASSERT(item);
193
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));
205 }
206
207 /* No subtree needs to be created */
208 return item;
209 }
210
211 /* Get next sibling */
212 cur = cur->next;
213 }
214
215 /* Unreachable */
216 ASSERT(false);
217 return NULL;
218}
219
220void sysinfo_set_item_val(const char *name, sysinfo_item_t **root,
221 unative_t val)
222{
223 if (root == NULL)
224 root = &global_root;
225
226 sysinfo_item_t *item = sysinfo_create_path(name, root);
227 if (item != NULL) {
228 item->val_type = SYSINFO_VAL_VAL;
229 item->val.val = val;
230 }
231}
232
233void sysinfo_set_item_data(const char *name, sysinfo_item_t **root,
234 void *data, size_t size)
235{
236 if (root == NULL)
237 root = &global_root;
238
239 sysinfo_item_t *item = sysinfo_create_path(name, root);
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;
252
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;
257 }
258}
259
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}
272
273void sysinfo_set_item_undefined(const char *name, sysinfo_item_t **root)
274{
275 if (root == NULL)
276 root = &global_root;
277
278 sysinfo_item_t *item = sysinfo_create_path(name, root);
279 if (item != NULL)
280 item->val_type = SYSINFO_VAL_UNDEFINED;
281}
282
283static void sysinfo_indent(unsigned int depth)
284{
285 unsigned int i;
286 for (i = 0; i < depth; i++)
287 printf(" ");
288}
289
290void sysinfo_dump(sysinfo_item_t **proot, unsigned int depth)
291{
292 if (proot == NULL)
293 proot = &global_root;
294
295 sysinfo_item_t *cur = *proot;
296
297 while (cur != NULL) {
298 sysinfo_indent(depth);
299
300 unative_t val;
301 size_t size;
302
303 switch (cur->val_type) {
304 case SYSINFO_VAL_UNDEFINED:
305 printf("+ %s\n", cur->name);
306 break;
307 case SYSINFO_VAL_VAL:
308 printf("+ %s -> %" PRIun" (%#" PRIxn ")\n", cur->name,
309 cur->val.val, cur->val.val);
310 break;
311 case SYSINFO_VAL_DATA:
312 printf("+ %s (%" PRIs" bytes)\n", cur->name,
313 cur->val.data.size);
314 break;
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);
327 }
328
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 }
343
344 cur = cur->next;
345 }
346}
347
348sysinfo_return_t sysinfo_get_item(const char *name, sysinfo_item_t **root)
349{
350 if (root == NULL)
351 root = &global_root;
352
353 sysinfo_item_t *item = sysinfo_find_item(name, *root);
354 sysinfo_return_t ret;
355
356 if (item != NULL) {
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;
363 ret.val = item->val.val;
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);
399 }
400
401 return ret;
402}
403
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}
408
409unative_t sys_sysinfo_get_value(void *path_ptr, size_t path_size,
410 void *value_ptr)
411{
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}
420
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);
425
426 if (ret.tag != SYSINFO_VAL_DATA)
427 return (unative_t) EINVAL;
428
429 return (unative_t) copy_to_uspace(size_ptr, &ret.data.size,
430 sizeof(ret.data.size));
431}
432
433unative_t sys_sysinfo_get_data(void *path_ptr, size_t path_size,
434 void *buffer_ptr, size_t buffer_size)
435{
436 sysinfo_return_t ret = sysinfo_get_item_uspace(path_ptr, path_size);
437
438 if (ret.tag != SYSINFO_VAL_DATA)
439 return (unative_t) EINVAL;
440
441 if (ret.data.size != buffer_size)
442 return ENOMEM;
443
444 return (unative_t) copy_to_uspace(buffer_ptr, ret.data.data,
445 ret.data.size);
446}
447
448/** @}
449 */
Note: See TracBrowser for help on using the repository browser.