how to build a kernel extension

John R. Miller/20000 john at fulcrum.austin.ibm.com
Wed Oct 31 07:10:28 AEST 1990


In article <4648 at spdcc.SPDCC.COM> dyer at spdcc.COM (Steve Dyer) writes:

   I have the manual named "Kernel Extensions and Device Support Programming
   Concepts", but while it goes into some issues in excruciating detail,
   I can't really find ANYWHERE where it addresses the simple step-by-step
   set of instructions you must follow to create a loadable kernel extension
   file once you compile your extension foo.c and get a foo.o file.

I'm including a trivial example below.  `unshar', `make all' and
you've got a loadable kernel extension that adds one.  `make' and you
can run the test as well.  Note that loading a kernel extension
requires "appropriate privilege" (i.e., root-ness).  Hope this helps.
--------
John R. Miller (not to be confused with my employer)		512/823-3867
john at fulcrum.austin.ibm.com 
... at cs.utexas.edu:ibmaus!auschs!fulcrum.austin.ibm.com!john


---X---X---X---X---X---X---X-cut here--X---X---X---X---X---X---X---X---X---
#! /bin/sh
#
# This is a shell archive.  Save this into a file, edit it
# and delete all lines above this comment.  Then give this
# file to sh by executing the command "sh file".  The files
# will be extracted into the current directory owned by
# you with default permissions.
#
# The files contained herein are:
#
# -rw-r--r--   1 john     staff        391 Oct 27 20:32 Makefile
# -rw-r--r--   1 john     staff         61 Oct 27 20:32 bingo.c
# -rw-r--r--   1 john     staff         22 Oct 27 21:03 bingo.exp
# -rw-r--r--   1 john     staff        339 Oct 27 20:49 loadbingo.c
# -rw-r--r--   1 john     staff        429 Oct 27 20:49 querybingo.c
# -rw-r--r--   1 john     staff        274 Oct 27 20:33 testofbingo.c
# -rw-r--r--   1 john     staff        506 Oct 27 20:49 unloadbingo.c
#
echo 'x - Makefile'
if test -f Makefile; then echo 'shar: not overwriting Makefile'; else
sed 's/^X//' << 'UnLiKeLy@@@StUfF' > Makefile
Xtestofbingo:	testofbingo.o
X		cc -o testofbingo testofbingo.o -bI:bingo.exp
X
Xloadbingo:	loadbingo.o
X		cc -o loadbingo loadbingo.o
X
Xquerybingo:	querybingo.o
X		cc -o querybingo querybingo.o
X
Xunloadbingo:	unloadbingo.o
X		cc -o unloadbingo unloadbingo.o
X
Xbingosyscall.o:	bingo.o bingo.exp
X		ld -o bingosyscall.o bingo.o -bE:bingo.exp -bM:SRE
X
Xall:	loadbingo querybingo unloadbingo bingosyscall.o
UnLiKeLy@@@StUfF
if test `wc -c < Makefile` -ne 391; then
	echo 'shar: Makefile was damaged during transit (should have been 391 bytes)'
fi
fi		; : end of overwriting check
echo 'x - bingo.c'
if test -f bingo.c; then echo 'shar: not overwriting bingo.c'; else
sed 's/^X//' << 'UnLiKeLy@@@StUfF' > bingo.c
X/* this game is rigged */
X
Xint
Xbingo(int i)
X{
X	return i+1;
X}
UnLiKeLy@@@StUfF
if test `wc -c < bingo.c` -ne 61; then
	echo 'shar: bingo.c was damaged during transit (should have been 61 bytes)'
fi
fi		; : end of overwriting check
echo 'x - bingo.exp'
if test -f bingo.exp; then echo 'shar: not overwriting bingo.exp'; else
sed 's/^X//' << 'UnLiKeLy@@@StUfF' > bingo.exp
X#!/unix
Xbingo	syscall
UnLiKeLy@@@StUfF
if test `wc -c < bingo.exp` -ne 22; then
	echo 'shar: bingo.exp was damaged during transit (should have been 22 bytes)'
