一. 問(wèn)題引入
linux5.10生成在/proc目錄下的文件時(shí),利用cat讀取文件,提示:
operation not permitted
該報(bào)錯(cuò)是錯(cuò)誤碼:EPERM,不允許操作
二. 問(wèn)題原因
發(fā)現(xiàn)是在移植內(nèi)核代碼時(shí),未對(duì)proc接口進(jìn)行適配。
linux-5.6引入結(jié)構(gòu)體struct proc_ops,用以替代struct file_operations在/proc下進(jìn)行文件操作。
proc_create中的proc_ops結(jié)構(gòu)體類型定義改變,導(dǎo)致不匹配
//struct proc_dir_entry *proc_create(const char *name, umode_t mode, struct proc_dir_entry *parent, const struct file_operations *proc_fops);
struct proc_dir_entry *proc_create(const char *name, umode_t mode, struct proc_dir_entry *parent, const struct proc_ops *proc_ops);
struct proc_ops定義:
//proc_fs.h
struct proc_ops {
int (*proc_open)(struct inode *, struct file *);
ssize_t (*proc_read)(struct file *, char __user *, size_t, loff_t *);
ssize_t (*proc_write)(struct file *, const char __user *, size_t, loff_t *);
loff_t (*proc_lseek)(struct file *, loff_t, int);
int (*proc_release)(struct inode *, struct file *);
__poll_t (*proc_poll)(struct file *, struct poll_table_struct *);
long (*proc_ioctl)(struct file *, unsigned int, unsigned long);
#ifdef CONFIG_COMPAT
long (*proc_compat_ioctl)(struct file *, unsigned int, unsigned long);
#endif
int (*proc_mmap)(struct file *, struct vm_area_struct *);
unsigned long (*proc_get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
};
引入原因,見提交:proc: decouple proc from VFS with “struct proc_ops”
貼一下提交的翻譯,有興趣的可以自己了解:
目前核心 /proc 代碼使用“結(jié)構(gòu)file_operations”作為自定義鉤子,但是,VFS 不直接調(diào)用它們。 每次 VFS 擴(kuò)展file_operations鉤子集時(shí),/proc 代碼都會(huì)無(wú)緣無(wú)故地膨脹。
引入“struct proc_ops”,它只包含 /proc 允許調(diào)用的那些鉤子(打開、釋放、讀取、寫入、ioctl、mmap、poll)。 它也不包含模塊指針。
三. 解決方法
將需要生成在/proc下的文件的結(jié)構(gòu)體用struct proc_ops代替struct file_operations,例如:文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-716910.html
//static const struct file_operations alignment_proc_fops = {
// .open = alignment_proc_open,
// .read = seq_read,
// .llseek = seq_lseek,
// .release = single_release,
// .write = alignment_proc_write,
//};
static const struct proc_ops alignment_proc_ops = {
.proc_open = alignment_proc_open,
.proc_read = seq_read,
.proc_lseek = seq_lseek,
.proc_release = single_release,
.proc_write = alignment_proc_write,
};
參考:【Linux】Ubuntu20.04 內(nèi)核5.11.8 用proc_create和seq_file創(chuàng)建proc文件文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-716910.html
到了這里,關(guān)于【Linux】linux5.6引入struct proc_ops,用以替代struct file_operations在/proc下進(jìn)行文件操作的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!