Newer
Older
ubix2 / src / sys / kernel / kernsysctl.c
@reddawg reddawg on 31 Oct 2006 6 KB Lots of changes
/*****************************************************************************************
 Copyright (c) 2002-2004 The UbixOS Project
 All rights reserved.

 Redistribution and use in source and binary forms, with or without modification, are
 permitted provided that the following conditions are met:

 Redistributions of source code must retain the above copyright notice, this list of
 conditions, the following disclaimer and the list of authors.  Redistributions in binary
 form must reproduce the above copyright notice, this list of conditions, the following
 disclaimer and the list of authors in the documentation and/or other materials provided
 with the distribution. Neither the name of the UbixOS Project nor the names of its
 contributors may be used to endorse or promote products derived from this software
 without specific prior written permission.

 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
 OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
 TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

 $Id$

*****************************************************************************************/

#include <ubixos/types.h>
#include <sys/sysproto.h>
#include <sys/thread.h>
#include <sys/kernsysctl.h>
#include <ubixos/endtask.h>
#include <lib/kprintf.h>
#include <lib/kmalloc.h>
#include <assert.h>
#include <string.h>

#define CTL_MAXNAME   24

static struct sysctl_entry *ctls = 0x0;

static struct sysctl_entry *sysctl_find(int *,int);

int sysctl_init() {
  struct sysctl_entry *tmpCtl = 0x0;
  if (ctls != 0x0) {
    kprintf("sysctl already Initialized\n");
    while (1);
    }

  ctls = (struct sysctl_entry *)kmalloc(sizeof(struct sysctl_entry));
  ctls->prev     = 0x0;
  ctls->id       = 0x0;
  ctls->children = 0x0;
  sprintf(ctls->name,"CTL_UNSPEC");

  tmpCtl = (struct sysctl_entry *)kmalloc(sizeof(struct sysctl_entry));
  tmpCtl->prev     = ctls;
  tmpCtl->id       = 1;
  tmpCtl->children = 0x0;
  sprintf(tmpCtl->name,"CTL_KERN");
  ctls->next = tmpCtl;

  tmpCtl->next = (struct sysctl_entry *)kmalloc(sizeof(struct sysctl_entry));
  tmpCtl->next->prev = tmpCtl;
  tmpCtl             = tmpCtl->next;
  tmpCtl->id         = 2;
  tmpCtl->children   = 0x0;
  sprintf(tmpCtl->name,"CTL_VM");

  tmpCtl->next = (struct sysctl_entry *)kmalloc(sizeof(struct sysctl_entry));
  tmpCtl->next->prev = tmpCtl;
  tmpCtl             = tmpCtl->next;
  tmpCtl->id         = 3;
  tmpCtl->children   = 0x0;
  sprintf(tmpCtl->name,"CTL_VFS");

  tmpCtl->next = (struct sysctl_entry *)kmalloc(sizeof(struct sysctl_entry));
  tmpCtl->next->prev = tmpCtl;
  tmpCtl             = tmpCtl->next;
  tmpCtl->id         = 4;
  tmpCtl->children   = 0x0;
  sprintf(tmpCtl->name,"CTL_NET");

  tmpCtl->next = (struct sysctl_entry *)kmalloc(sizeof(struct sysctl_entry));
  tmpCtl->next->prev = tmpCtl;
  tmpCtl             = tmpCtl->next;
  tmpCtl->id         = 5;
  tmpCtl->children   = 0x0;
  sprintf(tmpCtl->name,"CTL_DEBUG");

  tmpCtl->next = (struct sysctl_entry *)kmalloc(sizeof(struct sysctl_entry));
  tmpCtl->next->prev = tmpCtl;
  tmpCtl             = tmpCtl->next;
  tmpCtl->id         = 6;
  tmpCtl->children   = 0x0;
  sprintf(tmpCtl->name,"CTL_HW");

  tmpCtl->next = (struct sysctl_entry *)kmalloc(sizeof(struct sysctl_entry));
  tmpCtl->next->prev = tmpCtl;
  tmpCtl             = tmpCtl->next;
  tmpCtl->id         = 7;
  tmpCtl->children   = 0x0;
  sprintf(tmpCtl->name,"CTL_MACHDEP");

  tmpCtl->next = (struct sysctl_entry *)kmalloc(sizeof(struct sysctl_entry));
  tmpCtl->next->prev = tmpCtl;
  tmpCtl             = tmpCtl->next;
  tmpCtl->id         = 8;
  tmpCtl->children   = 0x0;
  sprintf(tmpCtl->name,"CTL_USER");

  tmpCtl->next = (struct sysctl_entry *)kmalloc(sizeof(struct sysctl_entry));
  tmpCtl->next->prev = tmpCtl;
  tmpCtl             = tmpCtl->next;
  tmpCtl->id         = 9;
  tmpCtl->children   = 0x0;
  sprintf(tmpCtl->name,"CTL_P1003_1B");

  return(0x0);
  }

