C 从“创建”“处理”“读取”条目移动到“处理”“创建并使用序列文件”
我正在尝试从不推荐使用的函数C 从“创建”“处理”“读取”条目移动到“处理”“创建并使用序列文件”,c,linux-kernel,C,Linux Kernel,我正在尝试从不推荐使用的函数create\u proc\u read\u条目改为使用proc\u create。我一直在研究使用seq_文件的新实现。以前,我使用/proc文件系统传递指向一个结构的指针,我将该结构从\u user复制到内核空间。这个结构中有我用来确定下一步调用哪个函数的信息(和其他数据)。我正在为新的实现而挣扎。具体地说,我如何获得指向从用户空间传递的结构的指针。根据我的研究,seq_文件实现使用seq文件方便地遍历结构,而不必处理指针 struct sCmd_t {
create\u proc\u read\u条目
改为使用proc\u create
。我一直在研究使用seq_文件的新实现。以前,我使用/proc
文件系统传递指向一个结构的指针,我将该结构从\u user
复制到内核空间。这个结构中有我用来确定下一步调用哪个函数的信息(和其他数据)。我正在为新的实现而挣扎。具体地说,我如何获得指向从用户空间传递的结构的指针。根据我的研究,seq_文件实现使用seq文件方便地遍历结构,而不必处理指针
struct sCmd_t {
u32 length;
u8 type;
int number;
u16 command;
u8 version;
} sCmd_t
ctrl_file = create_proc_read_entry("ctrl",
0666,
PROC_FILE,
read_proc_ctrl,
NULL);
ctrl_file->write_proc = write_proc_ctrl;
static int write_proc_ctrl( struct file *filp, const char __user *buff, unsigned long len, void *data){
sCmd_t *sCmd = NULL;
const unsigned char *curbuf = NULL;
sCmd = (sCmd_t *) kmalloc(sizeof(sCmd_t), GFP_KERNEL);
if (sCmd == NULL)
return ERROR;
memset(cmd, 0, sizeof(sCmd));
curbuf = buff;
if (copy_from_user( cmd, curbuf, sizeof(sCmd_t)))
return -EFAULT;
switch (sCmd->command){
//based on the command the appropriate
}
所以我要做的是从seq_show函数中获取指向我的结构的指针,但它不起作用
static int seq_show(struct seq_file *seqfile, void *v)
{
//in here I'm trying to grab a pointer to the struct here
sCmd_t *sCmd = NULL;
const unsigned char *curbuf = NULL;
sCmd = (sCmd_t *) kmalloc(sizeof(sCmd_t), GFP_KERNEL);
if (sCmd == NULL)
return ERROR;
memset(cmd, 0, sizeof(sCmd));
curbuf = v;
if (copy_from_user( cmd, curbuf, sizeof(sCmd_t)))
//blah blah blah.....
return 0;
}
static void *seq_start(struct seq_file *seqfile, loff_t *pos)
{
return pos;
}
static void *seq_next(struct seq_file *seqfile, void *v, loff_t *pos)
{
return pos;
}
static void seq_stop(struct seq_file *seqfile, void *v)
{
return;
}
static struct seq_operations seq_ops = {
.start = seq_start,
.next = seq_next,
.stop = seq_stop,
.show = seq_show
};
static int proc_open(struct inode *inode, struct file *file)
{
return seq_open(file, &seq_ops);
}
static const struct file_operations fops = {
.owner = THIS_MODULE,
.open = proc_open,
.read = seq_read,
.llseek = seq_lseek,
.release = single_release,
};
ctrl_file = proc_create("ctrl", 0644, proc_dir, &fops);
这只是第一次尝试。当我尝试这段代码时,当我解除结构的命令成员时,它会呕吐。正如您所看到的,我的结构非常小,所以这个实现不需要使用seq文件来检查它是否在另一个页面上运行。这就是为什么我试图在show函数中获取指针,因为我认为它必须迭代或“下一步”到结构或页面。从我所读到的内容来看,seq_文件迭代将使遍历结构变得容易,但我首先需要了解如何从用户空间获取指向写入
/proc
文件系统的内容的指针。seq_文件
是实现只读文件的帮助器。
show
回调的缓冲区是一个内核缓冲区,您应该在其中写入要返回到用户空间的数据
您可以选择是否使用序列文件
;但这在这里没有意义
但在任何情况下,procfs操作都是用struct file_operations
定义的;
要实现写回调,只需在
fops
中实现write
,感谢您的回复-因此,proc文件的回调已从proc\u dir\u entry结构移到file\u operations结构?现在文件_operations struct引用了proc文件?再次感谢您的回复,请参阅更新的问题。不要将您的问题编辑为对答案的回复;这将使任何现有答案无效,并使通过Google找到此页面的任何人感到困惑。您最初的问题,即如何告诉新的proc API有关写入回调的问题,已得到解决。至于正确处理缓冲区,这将是一个新问题;考虑分开张贴。(当你这样做时,包括cmd
的定义)是的,你是对的。我的seg故障是另外一个问题。