bool默认值 linux,如何在Linux设备驱动程序中设置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

// ...

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.

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;

}

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?

解决方案

Nice question!

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!

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".

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

Hope it helps

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值