首页 > 解决方案 > 难以将结构中的数据从内核空间传输到用户空间

问题描述

我正在尝试编写一个可以获取有关 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 应该将数据从内核空间传输到用户空间。

标签: clinuxlinux-kernelsystem-calls

解决方案


推荐阅读