/* mod-syscall-hello.c * Linux Kernel Module Programmer's Guide for 2.4 Kernel. * A simple system call, printk and return starting from the * Ch 8.1 file access spy program. * * We'll used an existing syscall entry (futex) and replace it * * Include file locations * is /usr/src/linux/include/linux * is /usr/src/linux/include/asm * is /usr/include/sys * * Source file locations * sys_call_table[] /usr/src/linux/arch/i386/kernel/entry.S */ /* kernel module programming */ #ifndef MODULE #define MODULE #endif #ifndef LINUX #define LINUX #endif #ifndef __KERNEL__ #define __KERNEL__ #endif #define MAX_BUF 80 #include /* needed by all modules */ #include /* for KERN_ALERT */ #include /* for __NR_futex system call number */ /* Not used yet */ /* #include user space memory access functions */ /* The kernel will fill in the call table at dynamic load (insmod) */ extern void *sys_call_table[]; /* prototypes */ asmlinkage void my_syscall_hello(void); int init_module(void); void cleanup_module(void); /* My hello world system call */ asmlinkage void my_syscall_hello(void) { printk(KERN_ALERT "my_syscall_hello: entering.\n"); printk(KERN_ALERT "my_syscall_hello: leaving.\n"); } /* Original system call in sys.c of src/linux/kernel */ asmlinkage long (*original_call) (void); /* Initialize the module at insmod - insert the new system call */ int init_module(void) { printk(KERN_ALERT "syscall module loaded\n"); /* save the original syscall entry and insert our own entry */ original_call = sys_call_table[__NR_futex]; sys_call_table[__NR_futex] = my_syscall_hello; /* A non-zero return means init_module failed; module can't be loaded */ return 0; } /* Restore the preious system call at rmmod */ void cleanup_module(void) { if (sys_call_table[__NR_futex] != my_syscall_hello) { printk(KERN_ALERT "syscall module entry has been corrupted\n"); } /* restore the original syscall entry */ sys_call_table[__NR_futex] = original_call; printk(KERN_ALERT "syscall module unloaded\n"); } MODULE_LICENSE("GPL");