Good day. I am writing a kernel module that displays all processes from the kernel. The problem is that only part of the process is output. Here is the code:

#include <linux/kernel.h> #include <linux/module.h> #include <linux/sched.h> MODULE_LICENSE("GPL"); int init_module( void ) { /* Выбираем отправную точку */ struct task_struct *task = &init_task; /* Перебираем элементы списка задач, пока снова не встретим init_task */ do { printk( KERN_INFO "Process: %s pid:[%d] \n", task->comm, task->pid); } while ( (task = next_task(task)) != &init_task ); return 0; } void cleanup_module( void ) { return; } 
  • one
    And such code also does not output all processes? // by the way, how exactly did you determine that "not all"? - aleksandr barakin
  • one
    And also note that wherever the for_each_process is used in the kernel, rcu_read_lock is always called before the loop, and rcu_read_unlock after the loop. Without bottom, the list of processes can be rebuilt 10 times while you read it, which will lead to unpredictable consequences - Mike
  • @alexanderbarakin should display all running processes, but only gives out a small part, despite the fact that it shows> 100 in the system monitor. - Max
  • @alexanderbarakin the code that you specified is similar to mine. - Max

1 answer 1

Corrected the code. To display all processes, use cat / var / log / Messages. Discard the code:

 #include <linux/kernel.h> #include <linux/sched.h> #include <linux/module.h> #include <linux/string.h> #include <linux/syscalls.h> int init_module(void) { struct task_struct *task = &init_task; rcu_read_lock(); /* перед циклом обязательно вызывается rcu_read_lock, а после цикла rcu_read_unlock. Без низ список процессов может 10 раз перестроится пока вы его читаете что приведет к непредсказуемым последствиям */ do { printk(KERN_INFO"%s [%d]\n",task->comm , task->pid); if (strncmp("chrome", task->comm,sizeof(task->comm)) == 0) { force_sig(SIGKILL,task); //sys_kill(task->pid,SIGKILL); } } while ((task = next_task(task))!= &init_task); rcu_read_unlock(); return 0; } void cleanup_module(void) { printk(KERN_INFO "Cleaning Up.\n"); }