6

I'm very new to kernel module programming and right now I'm trying to run the most basic hello world module program, however I could not get any output.

I have written the hello world program introduced in Linux Device Drivers 3rd ed and got some help from this website and this one.

hello.c

#include <linux/module.h>
#include <linux/kernel.h>

MODULE_LICENSE("GPL");

static int hello_init(void){
    printk("<1>Hello, world!\n");
    return 0;
}

static void hello_exit(void){
    printk(KERN_ALERT "Goodbye, world..\n");
}

module_init(hello_init);
module_exit(hello_exit);

The file is in /home/volkan/drive directory. Along with the c file, I have my Makefile

Makefile

obj-m += hello.o

From the terminal, I execute this command for compiling the module:

sudo make -C /lib/modules/3.8.0-19-generic/build M=/home/volkan/drive/ modules

Resulting in:

make: Entering directory `/usr/src/linux-headers-3.8.0-19-generic'
  CC [M]  /home/volkan/drive/hello.o
  Building modules, stage 2.
  MODPOST 1 modules
  CC      /home/volkan/drive/hello.mod.o
  LD [M]  /home/volkan/drive/hello.ko
make: Leaving directory `/usr/src/linux-headers-3.8.0-19-generic'

I assume up to this point, nothing went wrong. Now, I insert my module and then remove:

volkan@Varaquilex ~/drive $ sudo insmod ./hello.ko
volkan@Varaquilex ~/drive $ sudo rmmod hello
volkan@Varaquilex ~/drive $ 

There is no output. I also have little experience in linux, so explanatory answers are more than welcome. Am I doing something wrong? Why cannot I see any output?

Varaquilex
  • 3,447
  • 7
  • 40
  • 60
  • 1
    Do you have any output in `dmesg`? – terdon Dec 09 '13 at 16:49
  • When I `cat dmesg` I see lots of stuff, but nothing related to the module. I assume it should be in the end of file but not only its not in the end, its not in the middle or beginning either. – Varaquilex Dec 09 '13 at 16:52
  • However, `cat kern.log` did the trick: ` Dec 9 18:42:46 Varaquilex kernel: [ 2314.937815] <1>Hello, world! Dec 9 18:42:49 Varaquilex kernel: [ 2317.873400] Goodbye, world.. ` – Varaquilex Dec 09 '13 at 16:54

1 Answers1

5

The kernel messages are logged in the kern.log file located in /var/log. Depending on your system, it may also be in dmesg. So you have to cat accordingly.

Use the command cat /var/log/kern.log

Dec  9 18:51:10 Varaquilex kernel: [ 2818.079572] <1>Hello, world!
Dec  9 18:55:02 Varaquilex kernel: [ 3050.256134] Goodbye, world..
Varaquilex
  • 3,447
  • 7
  • 40
  • 60
  • 2
    Note they aren't *necessarily* there; this is determined in `/etc/syslog.conf` (but look for `/etc/rsyslog.conf` first, that will take precedence if it exists). You'll find some reference to `kern.log` in that file. You'll also notice the priorities (1 = alert, etc) are used there. Strange this didn't end up in `dmesg` tho. – CodeClown42 Dec 09 '13 at 17:00
  • Hmmm -- notice `<1>Hello, world!`. Methinks the API has changed a bit since your reference was written, and that's perhaps not being processed as a priority, so the default is used. Something to investigate, anyway. – CodeClown42 Dec 09 '13 at 17:07
  • I looked up `rsyslog.conf`. Nothing related to `kern.log` there. When I search for `kern` there is only 1 line that has a match: `$ModLoad imklog # provides kernel logging support`. Perhaps this is the reference to `kern.log` file? – Varaquilex Dec 09 '13 at 17:08
  • Maybe, although the directives should still be directives; it could be that `imklog` includes its own defaults (although the same module is used here, but I have no `/var/log/kern.log`...). There's probably also an `/etc/rsyslog.d/` directory with stuff included by `rsyslog.conf`; those files may or may not contain much. – CodeClown42 Dec 09 '13 at 17:12
  • Also possible: your system isn't actually using `rsyslogd` -- try `ps -A | grep syslog`, you'll get either `syslogd` or `rsyslogd`. They use different conf files (hence `/etc/syslog.conf`). – CodeClown42 Dec 09 '13 at 17:17