linux list_for_each_entry宏的正确参数是什么?

问题描述

我正在做一个作业,要求我们编写一个C加载式内核模块,该模块可遍历活动进程的链接列表。我的问题是list_for_each_entry()函数的正确参数是什么?我已经做过研究,但我仍然非常困惑。

到目前为止,这是我的代码

#include <linux/module.h>  
#include <linux/kernel.h>       
#include <linux/sched.h>  
#include <linux/init_task.h>  

int processCount = 0;
struct task_struct *initTask = &init_task;

int init_module(void)
{

    printk(KERN_INFO "Jared Rathbun\n");
    
    
    struct task_struct *type;

    list_for_each_entry(type,struct task_struct,children)
    {
        printk(KERN_INFO "Name: %-20s| PID: %-5d| State: %-4ld| Priority: %-5d| Policy: %-5d| Recent cpu: %-5d| Parent Name: %-20s| PPID: %-5d\n",type->comm,type->pid,type->state,type->prio,type->policy,type->recent_used_cpu,type->parent->comm,type->parent->pid);
    } 
    
    /* Print the init process */
    printk(KERN_INFO "Init Process\n");
    printk(KERN_INFO "Name: %-20s| PID: %-5d| State: %-3ld| Priority: %-5d| Policy: %-5d| Recent cpu: %-5d| Parent Name: %-20s| PPID: %-5d\n",initTask->comm,initTask->pid,initTask->state,initTask->prio,initTask->policy,initTask->recent_used_cpu,initTask->parent->comm,initTask->parent->pid);
    processCount++;
    
    /* Print the current process */
    printk(KERN_INFO "Current Process\n");
    printk(KERN_INFO "Name: %-20s| PID: %-5d| State: %-3ld| Priority: %-5d| Policy: %-5d| Recent cpu: %-5d| Parent Name: %-20s| PPID: %-5d\n",current->comm,current->pid,current->state,current->prio,current->policy,current->recent_used_cpu,current->parent->comm,current->parent->pid);
    processCount++;
    
    printk(KERN_INFO "TOTAL PROCESS COUNT: %d\n",processCount);
    
    return 0;
}

void cleanup_module( void )
{
    return;
}

MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Kernel Module that traverse the list of running processes on the OS");
MODULE_AUTHOR("Jared Rathbun");

谢谢!

更新:我已经修复了list_for_each_entry的参数,但是现在在使用insmod进行插入时会收到“已杀死”消息。有人知道是什么原因造成的吗?

#include <linux/module.h>  
#include <linux/kernel.h>       
#include <linux/sched.h>  
#include <linux/init_task.h>  

struct task_struct *initTask = &init_task;
struct list_head *listHead;
struct task_struct *type;

int init_module(void)
{
    int processCount = 0;

    printk(KERN_INFO "Jared Rathbun\n");
    
    /* Print the init process */
    printk(KERN_INFO "Init Process\n");
    printk(KERN_INFO "Name: %-20s| PID: %-5d| State: %-3ld| Priority: %-5d| Policy: %-5d| Recent cpu: %-5d| Parent Name: %-20s| PPID: %-5d\n",initTask->parent->pid);
    processCount++;
    
    /* Traverse the Linked List of processes */
    
    list_for_each_entry(type,listHead,sibling)
    {
        printk(KERN_INFO "Name: %-20s| PID: %-5d| State: %-4ld| Priority: %-5d| Policy: %-5d| Recent cpu: %-5d| Parent Name: %-20s| PPID: %-5d\n",type->parent->pid);
    } 
    
    /* Print the current process */
    printk(KERN_INFO "Current Process\n");
    printk(KERN_INFO "Name: %-20s| PID: %-5d| State: %-3ld| Priority: %-5d| Policy: %-5d| Recent cpu: %-5d| Parent Name: %-20s| PPID: %-5d\n",processCount);
    
    return 0;
}

void cleanup_module( void )
{
    return;
}

MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Kernel Module that traverse the list of running processes on the OS");
MODULE_AUTHOR("Jared Rathbun");

解决方法

source code

中所述

list_for_each_entry()-遍历给定类型的列表。 参数

  • @pos :类型*用作循环光标。
  • @head :列表的标题。
  • @member :结构中list_head的名称。

使用示例:list_for_each_entry(pos,head,member);