By Jonathan Corbet, Alessandro Rubini, Greg Kroah-Hartman
Book Price: $39.95 USD
£28.50 GBP
PDF Price: $31.99
Cover | Table of Contents | Online Book | Colophon
#include <linux/init.h>
#include <linux/module.h>
MODULE_LICENSE("Dual BSD/GPL");
static int hello_init(void)
{
printk(KERN_ALERT "Hello, world\n");
return 0;
}
static void hello_exit(void)
{
printk(KERN_ALERT "Goodbye, cruel world\n");
}
module_init(hello_init);
module_exit(hello_exit);
KERN_ALERT is the priority of the
message. We've specified a high
priority in this module, because a message with the default priority
might not show up anywhere useful, depending on the kernel version
you are running, the version of the
klogd
daemon, and your configuration. You can ignore this issue for now; we
explain it in Chapter 4.
#include <linux/module.h> #include <linux/init.h>
MODULE_LICENSE line:MODULE_LICENSE("GPL");
MODULE_AUTHOR (stating who wrote the
module), MODULE_DESCRIPTION (a human-readable
statement of what the module does), static int _ _init initialization_function(void)
{
/* Initialization code here */
}
module_init(initialization_function);
static, since they are not meant to be visible
outside the specific file; there is no hard rule about this, though,
as no function is exported to the rest of the kernel unless
explicitly requested. The _ _init token in the
definition may look a little strange; it is a hint to the kernel that
the given function is used only at initialization time. The module
loader drops the initialization function after the module is loaded,
making its memory available for other uses. There is a similar tag
(_ _initdata) for data used only during
initialization. Use of _ _init and _
_initdata is optional, but it is worth the trouble. Just be
sure not to use them for any function (or data structure) you will be
using after initialization completes. You may also encounter
_ _devinit and _ _devinitdata
in the kernel source; these translate to _ _init
and _ _initdata only if the kernel has not been
configured for hotpluggable devices. We will look at hotplug support
in Chapter 14.howmany and a character string called
whom. Our vastly more functional module then, at
load time, greets whom not just once, but
howmany times. Such a module could then be loaded
with a command line such as:insmod hellop howmany=10 whom="Mom"
module_param macro, which is defined in
moduleparam.h. module_param
takes three parameters: the name of the variable, its type, and a
permissions mask to be used for an accompanying sysfs entry. The
macro should be placed outside of any function and is typically found
near the head of the source file. So hellop
would declare its parameters and make them available to
#include <linux/init.h>
module_init(init_function);
module_exit(cleanup_function);
_ _init
_ _initdata
_ _exit
_ _exitdata
_ _init and _
_exit) and data (_ _initdata and
_ _exitdata) that are only used at module
initialization or cleanup time. Items marked for initialization may
be discarded once initialization completes; the exit items may be
discarded if module unloading has not been configured into the
kernel. These markers work by causing the relevant objects to be
placed in a special ELF section in the executable file.#include <linux/sched.h>
crw-rw-rw- 1 root root 1, 3 Apr 11 2002 null crw------- 1 root root 10, 1 Apr 11 2002 psaux crw------- 1 root root 4, 1 Oct 28 03:04 tty1 crw-rw-rw- 1 root tty 4, 64 Apr 11 2002 ttys0 crw-rw---- 1 root uucp 4, 65 Apr 11 2002 ttyS1 crw--w---- 1 vcsa tty 7, 1 Apr 11 2002 vcs1 crw--w---- 1 vcsa tty 7, 129 Apr 11 2002 vcsa1 crw-rw-rw- 1 root root 1, 5 Apr 11 2002 zero