简体   繁体   English

从内核linux中的pid获取完整的进程路径

[英]Get full process path from pid in kernel linux

I'm building custom kernel linux for my device (like embeded device). 我正在为我的设备(如嵌入式设备)构建自定义内核linux。 I want to log all information of kill system call. 我想记录所有杀死系统调用的信息。 So I modify code in kill system call and get pid to kill. 因此,我在kill系统调用中修改了代码并获取pid进行杀死。 But I can't get full process path for it, I have just get full path of current process. 但是我无法获得完整的过程路径,而只能获得当前过程的完整路径。 This is my code to get process path from pid. 这是我从pid获取进程路径的代码。

long GetProcessPathFromPid(const pid_t p_id, char* pszProcessPath, int iSize)
{
    struct task_struct *task = NULL;
    struct pid *pid_struct = NULL;
    struct mm_struct *mm = NULL;
    //struct file *exe_file = NULL;
    //char buf[256] = {0};
    //char *result = ERR_PTR(-ENOENT);
    //int iResultLength = 0;

    pid_struct = find_get_pid(p_id);
    if (pid_struct == NULL)
    {
        printk("Fail to find_get_pid.\n");
    }
    task = pid_task(pid_struct, PIDTYPE_PID);                               // Get task_struct
    if (task == NULL) 
    {
        printk("Fail to pid_task.\n");
    }
    mm = get_task_mm(task);                                                // Get mm of task_struct
    mmput(mm);

    printk("Finish hook kill process\n.");
    return 0;                                              // Get mm of task_struct
}

And this is error when I update firmware: 这是我更新固件时的错误:

Call Trace:
[<8042971c>] printk+0x24/0x30
[<800c37c8>] GetProcessPathFromPid+0x38/0x88
[<800c3b60>] LogKillProcess+0x64/0xf4
[<800443d4>] sys_kill+0x30/0x1d8
[<800354b0>] do_wait+0x11c/0x220
[<8042e234>] wait_for_completion_killable+0x18/0x30
[<80043a3c>] set_current_blocked+0x30/0x48
[<80533210>] repair_env_string+0x0/0x94
[<8003664c>] sys_wait4+0x80/0xfc
[<800344d8>] child_wait_callback+0x0/0x8c
[<80533210>] repair_env_string+0x0/0x94
[<8001b59c>] stack_done+0x20/0x40
[<800b07a8>] sys_close+0x0/0x158
[<80533210>] repair_env_string+0x0/0x94


Call Trace:
[<8042fe88>] _raw_spin_lock+0x10/0x3c
[<8002ef68>] get_task_mm+0x20/0x7c
[<800c37c8>] GetProcessPathFromPid+0x38/0x88
[<800c3b60>] LogKillProcess+0x64/0xf4
[<800443d4>] sys_kill+0x30/0x1d8
[<8001b59c>] stack_done+0x20/0x40


Code: 24630001  af830014  3c020001 <c0830000> 00622821  e0850000  10a0fffc  00032c02  3063ffff 
pe=GPON

---[ end trace aee100dae37dfc66 ]---
note: init[1] exited with preempt_count 1
FinWLmngr Daeish hook kill process
.mon is running
 !!!!   PLL locked !!!!!!     !!!!   RX CDR locked !!!!!!     !!!!   TX CDR locked !!!!!!    GPON BEN Calibration Done 
GPON SerDes Initialization Sequence Done 
Kernel panic - not syncing: Attempted to kill init! exitcode=0x0000000b

Can you help me? 你能帮助我吗? Thanks! 谢谢!

I found way to get full path process from pid. 我找到了从pid获取完整路径过程的方法。 Full path process is symlink of /proc//exe, so I must find symlink of it. 全路径过程是/ proc // exe的符号链接,因此我必须找到它的符号链接。 Here is code: 这是代码:

long GetSymLinkOfFile(const char *pzsFilePath, char *pszRealPath, int iSize)
{
    struct path path_struct;
    char pszBuffer[256];
    char *result = ERR_PTR(-ENOENT);
    int err = 0;
    int iLengthOfResult = 0;

    if (pzsFilePath == NULL || pszRealPath == NULL)
    {
        printk("Buffer is NULL.\n");
        return -EINVAL;
    }

    err = kern_path(pzsFilePath, LOOKUP_FOLLOW, &path_struct);

    if (err < 0)
    {
        printk("kern_path error, error code: %d", err);
        return iLengthOfResult;
    }

    result = d_path(&path_struct, pszBuffer, 256);

    if (IS_ERR(result) == true)
    {
        printk("d_path GetSymlink error: %s", result);
        return iLengthOfResult;
    }

    iLengthOfResult = strlen(result);

    if (iLengthOfResult <= iSize)
        strcpy(pszRealPath, result);

    return iLengthOfResult;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM