| Programming in User state can pass command-line arguments via main (), while writing a kernel module is via Module_param () Module_param macros are added to the Linux 2.6 kernel, which are defined in the Include/linux/moduleparam.h file, as follows: #define MODULE_PARAM (name, type, perm) Module_param_named (name, name, type, perm)
It uses 3 parameters: the name of the parameter variable to pass, the data type of the variable, and the permissions to access the parameter. <<< what is the function of the perm parameter. The final module_param field is a permission value that represents the properties of the file node for this parameter in the Sysfs file system. You should use the values defined in <linux/stat.h>. This value controls who can access the representation of these module parameters in Sysfs. When perm is 0 o'clock, this parameter does not exist for the corresponding file node under the Sysfs file system. Otherwise, when the module is loaded, a directory named after the module name appears in the/sys/module/directory with the given permission. Permissions are defined in Include/linux/stat.h Like what: #define S_IRWXU 00700 #define S_IRUSR 00400 #define S_IWUSR 00200 #define S_IXUSR 00100
#define S_IRWXG 00070 #define S_IRGRP 00040 #define S_IWGRP 00020 #define S_IXGRP 00010
#define S_IRWXO 00007 #define S_iroth 00004 #define S_iwoth 00002 #define S_ixoth 00001
The use of S_irugo as a parameter can be read by all, but can not be changed; s_irugo| S_IWUSR allows root to change parameters. Note that if a parameter is SYSFS modified, the parameter values that your module sees also change, but your module does not have any other notifications. You should not make the module parameters writable unless you are ready to detect the change and react accordingly.
>>> This macro definition should be placed outside of any function, typically appearing in front of the source file. Define such as: static char *whom = "World"; static int howmany = 1; Module_param (howmany, int, s_irugo); Module_param (whom, Charp, S_irugo); Module parameters support many types:BOOL InvboolA Boolean (True or False) value (The associated variable should be of type int). The Invbool type reverses the value, so the truth becomes false and vice versa.Charp:A character pointer value. The memory provides the string allocation for the user, and the pointer is set accordingly.int Long Short UINT ULONG ushortThe basic variable-length integer value. An unsigned value begins with U. Array parameters, which are provided with a comma-separated list, and are supported by the module loader. Declares an array parameter, using: Module_param_array (name,type,num,perm); Here name is the name of your array (also the parameter name), type is an array element, num is an integer variable, and perm is the normal permission value. If the array parameter is set at load time, num is set to the number of supplied numbers. The module loader rejects more values than the array can put down.test module, the source program hello.c content as follows:#include <linux/init.h> #include <linux/module.h> #include <linux/moduleparam.h> module_license ("Dual BSD/GPL");
static char *who= "world"; static int times = 1; Module_param (TIMES,INT,S_IRUSR); Module_param (WHO,CHARP,S_IRUSR);
static int hello_init (void) { int i; for (i=0;i<times;i++) PRINTK (Kern_alert "(%d) Hello,%s!\n", i,who); return 0; }
static void Hello_exit (void) { PRINTK (Kern_alert "Goodbye,%s!\n", who); }
Module_init (Hello_init); Module_exit (Hello_exit);
Compiling the build executable file Hello
Insert: # insmod Hello who= "World" times=5 appeared 5 times "hello,world!" : # (1) hello,world! # (2) hello,world! # (3) hello,world! # (4) hello,world! # (5) hello,world! Uninstall: # rmmod Hello appears: #Goodbye, world! |