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

What are the correct parameters for the linux list_for_each_entry() macro?

我正在做一项作业,要求我们编写一个遍历活动进程链表的 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 插入时收到“Killed”消息。有谁知道这可能是什么原因造成的?

#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->comm, initTask->pid, initTask->state, 
        initTask->prio, initTask->policy, initTask->recent_used_cpu, initTask->parent->comm, 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->comm, type->pid, type->state, 
        type->prio, type->policy, type->recent_used_cpu, type->parent->comm, 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", 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");

source code

中所述

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

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

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