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
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
40bool fb_exported = false;
41sysinfo_item_t *_root = NULL;
42
43static sysinfo_item_t *sysinfo_find_item(const char *name, sysinfo_item_t *subtree)
44{
45 if (subtree == NULL)
46 return NULL;
47
48 while (subtree != NULL) {
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 */
67 }
68 /* No matches try next */
69 subtree = subtree->next;
70 }
71 return NULL;
72}
73
74static sysinfo_item_t *sysinfo_create_path(const char *name, sysinfo_item_t **psubtree)
75{
76 sysinfo_item_t *subtree;
77 subtree = *psubtree;
78
79 if (subtree == NULL) {
80 sysinfo_item_t *item = malloc(sizeof(sysinfo_item_t), 0);
81 int i = 0, j;
82
83 ASSERT(item);
84 *psubtree = item;
85 item->next = NULL;
86 item->val_type = SYSINFO_VAL_UNDEFINED;
87 item->subinfo.table = NULL;
88
89 while (name[i] && (name[i] != '.'))
90 i++;
91
92 item->name = malloc(i, 0);
93 ASSERT(item->name);
94
95 for (j = 0; j < i; j++)
96 item->name[j] = name[j];
97 item->name[j] = 0;
98
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;
105 }
106
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
130 return NULL;
131 }
132 /* No matches try next or create new*/
133 if (subtree->next == NULL) {
134 sysinfo_item_t *item = malloc(sizeof(sysinfo_item_t), 0);
135
136 ASSERT(item);
137 subtree->next = item;
138 item->next = NULL;
139 item->val_type = SYSINFO_VAL_UNDEFINED;
140 item->subinfo.table = NULL;
141
142 i = 0;
143 while (name[i] && (name[i] != '.'))
144 i++;
145
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;
153
154 if(name[i]) { /* =='.' */
155 item->subinfo_type = SYSINFO_SUBINFO_TABLE;
156 return sysinfo_create_path(name + i + 1, &(item->subinfo.table));
157 }
158 item->subinfo_type = SYSINFO_SUBINFO_NONE;
159 return item;
160 } else
161 subtree = subtree->next;
162 }
163
164 panic("Not reached.");
165 return NULL;
166}
167
168void sysinfo_set_item_val(const char *name, sysinfo_item_t **root, unative_t val)
169{
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 */
178 item->val.val = val;
179 item->val_type = SYSINFO_VAL_VAL;
180 }
181}
182
183void sysinfo_set_item_function(const char *name, sysinfo_item_t **root, sysinfo_val_fn_t fn)
184{
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 */
193 item->val.fn = fn;
194 item->val_type = SYSINFO_VAL_FUNCTION;
195 }
196}
197
198
199void sysinfo_set_item_undefined(const char *name, sysinfo_item_t **root)
200{
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;
210}
211
212
213void sysinfo_dump(sysinfo_item_t **proot, int depth)
214{
215 sysinfo_item_t *root;
216 if (proot == NULL)
217 proot = &_root;
218
219 root = *proot;
220
221 while (root != NULL) {
222 int i;
223 unative_t val = 0;
224 const char *vtype = NULL;
225
226
227 for (i = 0; i < depth; i++)
228 printf(" ");
229
230 switch (root->val_type) {
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;
243 }
244
245 printf("%s %s val:%" PRIun "(%" PRIxn ") sub:%s\n", root->name, vtype, val,
246 val, (root->subinfo_type == SYSINFO_SUBINFO_NONE) ?
247 "NON" : ((root->subinfo_type == SYSINFO_SUBINFO_TABLE) ?
248 "TAB" : "FUN"));
249
250 if (root->subinfo_type == SYSINFO_SUBINFO_TABLE)
251 sysinfo_dump(&(root -> subinfo.table), depth + 1);
252
253 root = root->next;
254 }
255}
256
257sysinfo_rettype_t sysinfo_get_val(const char *name, sysinfo_item_t **root)
258{
259 // TODO: Implement Subsystem subinfo (by function implemented subinfo)
260
261 sysinfo_rettype_t ret = {0, false};
262
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)
270 return ret;
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);
278 }
279 return ret;
280}
281
282#define SYSINFO_MAX_LEN 1024
283
284unative_t sys_sysinfo_valid(unative_t ptr, unative_t len)
285{
286 char *str;
287 sysinfo_rettype_t ret = {0, 0};
288
289 if (len > SYSINFO_MAX_LEN)
290 return ret.valid;
291 str = malloc(len + 1, 0);
292
293 ASSERT(str);
294 if (!((copy_from_uspace(str, (void *) ptr, len + 1)) || (str[len])))
295 ret = sysinfo_get_val(str, NULL);
296
297 free(str);
298 return ret.valid;
299}
300
301unative_t sys_sysinfo_value(unative_t ptr, unative_t len)
302{
303 char *str;
304 sysinfo_rettype_t ret = {0, 0};
305
306 if (len > SYSINFO_MAX_LEN)
307 return ret.val;
308 str = malloc(len + 1, 0);
309
310 ASSERT(str);
311 if (!((copy_from_uspace(str, (void *) ptr, len + 1)) || (str[len])))
312 ret = sysinfo_get_val(str, NULL);
313
314 free(str);
315 return ret.val;
316}
317
318/** @}
319 */
Note: See TracBrowser for help on using the repository browser.