Skip to content

Instantly share code, notes, and snippets.

@masami256
Last active December 16, 2015 17:09
Show Gist options
  • Save masami256/5467884 to your computer and use it in GitHub Desktop.
Save masami256/5467884 to your computer and use it in GitHub Desktop.
DragonflyBSDのカーネルモジュールでHello World
#include <sys/param.h>
#include <sys/module.h>
#include <sys/kernel.h>
#include <sys/systm.h>
#include <sys/kthread.h>
static struct thread *hello_thread_data = NULL;
static void
hello_thread(void __unused *arg)
{
static unsigned long i = 0;
for (;;) {
kprintf("%s:%lu\n", __FUNCTION__, i++);
suspend_kproc(hello_thread_data, 1);
}
}
static int
hello_create_thraed(void)
{
int err = 0;
err = kthread_create(&hello_thread, NULL, &hello_thread_data, "hello_thread");
if (err)
kprintf("%s:Failed to create kernel thread\n", __FUNCTION__);
return err;
}
static int
hello_event_handler(module_t mod, int what, void *arg)
{
int ret = 0;
switch (what) {
case MOD_LOAD:
kprintf("[-]hello module is loaded\n");
hello_create_thraed();
break;
case MOD_UNLOAD:
kprintf("[-]hello module is unloaded\n");
break;
default:
kprintf("[-]unknown event\n");
ret = EOPNOTSUPP;
break;
}
return 0;
}
static moduledata_t hello_moduledata = { "hello", hello_event_handler, NULL };
DECLARE_MODULE(hello, hello_moduledata, SI_SUB_CONFIGURE, SI_ORDER_SECOND);
MODULE_VERSION(hello, 1);
KMOD = hello
SRCS = hello.c
.include <bsd.kmod.mk>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment