The kernel module traverses processes and task queues and saves them to the proc file.
A module uses it to traverse the parent process and task queue of the current process, and output the traversal result to a proc file (traversal can start from the current process, the parent process traverses the initialization process, and the task queue can be traversed using the for_each_process macro ).
The following is the implementation of my kernel module:
/*************************************** * ******************** Use the kernel module to start previous traversal from the current process, find the first process and save the traversal result to the proc file ********************** **************************************/# include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include # define MODULE_NAME "MyProcess" # define MYDATA_LEN 10000 // put user space data struct my_proc_data {char value [MYDATA_LEN] ;}; struct my_proc_data mydata, fathers_data; // proc Structure Variable struct proc_dir_entry * example_dir; // stores the task queue struct proc_dir_entry * date_file; // stores the parent process struct proc_dir_entry * father_file; static int param; module_param (param, int, 0644); // Read File driver function static int proc_read (char * page, char ** start, off_t off, int Count, int * eof, void * data) {int len; struct my_proc_data * mydatap = (struct my_poroc_data *) data; len + = sprintf (page, "% s ", mydatap-> value); return len;} // write the file driver function static int proc_write (struct file * file, const char * buffer, unsigned long count, void * data) {int len; struct my_proc_data * mydatap = (struct my_proc_data *) data; if (count> MYDATA_LEN) len = MYDATA_LEN; else len = count; if (copy_from_user (Mydatap-> value, buffer, len) {return-EFAULT;} mydatap-> value [len-1] = '\ 0'; return len ;} // load module int init_module (void) {// create the dir folder example_dir = (struct proc_dir_entry *) proc_mkdir ("mydir", 0); if (example_dir = 0) {printk ("mkdir fail !! \ N "); return-1 ;}// create the file date_file = (struct proc_dir_entry *) create_proc_entry (" myfile ", 0666, example_dir); if (date_file = 0) {printk ("create file fails !! \ N "); return-ENOMEM;} // create the file father_file = (struct proc_dir_entry *) create_proc_entry (" fathers ", 0666, example_dir); if (father_file = 0) {printk ("create file fails !! \ N "); return-ENOMEM;} struct task_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 ;}// uninstall module void cleanup_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 ");
The Makefile is as follows:
obj-m := process.oall: make -C /lib/modules/$(shell uname -r)/build M=$(shell pwd) modulesclean: make -C /lib/modules/$(shell uname -r)/build M=$(shell pwd) clean
Now we use the make command to compile. When the compilation is complete, we dynamically load the kernel module.
sudo insmod process.ko
Then we can view the files we created under the proc document:
cat /proc/mydir/myfile
After running:
Next we will look at the files of all parent processes:
cat /proc/mydir/fathers
Running effect: