c - 难以将结构中的数据从内核空间传输到用户空间
问题描述
我正在尝试编写一个可以获取有关 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 应该将数据从内核空间传输到用户空间。
解决方案
推荐阅读
- python - Python Subprocess.check_output() and call() will not properly execute commands (Windows 10)
- javascript - Vuex 模块:不能使用 mapActions
- python - How to expand your x axis of datetimeIndex with matplotlib Python
- javascript - 为什么这个谷歌应用程序脚本只有在我硬编码范围时才能工作?
- python - 根据另一列中字符串的内容向数据框中添加一列
- c - 代码如何在 C 中返回一个字符数组?
- c - 为什么 C stdout 不返回“ls”命令内容?
- python-3.x - 如何从 Python 脚本运行 Perl 程序,同时接收日志并避免脚本返回码崩溃:-9
- r - 如何在 R 中将随机和/或固定效果添加到 cloglog 回归中
- mongodb - 在 mongodb 中使用日期和时间对集合进行排序