我正在使用内核3.13.0编写一个Linux设备驱动程序,我很困惑,为什么我得到这个警告。
warning: initialization from incompatible pointer type [enabled by default] .read = read_proc, ^ warning: (near initialization for 'proc_fops.read') [enabled by default]
据我可以告诉我的proc函数的file_operations设置是相同的设备function。 我可以读/写到/ dev / MyDevice没有问题,也没有警告。 proc写函数不会抛出警告,只会读取。 我做错了什么?
/*****************************************************************************/ //DEVICE OPERATIONS /*****************************************************************************/ static ssize_t dev_read(struct file *pfil, char __user *pBuf, size_t len, loff_t *p_off) { //Not relevant to this question } static ssize_t dev_write(struct file *pfil, const char __user *pBuf, size_t len, loff_t *p_off) { //Not relevant to this question } static struct file_operations dev_fops = { //None of these cause a warning but the code is identical the proc code below .owner = THIS_MODULE, .read = dev_read, .write = dev_write }; /*****************************************************************************/ //PROCESS OPERATIONS /*****************************************************************************/ static int read_proc(struct file *pfil, char __user *pBuf, size_t len, loff_t *p_off) { //Not relevant to this question } static ssize_t write_proc(struct file *pfil, const char __user *pBuf, size_t len, loff_t *p_off) { //Not relevant to this question } struct file_operations proc_fops = { .owner = THIS_MODULE, .write = write_proc, .read = read_proc, //This line causes the warning. };
编辑:所以答案是,我是一个白痴没有看到“int”与“ssize_t”。 谢谢大家! Codenheim和Andrew Medico大致在同一时间得到了正确的答案,但是我select了Medico's,因为它对未来的访问者来说更加迂腐和明显。
你的read_proc
函数的返回类型(抛出警告)与干净地编译的函数不匹配。
static ssize_t dev_read(struct file *pfil, char __user *pBuf, size_t len, loff_t *p_off)
与
static int read_proc(struct file *pfil, char __user *pBuf, size_t len, loff_t *p_off)
ssize_t
和int
可以是不同的大小。 你的函数的返回类型应该是ssize_t
。
在处理文件操作时,只需遵循这个结构的规则
struct file_operations { struct module *owner; loff_t (*llseek) (struct file *, loff_t, int); ssize_t (*read) (struct file *, char __user *, size_t, loff_t *); ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *); ssize_t (*aio_read) (struct kiocb *, const struct iovec *, unsigned long, loff_t); ssize_t (*aio_write) (struct kiocb *, const struct iovec *, unsigned long, loff_t); ssize_t (*read_iter) (struct kiocb *, struct iov_iter *); ssize_t (*write_iter) (struct kiocb *, struct iov_iter *); int (*iterate) (struct file *, struct dir_context *); unsigned int (*poll) (struct file *, struct poll_table_struct *); long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long); long (*compat_ioctl) (struct file *, unsigned int, unsigned long); int (*mmap) (struct file *, struct vm_area_struct *); int (*open) (struct inode *, struct file *); int (*flush) (struct file *); int (*release) (struct inode *, struct file *); int (*fsync) (struct file *, loff_t, loff_t, int datasync); int (*aio_fsync) (struct kiocb *, int datasync); int (*fasync) (int, struct file *, int); int (*lock) (struct file *, int, struct file_lock *); ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int); unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long); int (*check_flags)(int); int (*flock) (struct file *, int, struct file_lock *); ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, size_t, unsigned int); ssize_t (*splice_read)(struct file *, struct pipe_inode_info *, size_t, unsigned int); int (*setlease)(struct file *, long arg, struct file_lock **); long (*fallocate)(struct file *, int mode, loff_t offset, loff_t len); int (*show_fdinfo)(struct seq_file *m, struct file *f); };
您可以在内核文档Documentation / filesystems / vfs.txt中找到这个结构,或者您可以使用来自coreel Source的tag vim -t file_operations找到它,或者您可以查看头文件/include/linux/fs.h。
只是你的错误是你使用静态int而不是使用ssize_t的返回类型。
函数指针需要一个返回类型为ssize_t
的函数,但是你已经给了它一个int
你需要一个ssize_t
,而不是一个int
。