2013-10-30 34 views
-1

所以我正在做一些關於linux內核的工作,我正在嘗試實現一個函數,但首先我必須在內核空間中定義一個結構體。我遇到了一個錯誤,但我不太清楚原因。在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: warning: ‘regparm’ attribute only applies to function types [-Wattributes] msgQueue421/msgQueue421.c:24:12: error: expected ‘;’, identifier or ‘(’ before ‘long’ make[1]: * [msgQueue421/msgQueue421.o] Error 1 make: * [msgQueue421] Error 2

任何想法,我做錯了嗎?

+0

如果你的第二個malloc失敗,你應該從第一個malloc釋放指針。 – ChuckCottrill

+1

請在您的代碼中添加註釋,以突出顯示哪些行是第24行和第73行(您有錯誤的行) –

+0

在行上,「newNode-> data = data;」你可能打算分配pdata – ChuckCottrill

回答

1

這將是很好知道線24 + 73是什麼,但「常量」的事情可能是由

newNode->data = data; 

造成你要分配pdata這裏(不是__user指針)。

關於第一個錯誤組的盲猜:有一個缺少#include,所以__user沒有定義。

而且BTW:

struct list_head * mylist; 

在嚴格的很可能是錯誤的,因爲你不能DEREF它。使用的普通struct list_head mylist;代替

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行發現錯誤,並且您發現該行沒有任何錯誤時,請始終查看錯誤發生前的最後幾行代碼,以查看是否缺少某些內容。