2012-04-22 78 views
0

我基本上遇到了我写的系统调用中非常奇怪的情况。我想检查一些值,如果它们是相同的返回-2表示发生了某种类型的错误。我正在使用printk()在我的“else if”之前打印这些变量的值,它表示它们彼此相等,但条件未被执行(即,我们不输入else if)I对于在内核中工作是一个相当新的东西,但这对我来说似乎非常陌生,并且想知道在内核中是否存在一些细微差别,我不知道如果有人可以冒险猜测为什么如果我知道我的价值变量条件将不会执行我真的很感激你的帮助linux内核+条件语句

// -------------------------------- ------- //

/* sys_receiveMsg421() 
    Description: 
    - Copies the first message in the mailbox into <msg> 
*/ 
asmlinkage long sys_receiveMsg421(unsigned long mbxID, char *msg, unsigned long N) 
{ 

int result = 0; 
int mboxIndex = checkBoxId(mbxID); 
int msgIndex = 0; 

//acquire the lock 
down_interruptible(&sem); 

//check to make sure the mailbox with <mbxID> exists 
if(!mboxIndex) 
{ 
    //free our lock 
    up(&sem); 
    return -1; 
} 

else 
    mboxIndex--; 

printk("<1>mboxIndex = %d\nNumber of messages = %dCurrent Msg = %d\n",mboxIndex, groupBox.boxes[mboxIndex].numMessages, groupBox.boxes[mboxIndex].currentMsg); 

//check to make sure we have a message to recieve 

-----------CODE NOT EXECUTING HERE------------------------------------------------ 
if(groupBox.boxes[mboxIndex].numMessages == groupBox.boxes[mboxIndex].currentMsg) 
{ 
    //free our lock 
    up(&sem); 
    return -2; 
} 
//retrieve the message 
else 
{ 
    //check to make sure the msg is a valid pointer before continuing 
    if(!access_ok(VERIFY_READ, msg, N * sizeof(char))) 
    { 
     printk("<1>Access has been denied for %lu\n", mbxID); 
     //free our lock 
     up(&sem); 
     return -1; 
    } 
    else 
    { 
     //calculate the index of the message to be retrieved    
     msgIndex = groupBox.boxes[mboxIndex].currentMsg;  

     //copy from kernel to user variable  
     result = copy_to_user(msg, groupBox.boxes[mboxIndex].messages[msgIndex], N); 

     //increment message position 
     groupBox.boxes[mboxIndex].currentMsg++; 

     //free our lock 
     up(&sem); 

     //return number of bytes copied 
     return (N - result); 
    } 
} 
} 

更新:通过仅改变返回值到别的解决我的问题,并能正常工作非常堰d

回答

4

请记住使用标点符号;我不喜欢在阅读问题时喘不过气来。

你知道,如果没有被输入块?在那里的一个printk(和另一个在另一个块中)会让你更进一步,不是吗?

至于这个问题:没有,没有特定的内核代码,这将使这个不行什么。

而且你似乎有同步覆盖,太。虽然:我看到你在关键部分之外获得mboxIndex。这会导致问题吗?从这个片段中很难看出,它甚至没有声明groupBox

1

也许numMessages和/或currentMsg定义为long
如果是这样,你printk,它采用%d,将打印只是位一些,所以你可能会认为他们是平等的,而事实并非如此。