我的系统调用无法正常工作
我需要创建 2 个新的系统调用来通过给定的 pid 设置和获取进程的属性。我更改了task_struct,添加了int z_value(这是我需要设置/获取的)
我还设法为创建的每个进程设置默认z_value(200)。
当我运行 get 系统调用时,我可以看到默认的 z_value 是正确的。但是当我尝试设置 z_value 时什么也没有发生。
没有编译错误,没有分段错误等。
这是我设置的系统调用。
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/uaccess.h>
asmlinkage void sys_set_z_value ( int user_value , pid_t pid )
{
rcu_read_lock();
struct task_struct *p = find_task_by_vpid(pid);
p->z_value = user_value;
rcu_read_unlock();
}
I need to create 2 new system calls to set and get a property from a process by the given pid. I changed task_struct, added int z_value (this is what I need to set/get)
I also managed to set a default z_value (200) for every process created.
When I run get system call, I can see that default z_value correct. But when I try to set the z_value nothing happens.
No compiling errors, no segmentation fault etc.
Here is my set system call.
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/uaccess.h>
asmlinkage void sys_set_z_value ( int user_value , pid_t pid )
{
rcu_read_lock();
struct task_struct *p = find_task_by_vpid(pid);
p->z_value = user_value;
rcu_read_unlock();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您对
copy_from_user
的调用毫无意义。不涉及指向用户空间内存的指针。user_value
是一个值,而不是一个指针。 (如果您打算将其作为指针,则需要修复所涉及的类型,但看起来您正在按值将此int
传递给系统调用。)只需分配p- >z_value = user_value;
。Your call to
copy_from_user
makes no sense. There's no pointer to userspace memory involved.user_value
is a value, not a pointer. (If you intended for it to be a pointer, you need to fix the types involved, but it looks like you're passing thisint
by value to the syscall.) Just assignp->z_value = user_value;
.long
类型。SYSCALL_DEFINE2()
宏。p
类型将触发警告)p
变量将是NULL
,并且您应该返回-ESRCH
关于这一点:
我希望您也注意了
init_task
,这是一个常见的错误。long
type.SYSCALL_DEFINE2()
macros.p
type in the middle of the function will trigger a warning)p
variable will beNULL
, and you should return-ESRCH
About this:
I hope that you took care of
init_task
too, it's a common mistake.两个建议:
1)引入故意错误,例如源文件中的一些随机字符,并确保您的内核构建失败。令人惊讶的是,由于您添加的代码未构建而导致的问题数量之多令人惊讶。
假设不是这样
2) 用大量 printk 加载代码,这样您就可以通过观察控制台窗口或运行后调用 dmesg 来了解它正在尝试执行的操作。将它们放在各处以检查每个假设 - 代码是否运行、变量是否符合您的想法等等。
Two suggestions:
1) Introduce an intentional error like some random characters in the source file and make sure your kernel build fails. Its amazing the number of problems that come down to the code you add not being built.
assuming that wasn't it
2) Load up your code with a lot of printk's so you can see what it's trying to do by either watching the console window or invoking dmesg after you've run it. Put them all over the place to check every assumption - that the code runs, that the variables are what you think, etc.