Subversion Repositories HelenOS-historic

Rev

Rev 1325 | Rev 1735 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

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