source: mainline/kernel/generic/src/sysinfo/sysinfo.c@ 07640dfd

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

remove unused variables and operations (as suggested by Clang static analyzer)

  • Property mode set to 100644
File size: 7.9 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>
[2666daa]39
[a71c158]40bool fb_exported = false;
[749122b]41sysinfo_item_t *_root = NULL;
[2666daa]42
[749122b]43static sysinfo_item_t *sysinfo_find_item(const char *name, sysinfo_item_t *subtree)
[2666daa]44{
[749122b]45 if (subtree == NULL)
46 return NULL;
47
[137691a]48 while (subtree != NULL) {
[749122b]49 int i = 0;
50 char *a = (char *) name;
51 char *b = subtree->name;
52
53 while ((a[i] == b[i]) && (b[i]))
54 i++;
55
56 if ((!a[i]) && (!b[i])) /* Last name in path matches */
57 return subtree;
58
59 if ((a[i] == '.') && (!b[i])) { /* Middle name in path matches */
60 if (subtree->subinfo_type == SYSINFO_SUBINFO_TABLE)
61 return sysinfo_find_item(a + i + 1, subtree->subinfo.table);
62
63 //if (subtree->subinfo_type == SYSINFO_SUBINFO_FUNCTION) /* Subinfo managed by subsystem */
64 // return NULL;
65
66 return NULL; /* No subinfo */
[2666daa]67 }
[749122b]68 /* No matches try next */
69 subtree = subtree->next;
[2666daa]70 }
71 return NULL;
72}
73
[749122b]74static sysinfo_item_t *sysinfo_create_path(const char *name, sysinfo_item_t **psubtree)
[2666daa]75{
76 sysinfo_item_t *subtree;
77 subtree = *psubtree;
78
[749122b]79 if (subtree == NULL) {
[7bb6b06]80 sysinfo_item_t *item = malloc(sizeof(sysinfo_item_t), 0);
81 int i = 0, j;
[2666daa]82
[7bb6b06]83 ASSERT(item);
84 *psubtree = item;
85 item->next = NULL;
86 item->val_type = SYSINFO_VAL_UNDEFINED;
87 item->subinfo.table = NULL;
[2666daa]88
[7bb6b06]89 while (name[i] && (name[i] != '.'))
90 i++;
[2666daa]91
[7bb6b06]92 item->name = malloc(i, 0);
93 ASSERT(item->name);
[2666daa]94
[7bb6b06]95 for (j = 0; j < i; j++)
96 item->name[j] = name[j];
97 item->name[j] = 0;
[2666daa]98
[7bb6b06]99 if (name[i]) { /* =='.' */
100 item->subinfo_type = SYSINFO_SUBINFO_TABLE;
101 return sysinfo_create_path(name + i + 1, &(item->subinfo.table));
102 }
103 item->subinfo_type = SYSINFO_SUBINFO_NONE;
104 return item;
[2666daa]105 }
106
[749122b]107 while (subtree != NULL) {
108 int i = 0, j;
109 char *a = (char *) name;
110 char *b = subtree->name;
111
112 while ((a[i] == b[i]) && (b[i]))
113 i++;
114
115 if ((!a[i]) && (!b[i])) /* Last name in path matches */
116 return subtree;
117
118 if ((a[i] == '.') && (!b[i])) { /* Middle name in path matches */
119 if (subtree->subinfo_type == SYSINFO_SUBINFO_TABLE)
120 return sysinfo_create_path(a + i + 1, &(subtree->subinfo.table));
121
122 if (subtree->subinfo_type == SYSINFO_SUBINFO_NONE) {
123 subtree->subinfo_type = SYSINFO_SUBINFO_TABLE;
124 return sysinfo_create_path(a + i + 1,&(subtree->subinfo.table));
125 }
126
127 //if (subtree->subinfo_type == SYSINFO_SUBINFO_FUNCTION) /* Subinfo managed by subsystem */
128 // return NULL;
129
[2666daa]130 return NULL;
131 }
132 /* No matches try next or create new*/
[749122b]133 if (subtree->next == NULL) {
134 sysinfo_item_t *item = malloc(sizeof(sysinfo_item_t), 0);
[2666daa]135
136 ASSERT(item);
[749122b]137 subtree->next = item;
138 item->next = NULL;
139 item->val_type = SYSINFO_VAL_UNDEFINED;
140 item->subinfo.table = NULL;
[2666daa]141
[749122b]142 i = 0;
143 while (name[i] && (name[i] != '.'))
144 i++;
[2666daa]145
[749122b]146 item->name = malloc(i, 0);
147 ASSERT(item->name);
148
149 for (j = 0; j < i; j++)
150 item->name[j] = name[j];
151
152 item->name[j] = 0;
[2666daa]153
[749122b]154 if(name[i]) { /* =='.' */
155 item->subinfo_type = SYSINFO_SUBINFO_TABLE;
156 return sysinfo_create_path(name + i + 1, &(item->subinfo.table));
[2666daa]157 }
[749122b]158 item->subinfo_type = SYSINFO_SUBINFO_NONE;
[2666daa]159 return item;
[137691a]160 } else
[749122b]161 subtree = subtree->next;
[2666daa]162 }
[f651e80]163
164 panic("Not reached.");
[2666daa]165 return NULL;
166}
167
[7f1c620]168void sysinfo_set_item_val(const char *name, sysinfo_item_t **root, unative_t val)
[2666daa]169{
[749122b]170 if (root == NULL)
171 root = &_root;
172
173 /* If already created create only returns pointer
174 If not, create it */
175 sysinfo_item_t *item = sysinfo_create_path(name, root);
176
177 if (item != NULL) { /* If in subsystem, unable to create or return so unable to set */
[0f269c2]178 item->val.val = val;
[749122b]179 item->val_type = SYSINFO_VAL_VAL;
180 }
[2666daa]181}
182
[749122b]183void sysinfo_set_item_function(const char *name, sysinfo_item_t **root, sysinfo_val_fn_t fn)
[2666daa]184{
[749122b]185 if (root == NULL)
186 root = &_root;
187
188 /* If already created create only returns pointer
189 If not, create it */
190 sysinfo_item_t *item = sysinfo_create_path(name, root);
191
192 if (item != NULL) { /* If in subsystem, unable to create or return so unable to set */
[0f269c2]193 item->val.fn = fn;
[749122b]194 item->val_type = SYSINFO_VAL_FUNCTION;
195 }
[2666daa]196}
197
198
[749122b]199void sysinfo_set_item_undefined(const char *name, sysinfo_item_t **root)
[2666daa]200{
[749122b]201 if (root == NULL)
202 root = &_root;
203
204 /* If already created create only returns pointer
205 If not, create it */
206 sysinfo_item_t *item = sysinfo_create_path(name, root);
207
208 if (item != NULL)
209 item->val_type = SYSINFO_VAL_UNDEFINED;
[2666daa]210}
211
212
[749122b]213void sysinfo_dump(sysinfo_item_t **proot, int depth)
[2666daa]214{
215 sysinfo_item_t *root;
[749122b]216 if (proot == NULL)
217 proot = &_root;
218
[2666daa]219 root = *proot;
220
[749122b]221 while (root != NULL) {
[2666daa]222 int i;
[7f1c620]223 unative_t val = 0;
[a000878c]224 const char *vtype = NULL;
[749122b]225
[2666daa]226
[749122b]227 for (i = 0; i < depth; i++)
228 printf(" ");
[2666daa]229
[749122b]230 switch (root->val_type) {
[f8ddd17]231 case SYSINFO_VAL_UNDEFINED:
232 val = 0;
233 vtype = "UND";
234 break;
235 case SYSINFO_VAL_VAL:
236 val = root->val.val;
237 vtype = "VAL";
238 break;
239 case SYSINFO_VAL_FUNCTION:
240 val = ((sysinfo_val_fn_t) (root->val.fn)) (root);
241 vtype = "FUN";
242 break;
[749122b]243 }
[2666daa]244
[0f269c2]245 printf("%s %s val:%" PRIun "(%" PRIxn ") sub:%s\n", root->name, vtype, val,
[f8ddd17]246 val, (root->subinfo_type == SYSINFO_SUBINFO_NONE) ?
247 "NON" : ((root->subinfo_type == SYSINFO_SUBINFO_TABLE) ?
248 "TAB" : "FUN"));
[2666daa]249
[749122b]250 if (root->subinfo_type == SYSINFO_SUBINFO_TABLE)
251 sysinfo_dump(&(root -> subinfo.table), depth + 1);
[2666daa]252
[749122b]253 root = root->next;
254 }
[2666daa]255}
256
[749122b]257sysinfo_rettype_t sysinfo_get_val(const char *name, sysinfo_item_t **root)
[2666daa]258{
[749122b]259 // TODO: Implement Subsystem subinfo (by function implemented subinfo)
[2666daa]260
[749122b]261 sysinfo_rettype_t ret = {0, false};
[2666daa]262
[749122b]263 if (root == NULL)
264 root = &_root;
265
266 sysinfo_item_t *item = sysinfo_find_item(name, *root);
267
268 if (item != NULL) {
269 if (item->val_type == SYSINFO_VAL_UNDEFINED)
[2666daa]270 return ret;
[749122b]271 else
272 ret.valid = true;
273
274 if (item->val_type == SYSINFO_VAL_VAL)
275 ret.val = item->val.val;
276 else
277 ret.val = ((sysinfo_val_fn_t) (item->val.fn)) (item);
[2666daa]278 }
279 return ret;
280}
[35a96cf]281
[5719f6d]282#define SYSINFO_MAX_LEN 1024
283
[7f1c620]284unative_t sys_sysinfo_valid(unative_t ptr, unative_t len)
[35a96cf]285{
286 char *str;
[749122b]287 sysinfo_rettype_t ret = {0, 0};
[5719f6d]288
289 if (len > SYSINFO_MAX_LEN)
290 return ret.valid;
[749122b]291 str = malloc(len + 1, 0);
292
[35a96cf]293 ASSERT(str);
[749122b]294 if (!((copy_from_uspace(str, (void *) ptr, len + 1)) || (str[len])))
295 ret = sysinfo_get_val(str, NULL);
296
[35a96cf]297 free(str);
298 return ret.valid;
299}
300
[7f1c620]301unative_t sys_sysinfo_value(unative_t ptr, unative_t len)
[35a96cf]302{
303 char *str;
[749122b]304 sysinfo_rettype_t ret = {0, 0};
[5719f6d]305
306 if (len > SYSINFO_MAX_LEN)
307 return ret.val;
[749122b]308 str = malloc(len + 1, 0);
309
[35a96cf]310 ASSERT(str);
[749122b]311 if (!((copy_from_uspace(str, (void *) ptr, len + 1)) || (str[len])))
312 ret = sysinfo_get_val(str, NULL);
313
[35a96cf]314 free(str);
315 return ret.val;
316}
[b45c443]317
[cc73a8a1]318/** @}
[b45c443]319 */
Note: See TracBrowser for help on using the repository browser.