kernel - Question about writing my own system call in FreeBSD -
ok, finish reading implementation of kill(2) of freebsd, , trying write own "kill". system call takes uid , signum , sends signal processes owned uid, excluding calling process.
how can pass uid system call? in kill(2), pid in argument struct kill_args. there structure contains uid way struct kill_args contains pid? if not, can define structure outside kernel?
it's easy, kind of involved process. here's module installs system call.
include bunch of stuff
#include <sys/types.h> #include <sys/param.h> #include <sys/proc.h> #include <sys/module.h> #include <sys/sysent.h> #include <sys/kernel.h> #include <sys/systm.h> #include <sys/sysproto.h> define structure hold arguments
struct mykill_args { int pid; int signo; }; define handling function
static int mykill(struct thread *td, void *args) { struct mykill_args *uap = args; uprintf("mykill called. pid=%d, signo=%d\n", uap->pid, uap->signo); return 0; } you need sysent object
static struct sysent mykill_sysent = { 2, /* number of arguments */ mykill /* function handling system call */ }; and offset @ system call installed.
/* choose "the next" value later. */ static int offset = no_syscall; load function
static int load(struct module *module, int cmd, void *arg) { int error = 0; switch (cmd) { case mod_load: uprintf("loading module. installing syscall at" " offset %d\n", offset); break; case mod_unload: uprintf("unloading module. syscall uninstalled from" " offset %d\n", offset); break; default: error = eopnotsupp; break; } return error; } install system call
syscall_module(mykill, &offset, &mykill_sysent, load, null); you can run system call using syscall(2). or using perl :)). here's example
[root@aiur /home/cnicutar/kld-syscall]# kldload ./mykill.ko loading module. installing syscall @ offset 210 [cnicutar@aiur ~/kld-syscall]$ perl -e 'syscall(210, 30, 15);' mykill called. pid=30, signo=15
Comments
Post a Comment