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$
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 kprintf("ASD");
00161 
00162   if (uap->newlen < 0) {
00163     kprintf("Changing Not supported yet.\n");
00164     endTask(_current->id);
00165     }
00166 
00167   tmpCtl = sysctl_find(uap->name,uap->namelen);
00168   if (tmpCtl == 0x0) { 
00169     kprintf("Invalid CTL\n");
00170     for (i = 0x0;i < uap->namelen;i++)
00171       kprintf("(%i)",uap->name[i]);
00172     kprintf("\n");
00173     endTask(_current->id);
00174     }
00175 
00176   if ((u_int32_t)uap->oldlenp < tmpCtl->val_len) 
00177      memcpy(uap->old,tmpCtl->value,(uInt32)uap->oldlenp);
00178   else
00179      memcpy(uap->old,tmpCtl->value,tmpCtl->val_len);
00180 
00181   td->td_retval[0] = 0x0;
00182 
00183   return(0x0);
00184   }
00185 
00186 static struct sysctl_entry *sysctl_find(int *name,int namelen) {
00187   int i = 0x0;
00188   struct sysctl_entry *tmpCtl = 0x0;
00189   struct sysctl_entry *lCtl = ctls;
00190 
00191   /* Loop Name Len */
00192   for (i = 0x0; i < namelen;i++) {
00193     for (tmpCtl = lCtl;tmpCtl != 0x0;tmpCtl = tmpCtl->next) {
00194       //kprintf("ctlName: [%s], ctlId; [%i]\n",tmpCtl->name,tmpCtl->id);
00195       if (tmpCtl->id == name[i]) {
00196          if ((i+1) == namelen) {
00197            return(tmpCtl);
00198            }
00199          lCtl = tmpCtl->children;
00200          break;
00201          }
00202       }
00203     }
00204   return(0x0);
00205   }
00206 
00207 int sysctl_add(int *name,int namelen,char *str_name,void *buf,int buf_size) {
00208   struct sysctl_entry *tmpCtl = 0x0;
00209   struct sysctl_entry *newCtl = 0x0;
00210 
00211   /* Check if it exists */
00212   tmpCtl = sysctl_find(name,namelen);
00213   if (tmpCtl != 0x0) {
00214     kprintf("Node Exists!\n");
00215     while (1);
00216     }
00217 
00218   /* Get Parent Node */
00219   tmpCtl = sysctl_find(name,namelen-1);
00220   if (tmpCtl == 0x0) {
00221     kprintf("Parent Node Non Existant\n");
00222     return(-1);
00223     }
00224   if (tmpCtl->children == 0x0) {
00225     tmpCtl->children = (struct sysctl_entry *)kmalloc(sizeof(struct sysctl_entry));
00226     tmpCtl->children->children = 0x0;
00227     tmpCtl->children->prev     = 0x0;
00228     tmpCtl->children->next     = 0x0;
00229     tmpCtl->children->id       = name[namelen-1];
00230     sprintf(tmpCtl->children->name,str_name);
00231     tmpCtl->children->value = (void  *)kmalloc(buf_size);
00232     memcpy(tmpCtl->children->value,buf,buf_size);
00233     tmpCtl->children->val_len = buf_size;
00234     }
00235   else {
00236     newCtl = (struct sysctl_entry *)kmalloc(sizeof(struct sysctl_entry)); 
00237     newCtl->prev     = 0x0;
00238     newCtl->next     = tmpCtl->children;
00239     newCtl->children = 0x0;
00240     newCtl->id       = name[namelen-1];
00241     sprintf(newCtl->name,str_name);
00242     newCtl->value    = (void *)kmalloc(buf_size);
00243     memcpy(newCtl->value,buf,buf_size);
00244     newCtl->val_len  = buf_size;
00245     tmpCtl->children->prev = newCtl;
00246     tmpCtl->children = newCtl;
00247     }
00248 
00249   return(0x0);
00250   }
00251 
00252 
00253 /***
00254  END
00255  ***/

Generated on Tue Dec 5 23:34:58 2006 for UbixOS V2 by  doxygen 1.4.7