Page 20 - DCAP103_Principle of operating system
P. 20

Unit 1: Operating System



                                                                                                  Notes
                           /* syscall.c
                          *
                          * System call “stealing” sample.
                          */
                          /* The necessary header files */
                          /* Standard in kernel modules */
                          #include <linux/kernel.h> /* We’re doing kernel work */
                          #include <linux/module.h> /* Specifically, a module */

                          /* Deal with CONFIG_MODVERSIONS */
                          #if CONFIG_MODVERSIONS==1
                          #define MODVERSIONS
                          #include <linux/modversions.h>
                          #endif
                          #include <sys/syscall.h> /* The list of system calls */
                          /* For the current (process) structure, we need
                          * this to know who the current user is. */
                          #include <linux/sched.h>
                          /* In 2.2.3 /usr/include/linux/version.h includes a
                          * macro for this, but 2.0.35 doesn’t - so I add it
                          * here if necessary. */
                          #ifndef KERNEL_VERSION
                          #define KERNEL_VERSION(a,b,c) ((a)*65536+(b)*256+(c))
                          #endif

                          #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,0)
                          #include <asm/uaccess.h>
                          #endif
                          /* The system call table (a table of functions). We
                          * just define this as external, and the kernel will
                          * fill it up for us when we are insmod’ed
                          */
                          extern void *sys_call_table[];

                          /* UID we want to spy on - will be filled from the
                          * command line
                          */int uid;
                          #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,0)
                          MODULE_PARM(uid, “i”);
                          #endif/
                          * A pointer to the original system call. The reason
                          * we keep this, rather than call the original function
                          * (sys_open), is because somebody else might have
                          * replaced the system call before us. Note that this
                          * is not 100% safe, because if another module
                          * replaced sys_open before us, then when we’re inserted
                          * we’ll call the function in that module - and it
                          * might be removed before we are.


                                             LOVELY PROFESSIONAL UNIVERSITY                                    13
   15   16   17   18   19   20   21   22   23   24   25