Implement a module that uses it to traverse the parent and task queues of the current process and output the traversed results to a proc file (the traversal can start from the current process, the parent process traverses to the initialization process, and the Traverse task queue can take advantage of the For_each_process macro).
Here is the implementation part of my kernel module:
/************************************************************* using kernel modules to start a previous traversal from the current process, until the first process is found * * and save the results of the traversal to the proc file ************************************************************/#include <linux/module.h>#include <linux/init.h>#include <linux/kernel.h>#include <linux/proc_fs.h>#include <linux/jiffies.h>#include <asm/uaccess.h>#include <linux/moduleparam.h>#include <linux/list.h>#include <linux/sched.h>#include <asm/current.h>#define MODULE_NAME "Myprocess"#define Mydata_len 10000//Put data in user spacestructmy_proc_data{CharValue[mydata_len];};structMy_proc_data Mydata,fathers_data;//PROC Structure VariablesstructProc_dir_entry *example_dir;//Store task QueuestructProc_dir_entry *date_file;//Store Parent ProcessstructProc_dir_entry *father_file;Static intParam;module_param (param,int,0644);//Read file driver functionStatic intProc_read (Char*page,Char**start,off_t off,intCountint*eof,void*data) {intLenstructMy_proc_data *mydatap = (structMy_poroc_data *) data; Len + =sprintf(Page,'%s ', Mydatap->value);returnLen;}//Write file driver functionStatic intProc_write (structFile *file,Const Char*buffer,unsigned LongCountvoid*data) {intLenstructMy_proc_data *mydatap = (structMy_proc_data *) data;if(Count > Mydata_len) LEN = Mydata_len;ElseLen = count;if(Copy_from_user (Mydatap->value,buffer,len)) {return-efault; } mydatap->value[len-1] =' + ';returnLen;}//Load ModuleintInit_module (void){//Create Dir folderExample_dir = (structProc_dir_entry *) Proc_mkdir ("Mydir",0);if(Example_dir = =0) {PRINTK ("MkDir fail!! \ n ");return-1; }//Create fileDate_file = (structProc_dir_entry *) Create_proc_entry ("MyFile",0666, Example_dir);if(Date_file = =0) {PRINTK (the Create file fails!! \ n ");return-enomem; }//Create fileFather_file = (structProc_dir_entry *) Create_proc_entry ("Fathers",0666, Example_dir);if(Father_file = =0) {PRINTK (the Create file fails!! \ n ");return-enomem; }structTask_struct *pos = Get_current (); For_each_process (POS) {strcat(Mydata.value,pos->comm);strcat(Mydata.value,"\ n"); } Date_file->data = &mydata; Date_file->read_proc = &proc_read; Date_file->write_proc = &proc_write; Date_file->owner = This_module; pos = Get_current (); while(pos! = &init_task) {strcat(Fathers_data.value,pos->parent->comm);strcat(Fathers_data.value,"=");strcat(Fathers_data.value,pos->comm);strcat(Fathers_data.value,"\ n"); pos = pos->parent; } Father_file->data = &fathers_data; Father_file->read_proc = &proc_read; Father_file->write_proc = &proc_write; Father_file->owner = This_module;return 0;}//Unload modulevoidCleanup_module (void) {Remove_proc_entry ("MyFile", Example_dir); Remove_proc_entry ("Fathers", Example_dir); Remove_proc_entry ("Mydir", NULL); Printk"goodbye!! \ n ");} Module_license ("GPL"); Module_description ("This is the description"); Module_author ("Bobo");
Among the makefile files are:
obj-m := process.oall: make -C /lib/modules/$(shell uname -r)/build M=$(pwd) modulesclean: make -C /lib/modules/$(shell uname -r)/build M=$(pwd
Now we compile with the make command, and when we compile, we dynamically load the kernel module
sudo insmod process.ko
Then we look at the files we created under the proc document:
cat /proc/mydir/myfile
The effect after operation is:
Let's take a look at all the files for the parent process:
cat /proc/mydir/fathers
The effect is:
Kernel module traversal process and task queue saved to the proc file