简体   繁体   English

linux内核+条件语句

[英]linux kernel + conditional statements

I basically am running into a very odd situation in a system call that I am writing. 在编写系统调用时,我基本上遇到了一个非常奇怪的情况。 I want to check some values if they are the same return -2 which indicates a certain type of error has occurred. 我想检查一些值,如果它们是相同的返回值-2,则表明发生了某种错误。 I am using printk() to print the values of the variables right before my "else if" and it says that they are equal to one another but yet the conditional is not being executed (ie we don't enter the else if) I am fairly new to working in the kernel but this seems very off to me and am wondering if there is some nuance of working in the kernel I am not aware of so if anyone could venture a guess as to why if I know the values of my variables the conditional would not execute I would really appreciate your help 我正在使用printk()在我的“ else if”之前打印变量的值,它说它们彼此相等,但是条件没有被执行(即,如果if,我们就不输入else)在内核中工作是相当陌生的,但是这对我来说似乎很奇怪,我想知道我是否不知道在内核中工作是否有些细微差别,因此是否有人可以猜测我为什么知道我的价值?有条件的变量不会执行,我非常感谢您的帮助

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

/*  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);
    }
}
}

UPDATE: Solved my problem by just changing the return value to something else and it works fine very weird though 更新:通过将返回值更改为其他值解决了我的问题,尽管它很奇怪,但效果很好

Please remember to use punctuation; 请记住使用标点符号; I don't like running out of breath while reading questions. 我不喜欢在阅读问题时喘不过气来。

Are you sure the if block isn't being entered? 您确定未输入if块吗? A printk there (and another in the corresponding else block) would take you one step further, no? 那里的printk(和相应的else块中的另一个)将使您更进一步,不是吗?

As for the question: No, there isn't anything specific to kernel code that would make this not work. 至于这个问题:不,没有任何特定于内核代码的东西会使它不起作用。

And you seem to have synchronization covered, too. 而且您似乎也涵盖了同步。 Though: I see that you're acquiring mboxIndex outside the critical section. 虽然:我看到您在关键部分之外获取了mboxIndex Could that cause a problem? 会造成问题吗? It's hard to tell from this snippet, which doesn't even have groupBox declared. 从这个片段中甚至都groupBox声明groupBox

Perhaps numMessages and/or currentMsg are defined as long ? 也许numMessages和/或currentMsg被定义为long
If so, your printk , which uses %d , would print just some of the bits, so you may think they're equal while they are not. 如果是这样,则使用%d printk将仅打印一些位,因此您可能会认为它们相等而不同。

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

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