Chapter 5 Chapter 6 Chapter 7 chapter 6 Part 1 6.1 Part 2 Part 3 145
146
Chapter 5 Chapter 6 Chapter 7 Part 1 Part 2 Part 3 147
148
Chapter 5 Chapter 6 Chapter 7 Part 1 Part 2 Part 3 149
150
Chapter 5 Chapter 6 Chapter 7 6.2 Part 3 Part 2 Part 1 151
152
Chapter 5 Chapter 6 Chapter 7 Part 3 Part 2 Part 1 153
154
Chapter 5 Chapter 6 Chapter 7 6.3 Part 1 Part 2 Part 3 155
#ifndef _ASM_I386_UNISTD_H_ #define _ASM_I386_UNISTD_H_ /* * This file contains the system call numbers. */ #define NR_exit 1 #define NR_fork 2 #define NR_read 3... #define NR_getdents64 220 #define NR_fcntl64 221... #ifndef _ASM_I386_UNISTD_H_ #define _ASM_I386_UNISTD_H_ /* 156
Chapter 5 Chapter 6 Chapter 7 * This file contains the system call numbers. */ #define NR_exit 1 #define NR_fork 2 #define NR_read 3... #define NR_getdents64 220 #define NR_fcntl64 221 #define NR_mysyscall 222 //... Part 1... #include <linux/config.h> #include <linux/sys.h> #include <linux/linkage.h> #include <asm/segment.h> #define ASSEMBLY #include <asm/smp.h> EBX ECX... = 0x00 = 0x04 Part 2 Part 3 157
... ENTRY(sys_call_table).long SYMBOL_NAME(sys_ni_syscall) /* 0. old "setup()" system call*/.long SYMBOL_NAME(sys_exit).long SYMBOL_NAME(sys_fork)....long SYMBOL_NAME(sys_getdents64) /* 220 */.long SYMBOL_NAME(sys_fcntl64).long SYMBOL_NAME(sys_mysyscall) /* 222 */.long SYMBOL_NAME(sys_ni_syscall) /* reserved for TUX */... /* Sys_mysyscall() */ #include <linux/unistd.h> #include <linux/errno.h> #include <linux/kernel.h> #include <linux/sched.h> asmlinkage int sys_mysyscall() { printk("hello Linux Kernel Programming!\n"); return 0; } 158
Chapter 5 Chapter 6 Chapter 7... O_TARGET := kernel.o export-objs = signal.o sys.o kmod.o context.o ksyms.o pm.o exec_domain.o printk.o Part 1 Part 2 Part 3 obj-y = sched.o dma.o fork.o exec_domain.o panic.o printk.o \ module.o exit.o itimer.o info.o time.o softirq.o resource.o \ sysctl.o acct.o capability.o ptrace.o timer.o user.o \ signal.o sys.o kmod.o context.o... 159
... O_TARGET := kernel.o export-objs = signal.o sys.o kmod.o context.o ksyms.o pm.o exec_domain.o printk.o obj-y = sched.o dma.o fork.o exec_domain.o panic.o printk.o \ module.o exit.o itimer.o info.o time.o softirq.o resource.o \ sysctl.o acct.o capability.o ptrace.o timer.o user.o \ signal.o sys.o kmod.o context.o mysyscall.o... /* */ mysyscall.c #include <linux/unistd.h> _syscall0(int, mysyscall); 160
Chapter 5 Chapter 6 Chapter 7 main() { int i; i = mysyscall(); } $ vi mysys.c /* mysys.c */ #include <linux/unistd.h> _syscall0(int, mysyscall); $ gcc -c mysys.c $ ls mysys.c mysys.o $ ar -r libmy.a mysys.o $ ranlib libmy.a $ vi mysyscall1.c /* mysyscall1,c */ main() Part 3 Part 2 Part 1 161
{ int i; i = mysyscall(); } $ gcc -o mysyscall1 mysyscall.c -L /root/lib -lmy $ mysyscall1 /* module.c */ #define KERNEL #define _LINUX #define MODULE #include <linux/kernel.h> #include <linux/module.h> 162
Chapter 5 Chapter 6 Chapter 7 int init_module() { printk( Hello Module Programming!\n ); return 0; } void cleanup_module() { printk( Good-Bye Module!\n ); } Part 1 $ ls module.c $ gcc -c module.c $ ls module.c module.o $ insmod module.o Hello Module Programming! $ lsmod Module size used by module 340 0(unused) $ rmmod module Good-Bye Module! Part 2 Part 3 163
/* new_write.c new_write() */ #define MODULE #define KERNEL #include <linux/sched.h> #include <linux/kernel.h> #include <linux/string.h> #include <asm/io.h> #include <linux/module.h> #include <asm/uaccess.h> #include <sys/syscall.h> extern void *sys_call_table[]; ssize_t (*old_write)(int f, const void *b, size_t n); ssize_t new_write(int fd, const void *buf, size_t count) { char *write_buf = (char *)kmalloc(count + 1, GFP_KERNEL); copy_from_user(write_buf, buf, count); if(current->uid!= 0) 164
Chapter 5 Chapter 6 Chapter 7 } { } if(strstr(write_buf, "2004")!= NULL) printk("happy New Year!\n"); kfree(write_buf); return old_write(fd, buf, count); Part 1 int init_module(void) { old_write = sys_call_table[sys_write]; sys_call_table[sys_write] = new_write; } return 0; void cleanup_module(void) { sys_call_table[sys_write] = old_write; } Part 2 $ echo "2004 01 01" $ Happy New Year! 2004 01 01 Part 3 165
166
Chapter 5 Chapter 6 Chapter 7 >>> Key word Part 3 Part 2 Part 1 167
>>> Question 01. 02. 03. 05. 06. 07. 04. 08. 09. 168
Chapter 5 Chapter 6 Chapter 7 10. Part 3 Part 2 Part 1 169
170