c - linux内核+条件语句

标签 c linux linux-kernel system-calls

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

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

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

更新:通过将返回值更改为其他值解决了我的问题,尽管它工作正常但很奇怪

最佳答案

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

您确定没有输入 if block 吗?那里的一个 printk(和相应的 else block 中的另一个)会让你更进一步,不是吗?

至于问题:不,没有任何特定于内核代码的东西会导致它不起作用。

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

关于c - linux内核+条件语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10264149/

相关文章:

linux - linq to entity Contains() 和嵌套查询

linux - 添加从 RPM 移植的 pkg 后,JFrog Artifactory 和 MergeList/var/lib/apt/lists "(NewVersion2)"问题

c - SOCK_STREAM 上的 Http 服务器 - 何时结束连接?

c - 如何在 Linux 内核中找到信号处理程序定义?

java - 在 O(n) 时间内找到正确的路径

c - 如何使用C将奇数和偶数位置位存储到另一个指针?

c - 如何解决 "msgget: No space left on device"错误?

c - 在内核和用户端保持 Netlink Socket 打开

linux-kernel - 更改 YOCTO Linux 内核版本

c - 二元炸弹第三阶段问题