static void def_ctls() {
  int name[CTL_MAXNAME], name_len;
  uInt32 page_val = 0x1000;
  name[0] = 6;
  name[1] = 7;
  name_len = 2; 
  sysctl_add(name,name_len,"page_size",&page_val,sizeof(uInt32));
  }

int __sysctl(struct thread *td, struct sysctl_args *uap) {
  struct sysctl_entry *tmpCtl = 0x0;
  if (ctls == 0x0) {
    def_ctls();
    } 
  if (uap->newlen < 0) {
    kprintf("Changing Not supported yet.\n");
    endTask(_current->id);
    }

  tmpCtl = sysctl_find(uap->name,uap->namelen);
  if (tmpCtl == 0x0) { 
    kprintf("Invalid CTL\n");
    endTask(_current->id);
    }

  if (uap->oldlenp < tmpCtl->val_len) 
     memcpy(uap->old,tmpCtl->value,(uInt32)uap->oldlenp);
  else
     memcpy(uap->old,tmpCtl->value,tmpCtl->val_len);

  td->td_retval[0] = 0x0;

  return(0x0);
  }

static struct sysctl_entry *sysctl_find(int *name,int namelen) {
  int i = 0x0;
  struct sysctl_entry *tmpCtl = 0x0;
  struct sysctl_entry *lCtl = ctls;

  /* Loop Name Len */
  for (i = 0; i < namelen;i++) {
    for (tmpCtl = lCtl;tmpCtl != 0x0;tmpCtl = tmpCtl->next) {
      //kprintf("ctlName: [%s], ctlId; [%i]\n",tmpCtl->name,tmpCtl->id);
      if (tmpCtl->id == name[i]) {
         if ((i+1) == namelen) {
           return(tmpCtl);
           }
         lCtl = tmpCtl->children;
         break;
         }
      }
    }
  return(0x0);
  }

int sysctl_add(int *name,int namelen,char *str_name,void *buf,int buf_size) {
  struct sysctl_entry *tmpCtl = 0x0;

  if (ctls == 0x0) {
    sysctl_init();
    }
  
  /* Check if it exists */
  tmpCtl = sysctl_find(name,namelen);
  if (tmpCtl != 0x0) {
    kprintf("Node Exists!\n");
    while (1);
    }

  /* Get Parent Node */
  tmpCtl = sysctl_find(name,namelen-1);
  if (tmpCtl == 0x0) {
    kprintf("Parent Node Non Existant\n");
    return(-1);
    }
  if (tmpCtl->children == 0x0) {
    tmpCtl->children = (struct sysctl_entry *)kmalloc(sizeof(struct sysctl_entry));
    tmpCtl->children->children = 0x0;
    tmpCtl->children->prev     = 0x0;
    tmpCtl->children->next     = 0x0;
    tmpCtl->children->id       = name[namelen-1];
    sprintf(tmpCtl->children->name,str_name);
    tmpCtl->children->value = (void  *)kmalloc(buf_size);
    memcpy(tmpCtl->children->value,buf,buf_size);
    tmpCtl->children->val_len = buf_size;
    }
  else {
    kprintf("CTL error\n");
    while (1);
    } 

  return(0x0);
  }


/***
 END
 ***/