sysinfo.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2006 Jakub Vana
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  *
00009  * - Redistributions of source code must retain the above copyright
00010  *   notice, this list of conditions and the following disclaimer.
00011  * - Redistributions in binary form must reproduce the above copyright
00012  *   notice, this list of conditions and the following disclaimer in the
00013  *   documentation and/or other materials provided with the distribution.
00014  * - The name of the author may not be used to endorse or promote products
00015  *   derived from this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00018  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00019  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00020  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
00021  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
00022  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00023  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00024  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00025  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00026  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027  */
00028 
00035 #include <sysinfo/sysinfo.h>
00036 #include <mm/slab.h>
00037 #include <print.h>
00038 #include <syscall/copy.h>
00039 
00040 sysinfo_item_t *_root = NULL;
00041 
00042 
00043 static sysinfo_item_t *sysinfo_find_item(const char *name, sysinfo_item_t *subtree)
00044 {
00045         if (subtree == NULL)
00046                 return NULL;
00047         
00048         while (subtree != NULL) {
00049                 int i = 0;
00050                 char *a = (char *) name;
00051                 char *b = subtree->name;
00052                 
00053                 while ((a[i] == b[i]) && (b[i]))
00054                         i++;
00055                 
00056                 if ((!a[i]) && (!b[i]))  /* Last name in path matches */
00057                         return subtree;
00058                 
00059                 if ((a[i] == '.') && (!b[i])) { /* Middle name in path matches */
00060                         if (subtree->subinfo_type == SYSINFO_SUBINFO_TABLE)
00061                                 return sysinfo_find_item(a + i + 1, subtree->subinfo.table);
00062                         
00063                         //if (subtree->subinfo_type == SYSINFO_SUBINFO_FUNCTION) /* Subinfo managed by subsystem */
00064                         //      return NULL; 
00065                         
00066                         return NULL; /* No subinfo */
00067                 }
00068                 /* No matches try next */
00069                 subtree = subtree->next;
00070                 i = 0;
00071         }
00072         return NULL;
00073 }
00074 
00075 static sysinfo_item_t *sysinfo_create_path(const char *name, sysinfo_item_t **psubtree)
00076 {
00077         sysinfo_item_t *subtree;
00078         subtree = *psubtree;
00079         
00080         if (subtree == NULL) {
00081                         sysinfo_item_t *item = malloc(sizeof(sysinfo_item_t), 0);
00082                         int i = 0, j;
00083                         
00084                         ASSERT(item);
00085                         *psubtree = item;
00086                         item->next = NULL;
00087                         item->val_type = SYSINFO_VAL_UNDEFINED;
00088                         item->subinfo.table = NULL;
00089 
00090                         while (name[i] && (name[i] != '.'))
00091                                 i++;
00092                         
00093                         item->name = malloc(i, 0);
00094                         ASSERT(item->name);
00095 
00096                         for (j = 0; j < i; j++)
00097                                 item->name[j] = name[j];
00098                         item->name[j] = 0;
00099                         
00100                         if (name[i]) { /* =='.' */
00101                                 item->subinfo_type = SYSINFO_SUBINFO_TABLE;
00102                                 return sysinfo_create_path(name + i + 1, &(item->subinfo.table));
00103                         }
00104                         item->subinfo_type = SYSINFO_SUBINFO_NONE;
00105                         return item;
00106         }
00107 
00108         while (subtree != NULL) {
00109                 int i = 0, j;
00110                 char *a = (char *) name;
00111                 char *b = subtree->name;
00112                 
00113                 while ((a[i] == b[i]) && (b[i]))
00114                         i++;
00115                 
00116                 if ((!a[i]) && (!b[i])) /* Last name in path matches */
00117                         return subtree;
00118                 
00119                 if ((a[i] == '.') && (!b[i])) { /* Middle name in path matches */
00120                         if (subtree->subinfo_type == SYSINFO_SUBINFO_TABLE)
00121                                 return sysinfo_create_path(a + i + 1, &(subtree->subinfo.table));
00122                         
00123                         if (subtree->subinfo_type == SYSINFO_SUBINFO_NONE) {
00124                                 subtree->subinfo_type = SYSINFO_SUBINFO_TABLE;
00125                                 return sysinfo_create_path(a + i + 1,&(subtree->subinfo.table));
00126                         }
00127                         
00128                         //if (subtree->subinfo_type == SYSINFO_SUBINFO_FUNCTION) /* Subinfo managed by subsystem */
00129                         //      return NULL; 
00130                         
00131                         return NULL;
00132                 }
00133                 /* No matches try next or create new*/
00134                 if (subtree->next == NULL) {
00135                         sysinfo_item_t *item = malloc(sizeof(sysinfo_item_t), 0);
00136                         
00137                         ASSERT(item);
00138                         subtree->next = item;
00139                         item->next = NULL;
00140                         item->val_type = SYSINFO_VAL_UNDEFINED;
00141                         item->subinfo.table = NULL;
00142 
00143                         i = 0;
00144                         while (name[i] && (name[i] != '.'))
00145                                 i++;
00146 
00147                         item->name = malloc(i, 0);
00148                         ASSERT(item->name);
00149                         
00150                         for (j = 0; j < i; j++)
00151                                 item->name[j] = name[j];
00152                         
00153                         item->name[j] = 0;
00154 
00155                         if(name[i]) { /* =='.' */
00156                                 item->subinfo_type = SYSINFO_SUBINFO_TABLE;
00157                                 return sysinfo_create_path(name + i + 1, &(item->subinfo.table));
00158                         }
00159                         item->subinfo_type = SYSINFO_SUBINFO_NONE;
00160                         return item;
00161                 } else {
00162                         subtree = subtree->next;
00163                         i = 0;
00164                 }       
00165         }
00166         panic("Not reached\n");
00167         return NULL;
00168 }
00169 
00170 void sysinfo_set_item_val(const char *name, sysinfo_item_t **root, __native val)
00171 {
00172         if (root == NULL)
00173                 root = &_root;
00174         
00175         /* If already created create only returns pointer 
00176            If not, create it */
00177         sysinfo_item_t *item = sysinfo_create_path(name, root);
00178         
00179         if (item != NULL) { /* If in subsystem, unable to create or return so unable to set */
00180                 item->val.val=val;                   
00181                 item->val_type = SYSINFO_VAL_VAL;
00182         }
00183 }
00184 
00185 void sysinfo_set_item_function(const char *name, sysinfo_item_t **root, sysinfo_val_fn_t fn)
00186 {
00187         if (root == NULL)
00188                 root = &_root;
00189         
00190         /* If already created create only returns pointer 
00191            If not, create it */
00192         sysinfo_item_t *item = sysinfo_create_path(name, root);
00193         
00194         if (item != NULL) { /* If in subsystem, unable to create or return so  unable to set */
00195                 item->val.fn=fn;                   
00196                 item->val_type = SYSINFO_VAL_FUNCTION;
00197         }
00198 }
00199 
00200 
00201 void sysinfo_set_item_undefined(const char *name, sysinfo_item_t **root)
00202 {
00203         if (root == NULL)
00204                 root = &_root;
00205         
00206         /* If already created create only returns pointer 
00207            If not, create it */
00208         sysinfo_item_t *item = sysinfo_create_path(name, root);
00209         
00210         if (item != NULL)
00211                 item->val_type = SYSINFO_VAL_UNDEFINED;
00212 }
00213 
00214 
00215 void sysinfo_dump(sysinfo_item_t **proot, int depth)
00216 {
00217         sysinfo_item_t *root;
00218         if (proot == NULL)
00219                 proot = &_root;
00220         
00221         root = *proot;
00222         
00223         while (root != NULL) {
00224                 int i;
00225                 __native val = 0;
00226                 char *vtype = NULL;
00227                 
00228                 
00229                 for (i = 0; i < depth; i++)
00230                         printf("  ");
00231                 
00232                 switch (root->val_type) {
00233                         case SYSINFO_VAL_UNDEFINED:
00234                                 val = 0;
00235                                 vtype = "UND";
00236                                 break;
00237                         case SYSINFO_VAL_VAL:
00238                                 val = root->val.val;
00239                                 vtype = "VAL";
00240                                 break;
00241                         case SYSINFO_VAL_FUNCTION:
00242                                 val = ((sysinfo_val_fn_t) (root->val.fn)) (root);
00243                                 vtype = "FUN";
00244                                 break;
00245                 }
00246                 
00247                 printf("%s    %s val:%d(%x) sub:%s\n", root->name, vtype, val, val, (root->subinfo_type == SYSINFO_SUBINFO_NONE) ? "NON" : ((root->subinfo_type == SYSINFO_SUBINFO_TABLE) ? "TAB" : "FUN"));
00248                 
00249                 if (root->subinfo_type == SYSINFO_SUBINFO_TABLE)
00250                         sysinfo_dump(&(root -> subinfo.table), depth + 1);
00251                 
00252                 root = root->next;
00253         }
00254 }
00255 
00256 sysinfo_rettype_t sysinfo_get_val(const char *name, sysinfo_item_t **root)
00257 {
00258         // TODO: Implement Subsystem subinfo (by function implemented subinfo)
00259 
00260         sysinfo_rettype_t ret = {0, false};
00261 
00262         if (root == NULL)
00263                 root = &_root;
00264         
00265         sysinfo_item_t *item = sysinfo_find_item(name, *root);
00266         
00267         if (item != NULL) {
00268                 if (item->val_type == SYSINFO_VAL_UNDEFINED) 
00269                         return ret;
00270                 else
00271                         ret.valid = true;
00272                 
00273                 if (item->val_type == SYSINFO_VAL_VAL)
00274                         ret.val = item->val.val;
00275                 else
00276                         ret.val = ((sysinfo_val_fn_t) (item->val.fn)) (item);
00277         }
00278         return ret;
00279 }
00280 
00281 __native sys_sysinfo_valid(__native ptr, __native len)
00282 {
00283         char *str;
00284         sysinfo_rettype_t ret = {0, 0};
00285         str = malloc(len + 1, 0);
00286         
00287         ASSERT(str);
00288         if (!((copy_from_uspace(str, (void *) ptr, len + 1)) || (str[len])))
00289                 ret = sysinfo_get_val(str, NULL);
00290         
00291         free(str);
00292         return ret.valid;
00293 }
00294 
00295 __native sys_sysinfo_value(__native ptr, __native len)
00296 {
00297         char *str;
00298         sysinfo_rettype_t ret = {0, 0};
00299         str = malloc(len + 1, 0);
00300         
00301         ASSERT(str);
00302         if (!((copy_from_uspace(str, (void *) ptr, len + 1)) || (str[len]))) 
00303                 ret = sysinfo_get_val(str, NULL);
00304         
00305         free(str);
00306         return ret.val;
00307 }
00308 

Generated on Sun Jun 18 17:17:05 2006 for HelenOS Kernel (ppc32) by  doxygen 1.4.6