fi
fi		; : end of overwriting check
echo 'x - loadbingo.c'
if test -f loadbingo.c; then echo 'shar: not overwriting loadbingo.c'; else
sed 's/^X//' << 'UnLiKeLy@@@StUfF' > loadbingo.c
X/* load the well known bingo sbr */
X
X#include <sys/sysconfig.h>
X
Xmain()
X{
X	int kmod_id;
X	struct cfg_load cfg_ld;
X	char *path = "./bingosyscall.o";
X
X	cfg_ld.path = path;
X	if (sysconfig(SYS_KLOAD, (void *)&cfg_ld, (int)sizeof(cfg_ld))) {
X		perror("sysconfig(SYS_KLOAD)");
X		exit(1);
X	}
X	printf("Its kmid is %d.\n", cfg_ld.kmid);
X	exit(0);
X}
UnLiKeLy@@@StUfF
if test `wc -c < loadbingo.c` -ne 339; then
	echo 'shar: loadbingo.c was damaged during transit (should have been 339 bytes)'
fi
fi		; : end of overwriting check
echo 'x - querybingo.c'
if test -f querybingo.c; then echo 'shar: not overwriting querybingo.c'; else
sed 's/^X//' << 'UnLiKeLy@@@StUfF' > querybingo.c
X/* is the well known bingo sbr loaded? */
X
X#include <sys/sysconfig.h>
X
Xmain()
X{
X	int kmod_id;
X	struct cfg_load cfg_ld;
X	char *path = "./bingosyscall.o";
X
X	cfg_ld.path = path;
X	if (sysconfig(SYS_QUERYLOAD, (void *)&cfg_ld, (int)sizeof(cfg_ld))) {
X		perror("sysconfig(SYS_KULOAD)");
X		exit(1);
X	}
X	if (cfg_ld.kmid)
X		printf("It's there all right.  Its kmid is %d.\n", cfg_ld.kmid);
X	else
X		printf("It's not there.\n");
X	exit(0);
X}
UnLiKeLy@@@StUfF
if test `wc -c < querybingo.c` -ne 429; then
	echo 'shar: querybingo.c was damaged during transit (should have been 429 bytes)'
fi
fi		; : end of overwriting check
echo 'x - testofbingo.c'
if test -f testofbingo.c; then echo 'shar: not overwriting testofbingo.c'; else
sed 's/^X//' << 'UnLiKeLy@@@StUfF' > testofbingo.c
X/* ah.  but does bingo() work? */
X
X#include <stdio.h>
X
Xmain()
X{
X	int rc;
X
X	if ((rc = bingo(3)) != 4) {
X		perror("bingo");
X		fprintf(stderr, "bingo() is a fraud!\n");
X		fprintf(stderr, "I sent 3 and it returned %d\n", rc);
X		exit(1);
X	}
X	printf("all is well\n");
X	exit(0);
X}
UnLiKeLy@@@StUfF
if test `wc -c < testofbingo.c` -ne 274; then
	echo 'shar: testofbingo.c was damaged during transit (should have been 274 bytes)'
fi
fi		; : end of overwriting check
echo 'x - unloadbingo.c'
if test -f unloadbingo.c; then echo 'shar: not overwriting unloadbingo.c'; else
sed 's/^X//' << 'UnLiKeLy@@@StUfF' > unloadbingo.c
X/* unload the well known bingo sbr */
X
X#include <sys/sysconfig.h>
X
Xmain()
X{
X	int kmod_id;
X	struct cfg_load cfg_ld;
X	char *path = "./bingosyscall.o";
X
X	cfg_ld.path = path;
X	if (sysconfig(SYS_QUERYLOAD, (void *)&cfg_ld, (int)sizeof(cfg_ld))) {
X		perror("sysconfig(SYS_KULOAD)");
X		exit(1);
X	}
X	if (!cfg_ld.kmid) {
X		printf("I can't unload it.  It's not there.\n");
X		exit(1);
X	}
X	if (sysconfig(SYS_KULOAD, (void *)&cfg_ld, (int)sizeof(cfg_ld))) {
X		perror("sysconfig(SYS_KULOAD)");
X		exit(1);
X	}
X	exit(0);
X}
UnLiKeLy@@@StUfF
if test `wc -c < unloadbingo.c` -ne 506; then
	echo 'shar: unloadbingo.c was damaged during transit (should have been 506 bytes)'
fi
fi		; : end of overwriting check
exit 0
--
John R. Miller
office: 512/823-3867		john at fulcrum.austin.ibm.com
home:	512/331-0155		john at crcaus.cactus.org



More information about the Comp.unix.aix mailing list