I'm trying to write a simple kernel module, just for didactic purpose. In particular, what is giving me headaches is the path lookup. I'd like to have a sysfs entry and in the store operation I want to receive a buffer and I want to save it only if it's a valid path. So I tried to use the vfs_path_lookup Exported Symbols from here namei.c. But even inserting a valid path, it prints me the ENOENT error.
This is my code:
Into the header:
extern int vfs_path_lookup(struct dentry *, struct vfsmount *, const char *, unsigned int, struct path *path);
Into the module store function:
ssize_t path_store(struct kobject *kobj, struct kobj_attribute *attr, const char *buf, size_t count){
int err = vfs_path_lookup(current->fs->root.dentry, current->fs->root.mnt, buf, LOOKUP_DIRECTORY, base_path);
printk(KERN_DEBUG "%s\n", __FUNCTION__);
printk(KERN_DEBUG "Received: %s err is %d\n", buf, err);
strncpy(base_addr, buf, MAX_FILENAME_SIZE);
printk(KERN_DEBUG "Base Addr: %s\n", base_addr);
return strlen(base_addr);
}
What I tried to write into the file is the string "/home/osboxes/Documents" that is an existing directory. I suspect that I didn't get the real usage of the function, maybe something with flags. Thank you in advance for your help.
Edit: This is the example requested into the comments (Thanks again)
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/namei.h>
#include <linux/slab.h>
#include <linux/err.h>
#include <linux/kobject.h>
#include <linux/sysfs.h>
#define MAX_FILENAME_SIZE 256
struct path *base_path ;
struct kobject *conf_kobj;
char base_addr[MAX_FILENAME_SIZE] = "/home/osboxes/Documenti";
struct kobj_attribute *get_attribute(char *name, umode_t mode, ssize_t (*show)(struct kobject *, struct kobj_attribute *, char *), ssize_t (*store)(struct kobject *, struct kobj_attribute *,
const char *, size_t));
ssize_t path_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf){
printk(KERN_DEBUG "%s\n", __FUNCTION__);
return sprintf(buf, "%s", base_addr);
}
ssize_t path_store(struct kobject *kobj, struct kobj_attribute *attr, const char *buf, size_t count){
int err = kern_path(buf, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, base_path);
printk(KERN_DEBUG "%s\n", __FUNCTION__);
printk(KERN_DEBUG "Received: %s err is %d\n", buf, err);
strncpy(base_addr, buf, MAX_FILENAME_SIZE);
printk(KERN_DEBUG "Base Addr: %s\n", base_addr);
return strlen(base_addr);
}
static int __init init_mymodule(void)
{
struct kobj_attribute *path_attr;
printk(KERN_DEBUG "Module inserted\n");
path_attr = get_attribute(base_addr, 0666, path_show, path_store);
base_path = kmalloc(sizeof(struct path), GFP_KERNEL);
conf_kobj = kobject_create_and_add("conf", kernel_kobj);
sysfs_create_file(conf_kobj, &path_attr->attr);
return 0;
}
struct kobj_attribute *get_attribute(char *name, umode_t mode, ssize_t (*show)(struct kobject *kobj, struct kobj_attribute *attr, char *buf), ssize_t (*store)(struct kobject *kobj, struct kobj_attribute *attr,
const char *buf, size_t count))
{
struct kobj_attribute *attribute = kmalloc(sizeof(struct kobj_attribute), GFP_KERNEL);
attribute->attr.name = name;
attribute->attr.mode = mode;
if(show)
attribute->show = show;
if(store)
attribute->store = store;
return attribute;
}
static void __exit exit_mymodule(void)
{
kobject_put(conf_kobj);
kfree(base_addr);
printk(KERN_INFO "Module removed\n");
}
module_init(init_mymodule);
module_exit(exit_mymodule);
MODULE_LICENSE("GPL");
I also replaced vfs_path_lookup with kern_path as @Tsyvarev suggested. The error comes when I write into the terminal
echo "/home/osboxes/Documenti"> /sys/kernel/conf/path
When I use the address into the "buf" variable, in this case it's hardcoded, but doesn't work.