Rev 1322 | Rev 1326 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1322 | Rev 1325 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (C) 2006 Jakub Vana |
2 | * Copyright (C) 2006 Jakub Vana |
3 | * All rights reserved. |
3 | * All rights reserved. |
4 | * |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
7 | * are met: |
8 | * |
8 | * |
9 | * - Redistributions of source code must retain the above copyright |
9 | * - Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * - Redistributions in binary form must reproduce the above copyright |
11 | * - Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
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 |
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. |
15 | * derived from this software without specific prior written permission. |
16 | * |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
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 |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
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 |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
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 |
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. |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
27 | */ |
28 | 28 | ||
29 | #include <sysinfo/sysinfo.h> |
29 | #include <sysinfo/sysinfo.h> |
30 | #include <mm/slab.h> |
30 | #include <mm/slab.h> |
31 | #include <print.h> |
31 | #include <print.h> |
32 | #include <syscall/copy.h> |
32 | #include <syscall/copy.h> |
33 | 33 | ||
34 | sysinfo_item_t *_root=NULL; |
34 | sysinfo_item_t *_root=NULL; |
35 | 35 | ||
36 | 36 | ||
37 | static sysinfo_item_t* sysinfo_find_item(const char *name,sysinfo_item_t *subtree) |
37 | static sysinfo_item_t* sysinfo_find_item(const char *name,sysinfo_item_t *subtree) |
38 | { |
38 | { |
39 | if(subtree==NULL) return NULL; |
39 | if(subtree==NULL) return NULL; |
40 | while(subtree!=NULL) |
40 | while(subtree!=NULL) |
41 | { |
41 | { |
42 | int i; |
42 | int i; |
43 | char *a,*b; |
43 | char *a,*b; |
44 | a=(char *)name; |
44 | a=(char *)name; |
45 | b=subtree->name; |
45 | b=subtree->name; |
46 | while((a[i]==b[i])&&(b[i])) i++; |
46 | while((a[i]==b[i])&&(b[i])) i++; |
47 | if((!a[i]) && (!b[i])) return subtree; /*Last name in path matches*/ |
47 | if((!a[i]) && (!b[i])) return subtree; /*Last name in path matches*/ |
48 | if((a[i]=='.') && (!b[i])) /*Middle name in path matches*/ |
48 | if((a[i]=='.') && (!b[i])) /*Middle name in path matches*/ |
49 | { |
49 | { |
50 | if(subtree->subinfo_type==SYSINFO_SUBINFO_TABLE) return sysinfo_find_item(a+i+1,subtree->subinfo.table); |
50 | if(subtree->subinfo_type==SYSINFO_SUBINFO_TABLE) return sysinfo_find_item(a+i+1,subtree->subinfo.table); |
51 | //if(subtree->subinfo_type==SYSINFO_SUBINFO_FUNCTION) return NULL; /* Subinfo managed by subsystem*/ |
51 | //if(subtree->subinfo_type==SYSINFO_SUBINFO_FUNCTION) return NULL; /* Subinfo managed by subsystem*/ |
52 | return NULL; /*No subinfo*/ |
52 | return NULL; /*No subinfo*/ |
53 | } |
53 | } |
54 | /* No matches try next*/ |
54 | /* No matches try next*/ |
55 | subtree=subtree->next; |
55 | subtree=subtree->next; |
56 | i=0; |
56 | i=0; |
57 | } |
57 | } |
58 | return NULL; |
58 | return NULL; |
59 | } |
59 | } |
60 | 60 | ||
61 | static sysinfo_item_t* sysinfo_create_path(const char *name,sysinfo_item_t **psubtree) |
61 | static sysinfo_item_t* sysinfo_create_path(const char *name,sysinfo_item_t **psubtree) |
62 | { |
62 | { |
63 | sysinfo_item_t *subtree; |
63 | sysinfo_item_t *subtree; |
64 | subtree = *psubtree; |
64 | subtree = *psubtree; |
65 | 65 | ||
66 | if(subtree==NULL) |
66 | if(subtree==NULL) |
67 | { |
67 | { |
68 | sysinfo_item_t *item; |
68 | sysinfo_item_t *item; |
69 | int i=0,j; |
69 | int i=0,j; |
70 | 70 | ||
71 | item = malloc(sizeof(sysinfo_item_t),0); |
71 | item = malloc(sizeof(sysinfo_item_t),0); |
72 | ASSERT(item); |
72 | ASSERT(item); |
73 | *psubtree = item; |
73 | *psubtree = item; |
74 | item -> next = NULL; |
74 | item -> next = NULL; |
75 | item -> val_type = SYSINFO_VAL_UNDEFINED; |
75 | item -> val_type = SYSINFO_VAL_UNDEFINED; |
76 | item -> subinfo.table = NULL; |
76 | item -> subinfo.table = NULL; |
77 | 77 | ||
78 | while(name[i]&&(name[i]!='.')) i++; |
78 | while(name[i]&&(name[i]!='.')) i++; |
79 | 79 | ||
80 | item -> name = malloc(i,0); |
80 | item -> name = malloc(i,0); |
81 | ASSERT(item -> name); |
81 | ASSERT(item -> name); |
82 | 82 | ||
83 | for(j=0;j<i;j++) item->name[j]=name[j]; |
83 | for(j=0;j<i;j++) item->name[j]=name[j]; |
84 | item->name[j]=0; |
84 | item->name[j]=0; |
85 | 85 | ||
86 | if(name[i]/*=='.'*/) |
86 | if(name[i]/*=='.'*/) |
87 | { |
87 | { |
88 | item -> subinfo_type = SYSINFO_SUBINFO_TABLE; |
88 | item -> subinfo_type = SYSINFO_SUBINFO_TABLE; |
89 | return sysinfo_create_path(name+i+1,&(item->subinfo.table)); |
89 | return sysinfo_create_path(name+i+1,&(item->subinfo.table)); |
90 | } |
90 | } |
91 | item -> subinfo_type = SYSINFO_SUBINFO_NONE; |
91 | item -> subinfo_type = SYSINFO_SUBINFO_NONE; |
92 | return item; |
92 | return item; |
93 | } |
93 | } |
94 | 94 | ||
95 | while(subtree!=NULL) |
95 | while(subtree!=NULL) |
96 | { |
96 | { |
97 | int i=0,j; |
97 | int i=0,j; |
98 | char *a,*b; |
98 | char *a,*b; |
99 | a=(char *)name; |
99 | a=(char *)name; |
100 | b=subtree->name; |
100 | b=subtree->name; |
101 | while((a[i]==b[i])&&(b[i])) i++; |
101 | while((a[i]==b[i])&&(b[i])) i++; |
102 | if((!a[i]) && (!b[i])) return subtree; /*Last name in path matches*/ |
102 | if((!a[i]) && (!b[i])) return subtree; /*Last name in path matches*/ |
103 | if((a[i]=='.') && (!b[i])) /*Middle name in path matches*/ |
103 | if((a[i]=='.') && (!b[i])) /*Middle name in path matches*/ |
104 | { |
104 | { |
105 | if(subtree->subinfo_type==SYSINFO_SUBINFO_TABLE) return sysinfo_create_path(a+i+1,&(subtree->subinfo.table)); |
105 | if(subtree->subinfo_type==SYSINFO_SUBINFO_TABLE) return sysinfo_create_path(a+i+1,&(subtree->subinfo.table)); |
106 | if(subtree->subinfo_type==SYSINFO_SUBINFO_NONE) |
106 | if(subtree->subinfo_type==SYSINFO_SUBINFO_NONE) |
107 | { |
107 | { |
108 | subtree->subinfo_type=SYSINFO_SUBINFO_TABLE; |
108 | subtree->subinfo_type=SYSINFO_SUBINFO_TABLE; |
109 | return sysinfo_create_path(a+i+1,&(subtree->subinfo.table)); |
109 | return sysinfo_create_path(a+i+1,&(subtree->subinfo.table)); |
110 | } |
110 | } |
111 | //if(subtree->subinfo_type==SYSINFO_SUBINFO_FUNCTION) return NULL; /* Subinfo managed by subsystem*/ |
111 | //if(subtree->subinfo_type==SYSINFO_SUBINFO_FUNCTION) return NULL; /* Subinfo managed by subsystem*/ |
112 | return NULL; |
112 | return NULL; |
113 | } |
113 | } |
114 | /* No matches try next or create new*/ |
114 | /* No matches try next or create new*/ |
115 | if(subtree->next==NULL) |
115 | if(subtree->next==NULL) |
116 | { |
116 | { |
117 | sysinfo_item_t *item; |
117 | sysinfo_item_t *item; |
118 | 118 | ||
119 | item = malloc(sizeof(sysinfo_item_t),0); |
119 | item = malloc(sizeof(sysinfo_item_t),0); |
120 | ASSERT(item); |
120 | ASSERT(item); |
121 | subtree -> next = item; |
121 | subtree -> next = item; |
122 | item -> next = NULL; |
122 | item -> next = NULL; |
123 | item -> val_type = SYSINFO_VAL_UNDEFINED; |
123 | item -> val_type = SYSINFO_VAL_UNDEFINED; |
124 | item -> subinfo.table = NULL; |
124 | item -> subinfo.table = NULL; |
125 | 125 | ||
126 | i=0; |
126 | i=0; |
127 | while(name[i]&&(name[i]!='.')) i++; |
127 | while(name[i]&&(name[i]!='.')) i++; |
128 | 128 | ||
129 | item -> name = malloc(i,0); |
129 | item -> name = malloc(i,0); |
130 | ASSERT(item -> name); |
130 | ASSERT(item -> name); |
131 | for(j=0;j<i;j++) item->name[j]=name[j]; |
131 | for(j=0;j<i;j++) item->name[j]=name[j]; |
132 | item->name[j]=0; |
132 | item->name[j]=0; |
133 | 133 | ||
134 | if(name[i]/*=='.'*/) |
134 | if(name[i]/*=='.'*/) |
135 | { |
135 | { |
136 | item -> subinfo_type = SYSINFO_SUBINFO_TABLE; |
136 | item -> subinfo_type = SYSINFO_SUBINFO_TABLE; |
137 | return sysinfo_create_path(name+i+1,&(item->subinfo.table)); |
137 | return sysinfo_create_path(name+i+1,&(item->subinfo.table)); |
138 | } |
138 | } |
139 | item -> subinfo_type = SYSINFO_SUBINFO_NONE; |
139 | item -> subinfo_type = SYSINFO_SUBINFO_NONE; |
140 | return item; |
140 | return item; |
141 | 141 | ||
142 | } |
142 | } |
143 | else |
143 | else |
144 | { |
144 | { |
145 | subtree=subtree->next; |
145 | subtree=subtree->next; |
146 | i=0; |
146 | i=0; |
147 | } |
147 | } |
148 | } |
148 | } |
149 | panic("Not reached\n"); |
149 | panic("Not reached\n"); |
150 | return NULL; |
150 | return NULL; |
151 | } |
151 | } |
152 | 152 | ||
153 | void sysinfo_set_item_val(const char *name,sysinfo_item_t **root,__native val) |
153 | void sysinfo_set_item_val(const char *name,sysinfo_item_t **root,__native val) |
154 | { |
154 | { |
155 | if(root==NULL) root=&_root; |
155 | if(root==NULL) root=&_root; |
156 | sysinfo_item_t *item; |
156 | sysinfo_item_t *item; |
157 | item = sysinfo_create_path(name,root); /* If already created create only returns pointer |
157 | item = sysinfo_create_path(name,root); /* If already created create only returns pointer |
158 | If ! , create it */ |
158 | If ! , create it */ |
159 | if(item!=NULL) /* If in subsystem, unable to create or return so unable to set */ |
159 | if(item!=NULL) /* If in subsystem, unable to create or return so unable to set */ |
160 | { |
160 | { |
161 | item->val.val=val; |
161 | item->val.val=val; |
162 | item -> val_type = SYSINFO_VAL_VAL; |
162 | item -> val_type = SYSINFO_VAL_VAL; |
163 | } |
163 | } |
164 | } |
164 | } |
165 | 165 | ||
166 | void sysinfo_set_item_function(const char *name,sysinfo_item_t **root,sysinfo_val_fn_t fn) |
166 | void sysinfo_set_item_function(const char *name,sysinfo_item_t **root,sysinfo_val_fn_t fn) |
167 | { |
167 | { |
168 | if(root==NULL) root=&_root; |
168 | if(root==NULL) root=&_root; |
169 | sysinfo_item_t *item; |
169 | sysinfo_item_t *item; |
170 | item = sysinfo_create_path(name,root); /* If already created create only returns pointer |
170 | item = sysinfo_create_path(name,root); /* If already created create only returns pointer |
171 | If ! , create it */ |
171 | If ! , create it */ |
172 | if(item!=NULL) /* If in subsystem, unable to create or return so unable to set */ |
172 | if(item!=NULL) /* If in subsystem, unable to create or return so unable to set */ |
173 | { |
173 | { |
174 | item->val.fn=fn; |
174 | item->val.fn=fn; |
175 | item -> val_type = SYSINFO_VAL_FUNCTION; |
175 | item -> val_type = SYSINFO_VAL_FUNCTION; |
176 | } |
176 | } |
177 | } |
177 | } |
178 | 178 | ||
179 | 179 | ||
180 | void sysinfo_set_item_undefined(const char *name,sysinfo_item_t **root) |
180 | void sysinfo_set_item_undefined(const char *name,sysinfo_item_t **root) |
181 | { |
181 | { |
182 | if(root==NULL) root=&_root; |
182 | if(root==NULL) root=&_root; |
183 | sysinfo_item_t *item; |
183 | sysinfo_item_t *item; |
184 | item = sysinfo_find_item(name,*root); |
184 | item = sysinfo_find_item(name,*root); |
185 | if(item!=NULL) item -> val_type = SYSINFO_VAL_UNDEFINED; |
185 | if(item!=NULL) item -> val_type = SYSINFO_VAL_UNDEFINED; |
186 | } |
186 | } |
187 | 187 | ||
188 | 188 | ||
189 | void sysinfo_dump(sysinfo_item_t **proot,int depth) |
189 | void sysinfo_dump(sysinfo_item_t **proot,int depth) |
190 | { |
190 | { |
191 | sysinfo_item_t *root; |
191 | sysinfo_item_t *root; |
192 | if(proot==NULL) proot=&_root; |
192 | if(proot==NULL) proot=&_root; |
193 | root = *proot; |
193 | root = *proot; |
194 | 194 | ||
195 | while(root!=NULL) |
195 | while(root!=NULL) |
196 | { |
196 | { |
197 | 197 | ||
198 | int i; |
198 | int i; |
199 | __native val=0; |
199 | __native val=0; |
200 | char *vtype=NULL; |
200 | char *vtype=NULL; |
201 | 201 | ||
202 | 202 | ||
203 | for(i=0;i<depth;i++) printf(" "); |
203 | for(i=0;i<depth;i++) printf(" "); |
204 | 204 | ||
205 | switch (root->val_type) |
205 | switch (root->val_type) |
206 | { |
206 | { |
207 | case (SYSINFO_VAL_UNDEFINED): |
207 | case (SYSINFO_VAL_UNDEFINED): |
208 | val=0; |
208 | val=0; |
209 | vtype="UND"; |
209 | vtype="UND"; |
210 | break; |
210 | break; |
211 | case (SYSINFO_VAL_VAL): |
211 | case (SYSINFO_VAL_VAL): |
212 | val=root->val.val; |
212 | val=root->val.val; |
213 | vtype="VAL"; |
213 | vtype="VAL"; |
214 | break; |
214 | break; |
215 | case (SYSINFO_VAL_FUNCTION): |
215 | case (SYSINFO_VAL_FUNCTION): |
216 | val=((sysinfo_val_fn_t)(root->val.fn))(root); |
216 | val=((sysinfo_val_fn_t)(root->val.fn))(root); |
217 | vtype="FUN"; |
217 | vtype="FUN"; |
218 | break; |
218 | break; |
219 | } |
219 | } |
220 | printf("%s %s val:%d(%X) sub:%s\n",root->name,vtype,val,val, |
220 | printf("%s %s val:%d(%X) sub:%s\n",root->name,vtype,val,val, |
221 | (root->subinfo_type==SYSINFO_SUBINFO_NONE)?"NON":((root->subinfo_type==SYSINFO_SUBINFO_TABLE)?"TAB":"FUN")); |
221 | (root->subinfo_type==SYSINFO_SUBINFO_NONE)?"NON":((root->subinfo_type==SYSINFO_SUBINFO_TABLE)?"TAB":"FUN")); |
222 | 222 | ||
223 | 223 | ||
224 | if(root -> subinfo_type == SYSINFO_SUBINFO_TABLE) |
224 | if(root -> subinfo_type == SYSINFO_SUBINFO_TABLE) |
225 | sysinfo_dump(&(root->subinfo.table),depth+1); |
225 | sysinfo_dump(&(root->subinfo.table),depth+1); |
226 | root=root->next; |
226 | root=root->next; |
227 | } |
227 | } |
228 | } |
228 | } |
229 | 229 | ||
230 | sysinfo_rettype_t sysinfo_get_val(const char *name,sysinfo_item_t **root) |
230 | sysinfo_rettype_t sysinfo_get_val(const char *name,sysinfo_item_t **root) |
231 | { |
231 | { |
232 | /*TODO: |
232 | /*TODO: |
233 | Implement Subsystem subinfo (by function implemented subinfo) |
233 | Implement Subsystem subinfo (by function implemented subinfo) |
234 | */ |
234 | */ |
235 | 235 | ||
236 | sysinfo_rettype_t ret={0,false}; |
236 | sysinfo_rettype_t ret={0,false}; |
237 | 237 | ||
238 | if(root==NULL) root=&_root; |
238 | if(root==NULL) root=&_root; |
239 | sysinfo_item_t *item; |
239 | sysinfo_item_t *item; |
240 | item = sysinfo_find_item(name,*root); |
240 | item = sysinfo_find_item(name,*root); |
241 | if(item!=NULL) |
241 | if(item!=NULL) |
242 | { |
242 | { |
243 | if (item -> val_type == SYSINFO_VAL_UNDEFINED) |
243 | if (item -> val_type == SYSINFO_VAL_UNDEFINED) |
244 | return ret; |
244 | return ret; |
245 | else ret.valid=true; |
245 | else ret.valid=true; |
246 | if (item -> val_type == SYSINFO_VAL_VAL) |
246 | if (item -> val_type == SYSINFO_VAL_VAL) |
247 | ret.val=item -> val.val; |
247 | ret.val=item -> val.val; |
248 | else ret.val=((sysinfo_val_fn_t)(item->val.fn))(item); |
248 | else ret.val=((sysinfo_val_fn_t)(item->val.fn))(item); |
249 | } |
249 | } |
250 | return ret; |
250 | return ret; |
251 | } |
251 | } |
252 | 252 | ||
253 | __native sys_sysinfo_valid(__native ptr,__native len) |
253 | __native sys_sysinfo_valid(__native ptr,__native len) |
254 | { |
254 | { |
255 | char *str; |
255 | char *str; |
256 | sysinfo_rettype_t ret; |
256 | sysinfo_rettype_t ret={0,0}; |
257 | str=malloc(len+1,0); |
257 | str=malloc(len+1,0); |
258 | ASSERT(str); |
258 | ASSERT(str); |
259 | if(!((copy_from_uspace(str,(void *)ptr,len+1))||(str[len]))) |
259 | if(!((copy_from_uspace(str,(void *)ptr,len+1))||(str[len]))) |
260 | ret=sysinfo_get_val(str,NULL); |
260 | ret=sysinfo_get_val(str,NULL); |
261 | free(str); |
261 | free(str); |
262 | return ret.valid; |
262 | return ret.valid; |
263 | } |
263 | } |
264 | 264 | ||
265 | __native sys_sysinfo_value(__native ptr,__native len) |
265 | __native sys_sysinfo_value(__native ptr,__native len) |
266 | { |
266 | { |
267 | char *str; |
267 | char *str; |
268 | sysinfo_rettype_t ret; |
268 | sysinfo_rettype_t ret={0,0}; |
269 | str=malloc(len+1,0); |
269 | str=malloc(len+1,0); |
270 | ASSERT(str); |
270 | ASSERT(str); |
271 | if(!((copy_from_uspace(str,(void *)ptr,len+1))||(str[len]))) |
271 | if(!((copy_from_uspace(str,(void *)ptr,len+1))||(str[len]))) |
272 | ret=sysinfo_get_val(str,NULL); |
272 | ret=sysinfo_get_val(str,NULL); |
273 | free(str); |
273 | free(str); |
274 | return ret.val; |
274 | return ret.val; |
275 | } |
275 | } |
276 | 276 | ||
277 | 277 | ||
278 | 278 |