/* hello.c 
 * 
 * "Hello, world" - the loadable kernel module version. 
 *
 * Compile this with 
 *
 * $ gcc -c hello.c -Wall -nostdinc -I /usr/src/linux/include -I /usr/lib/gcc/i586-mandrake-linux-gnu/3.4.1/include/
 *
 * Once compiled, it must be converted to a kernel object (.ko) file
 * using the linux kernel makefiles, and inserted into the kernel.
 * If you have your Makefile set up properly, that should look like this
 *
 * $ sudo make -C /usr/src/linux-`uname -r` SUBDIRS=$PWD modules
 * $ sudo insmod hello.ko
 *
 * Your Makefile must include this line first: 
 *
 * obj-m += hello.o
 *
 */

/* Declare what kind of code we want from the header files */
#define __KERNEL__         /* We're part of the kernel */
#define MODULE             /* Not a permanent part, though. */
/* If the lines above were not included, we could use compiler flags instead:
 * gcc -c hello.c -Wall -nostdinc -I /usr/src/linux/include -I /usr/lib/gcc/i586-mandrake-linux-gnu/3.4.1/include/ -D MODULE -D __KERNEL__
 */

/* Standard headers */
#include <linux/module.h>       /* Needed by all LKMs */
#include <linux/kernel.h>       /* Needed for KERN_ALERT */
#include <linux/init.h>         /* Needed for the macros */

/* Documentation macros.  These are not used by the kernel, but it
 * may complain if they are not provided. */

MODULE_AUTHOR("Nicholas R. Howe");
MODULE_DESCRIPTION("Simple Kernel Module");
MODULE_LICENSE("GPL");

/* Initialize the LKM */
/* This function isn't going to be used by other parts of the kernel,
 * so it is declared static.
 */
static int __init hello_init_module(void)
{
  /* The line below will print to the system log in /var/log/messages,
   * and may also show up on the console if it is visible.
   * (The console is probably not visible if you are running X windows.)
   */
  printk("<1>Hello, world - this is the kernel speaking.\n");

  /* If we return a non zero value, it means that 
   * init_module failed and the LKM can't be loaded 
   */
  return 0;
}


/* Cleanup - undo whatever hello_init_module did */
static void __exit hello_exit_module(void)
{
  /* KERN_ALERT is a macro that adds the <1> seen in the first
   * call to printk() above.  It is better style to use the
   * macros, as shown below.
   */
  printk(KERN_ALERT "Short is the life of an LKM.\n");
}

/* The macros below register the init and exit functions with the kernel.
 * On some older modules, this may be omitted, in which case you will
 * instead see functions called init_module() and cleanup_module().
 * The macros allow us to use any name we desire.
 */
module_init(hello_init_module);
module_exit(hello_exit_module);
