diff --git a/LKMPG-3.16.html b/LKMPG-3.16.html index 06d6254..12b7062 100644 --- a/LKMPG-3.16.html +++ b/LKMPG-3.16.html @@ -3,7 +3,7 @@ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> - + The Linux Kernel Module Programming Guide @@ -283,7 +283,7 @@ for the JavaScript code in this tag.
  • Using /proc For Input
  • Talking To Device Files @@ -377,7 +377,7 @@ If you publish or distribute this book commercially, donations, royalties, and/o

    -The Linux Kernel Module Programming Guide was originally written for the 2.2 kernels by Ori Pomerantz. Eventually, Ori no longer had time to maintain the document. After all, the Linux kernel is a fast moving target. Peter Jay Salzman took over maintenance and updated it for the 2.4 kernels. Eventually, Peter no longer had time to follow developments with the 2.6 kernel, so Michael Burian became a co-maintainer to update the document for the 2.6 kernels. Bob Mottram updated the examples for 3.16 kernels. +The Linux Kernel Module Programming Guide was originally written for the 2.2 kernels by Ori Pomerantz. Eventually, Ori no longer had time to maintain the document. After all, the Linux kernel is a fast moving target. Peter Jay Salzman took over maintenance and updated it for the 2.4 kernels. Eventually, Peter no longer had time to follow developments with the 2.6 kernel, so Michael Burian became a co-maintainer to update the document for the 2.6 kernels. Bob Mottram updated the examples for 3.8 and 3.16 kernels.

    @@ -986,56 +986,11 @@ Module xxxxxx loaded, with warnings

    -There is a mechanism to identify code licensed under the GPL (and friends) so that people can be warned about closed source proprietary stuff, which is likely to be a security problem. This is accomplished by the MODULE_LICENSE() macro which is demonstrated in the next piece of code. By setting the license to GPL, you can keep the warning from being printed. This license mechanism is defined and documented in linux/module.h: -

    - -
    - -
    /*
    - * The following license idents are currently accepted as indicating free
    - * software modules
    - *
    - *      "GPL"                           [GNU Public License v2 or later]
    - *      "GPL v2"                        [GNU Public License v2]
    - *      "GPL and additional rights"     [GNU Public License v2 rights and more]
    - *      "Dual BSD/GPL"                  [GNU Public License v2
    - *                                       or BSD license choice]
    - *      "Dual MIT/GPL"                  [GNU Public License v2
    - *                                       or MIT license choice]
    - *      "Dual MPL/GPL"                  [GNU Public License v2
    - *                                       or Mozilla license choice]
    - *
    - * The following other idents are available
    - *
    - *      "Proprietary"                   [Non free products]
    - *
    - * There are dual licensed components, but when running with Linux it is the
    - * GPL that is relevant so this is a non issue. Similarly LGPL linked with GPL
    - * is a GPL combined work.
    - *
    - * This exists for several reasons
    - * 1.   So modinfo can show license info for users wanting to vet their setup
    - *      is free
    - * 2.   So the community can ignore bug reports including proprietary modules
    - * 3.   So vendors can do likewise based on their own policies
    - */
    -
    -
    - -

    -Similarly, MODULE_DESCRIPTION() is used to describe what the module does, MODULE_AUTHOR() declares the module's author, and MODULE_SUPPORTED_DEVICE() declares what types of devices the module supports. +You can use a few macros to indicate the license for your module. Some examples are "GPL", "GPL v2", "GPL and additional rights", "Dual BSD/GPL", "Dual MIT/GPL", "Dual MPL/GPL" and "Proprietary". They're defined within linux/module.h.

    -These macros are all defined in linux/module.h and aren't used by the kernel itself. They're simply for documentation and can be viewed by a tool like objdump. As an exercise to the reader, try and search fo these macros in linux/drivers to see how module authors use these macros to document their modules. -

    - -

    -I'd recommend to use something like grep -inr MODULE_AUTHOR in usr/src/linux-3.16.x. People unfamiliar with command line tools will probably like some web base solution, search for sites that offer kernel trees that got indexed with LXR. (or setup it up on your local machine). -

    - -

    -Users of traditional Unix editors, like emacs or vi will also find tag files useful. They can be generated by make tags or make TAGS in usr/src/linux-3.16.x. Once you've got such a tagfile in your kernel tree you can put the cursor on some function call and use some key combination to directly jump to the definition function. +To reference what license you're using a macro is available called MODULE_LICENSE. This and a few other macros describing the module are illustrated in the below example.

    @@ -1050,8 +1005,11 @@ Users of traditional Unix editors, like emacs or vi will also find tag files use #include <linux/module.h> /* Needed by all modules */ #include <linux/kernel.h> /* Needed for KERN_INFO */ #include <linux/init.h> /* Needed for the macros */ -#define DRIVER_AUTHOR "Peter Jay Salzman <p@dirac.org>" -#define DRIVER_DESC "A sample driver" + +MODULE_LICENSE("GPL"); +MODULE_AUTHOR("Bob Mottram"); +MODULE_DESCRIPTION("A sample driver"); +MODULE_SUPPORTED_DEVICE("testdevice"); static int __init init_hello_4(void) { @@ -1066,28 +1024,6 @@ Users of traditional Unix editors, like emacs or vi will also find tag files use module_init(init_hello_4); module_exit(cleanup_hello_4); - -/* - * You can use strings, like this: - */ - -/* - * Get rid of taint message by declaring code as GPL. - */ -MODULE_LICENSE("GPL"); - -/* - * Or with defines, like this: - */ -MODULE_AUTHOR(DRIVER_AUTHOR); /* Who wrote this module? */ -MODULE_DESCRIPTION(DRIVER_DESC); /* What does this module do */ - -/* - * This module uses /dev/testdevice. The MODULE_SUPPORTED_DEVICE macro might - * be used in the future to help automatic configuration of modules, but is - * currently unused other than for documentation purposes. - */ -MODULE_SUPPORTED_DEVICE("testdevice"); @@ -2075,8 +2011,7 @@ HelloWorld! size_t buffer_length, loff_t * offset) { int ret=0; - if(strlen(buffer) ==0) - { + if(strlen(buffer) ==0) { printk(KERN_INFO "procfile read %s\n",filePointer->f_path.dentry->d_name.name); ret=copy_to_user(buffer,"HelloWorld!\n",sizeof("HelloWorld!\n")); ret=sizeof("HelloWorld!\n"); @@ -2093,8 +2028,7 @@ HelloWorld! int init_module() { Our_Proc_File = proc_create(procfs_name,0644,NULL,&proc_file_fops); - if(NULL==Our_Proc_File) - { + if(NULL==Our_Proc_File) { proc_remove(Our_Proc_File); printk(KERN_ALERT "Error:Could not initialize /proc/%s\n",procfs_name); return -ENOMEM; @@ -2174,8 +2108,7 @@ The only memory segment accessible to a process is its own, so when writing regu size_t buffer_length, loff_t * offset) { int ret=0; - if(strlen(buffer) ==0) - { + if(strlen(buffer) ==0) { printk(KERN_INFO "procfile read %s\n",filePointer->f_path.dentry->d_name.name); ret=copy_to_user(buffer,"HelloWorld!\n",sizeof("HelloWorld!\n")); ret=sizeof("HelloWorld!\n"); @@ -2215,8 +2148,7 @@ The only memory segment accessible to a process is its own, so when writing regu int init_module() { Our_Proc_File = proc_create(PROCFS_NAME,0644,NULL,&proc_file_fops); - if(NULL==Our_Proc_File) - { + if(NULL==Our_Proc_File) { proc_remove(Our_Proc_File); printk(KERN_ALERT "Error:Could not initialize /proc/%s\n",PROCFS_NAME); return -ENOMEM; @@ -2245,7 +2177,7 @@ The only memory segment accessible to a process is its own, so when writing regu

    Manage /proc file with standard filesystem

    -We have seen how to read and write a /proc file with the /proc interface. But it's also possible to manage /proc file with inodes. The main interest is to use advanced function, like permissions. +We have seen how to read and write a /proc file with the /proc interface. But it's also possible to manage /proc file with inodes. The main concern is to use advanced functions, like permissions.

    @@ -2676,11 +2608,35 @@ You can also read the code of fs/seq_file.c in the linux kernel.

    Using /proc For Input

    -

    TODO : Write a chapter about sysfs

    +

    sysfs: Interacting with your module

    -This is just a placeholder for now. Finally I'd like to see a (yet to be written) chapter about sysfs instead here. If you are familiar with sysfs and would like to take part in writing this chapter, feel free to contact us (the LKMPG maintainers) for further details. +sysfs allows you to interact with the running kernel by reading or setting variables inside of modules. This can be useful for debugging purposes, or just as an interface for userland applications.

    + +

    +An example of a hello world module which includes a variable accessible via sysfs is given below. +

    + +
    + +
    TODO
    +
    +
    + +

    +Then to test it: +

    + +
    + +
    make
    +sudo insmod hello-sysfs.ko
    +sudo lsmod | grep hello_sysfs
    +
    +sudo rmmod hello_sysfs
    +
    +
    diff --git a/LKMPG-3.16.org b/LKMPG-3.16.org index 93e1eb6..e488d29 100644 --- a/LKMPG-3.16.org +++ b/LKMPG-3.16.org @@ -401,46 +401,9 @@ Warning: loading xxxxxx.ko will taint the kernel: no license Module xxxxxx loaded, with warnings #+END_SRC -There is a mechanism to identify code licensed under the GPL (and friends) so that people can be warned about closed source proprietary stuff, which is likely to be a security problem. This is accomplished by the *MODULE_LICENSE()* macro which is demonstrated in the next piece of code. By setting the license to GPL, you can keep the warning from being printed. This license mechanism is defined and documented in *linux/module.h*: +You can use a few macros to indicate the license for your module. Some examples are /"GPL"/, /"GPL v2"/, /"GPL and additional rights"/, /"Dual BSD/GPL"/, /"Dual MIT/GPL"/, /"Dual MPL/GPL"/ and /"Proprietary"/. They're defined within *linux/module.h*. -#+BEGIN_SRC c -/* - * The following license idents are currently accepted as indicating free - * software modules - * - * "GPL" [GNU Public License v2 or later] - * "GPL v2" [GNU Public License v2] - * "GPL and additional rights" [GNU Public License v2 rights and more] - * "Dual BSD/GPL" [GNU Public License v2 - * or BSD license choice] - * "Dual MIT/GPL" [GNU Public License v2 - * or MIT license choice] - * "Dual MPL/GPL" [GNU Public License v2 - * or Mozilla license choice] - * - * The following other idents are available - * - * "Proprietary" [Non free products] - * - * There are dual licensed components, but when running with Linux it is the - * GPL that is relevant so this is a non issue. Similarly LGPL linked with GPL - * is a GPL combined work. - * - * This exists for several reasons - * 1. So modinfo can show license info for users wanting to vet their setup - * is free - * 2. So the community can ignore bug reports including proprietary modules - * 3. So vendors can do likewise based on their own policies - */ -#+END_SRC - -Similarly, *MODULE_DESCRIPTION()* is used to describe what the module does, *MODULE_AUTHOR()* declares the module's author, and *MODULE_SUPPORTED_DEVICE()* declares what types of devices the module supports. - -These macros are all defined in *linux/module.h* and aren't used by the kernel itself. They're simply for documentation and can be viewed by a tool like objdump. As an exercise to the reader, try and search fo these macros in *linux/drivers* to see how module authors use these macros to document their modules. - -I'd recommend to use something like *grep -inr MODULE_AUTHOR* in */usr/src/linux-3.16.x/*. People unfamiliar with command line tools will probably like some web base solution, search for sites that offer kernel trees that got indexed with LXR. (or setup it up on your local machine). - -Users of traditional Unix editors, like emacs or vi will also find tag files useful. They can be generated by make tags or make TAGS in */usr/src/linux-3.16.x/*. Once you've got such a tagfile in your kernel tree you can put the cursor on some function call and use some key combination to directly jump to the definition function. +To reference what license you're using a macro is available called *MODULE_LICENSE*. This and a few other macros describing the module are illustrated in the below example. *** Example: hello-4.c #+BEGIN_SRC c @@ -450,8 +413,11 @@ Users of traditional Unix editors, like emacs or vi will also find tag files use #include /* Needed by all modules */ #include /* Needed for KERN_INFO */ #include /* Needed for the macros */ -#define DRIVER_AUTHOR "Peter Jay Salzman " -#define DRIVER_DESC "A sample driver" + +MODULE_LICENSE("GPL"); +MODULE_AUTHOR("Bob Mottram"); +MODULE_DESCRIPTION("A sample driver"); +MODULE_SUPPORTED_DEVICE("testdevice"); static int __init init_hello_4(void) { @@ -466,28 +432,6 @@ static void __exit cleanup_hello_4(void) module_init(init_hello_4); module_exit(cleanup_hello_4); - -/* - * You can use strings, like this: - */ - -/* - * Get rid of taint message by declaring code as GPL. - */ -MODULE_LICENSE("GPL"); - -/* - * Or with defines, like this: - */ -MODULE_AUTHOR(DRIVER_AUTHOR); /* Who wrote this module? */ -MODULE_DESCRIPTION(DRIVER_DESC); /* What does this module do */ - -/* - * This module uses /dev/testdevice. The MODULE_SUPPORTED_DEVICE macro might - * be used in the future to help automatic configuration of modules, but is - * currently unused other than for documentation purposes. - */ -MODULE_SUPPORTED_DEVICE("testdevice"); #+END_SRC ** Passing Command Line Arguments to a Module