The module boilerplate of the preceding project just printed some static text. However, it is quite typical for a module to have some variables that must be accessed during the lifetime of the module. Usually, Rust does not use mutable global variables because they are not safe and just defines them in the main function and passes them as arguments to the functions called by main. However, kernel modules do not have a main function. They have entry points called by the kernel and so, to keep shared mutable variables, some unsafe code must be used.
The State project shows you how to define and use shared mutable variables. To run it, enter the state folder and type ./bd. Then, type the following four commands:
sudo insmod state.ko
lsmod | grep -w state
sudo rmmod state
dmesg --color=always | tail
Let's see what we did there:
- The first command will load the module into the kernel with no output to the console.
- The second command will show that the module is loaded...