Last active
January 24, 2016 12:54
-
-
Save bave/20893af9bd2c52190add to your computer and use it in GitHub Desktop.
freebsd: kernel proc and thread on kernel module
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <sys/cdefs.h> | |
#include <sys/types.h> | |
#include <sys/param.h> | |
#include <sys/systm.h> | |
#include <sys/proc.h> | |
#include <sys/lock.h> | |
#include <sys/mutex.h> | |
#include <sys/kernel.h> | |
#include <sys/kthread.h> | |
#include <sys/sysctl.h> | |
#include <sys/module.h> | |
#include <sys/malloc.h> | |
static const char ktname[] = "kt_test"; | |
static MALLOC_DEFINE(M_KT, ktname, "hoge"); | |
int kt_proc_load(void); | |
int kt_proc_unload(void); | |
static struct mtx kt_mtx; | |
static struct proc* kt_proc; | |
static int kt_run_flag = 0; | |
static void | |
kt_thread(void* arg) | |
{ | |
/* | |
struct thread* td_cur = curthread | |
struct proc* proc_cur = td_cur->td_proc; | |
*/ | |
mtx_lock(&kt_mtx); | |
while(kt_run_flag) { | |
mtx_unlock(&kt_mtx); | |
printf("kt_thread\n"); | |
pause("pause_kt_thread", hz); | |
mtx_lock(&kt_mtx); | |
} | |
mtx_unlock(&kt_mtx); | |
kthread_exit(); | |
} | |
static void | |
kt_process(void* args) | |
{ | |
int err; | |
struct thread* td_cur = curthread; | |
struct proc* proc_cur = td_cur->td_proc; | |
struct thread* td_new; | |
err = kthread_add(kt_thread, NULL, proc_cur, &td_new, 0, 0, "kt_thread"); | |
mtx_lock(&kt_mtx); | |
while(kt_run_flag) { | |
mtx_unlock(&kt_mtx); | |
printf("kt_proc\n"); | |
pause("pause_kt_proc", hz); | |
mtx_lock(&kt_mtx); | |
} | |
mtx_unlock(&kt_mtx); | |
kproc_exit(0); | |
} | |
int | |
kt_proc_load(void) | |
{ | |
int err; | |
kt_run_flag = 1; | |
mtx_init(&kt_mtx, "kt_mtx", NULL, MTX_DEF); | |
err = kproc_create(kt_process, "kt_proc", &kt_proc, 0, 0, "kt_proc"); | |
return err; | |
} | |
int | |
kt_proc_unload(void) | |
{ | |
mtx_lock(&kt_mtx); | |
kt_run_flag = 0; | |
mtx_unlock(&kt_mtx); | |
pause("unloading wait", 2*hz); | |
mtx_destroy(&kt_mtx); | |
return 0; | |
} | |
// kproc_test : kt | |
static int | |
kt_modevent(module_t mod, int type, void *data) | |
{ | |
int err = 0; | |
switch (type) | |
{ | |
case MOD_LOAD: | |
{ | |
//kldload時に呼び出される | |
printf("load kt_test\n"); | |
err = kt_proc_load(); | |
if (err) return EOPNOTSUPP; | |
break; | |
} | |
case MOD_UNLOAD: | |
{ | |
//kldunload時に呼び出される | |
printf("unload kt_test\n"); | |
err = kt_proc_unload(); | |
if (err) return EOPNOTSUPP; | |
break; | |
} | |
case MOD_SHUTDOWN: | |
{ | |
//システムシャットダウン時に呼ばれる | |
break; | |
} | |
case MOD_QUIESCE: | |
{ | |
//kldunloadの最初に"MOD_QUIESCE"が呼び出される | |
//return で"!0"が返されると"MOD_UNLOAD"が呼ばれない. | |
break; | |
} | |
default: | |
{ | |
// Operation not supported | |
return EOPNOTSUPP; | |
} | |
} | |
return 0; | |
} | |
static moduledata_t kt_mod = { | |
"kproc_test", | |
kt_modevent, | |
0 | |
}; | |
DECLARE_MODULE(kt, kt_mod, SI_SUB_EXEC, SI_ORDER_ANY); | |
MODULE_VERSION(kproc_test, 1); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment