我正在进行一个系统调用,它以一定的状态遍历每个进程(作为参数传递给syscall ),并显示它们的名称、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(任务->状态==状态)的条件,这对我来说非常奇怪。我只需要打印处于状态' state‘的进程的信息(例如,TASK_RUNNING = 0,EXIT_ZOMBIE = 32,TASK_WAKING = 256,TASK_INTERRUPTIBLE = 1等)。
哦,我还想让syscall返回它的syscall号,但是我为两个系统定义了它: 32位和64位,它们在syscall_32.tbl和syscall_64.tbl表中的编号是不同的,所以我想我不能硬编码syscall号。有什么宏可以用来返回这个值吗?
谢谢。PS:我正在开发kubuntu 16.04LTS,我正在使用Linux内核档案中的内核“Linux3.16.43.tar.xz”
发布于 2017-05-06 16:27:16
我认为问题是‘任务’没有初始化,但这有点奇怪,因为它应该指向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;
}
https://stackoverflow.com/questions/43792015
复制相似问题