写在前面
因项目需要,需要在用户态访问一些文件系统信息,因此在/proc/fs/f2fs/XXX/下加了一些可读写文件,但是非root模式下apk或者adb都访问不了这里的文件(下图),在f2fs 大佬的帮助下解决了这个问题。
尝试一
在sysfs.c中,修改F2FS_RW_ATTR(struct_type, struct_name, name, elname)
,将0644改为0777
#define F2FS_RW_ATTR(struct_type, struct_name, name, elname) \
F2FS_ATTR_OFFSET(struct_type, name, 0777, \
f2fs_sbi_show, f2fs_sbi_store, \
offsetof(struct struct_name, elname))
尝试二
kobject - ktype - get_ownership
在kobject 中的ktype 中实现下get_ownership
这个函数,把owner修改成非root用户
struct kobject {
const char *name;
struct list_head entry;
struct kobject *parent;
struct kset *kset;
struct kobj_type *ktype;
struct kernfs_node *sd; /* sysfs directory entry */
struct kref kref;
#ifdef CONFIG_DEBUG_KOBJECT_RELEASE
struct delayed_work release;
#endif
unsigned int state_initialized:1;
unsigned int state_in_sysfs:1;
unsigned int state_add_uevent_sent:1;
unsigned int state_remove_uevent_sent:1;
unsigned int uevent_suppress:1;
};
struct kobj_type {
void (*release)(struct kobject *kobj);
const struct sysfs_ops *sysfs_ops;
struct attribute **default_attrs; /* use default_groups instead */
const struct attribute_group **default_groups;
const struct kobj_ns_type_operations *(*child_ns_type)(struct kobject *kobj);
const void *(*namespace)(struct kobject *kobj);
void (*get_ownership)(struct kobject *kobj, kuid_t *uid, kgid_t *gid);
};
对于这两个结构体的解释可参考这里
找到f2fs中的f2fs_ktype
,在里面添加函数
static struct kobj_type f2fs_ktype = {
.default_attrs = f2fs_attrs,
.sysfs_ops = &f2fs_attr_ops,
.release = f2fs_sb_release,
.get_ownership = f2fs_get_ownership,//add qwj
};
自己编写了f2fs_get_ownership
函数,uid和gid可在adb shell
中使用id查看
static void f2fs_get_ownership(struct kobject *kobj, kuid_t *uid, kgid_t *gid)//add qwj
{
// *uid = make_kuid(current_user_ns(), 0777);
// *gid = make_kgid(current_user_ns(), 0777);
*uid = KUIDT_INIT(2000);
*gid = KGIDT_INIT(2000);
}
尝试三
关闭SELinux
adb shell
su
setenforce 0