In the previous blog post, we have compiled the First_drv.ko file by makefile, which is a driver module that can be installed in Ubuntu.
Execution in/work/my_drivers/first_drv/1th/directory: Insmod First_drv.ko
If you are executing this command in a normal user state, you can see that the system reminds us: insmod:error inserting ' First_drv.ko ':-1 operation not permitted
This is because installing the driver module requires Super privilege and you can execute it in a normal user state: sudo insmod First_drv.ko, or switch directly to the root user, perform insmod First_drv.ko
Enter after found nothing happened, in fact, when we execute Insmod First_drv.ko, it is equivalent to call the FIRST_DRV.C Module_init (first_drv_init), we in the previous blog post said Module_ Init () defines a struct, and when we write Module_init (first_drv_init), there is a function pointer in the struct that points to the First_drv_init function, which jumps to the execution first_drv_ The contents of the INIT function:
static int __init first_drv_init(void)
{
printk(KERN_INFO"hello world!\n");
return 0;
}
It is easy to know that this function will print "Hello world!" when it is called, but if we analyze it correctly, why just don't see the command line output "Hello world!" it.
Someone might want to be the reason for the print level, so enter CAT/PROC/SYS/KERNEL/PRINTK,
Printed: 4 4 1 7
The first one sure is 4, the current print level "Kern_info" is 6, so instead of 7, you can let "kern_info" to print out "Hello world!" Print level, but in fact, even after changing to 7, we still don't see any movement in the command line after we execute Insmod First_drv.ko.
In fact, the data that is strictly limited to PRINTK output in Ubuntu is shown on the command line, and there is a set of mechanisms within which we will not see the "Hello world!", regardless of the print level.
But fortunately we can see the hidden kernel print information of Ubuntu by executing the DMESG command:
[0.000000] Initializing cgroup Subsys cpuset
[0.000000] Initializing cgroup Subsys CPU
···
[0.000000] Tsc:frequency read from the hypervisor
[0.000000] detected 2501.000 MHz processor.
[0.001144] Console:colour vga+ 80x25
[0.001146] console [TTY0] enabled
···
[2.665131] ext4-fs:file extents enabled
[2.666123] Ext4-fs:mballoc enabled
[2.666135] Ext4-fs (SDB1): mounted filesystem with ordered data mode
[2.737083] Installing KNFSD (Copyright (C) 1996 [email protected]).
[2.811797] Eth4:link up
[5.129212] svc:failed to register LOCKDV1 RPC service (errno 97).
[5.129735] Nfsd:using/var/lib/nfs/v4recovery as the NFSV4 State recovery Directory
[5.129842] nfsd:starting 90-second grace period
[14.218140] Eth4:no IPv6 Routers present
[979.436592] first_drv:module license ' unspecified ' taints kernel.
[979.436620] Disabling lock debugging due to kernel taint
[979.437252] Hello world!
The last three lines of printed information are related to our installation of the First_drv.ko driver module, which I marked in red, and the last line of print information is the "Hello world!" We dreamed of, which was the successful invocation of the driver module in the process of first_drv_ The proof of the init function.
But our drive module is simple, except for our handwritten "Hello world!" and "Goodbye world ..." is no longer possible to print anything else, so what is the bottom line 3rd and the 2nd line?
This is in the process of installing the driver module, the system can not find the driver license information, remember we said before First_ DRV.C This driver has a disadvantage, this is its shortcomings, it is like you run a restaurant, but there is no health permit, even if the taste of food delicious, customers will be a little worried about the meal.
In fact, this license is just a form, which license is the same, after all, the license and food is not necessarily related to health, the key is to be regarded as food people have no attention to health.
Our module license is the same, and our program does not have the slightest relationship, the program is not written any worse, as long as add a module license, the kernel will be a silent to accept you, if you code to write a specification, only lost the module license, then the kernel will shout a few words:"First_drv: Module license ' unspecified ' taints kernel., "disabling lock debugging due to kernel taint", but the drive module is still working, But in order to better meet the requirements of the core, we'd better develop a good habit, plus module license, so as not to have unexpected trouble.
We only need to add one more sentence at first_drv.c: module_license ("GPL");
The first_drv.c that conform to the kernel specification are as follows:
#include <linux/module.h>
#include <linux/init.h>
static int __init first_drv_init(void)
{
printk(KERN_INFO"hello world!\n");
return 0;
}
static int __exit first_drv_exit(void)
{
printk(KERN_INFO"goodbye world...\n");
return 0;
}
module_init(first_drv_init);
module_exit(first_drv_exit);
MODULE_LICENSE("GPL");
Perform the Make recompile first_drv.c, and the newly generated First_drv.ko will overwrite the old First_drv.ko, so do not have to do clean delete the old file every time.
But the old First_drv.ko is also installed on the kernel, how do we uninstall the old First_drv.ko before installing the new First_drv.ko?
Execution Lsmod can view the installed driver modules in Ubuntu, which can be seen at the top of a large page of printed information:
Module Size used by
First_drv 1020 0
Binfmt_misc 8356 1
NFSD 241104 9
Exportfs 4412 1 NFSD
NFS 271880 0
This means that the First_drv driver module is still in the kernel and we use the Rmmod command to uninstall it:
Execute sudo rmmod first_drv or sudo rmmod first_drv.ko (no need to add sudo in the root user state)
After the carriage return nothing happens, the execution DMESG can see the last line more:
[2333.481131] Goodbye world ...
This is exactly the "Goodbye World ..." printed in the First_drv_exit function, and the same principle as the Insmod First_drv.ko, when the Rmmod First_drv.ko is executed, it is equivalent to calling First_ Module_exit (First_drv_exit) in drv.c, and then calls the First_drv_exit function to print out the "Goodbye World ..." message.
In fact, Ubuntu each time only the driver module without license issued a warning printing information, for the same driver without license, even if the uninstall and reload, Ubuntu will never print a warning message, there is a "total disappointment" to the driver, in order to let the system re-understand our new driver , and determine that the kernel will no longer print the warning message after the license has been added, we will restart Ubuntu, reboot, after executing sudo insmod first_drv.ko in this directory, then perform the DMESG print driver installation information:
...
[5.714574] svc:failed to register LOCKDV1 RPC service (errno 97).
[5.715109] Nfsd:using/var/lib/nfs/v4recovery as the NFSV4 State recovery Directory
[5.715371] nfsd:starting 90-second grace period
[14.690813] Eth4:no IPv6 Routers present
[89.383529] Hello world!
The Ubuntu kernel has fully endorsed our driver module first_drv after only "Hello world!" has been printed to prove that the "GPL" license has been added to the driver module.
This article is from the "12253782" blog, please be sure to keep this source http://12263782.blog.51cto.com/12253782/1873267
1th Linux Driver ___ Install the kernel of the driver module Love me again.