显示按状态过滤的进程的系统调用

System call that shows processes filtered by their status

我正在进行一个系统调用,它循环遍历每个具有特定状态的进程(作为参数传递给系统调用)并显示它们的名称、PID、UID 和它们的子进程的名称。这是我目前所拥有的:

asmlinkage int sys_procinfo(int state){
    struct task_struct *task;
    struct task_struct *child_ptr;
    struct list_head *list;


    for_each_process(task) {
        if(task->state == state){
            /* print info about the parent */
            printk(KERN_INFO "nombre:%s[pid:%d]estado:%ld[uid:%d]\n", task->comm, task->pid, task->state, task->uid);


            list_for_each(list, &task->children) {
                child_ptr = list_entry(list, struct task_struct, sibling);
                /* child_ptr now points to one of current's children */
                printk(KERN_INFO "Hijo %s\n", child_ptr->comm);
            }
        }
    }
    return 0;
}

这打印了系统的所有进程及其子进程,完全忽略了条件 if(task->state == state),这对我来说很奇怪。我只需要打印处于 'state' 状态的进程的信息(例如 TASK_RUNNING = 0,EXIT_ZOMBIE = 32,TASK_WAKING = 256,TASK_INTERRUPTIBLE = 1 等)。

哦,我也希望系统调用到 return 它的系统调用编号,但我为两个系统定义了它:32 位和 64 位,它们的编号在 syscall_32.tbl 和 syscall_64.tbl 表不同,所以我想我不能硬编码系统调用号。我可以使用任何宏来 return 该值吗?

谢谢。 PS:我正在使用 kubuntu 16.04LTS,我正在使用 Linux 内核档案

中的内核 "linux-3.16.43.tar.xz"

我认为问题是 'task' 没有初始化,但这有点奇怪,因为它应该指向 for_each_process() 宏中的每个进程。我所做的是首先指向 init(或 systemd)进程,然后循环遍历每个进程及其 children。这是内核中的 sys.c 文件,它打印每个处于 'state' 状态(如果它是有效状态)的进程 运行 的名称以及它们的 PID 和 UID,以及名称他们 children。它可以像 syscall(SYS_procinfo_64, state);

这样调用
asmlinkage int sys_procinfo(int state) {
    struct task_struct *tarea;
    struct task_struct *hijo;
    struct list_head *lista_procesos;

    if(state != 0 && state != 1 && state != 2 && state != 4 && state != 8 && state != 16 && state != 32
        && state != 64 && state != 128 && state != 256 && state != 512 && state != 1024 && state != 2048
        && state != 4096){
        /* Si el estado ingresado no es valido, retornar -1 */
        return -1;
    }
    /* tarea apunta hacia init para recorrer los procesos */
    for (tarea = current; tarea != &init_task; tarea = tarea->parent)
        ;
    /* recorrer todos los procesos con el puntero tarea */
    for_each_process(tarea) {
        /* mostrar informacion de procesos en el estado 'state' */
        if(tarea->state == state || task->exit_state == state) {
            /* informacion del padre */
            printk(KERN_INFO "Proceso %s     pid: %d    uid: %u\n", tarea->comm, tarea->pid, current_uid().val);
            /* informacion de los hijos */
            list_for_each(lista_procesos, &tarea->children) {
                hijo = list_entry(lista_procesos, struct task_struct, sibling);
                printk(KERN_INFO "Hijo %s\n", hijo->comm);
            }
        }
    }
    return state;
}