linux-kernellinux-device-driverinsmod

kernel log wouldn't show "Hello kernel" until driver removed


I have just started writing a character driver.So, i when inserted my first driver code that prints "hello kernel" from init_module1 and "Bye kernel" from exit module in kernel log. When i insert the driver and use dmesg to see kernel log i cant find the "Hello kernel" message but when i remove the driver(using rmmod) i get both "Hello kernel" as well as "Bye kernel" in the log. Cant figure out how & why. This is my code...

header.h

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

    MODULE_LICENSE("GPL");

init.c

    #include"header.h"

    static int init_module1(void)
    {
      printk(KERN_ALERT "Hello kernel");
      return 0;
    }
    module_init(init_module1);

exit.c

    #include"header.h"

    static void exit_module(void)
    {
      printk(KERN_ALERT "Bye Kernel");
    }
    module_exit(exit_module);

Makefile-

    INSTALLDIR= $(shell pwd)/modules
    ifneq ($(KERNELRELEASE),)
    obj-m := c.o
    c-objs := init.o exit.o

    else
              KERNDIR ?= /lib/modules/$(shell uname -r)/build
              PWD := $(shell pwd)

    default:
              $(MAKE) -C $(KERNDIR) M=$(PWD) modules
              @rm -rf $(INSTALLDIR)
              @mkdir $(INSTALLDIR)
              @mv *.ko *.mod.c *.o .*.cmd $(INSTALLDIR)

    clean:
              rm -rf $(INSTALLDIR)

    endif

Solution

  • The kernel log handles only complete lines. Add the missing new line character:

         printk(KERN_ALERT "Hello kernel\n");