libjmmcg  release_579_6_g8cffd
A C++ library containing an eclectic mix of useful, advanced components.
ioctl_example.c
Go to the documentation of this file.
1 /******************************************************************************
2 ** Copyright © 2017 by J.M.McGuiness, libjmmcg@hussar.me.uk
3 **
4 ** This library is free software; you can redistribute it and/or
5 ** modify it under the terms of the GNU Lesser General Public
6 ** License as published by the Free Software Foundation; either
7 ** version 2.1 of the License, or (at your option) any later version.
8 **
9 ** This library is distributed in the hope that it will be useful,
10 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 ** Lesser General Public License for more details.
13 **
14 ** You should have received a copy of the GNU Lesser General Public
15 ** License along with this library; if not, write to the Free Software
16 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18 
19 /**
20  * \file ioctl_example.c
21  * \brief An example loadable kernel module (LKM) that can display a message
22  * in the /var/log/[kern.log|messages] file when the module is loaded and removed. The module can accept an
23  * argument when it is loaded -- the name, which appears in the kernel log files.
24 
25  Details for how to create this came from:
26 
27  -# <a href="http://tldp.org/LDP/lkmpg/2.4/html/x856.html"/>
28  -# <a href="linux-kernel-labs.github.io/master/labs/kernel_modules.html"/>
29  -# <a href="http://tldp.org/HOWTO/Module-HOWTO/"/>
30  -# <a href="http://derekmolloy.ie/writing-a-linux-kernel-module-part-1-introduction"/>
31  -# <a href="https://wiki.archlinux.org/index.php/Compile_kernel_module"/>
32 */
33 
34 #include "config.h"
35 
36 #include <linux/init.h> // Macros used to mark up functions e.g., __init __exit.
37 #include <linux/module.h> // Core header for loading LKMs into the kernel.
38 #include <linux/kernel.h> // Contains types, macros, functions for the kernel.
39 
40 MODULE_LICENSE(LIBJMMCG_MODULE_LICENSE); ///< The license type -- this affects runtime behavior.
41 MODULE_AUTHOR(LIBJMMCG_MODULE_AUTHOR); ///< The author -- visible when you use modinfo.
42 MODULE_DESCRIPTION(LIBJMMCG_MODULE_DESCRIPTION); ///< The description -- see modinfo.
43 MODULE_VERSION(LIBJMMCG_MODULE_VERSION); ///< The version of the module.
44 
45 static char *name = LIBJMMCG_MODULE_NAME; ///< An LKM argument -- default value is "LIBJMMCG_MODULE_NAME".
46 module_param(name, charp, S_IRUGO); ///< Param desc. charp = char ptr, S_IRUGO can be read/not changed.
47 MODULE_PARM_DESC(name, "The module name to display in /var/log/kern.log"); ///< Parameter description.
48 
49 /** \brief The LKM initialization function.
50  * The static keyword restricts the visibility of the function to within this C file. The __init
51  * macro means that for a built-in driver (not a LKM) the function is only used at initialization
52  * time and that it can be discarded and its memory freed up after that point.
53  * \return Returns 0 if successful.
54  */
55 static int __init init_the_module(void) {
56  printk(KERN_INFO "%s v%s initialised.\n", name, LIBJMMCG_MODULE_VERSION);
57  return 0;
58 }
59 
60 /** \brief The LKM cleanup function.
61  * Similar to the initialization function, it is static. The __exit macro notifies that if this
62  * code is used for a built-in driver (not a LKM) that this function is not required.
63  */
64 static void __exit exit_the_module(void) {
65  printk(KERN_INFO "%s unloaded.\n", name);
66 }
67 
68 /** \brief A module must use the module_init() module_exit() macros from linux/init.h, which
69  * identify the initialization function at insertion time and the cleanup function (as
70  * listed above).
71  */
72 module_init(init_the_module);
73 module_exit(exit_the_module);