难以将结构数据从内核空间传输到用户空间

时间:2018-08-01 10:03:53

标签: c linux linux-kernel system-calls

我正在尝试编写一个系统调用,该调用可以获取有关Linux中进程的信息:

我的系统调用原型是:long pid_info(struct info *info, int pid)

我具有以下结构:

struct info {
        int pid;
        char cwd[4096];
        char root[4096];
}

在我的系统调用中,我有以下代码:

SYSCALL_DEFINE2(pid_info, struct info *, info, int, pid)
{
        ...
        int err;
        char *path_root;

        ...
        // After getting the root path
        err = copy_to_user(info->root, path_root, 
                    strlen(path_root) + 1);
        if (err)
                return -EFAULT;
        // Just to check if it copied to info->root (and later when i run dmesg it prints the path).
        pr_info("Root path (user) : %s\n", info->root);
        ...

        return (0);
}

我主要测试系统调用:

int main(void)
{
        struct info info;
        int ret;

        ret = syscall(355, &info, 190);
        if (ret == -1)
                return 0;
        //This print nothing even though the dmesg log says it was copied.
        printf("Path : %s\n", info->root);
        return (0);
}

由于copy_to_user应该将数据从内核空间传输到用户空间,因此我复制的数据以某种方式消失了并且正在寻找原因。

0 个答案:

没有答案
相关问题