[英]Creating structs in linux kernel space
因此,我在Linux内核上为类做一些工作,并且尝试实现一个函数,但首先必须在内核空间中定义一个结构。 我收到一个错误,但我不太确定原因。
我认为它与我一开始定义的结构有关,但似乎找不到任何问题。
更新:好的,我已经解决了其中一个问题。 因此,我将更新我的代码段并标记错误中指定的行。 第24行是该结构结尾之后的行。
这是我在做什么:
#include <linux/kernel.h>
#include <linux/list.h>
#include <linux/klist.h>
#include <linux/errno.h>
#include <asm/uaccess.h>
#include <linux/slab.h>
/********************************************
*This function adds a new item to the queue
*
*If not enough memory return -ENOMEM
*If there is an error accessing the upper space point return -efault
*If len is negative return -EINVAL
*Returns 0 on success
******************************/
struct dataNode
{
const void * data;
int length;
struct list_head * mylist;
}
asmlinkage long sys_writeMsgQueue421(const void __user *data, long len) //**Line 24**//
{
newNode->data = pdata;
newNode->length = len;
//****Need to add to the linked list****//
printk("This was passed in: %p and %ld \n",data , len);
return 0;
}
asmlinkage long sys_readMsgQueue421(void)
{
printk("This is the read function!\n");
return 0;
}
asmlinkage long sys_emptyMsgQueue421(void)
{
printk("This is the clear function!\n");
return 0;
}
运行make命令时出现以下错误:
CC msgQueue421 / msgQueue421.o msgQueue421 / msgQueue421.c:24:1:警告:“ regparm”属性仅适用于函数类型[-Wattributes] msgQueue421 / msgQueue421.c:24:12:错误:预期的“;”,标识符或'('在'long'之前make [1]: * [msgQueue421 / msgQueue421.o]错误1 make:* [msgQueue421]错误2
知道我在做什么错吗?
您现在遇到的问题(由于还有其他问题,其中一些已经指出)是您缺少;
关闭您的struct
声明:
struct dataNode
{
void *data;
int length;
struct list_head * mylist;
}
asmlinkage long sys_writeMsgQueue421(const void __user *data, long len)
编译器尝试解析此代码,并认为asmlinkage long ...
等都是将成为struct dataNode
实例的变量名称。 当然, asmlinkage
几乎可以肯定是一个宏,膨胀到谁知道和long
是一种保留字,不能是一个变量的名称。
这样您会出错。 很多很多的错误。
一般而言,当您在第N
行遇到错误时,发现该行没有任何错误时,请始终查看该错误之前的最后几行代码,以查看是否缺少某些内容。
知道24 + 73行是什么,这很高兴,但“ const”可能是由
newNode->data = data;
您要在此处分配pdata
(而不是__user指针)。
对第一个错误组进行盲目的猜测:缺少一个#include,因此未定义__user。
顺便说一句:
struct list_head * mylist;
在您严格中很可能是错误的,因为您不能取消引用它。 使用普通的struct list_head mylist;
代替
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.