导航

Linux device driver 关于driver返回值和用户空间处理

Posted on 2011-02-28 09:47  cornflower  阅读(284)  评论(0编辑  收藏  举报
以write为例子.

in Userspace:

int ret; 

ret=write(serial_fd,(char *)write_buf,sizeof(write_buf));

if(ret<0)

printf("strerror: %s\n", strerror(errno));

 }

in Driver

xxx_write()

return -EAGAIN;//Try_again 

 }

 

A common mistake is to do


if (somecall() == -1) {
    printf("somecall() failed\n");
    if (errno == ...) { ... }
}
where errno no longer needs to have the value it had upon return from somecall() (i.e., it may have been changed by the printf()). If the value of errno should be preserved across a library call, it must be saved:
if (somecall() == -1) {
    int errsv = errno;
    printf("somecall() failed\n");
    if (errsv == ...) { ... }
}
It was common in traditional C to declare errno manually (i.e., extern int errno) instead of including <errno.h>. Do not do this. It will not work with modern versions of the C library. However, on (very) old Unix systems, there may be no <errno.h> and the declaration is needed.