[ Bottom of Page | Previous Page | Next Page | Contents | Index | Library Home | Legal | Search ]

Kernel Extensions and Device Support Programming Concepts

demokext.c Example File

#include <sys/types.h>
#include <sys/malloc.h>
#include <sys/uio.h>
#include <sys/dump.h>
#include <sys/errno.h>
#include <sys/uprintf.h>
#include <fcntl.h>
#include "demo.h"


/* Log routine prototypes */
int open_log(char *path, struct file **fpp);
int write_log(struct file *fpp, char *buf, int *bytes_written);
int close_log(struct file *fpp);

/* Unexported symbol */
int demokext_i = 9;
/* Exported symbol */
int demokext_j = 99;

/*
 * Kernel extension entry point, called at config. time.
 *
 * input:
 *     cmd - unused (typically 1=config, 2=unconfig)
 *     uiop - points to the uio structure.
 */
int
demokext(int cmd, struct uio *uiop)
       {
       int rc;
       char *bufp;
       struct file *fpp;
       int fstat;
       char buf[100];
       int bytes_written;
       static int j = 0;

       /*
        * Open the log file.
        */
       strcpy(buf, "./demokext.log");
       fstat = open_log(buf, &fpp);
       if (fstat != 0) return(fstat);

       /*
        * Put a message out to the log file.
        */
       strcpy(buf, "demokext was called for configuration\n");
       fstat = write_log(fpp, buf, &bytes_written);
       if (fstat != 0) return(fstat);

       /*
        * Increment or decrement j and demokext_j based on
        * the input value for cmd.
        */
       {
       switch (cmd)
              {
              case 1:  /* Increment */	
                     sprintf(buf, "Before increment: j=%d demokext_j=%d\n",
                             j, demokext_j);
                     write_log(fpp, buf, &bytes_written);
                     demokext_j++;
                     j++;
                     sprintf(buf, "After increment: j=%d demokext_j=%d\n",
                             j, demokext_j);
                     write_log(fpp, buf, &bytes_written);
                     break;

              case 2:  /* Decrement */
                     sprintf(buf, "Before decrement: j=%d demokext_j=%d\n",
                             j, demokext_j);
                     write_log(fpp, buf, &bytes_written);
                     demokext_j--;
                     j--;
                     sprintf(buf, "After decrement: j=%d demokext_j=%d\n",
                             j, demokext_j);
                     write_log(fpp, buf, &bytes_written);
                     break;

              default:  /* Unknown command value */
                     sprintf(buf, "Received unknown command of %d\n", cmd);
                     write_log(fpp, buf, &bytes_written);
                     break;
              }
       }

       /*
        * Close the log file.
        */
       fstat = close_log(fpp);
       if (fstat !=0 ) return(fstat);
       return(0);
}

/***************************************************
 * Routines for logging debug information:         *
 * open_log - Opens a log file                     *
 * write_log - Output a string to a log file       *
 * close_log - Close a log file                    *
 ***************************************************/
int open_log (char *path, struct file **fpp)
       {
       int rc;
       rc = fp_open(path, O_CREAT | O_APPEND | O_WRONLY,
                    S_IRUSR | S_IWUSR, 0, SYS_ADSPACE, fpp);
       return(rc);
       }

int write_log(struct file *fpp, char *buf, int *bytes_written)
       {
       int rc;
       rc = fp_write(fpp, buf, strlen(buf), 0, SYS_ADSPACE, bytes_written);
       return(rc);
       }

int close_log(struct file *fpp)
       {
       int rc;
       rc = fp_close(fpp);
       return(rc);
       }

[ Top of Page | Previous Page | Next Page | Contents | Index | Library Home | Legal | Search ]