/* syscall-hello.c * A simple system call, printk and return starting from the * Ch 8.1 file access spy program. * Linux Kernel Module Programmer's Guide for 2.4 Kernel * * We'll used an existing syscall entry (futex) and replace it */ /* 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 /* needed for KERN_ALERT */ #include /* the list of system calls */ #include /* pre-defined system call number constants */ #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");