Intro to Kernel Modules and proc Sarah Diesburg
Intro to Kernel Modules and /proc Sarah Diesburg CS 3430 Operating Systems 1
Kernel Modules Or “drivers”, if you prefer… 2
Kernel Module n n A kernel module is a portion of kernel functionality that can be dynamically loaded into the operating system at run-time Example q q USB drivers File system drivers Disk drivers Cryptographic libraries 3
Why not just compile everything into the kernel? n Each machine only needs a certain number of drivers q n Load only the modules you need q n For example, should not have to load every single motherboard driver Smaller system footprint Dynamically load modules for new devices q Camera, new printer, etc. 4
Creating a Kernel Module n Hello world example 5
Sample Kernel Module: hello. c #include <linux/init. h> #include <linux/module. h> MODULE_LICENSE(“Dual BSD/GPL”); static int hello_init(void) { printk(KERN_ALERT “Hello, world!n”); return 0; } static void hello_exit(void) { printk(KERN_ALERT “Goodbye, sleepy world. n”); } module_init(hello_init); module_exit(hello_exit); 6
Sample Kernel Module: hello. c Module headers #include <linux/init. h> #include <linux/module. h> MODULE_LICENSE(“Dual BSD/GPL”); static int hello_init(void) { printk(KERN_ALERT “Hello, world!n”); return 0; } static void hello_exit(void) { printk(KERN_ALERT “Goodbye, sleepy world. n”); } module_init(hello_init); module_exit(hello_exit); 7
Sample Kernel Module: hello. c #include <linux/init. h> #include <linux/module. h> MODULE_LICENSE(“Dual BSD/GPL”); License declaration static int hello_init(void) { printk(KERN_ALERT “Hello, world!n”); return 0; } static void hello_exit(void) { printk(KERN_ALERT “Goodbye, sleepy world. n”); } module_init(hello_init); module_exit(hello_exit); 8
Sample Kernel Module: hello. c #include <linux/init. h> #include <linux/module. h> MODULE_LICENSE(“Dual BSD/GPL”); static int hello_init(void) { printk(KERN_ALERT “Hello, world!n”); return 0; } Initialization function, runs when module loaded static void hello_exit(void) { printk(KERN_ALERT “Goodbye, sleepy world. n”); } module_init(hello_init); module_exit(hello_exit); Tells kernel which function to run on load 9
Sample Kernel Module: hello. c #include <linux/init. h> #include <linux/module. h> MODULE_LICENSE(“Dual BSD/GPL”); static int hello_init(void) { printk(KERN_ALERT “Hello, world!n”); return 0; } Exit function, runs when module exits static void hello_exit(void) { printk(KERN_ALERT “Goodbye, sleepy world. n”); } module_init(hello_init); module_exit(hello_exit); Tells kernel which function to run on exit 10
Sample Kernel Module: Makefile ifneq ($(KERNELRELEASE), ) obj-m : = hello. o else KERNELDIR ? = /lib/modules/`uname -r`/build/ PWD : = `pwd` default: $(MAKE) -C $(KERNELDIR) M=$(PWD) modules endif clean: rm -f *. ko *. o Module* *mod* 11
Compile the Kernel Module /usr/src/hello$> make n Creates hello. ko – This is the finished kernel module! 12
Inserting and Removing the Module n insmod – insert a module /usr/src/hello$> sudo insmod hello. ko n rmmod – remove a module /usr/src/hello$> sudo rmmod hello. ko 13
Listing Modules n lsmod – lists all running modules /usr/src/hello$>lsmod 14
Where is it printing? n n Look inside /var/log/syslog Hint – to watch syslog in realtime, issue the following command in a second terminal: $> sudo tail –f /var/log/syslog n Demo… 15
Kernel Module vs User Application n All kernel modules are event-driven q q q n No standard C library q n n Register functions Wait for requests and service them Server/client model Why not? No floating point support Segmentation fault could freeze/crash your system q Kernel ‘oops’! 16
Kernel Functions n n printk() instead of printf() kmalloc() instead of malloc() kfree() instead of free() Where can I find definitions of these kernel functions? 17
Kernel manpages n n Section 9 of manpages Use manpages $> sudo apt-get install linux-manual-3. 16 $> man printk 18
Kernel Headers n n n n #include <linux/init. h> /* module stuff */ #include <linux/module. h> /* module stuff */ #include <asm/semaphore. h> /* locks */ #include <linux/list. h> /* linked lists */ #include <linux/string. h> /* string functions! */ Look inside linux-4. 15. 4/include/ for more… Google is also your friend 19
Project 2: Kernel Modules 20
procfs Kernel Module n procfs “hello world” example q n Creates a read/write procfs entry Steps q Create entry in module_init function n q Registers reading/writing function using file_operations structure Delete entry in module_cleanup function 21
Testing Procfs $> $> $> sudo insmod hello_proc. ko sudo tail /var/log/syslog cat /proc/helloworld echo 2 > /proc/helloworld sudo rmmod hello_proc 22
Part 2: Remember n You will write your own proc module called remember that q q Allows the user to write a string to /proc/remember (max length 80) Allows the user to read /proc/remember and get back the string that was just added n If no string was added, the string EMPTY should be read instead 23
Remember Example #> echo “Hello there” > /proc/remember #> cat /proc/remember Hello there #> 24
- Slides: 24