简体   繁体   English

如何在Linux内核(sys.c)中创建可以由常规程序访问的结构类型?

[英]How can I create a struct type in the linux kernel (sys.c) that can be accessed by regular programs?

I have implemented a few of my own syscalls in the sys.c file of the linux kernel. 我已经在Linux内核的sys.c文件中实现了一些自己的syscall。 In my implementations I used two struct types that I created in the same file. 在我的实现中,我使用了在同一文件中创建的两种结构类型。 I also would like to be able to use these structs outside of the kernel. 我还希望能够在内核之外使用这些结构。 Is there a way to use kernel-prototyped structs in regular programs, in the same way that the syscalls can be accessed by the regular programs? 有没有办法在常规程序中使用内核原型结构,就像常规程序可以访问syscall一样?

Specifically, I need to create one of these structs in a user program, which is then passed as an argument to the syscalls. 具体来说,我需要在用户程序中创建这些结构之一,然后将其作为参数传递给syscalls。 I think I need the struct to be in sys.c because it uses "struct task_struct", which is part of linux/sched.h. 我认为我需要将struct放在sys.c中,因为它使用的是“ struct task_struct”,它是linux / sched.h的一部分。

If there is a way to do this, or if I am doing something completely wrong I would be happy to know either way. 如果有办法做到这一点,或者如果我做某件事完全错误,我将很高兴知道这两种方式。 Thanks! 谢谢!

The usual C way of using the same declarations in multiple programs (here: kernel and user program) is putting the declarations in a header file and including that in the programs. 在多个程序(此处为内核程序和用户程序)中使用相同声明的通常C方法是将声明放入头文件中,并将其包括在程序中。 If your structure declaration uses a declaration from linux/sched.h, you may want to #include <linux/sched.h> in the header file. 如果您的结构声明使用linux / sched.h中的声明,则可能需要#include <linux/sched.h>在头文件中。 (For getting struct task_struct , it may be necessary to #define __KERNEL__ . What your user program does with a task_struct, only you can tell.) (要获取struct task_struct ,可能需要#define __KERNEL__ 。只有您能告诉您的用户程序如何使用task_struct。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM