[ Next Article | Previous Article | Book Contents | Library Home | Legal | Search ]
Kernel Extensions and Device Support Programming Concepts

demo.c Example File

#include <sys/types.h>
#include <sys/sysconfig.h>
#include <memory.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include "demo.h"

/* Extension loading data */
struct cfg_load cfg_load;
extern int sysconfig();
extern int errno;

#define NAME_SIZE 256
#define LIBPATH_SIZE 256

main(argc,argv)
int argc;
char *argv[];
	{
	char path[NAME_SIZE];
	char libpath[LIBPATH_SIZE];
	char buf[BUFLEN];
	struct cfg_kmod cfg_kmod;
	struct extparms extparms = {argc,argv,buf,BUFLEN};
	int option = 1;
	int status = 0;

	/*
	 * Load the demo kernel extension.
	 */
	memset(path, 0, sizeof(path));
	memset(libpath, 0, sizeof(libpath));
	strcpy(path, "./demokext");
	cfg_load.path = path;
	cfg_load.libpath = libpath;
	if (sysconfig(SYS_KLOAD, &cfg_load, sizeof(cfg_load)) == CONF_SUCC) 
		{
		printf("Kernel extension ./demokext was succesfully loaded, kmid=%x\n",
		       cfg_load.kmid);
		}
	else
		{
		printf("Encountered errno=%d loading kernel extension %s\n", 
		       errno, cfg_load.path);
		exit(1);
		}

	/*
	 * Loop alterantely allocating and freeing 16K from memory.
	 */
	option = 1;
	while (option != 0)
		{
		printf("\n\n");
		printf("0. Quit and unload kernel extension\n");
		printf("1. Configure kernel extension - increment counter\n");
		printf("2. Configure kernel extension - decrement counter\n");
		printf("\n");
		printf("Enter choice: ");
		scanf("%d", &option);
		switch (option)
			{
			case 0:
				break;
			case 1:
				bzero(buf,BUFLEN);
				strcpy(buf,"sample string");
				cfg_kmod.kmid = cfg_load.kmid;
				cfg_kmod.cmd = 1;
				cfg_kmod.mdiptr = (char *)&extparms;
				cfg_kmod.mdilen = sizeof(extparms);
				if (sysconfig(SYS_CFGKMOD,&cfg_kmod, sizeof(cfg_kmod))==CONF_SUCC)
					{
					printf("Kernel extension %s was successfully configured\n",
					       cfg_load.path);
					}
				else
					{
					printf("errno=%d configuring kernel extension %s\n",
					       errno, cfg_load.path);
					}
				break;
			case 2:
				bzero(buf,BUFLEN);
				strcpy(buf,"sample string");
				cfg_kmod.kmid = cfg_load.kmid;
				cfg_kmod.cmd = 2;
				cfg_kmod.mdiptr = (char *)&extparms;
				cfg_kmod.mdilen = sizeof(extparms);
				if (sysconfig(SYS_CFGKMOD,&cfg_kmod, sizeof(cfg_kmod))==CONF_SUCC)
					{
					printf("Kernel extension %s was successfully configured\n",
					       cfg_load.path);
					}
				else
					{
					printf("errno=%d configuring kernel extension %s\n",
					       errno, cfg_load.path);
					}
				break;
			default:
				printf("\nUnknown option\n");
				break;
			}
		}	


	/*
	 * Unload the demo kernel extension.
	 */
   if (sysconfig(SYS_KULOAD, &cfg_load, sizeof(cfg_load)) == CONF_SUCC)
		{
		printf("Kernel extension %s was successfully unloaded\n", cfg_load.path);
		}
	else
		{
		printf("errno=%d unloading kernel extension %s\n", errno, cfg_load.path);
		}
	}
	

[ Next Article | Previous Article | Book Contents | Library Home | Legal | Search ]