如何在Linux设备驱动程序中设置errno? [英] How to set errno in Linux device driver?

查看:366
本文介绍了如何在Linux设备驱动程序中设置errno?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在设计Linux字符设备驱动程序.我想在ioctl()系统调用中发生错误时设置errno.

I am designing a Linux character device driver. I want to set errno when error occurs in ioctl() system call.

long my_own_ioctl(struct file *file, unsigned int req, unsigned long arg)
{
    long ret = 0;
    BOOL isErr = FALSE;

    // some operation
    // ...

    if (isErr) {
        // set errno
        // ...                 <--- What should I do?
        ret = -1;
    }

    return ret;
}

我应该怎么做才能做到这一点?预先谢谢你!

What should I do to achieve that? Thank you at advance!

请允许我详细解释我的申请.

Please allow me to explain my application with more detail.

我的设备位于/dev/myCharDev中.我的用户空间应用程序是这样的:

My device is located in /dev/myCharDev. My user space application is like this:

#define _COMMAND                    (1)
#define _ERROR_COMMAND_PARAMETER    (-1)

int main()
{
    int fd = open("/dev/myCharDec", O_RDONLY);
    int errnoCopy;

    if (fd) {
        if (ioctl(fd, _COMMAND, _ERROR_COMMAND_PARAMETER) < 0) {      // should cause error in ioctl()
            errnoCopy = errno;
            printf("Oops, error occurred: %s\n", strerr(errnoCopy));  // I want this "errno" printed correctly
        }

        close(fd);
    }

    return 0;
}

正如我在上面的评论中提到的,我应该如何在自己的设备驱动程序代码中设置" errno ",并使它可以被用户空间应用程序读取?

As I mentioned in the comments above, How should I set the "errno" in my own device driver codes and make it readable by user space application?

推荐答案

很好的问题!

好吧,您可以将errno视为全局变量(简而言之,它是一个extern int). errno在errno.h库中为错误代码提供了大量预定义的宏.您可以在此处查看.其中一些错误代码很可能描述了您想要显示的内容.选择正确的变量,将其设置为您定义的变量,然后(重要!)立即退出!

Ok, you could think of errno as global variable (to be honnest, it is an extern int). errno has plenty of pre-defined macros for errorcodes in the errno.h library. You can have a look here. It is very likely that some of these errorcodes describe what you want to show. Pick up the right one, set it like if it was a variable you defined, and (important!) exit immediately!

您可能会问自己,设置errno是否是解决问题的正确方法.您始终可以定义(* int)并开发自己的错误代码和错误处理机制. Errno的目的是显示和解释系统错误.您是否将您的代码视为系统"的一部分(如我所见,您开发了自己的系统调用,所以可能是这种情况)?因此,继续使用errno来解释您的系统错误".

You may ask yourself though if setting errno is the right approach to your problem. You can always define an (*int) and develop your own error codes, and error handling mechanism. Errno's purpose is to show and explain system errors. Do u consider your code part of the "system" (as I can see you develop your own system call, so this might be the case) ? So go on and use errno to explain your "system error".

编辑(关于问题更新):确定更多信息.如我所说,errno是一个extern int,由内核设置.设置errno的值只是系统调用的返回值.然后,Linux内核通过库errno.h解释此负值.因此,仅通过返回(EBUSY只是一个示例-您可以使用所有预定义的错误类型)来设置示例错误消息,即可从系统调用中返回所需的错误消息.示例:

Edit (On question update): Ok more info. As i said errno is an extern int and is set by the kernel. The value at which errno is set is simply the return value of the system call. Linux kernel then interprets this negative value through the library errno.h. So an example error message is set simply by returning (EBUSY is just an example - you can use all of the predifined error types) the error message you want from your system call. Example:

return -EBUSY

希望有帮助

这篇关于如何在Linux设备驱动程序中设置errno?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