我正在使用内核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]
据我所知,我的file_operations设置proc函数与设备函数相同。我可以读/写/ 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几乎在同一时间得到了正确答案,但我之所以选择Medico,是因为它对未来的访客更为迂腐和明显。
答案 0 :(得分:5)
read_proc
函数的返回类型(抛出警告)与编译干净的函数不匹配。
static ssize_t dev_read(struct file *pfil, char __user *pBuf, size_t len, loff_t *p_off)
VS
static int read_proc(struct file *pfil, char __user *pBuf, size_t len, loff_t *p_off)
ssize_t
和int
的大小可能不同。您的函数的返回类型应为ssize_t
。
答案 1 :(得分:3)
使用文件操作时,只需按照此结构遵循规则
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); };
您可以在Kernel Documentation Documentation / filesystems / vfs.txt中找到此结构,或者您可以使用来自Kernel Source的标记vim -t file_operations找到它,或者您可以查看头文件/include/linux/fs.h.
只是你的错误是返回类型你使用静态int而不是使用ssize_t。
答案 2 :(得分:1)
函数指针需要一个返回类型为ssize_t
的函数,但是你给它一个int
您需要ssize_t
,而不是int
。