我的系统调用无法正常工作

发布于 2024-10-03 06:13:31 字数 566 浏览 1 评论 0原文

我需要创建 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

请止步禁区 2024-10-10 06:13:31

您对 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 this int by value to the syscall.) Just assign p->z_value = user_value;.

泛泛之交 2024-10-10 06:13:31
  • 您的系统调用应返回 long 类型。
  • 您应该使用 SYSCALL_DEFINE2() 宏。
  • 您的代码不应在没有警告的情况下编译(并且在函数中间声明 p 类型将触发警告)
  • 如果您获得无效的 pid,您的 p 变量将是NULL,并且您应该返回-ESRCH

关于这一点:

我还设法为创建的每个进程设置默认 z_value (200)。

我希望您也注意了 init_task,这是一个常见的错误。

  • Your syscall should return a long type.
  • You should use the SYSCALL_DEFINE2() macros.
  • Your code should not compile without warnings (and declaring the p type in the middle of the function will trigger a warning)
  • If you get an invalid pid, your p variable will be NULL, and you should return -ESRCH

About this:

I also managed to set a default z_value (200) for every process created.

I hope that you took care of init_task too, it's a common mistake.

你在我安 2024-10-10 06:13:31

两个建议:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文