加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

linux-kernel – 在设备驱动程序中将自定义标志传递给“open”

发布时间:2020-12-13 22:57:55 所属栏目:Linux 来源:网络整理
导读:我需要将一些自定义标志传递给我的设备驱动程序的open()调用. 我在LDD3中找到了这个例子: int dev_open(struct inode *inode,struct file *filp){ if ((filp-f_flags O_ACCMODE) == O_WRONLY) { ... }} 我的问题是:是否可以定义其他标志(如O_ACCMODE和O_WR
我需要将一些自定义标志传递给我的设备驱动程序的open()调用.

我在LDD3中找到了这个例子:

int dev_open(struct inode *inode,struct file *filp)
{
    if ((filp->f_flags & O_ACCMODE) == O_WRONLY) {
        ...
    }
}

我的问题是:是否可以定义其他标志(如O_ACCMODE和O_WRONLY)而不与任何其他标志冲突?

解决方法

是的,这是可能的.看看 include/uapi/asm-generic/fcntl.h.注意下一条评论:

/*
 * When introducing new O_* bits,please check its uniqueness in fcntl_init().
 */

现在看看fcntl_init()函数(在fs/fcntl.c定义):

/*
 * Please add new bits here to ensure allocation uniqueness.
 * Exceptions: O_NONBLOCK is a two bit define on parisc; O_NDELAY
 * is defined as O_NONBLOCK on some platforms and not on others.
 */
BUILD_BUG_ON(20 - 1 /* for O_RDONLY being 0 */ != HWEIGHT32(
    O_RDONLY        | O_WRONLY      | O_RDWR        |
    O_CREAT         | O_EXCL        | O_NOCTTY      |
    O_TRUNC         | O_APPEND      | /* O_NONBLOCK | */
    __O_SYNC        | O_DSYNC       | FASYNC        |
    O_DIRECT        | O_LARGEFILE   | O_DIRECTORY   |
    O_NOFOLLOW      | O_NOATIME     | O_CLOEXEC     |
    __FMODE_EXEC    | O_PATH        | __O_TMPFILE
    ));

首先,您需要为新定义找到唯一值,因此可以使用fcntl_init()中列出的标志按位或按位.接下来,您需要将新定义添加到include / uapi / asm-generic / fcntl.h.最后将新的define添加到fcntl_init(),因此将在编译时进行检查.

最后,它归结为找到与现有定义不冲突的价值.例如.我可以看到所有10,100,1000,10000,100000,1000000和10000000都被使用.因此,对于新标志,您可以使用100000000,200000000,4000000和800000000值.

更新:正如SailorCaire正确提到的那样,您还需要在BUILD_BUG_ON()宏中增加第一个数字.例如,如果它最初是BUILD_BUG_ON(20 – 1,并且您要在此列表中添加一个元素,则应将其设为BUILD_BUG_ON(21 – 1.

更新2:SailorCaire的另一个有价值的补充:

By the way,you’ll need to do make install_headers,copy the new headers,and it looks like you’ll need to recompile glibc so it becomes aware of the API change.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读