2

Hey, I'm trying to play around a with sysfs a little bit, to get some data from user space into my kernel module. Here is the code:

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/elf.h>
#include <linux/fs.h>
#include <linux/slab.h>
#include <linux/kobject.h>
#include <linux/sysfs.h>

#define PRINT(x) printk(KERN_INFO x "\n")
#define ERROR(fmt,arg...) printk(KERN_ALERT "Error - " fmt,##arg)



ssize_t mystore (struct kobject *obj,
                    struct attribute *attr, const char *buff, size_t count)
{
 /* doing a little bit */
        return count;
}


ssize_t myshow (struct kobject *obj, struct attribute *attr, char *buff)
{
    return 0;
}

char file_name[] = "myfile";
static struct attribute myattribute = {
    .name = file_name,
    .mode = S_IRUSR | S_IWUSR
};
static struct sysfs_ops mysysfs_ops = {
    .show = myshow,
    .store = mystore
};
static struct kobj_type mykobj_type = {
    .sysfs_ops = &mysysfs_ops,
};
static struct kobject mykobject = {
    .name = "mykobject",
    .ktype = &mykobj_type
};

static int __init start(void)
{
    int tmp;

    PRINT("Initializing module\n");

    if (kobject_init_and_add(&mykobject, &mykobj_type, NULL, "mykobj") != 0) {
        ERROR ("Digsig key failed to register properly\n");
        return -1;
    }
    if ((tmp = sysfs_create_file(&mykobject, &myattribute)) != 0) {
        ERROR ("Create file failed\n");
        return -1;
    }
    PRINT("INIT CORRECT");
    return 0;
}

static void __exit close(void)
{
    PRINT("Deinitializing module\n");
    sysfs_remove_file(&mykobject, &myattribute);
    kobject_del(&mykobject);
}

module_init(start);
module_exit(close);

When I compile my module, all works fine, but when I try to run it I get a insmod: error inserting 'mytester.ko': -1 Unknown symbol in module

Using dmesg I get more details:

[18335.892462] mytester: Unknown symbol sysfs_remove_file (err 0)
[18335.892462] mytester: Unknown symbol sysfs_create_file (err 0)
[18335.892646] mytester: Unknown symbol kobject_init_and_add (err 0)

And that's the point. I don't understand this message, because both kobject.h and sysfs.h are included. So I can't really understand what is happening here. Even if I comment out my whole function mystore to a simple return (as shown) the error is the same. So the error is not elsewhere....

1 Answer 1

8

The sysfs_remove_file and other functions in your example are exported GPL only, and can only be accessed from a module marked with MODULE_LICENSE("GPL");. See the Linux Kernel FAQ for some more on this. If your module is for in-house use, or you plan to distribute as open source, this shouldn't be a problem. Otherwise, you may need to reconsider how you interface to the kernel.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, as I'm just learning about this I did not take care of such things obv :) But other than that: Can anyone see a bug in the code (especially the kobject- and sysfil-creation)? Because now it gets killed with a segfault :( Probably at the kobject_init_and_add Call

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.