previous | start | next

A kernel module source code

(The following examples are from The Linux Kernel Module Programming Guide by Peter J. Saltzman, Michael Burian, and Ori Pomerantz.)

Here is their first kernel module code.

/*  
 *  hello-1.c - The simplest kernel module.
 */
    #include <linux/module.h>/* Needed by all modules */
    #include <linux/kernel.h>/* Needed for KERN_INFO */

int init_module(void)
{
        printk(KERN_INFO "Hello world 1.\n");

        /* 
         * A non 0 return means init_module failed; module can't be
        loaded. 
         */
        return 0;
}

void cleanup_module(void)
{
        printk(KERN_INFO "Goodbye world 1.\n");
}

What is KERN_INFO?

Where does printk output go?

Output goes to a log file:

 /var/log/syslog
   

and will also go to the "console" (not to an xterm window) provided the priority level is sufficiently high:

From /usr/src/[kernel version]/include/linux/printk.h

#define KERN_EMERG      "<0>"   /* system is unusable */
#define KERN_ALERT      "<1>"   /* action must be taken immediately    */
#define KERN_CRIT       "<2>"   /* critical conditions    */
#define KERN_ERR        "<3>"   /* error conditions    */
#define KERN_WARNING    "<4>"   /* warning conditions    */
#define KERN_NOTICE     "<5>"   /* normal but significant condition    */
#define KERN_INFO       "<6>"   /* informational    */
#define KERN_DEBUG      "<7>"   /* debug-level messages    */

Changing KERN_INFO to KERN_ALERT will cause the output to go to the console.



previous | start | next