kern_sysctl.c

Go to the documentation of this file.
00001 /*****************************************************************************************
00002  Copyright (c) 2002-2004 The UbixOS Project
00003  All rights reserved.
00004 
00005  Redistribution and use in source and binary forms, with or without modification, are
00006  permitted provided that the following conditions are met:
00007 
00008  Redistributions of source code must retain the above copyright notice, this list of
00009  conditions, the following disclaimer and the list of authors.  Redistributions in binary
00010  form must reproduce the above copyright notice, this list of conditions, the following
00011  disclaimer and the list of authors in the documentation and/or other materials provided
00012  with the distribution. Neither the name of the UbixOS Project nor the names of its
00013  contributors may be used to endorse or promote products derived from this software
00014  without specific prior written permission.
00015 
00016  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
00017  EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
00018  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
00019  THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00020  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
00021  OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00022  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
00023  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00024  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00025 
00026  $Id: kern__sysctl_8c-source.html 88 2016-01-12 00:11:29Z reddawg $
00027 
00028 *****************************************************************************************/
00029 
00030 #include <ubixos/types.h>
00031 #include <sys/sysproto.h>
00032 #include <sys/thread.h>
00033 #include <sys/kern_sysctl.h>
00034 #include <ubixos/endtask.h>
00035 #include <ubixos/kpanic.h>
00036 #include <lib/kprintf.h>
00037 #include <lib/kmalloc.h>
00038 #include <assert.h>
00039 #include <string.h>
00040 
00041 static struct sysctl_entry *ctls = 0x0;
00042 
00043 static struct sysctl_entry *sysctl_find(int *,int);
00044 
00045 /* This is a cheat for now */
00046 static void def_ctls() {
00047   int name[CTL_MAXNAME], name_len;
00048   uInt32 page_val = 0x1000;
00049   name[0] = 6;
00050   name[1] = 7;
00051   name_len = 2; 
00052   sysctl_add(name,name_len,"page_size",&page_val,sizeof(uInt32));
00053   /* Clock Rate */
00054   name[0] = 1;
00055   name [1] = 12;
00056   page_val = 0x3E8;
00057   sysctl_add(name,name_len,"page_size",&page_val,sizeof(uInt32));
00058   /* User Stack */
00059   name[0] = 1;
00060   name [1] = 33;
00061   page_val = 0xCBE8000;
00062   sysctl_add(name,name_len,"page_size",&page_val,sizeof(uInt32));
00063   }
00064 
00065 int sysctl_init() {
00066   struct sysctl_entry *tmpCtl = 0x0;
00067   if (ctls != 0x0) {
00068     kprintf("sysctl already Initialized\n");
00069     while (1);
00070     }
00071 
00072   ctls = (struct sysctl_entry *)kmalloc(sizeof(struct sysctl_entry));
00073   ctls->prev     = 0x0;
00074   ctls->id       = CTL_UNSPEC;
00075   ctls->children = 0x0;
00076   sprintf(ctls->name,"unspec");
00077 
00078   tmpCtl = (struct sysctl_entry *)kmalloc(sizeof(struct sysctl_entry));
00079   tmpCtl->prev     = ctls;
00080   tmpCtl->id       = CTL_KERN;
00081   tmpCtl->children = 0x0;
00082   sprintf(tmpCtl->name,"kern");
00083   ctls->next = tmpCtl;
00084 
00085   tmpCtl->next = (struct sysctl_entry *)kmalloc(sizeof(struct sysctl_entry));
00086   tmpCtl->next->prev = tmpCtl;
00087   tmpCtl             = tmpCtl->next;
00088   tmpCtl->id         = CTL_VM;
00089   tmpCtl->children   = 0x0;
00090   sprintf(tmpCtl->name,"vm");
00091 
00092   tmpCtl->next = (struct sysctl_entry *)kmalloc(sizeof(struct sysctl_entry));
00093   tmpCtl->next->prev = tmpCtl;
00094   tmpCtl             = tmpCtl->next;
00095   tmpCtl->id         = CTL_VFS;
00096   tmpCtl->children   = 0x0;
00097   sprintf(tmpCtl->name,"vfs");
00098 
00099   tmpCtl->next = (struct sysctl_entry *)kmalloc(sizeof(struct sysctl_entry));
00100   tmpCtl->next->prev = tmpCtl;
00101   tmpCtl             = tmpCtl->next;
00102   tmpCtl->id         = CTL_NET;
00103   tmpCtl->children   = 0x0;
00104   sprintf(tmpCtl->name,"net");
00105 
00106   tmpCtl->next = (struct sysctl_entry *)kmalloc(sizeof(struct sysctl_entry));
00107   tmpCtl->next->prev = tmpCtl;
00108   tmpCtl             = tmpCtl->next;
00109   tmpCtl->id         = CTL_DEBUG;
00110   tmpCtl->children   = 0x0;
00111   sprintf(tmpCtl->name,"debug");
00112 
00113   tmpCtl->next = (struct sysctl_entry *)kmalloc(sizeof(struct sysctl_entry));
00114   tmpCtl->next->prev = tmpCtl;
00115   tmpCtl             = tmpCtl->next;
00116   tmpCtl->id         = CTL_HW;
00117   tmpCtl->children   = 0x0;
00118   sprintf(tmpCtl->name,"hw");
00119 
00120   tmpCtl->next = (struct sysctl_entry *)kmalloc(sizeof(struct sysctl_entry));
00121   tmpCtl->next->prev = tmpCtl;
00122   tmpCtl             = tmpCtl->next;
00123   tmpCtl->id         = CTL_MACHDEP;
00124   tmpCtl->children   = 0x0;
00125   sprintf(tmpCtl->name,"machdep");
00126 
00127   tmpCtl->next = (struct sysctl_entry *)kmalloc(sizeof(struct sysctl_entry));
00128   tmpCtl->next->prev = tmpCtl;
00129   tmpCtl             = tmpCtl->next;
00130   tmpCtl->id         = CTL_USER;
00131   tmpCtl->children   = 0x0;
00132   sprintf(tmpCtl->name,"user");
00133 
00134   tmpCtl->next = (struct sysctl_entry *)kmalloc(sizeof(struct sysctl_entry));
00135   tmpCtl->next->prev = tmpCtl;
00136   tmpCtl             = tmpCtl->next;
00137   tmpCtl->id         = CTL_P1003_1B;
00138   tmpCtl->children   = 0x0;
00139   sprintf(tmpCtl->name,"p1003_1b");
00140 
00141   tmpCtl->next = (struct sysctl_enctry *)kmalloc(sizeof(struct sysctl_entry));
00142   tmpCtl->next->prev = tmpCtl;
00143   tmpCtl             = tmpCtl->next;
00144   tmpCtl->id         = CTL_UBIX;
00145   tmpCtl->children   = 0x0;
00146   sprintf(tmpCtl->name,"ubix");
00147 
00148   def_ctls();
00149 
00150   return(0x0);
00151   }
00152 
00153 int __sysctl(struct thread *td, struct sysctl_args *uap) {
00154   struct sysctl_entry *tmpCtl = 0x0;
00155   int i = 0;
00156 
00157   if (ctls == 0x0)
00158     K_PANIC("sysctl not initialized");
00159 
00160   if (uap->newlen < 0) {
00161     kprintf("Changing Not supported yet.\n");
00162     endTask(_current->id);
00163     }
00164 
00165   tmpCtl = sysctl_find(uap->name,uap->namelen);
00166   if (tmpCtl == 0x0) { 
00167     kprintf("Invalid CTL\n");
00168     for (i = 0x0;i < uap->namelen;i++)
00169       kprintf("(%i)",uap->name[i]);
00170     kprintf("\n");
00171     endTask(_current->id);
00172     }
00173 
00174   if ((uint32_t)uap->oldlenp < tmpCtl->val_len) 
00175      memcpy(uap->old,tmpCtl->value,(uInt32)uap->oldlenp);
00176   else
00177      memcpy(uap->old,tmpCtl->value,tmpCtl->val_len);
00178 
00179   td->td_retval[0] = 0x0;
00180 
00181   return(0x0);
00182   }
00183 
00184 static struct sysctl_entry *sysctl_find(int *name,int namelen) {
00185   int i = 0x0;
00186   struct sysctl_entry *tmpCtl = 0x0;
00187   struct sysctl_entry *lCtl = ctls;
00188 
00189   /* Loop Name Len */
00190   for (i = 0x0; i < namelen;i++) {
00191     for (tmpCtl = lCtl;tmpCtl != 0x0;tmpCtl = tmpCtl->next) {
00192       //kprintf("ctlName: [%s], ctlId; [%i]\n",tmpCtl->name,tmpCtl->id);
00193       if (tmpCtl->id == name[i]) {
00194          if ((i+1) == namelen) {
00195            return(tmpCtl);
00196            }
00197          lCtl = tmpCtl->children;
00198          break;
00199          }
00200       }
00201     }
00202   return(0x0);
00203   }
00204 
00205 int sysctl_add(int *name,int namelen,char *str_name,void *buf,int buf_size) {
00206   struct sysctl_entry *tmpCtl = 0x0;
00207   struct sysctl_entry *newCtl = 0x0;
00208 
00209   /* Check if it exists */
00210   tmpCtl = sysctl_find(name,namelen);
00211   if (tmpCtl != 0x0) {
00212     kprintf("Node Exists!\n");
00213     while (1);
00214     }
00215 
00216   /* Get Parent Node */
00217   tmpCtl = sysctl_find(name,namelen-1);
00218   if (tmpCtl == 0x0) {
00219     kprintf("Parent Node Non Existant\n");
00220     return(-1);
00221     }
00222   if (tmpCtl->children == 0x0) {
00223     tmpCtl->children = (struct sysctl_entry *)kmalloc(sizeof(struct sysctl_entry));
00224     tmpCtl->children->children = 0x0;
00225     tmpCtl->children->prev     = 0x0;
00226     tmpCtl->children->next     = 0x0;
00227     tmpCtl->children->id       = name[namelen-1];
00228     sprintf(tmpCtl->children->name,str_name);
00229     tmpCtl->children->value = (void  *)kmalloc(buf_size);
00230     memcpy(tmpCtl->children->value,buf,buf_size);
00231     tmpCtl->children->val_len = buf_size;
00232     }
00233   else {
00234     newCtl = (struct sysctl_entry *)kmalloc(sizeof(struct sysctl_entry)); 
00235     newCtl->prev     = 0x0;
00236     newCtl->next     = tmpCtl->children;
00237     newCtl->children = 0x0;
00238     newCtl->id       = name[namelen-1];
00239     sprintf(newCtl->name,str_name);
00240     newCtl->value    = (void *)kmalloc(buf_size);
00241     memcpy(newCtl->value,buf,buf_size);
00242     newCtl->val_len  = buf_size;
00243     tmpCtl->children->prev = newCtl;
00244     tmpCtl->children = newCtl;
00245     }
00246 
00247   return(0x0);
00248   }
00249 
00250 
00251 /***
00252  END
00253  ***/

Generated on Fri Dec 15 11:18:55 2006 for UbixOS V2 by  doxygen 1.4.7