2017-05-04 21 views
0

我想提出一个系统调用,通过特定状态(传递给系统调用的参数)每个过程循环过滤的过程,并显示其名称,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; 
} 

这将打印系统的所有进程和他们的孩子,全然不顾条件,如果(任务 - >状态==状态),这是非常奇怪的我。我只需要打印处于“状态”状态的进程的信息(例如,TASK_RUNNING = 0,EXIT_ZOMBIE = 32,TASK_WAKING = 256,TASK_INTERRUPTIBLE = 1等)。

哦,我也想系统调用返回它的系统调用号码,但我定义了两个系统:32和64位,他们在syscall_32.tbl和syscall_64.tbl表中的数字是不同的,所以我认为我无法硬编码系统调用号码。有没有我可以用来返回该值的宏?

谢谢。 PS:我正在使用kubuntu 16.04LTS,并且正在使用Linux内核档案中的内核“linux-3.16.43.tar.xz”

+0

请提供详细信息,使您的问题可再现。我不是一个Linux大师(即使不是Linux程序员),但(在谷歌的帮助下),我可能会提供一个答案(正如我以前所做的那样)。一个[最小,完整和可验证的例子](http://stackoverflow.com/help/mcve)将提供必要的头文件,这将使我找到正确的手册页。 'for_each_process()'和'list_for_each()'可能是在你未提供的代码中定义或从其他地方包含的宏。这听起来很有趣,但缺少足够的答案... – Scheff

+0

那么,这实际上是代码,它在内核的sys.c文件中。这些宏在内核中,我没有定义它们。无论如何,我找到了答案,我会发布它。 – Sebasuraa

回答

1

我认为问题在于'任务'初始化,但这有点奇怪,因为它应该指向for_each_process()宏中的每个进程。我所做的是首先指向init(或systemd)进程,然后遍历每个进程及其子进程。这是内核中的sys.c文件,它打印出沿着它们的PID和UID以及它们的子项名称的状态'state'(如果它是有效状态)中运行的每个进程的名称。它可以像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; 
}