forked from illacceptanything/illacceptanything
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# Makefile for hello world kernel module | ||
obj-m := hello.o # Module Name is hello.c | ||
|
||
KDIR := /lib/modules/$(shell uname -r)/build | ||
|
||
all: | ||
$(MAKE) -C $(KDIR) M=$(PWD) modules | ||
|
||
clean: | ||
$(MAKE) -C $(KDIR) M=$(PWD) clean | ||
$(RM) Module.markers modules.order |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
hello.c | ||
--- | ||
|
||
The simplest kernel module. | ||
|
||
### Getting started | ||
|
||
Hmmm... | ||
|
||
### Building | ||
|
||
Yeah... try `make` | ||
|
||
### Testing | ||
|
||
`./test.sh` | ||
|
||
Expected output: http://showterm.io/b0ef83393d4ba67b40480 | ||
|
||
### Credits | ||
|
||
Thanks to [this tutorial](http://www.tldp.org/LDP/lkmpg/2.6/html/) by [Peter Jay Salzman](http://dirac.org/). | ||
|
||
And some thanks to my friend [Dimitris Zervas](http://dzervas.gr/) for helping me realize what a Makefile | ||
requires to build a kernel module. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
* hello.c - The simplest Linux kernel module. | ||
*/ | ||
|
||
#include <linux/module.h> /* Needed by all modules */ | ||
#include <linux/kernel.h> /* Needed for KERN_ALERT */ | ||
|
||
int init_module(void) | ||
{ | ||
printk(KERN_ALERT "Hello from the Kernel!\n"); | ||
|
||
// A non 0 return means init_module failed; module can't be loaded. | ||
return 0; | ||
} | ||
|
||
void cleanup_module(void) | ||
{ | ||
printk(KERN_ALERT "Goodbye, I'm out.\n"); | ||
} | ||
|
||
MODULE_LICENSE("GPL"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#!/bin/bash | ||
# Tests the kernel module | ||
echo 'Begin tests' | ||
|
||
# Build | ||
echo 'Running "make all" to build module' | ||
make all | ||
|
||
# Load module | ||
echo 'Running "sudo insmod hello.ko" to load module' | ||
sudo insmod hello.ko | ||
|
||
# Test if module loaded | ||
echo 'Running "lsmod | grep 'hello'" to test if module loaded properly' | ||
lsmod | grep 'hello' | ||
|
||
# Unload module | ||
echo 'Running "sudo rmmod hello" to unload module' | ||
sudo rmmod hello | ||
|
||
# Test if module unloaded | ||
echo 'Running "lsmod | grep 'hello'" to test if module unloaded properly' | ||
lsmod | grep 'hello' | ||
|
||
# Clean up like a good kid | ||
echo 'Running "make clean" to clean everything up' | ||
make clean | ||
|
||
# Check module | ||
echo 'Running "tail -n 10 /var/log/syslog" to see if module did what it was supposed to' | ||
tail -n 10 /var/log/syslog | ||
journalctl -r | ||
|
||
echo 'Tests finished' |