linux get process name from pid within kernel

前端 未结 4 1119
暗喜
暗喜 2021-01-05 07:30

hi i have used sys_getpid() from within kernel to get process id how can I find out process name from kernel struct? does it exist in kernel??

thanks very much

4条回答
  •  囚心锁ツ
    2021-01-05 07:41

    My kernel module loads with "modprobe -v my_module --allow-unsupported -o some-data" and I extract the "some-data" parameter. The following code gave me the entire command line, and here is how I parsed out the parameter of interest:

    struct mm_struct *mm;
    unsigned char x, cmdlen;
    
    mm = get_task_mm(current);
    down_read(&mm->mmap_sem);
    
    cmdlen = mm->arg_end - mm->arg_start;
    for(x=0; xarg_start + x) == '-' && *(unsigned char *)(mm->arg_start + (x+1)) == 'o') {
            break;
        }
    }
    up_read(&mm->mmap_sem);
    
    if(x == cmdlen) {
        printk(KERN_ERR "inject: ERROR - no target specified\n");
        return -EINVAL;
    }
    
    strcpy(target,(unsigned char *)(mm->arg_start + (x+3)));
    

    "target" holds the string after the -o parameter. You can compress this somewhat - the caller (in this case, modprobe) will be the first string in mm->arg_start - to suit your needs.

提交回复
热议问题