/* * Sample syscall module */ #include #include #include #include #include #include #include #include #include /* Our new System calls + their args structs (syscalls take only a void*) */ struct setDummy_args { int pid; int dummy; }; static int setDummy(struct thread *td, struct setDummy_args *arg) { struct proc *proc_res = NULL; int prev_dummy = 0; printf("setDummy args = %d %d\n", arg->pid, arg->dummy); proc_res = pfind(arg->pid); if (proc_res == NULL) { curthread->td_retval[0] = -1; } else { prev_dummy = proc_res->dummy; proc_res->dummy = arg->dummy; curthread->td_retval[0] = arg->dummy; PROC_UNLOCK(proc_res); printf("setDummy() - pid %d, prev. dummy %d\n", arg->pid, prev_dummy); } return 0; } struct getDummy_args { int pid; }; static int getDummy(struct thread *td, struct getDummy_args *arg) { int ret_dummy = -1; struct proc *proc_res = NULL; printf("getDummy args = %d\n", arg->pid); proc_res = pfind(arg->pid); if (proc_res == NULL) { curthread->td_retval[0] = -1; } else { ret_dummy = proc_res->dummy; curthread->td_retval[0] = ret_dummy; PROC_UNLOCK(proc_res); printf("getDummy() - pid %d, dummy %d\n", arg->pid, ret_dummy); } return 0; } static struct sysent sysent_setDummy = {2, /* number of parameters to syscall */ setDummy}; /* Function pointer to syscall */ static struct sysent sysent_getDummy = {1, getDummy}; /* Placeholder for offset value of syscall number */ static int syscallnum[2] = {NO_SYSCALL, NO_SYSCALL}; static char str[2][9] = {"setDummy", "getDummy"}; /* Entry & Exit point to the module */ static int load_handler(struct module *m, int cmd, void *arg) { int err = 0; switch (cmd) { case MOD_LOAD: printf("Syscall %s() loaded at offset %d.\n", str[(int)arg], syscallnum[(int)arg]); break; case MOD_UNLOAD: printf("Syscall %s() unloaded at offset %d.\n", str[(int)arg], syscallnum[(int)arg]); break; default: err = EINVAL; break; } return err; } /* Macro - Glue to the whole module */ /* Read "man 9 SYSCALL_MODULE" */ SYSCALL_MODULE(setDummy, &syscallnum[0], &sysent_setDummy, load_handler, 0); SYSCALL_MODULE(getDummy, &syscallnum[1], &sysent_getDummy, load_handler, 1);