Dynamic Kernel Linker in FreeBSD 4.X

Skeleton on Howto make a Dynamic Kernel Linker in FreeBSD 4.X
$FreeBSD: kld.c,v 1.0 2003/09/21 11:29:52 padik GMT Exp $\\


#include <sys/param.h>
#include <sys/proc.h>
#include <sys/module.h>
#include <sys/kernel.h>
#include <sys/systm.h>
#include <sys/syscall.h>
#include <sys/sysent.h>
#include <sys/sysproto.h>
#include <sys/sysctl.h>
#include <sys/linker.h>
#include <sys/unistd.h>

static int syscall_trap_perform_mkdir (struct proc *p, struct mkdir_args *ua) {
uprintf("FREEBSD KERNEL MODULE SYSCALL_TRAP_MKDIR SUCCESSFUL : %s\n", ua->path);
return mkdir(p, ua);
}

static struct sysent syscall_trap_revise_mkdir = {
1,
syscall_trap_perform_mkdir
};

static int syscall_trap_init(module_t mod, int cmd, void *arg) {
int ret = 0;
switch (cmd) {
case MOD_LOAD:
sysent[SYS_mkdir] = syscall_trap_revise_mkdir;
uprintf("syscall_trap_comment_load\n");
break;
case MOD_UNLOAD:
uprintf("syscall_trap_comment_unload\n");
sysent[SYS_mkdir].sy_call = (sy_call_t *)mkdir;
break;
default:
ret = EINVAL;
}
return(ret);
}

static struct moduledata syscall_trap_module = {
"syscall_trap_comment", syscall_trap_init, NULL
};

DECLARE_MODULE(syscall, syscall_trap_module, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);

/* Makefile
* SRCS    = kld.c
* KMOD    = kld
* KO      = ${KMOD}.ko
* KLDMOD  = t
* .include <bsd.kmod.mk> */

/* root# kldload -v ./kld.ko
* root# kldstat
* root# mkdir anything
* *** You should see the result. The kernel module trapping the mkdir
* *** and displaying the message before creating a new folder.
* root# kldunload -v kld.ko */

This code is based on two source made by the following authors:

thc.org/papers/bsdkern.html
s0ftpj.org/en/site.html

Advertisements

Published by

padik

FreeBSD, CodeIgniter, TCG

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s