Find the answer to your Linux question:
Results 1 to 4 of 4
I am trying to follow this example people.ee.ethz.ch/~arkeller/linux/code/sysfs_ex2.c which is basically a kernel module which will create a fake bus device with a busval attribute. According to the comments, I ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jun 2014
    Posts
    2

    Echo 'file exists' error with bus device in kernel module


    I am trying to follow this example people.ee.ethz.ch/~arkeller/linux/code/sysfs_ex2.c which is basically a kernel module which will create a fake bus device with a busval attribute. According to the comments, I should be able to write to busval (which should fire up the store() function). However when i do
    Code:
    echo 2 > busval
    I get the message
    Code:
    busval: File exists
    I tried
    Code:
    echo 2 >> busval
    and I get
    Code:
    busval: Permission denied
    I get a similar error when i write a c program to open and write to the file. Using sudo doesn't fix the problem. Am I doing anything wrong?

  2. #2
    Linux Engineer
    Join Date
    Dec 2013
    Posts
    1,201
    I think you posted a link to code other then what you intended. There are no comments in that code and what it does, according to the article, is create a couple of files in the sysfs.

    Perhaps check the link and post the one to the code your implementing?

  3. #3
    Just Joined!
    Join Date
    Jun 2014
    Posts
    2
    Quote Originally Posted by gregm View Post
    I think you posted a link to code other then what you intended. There are no comments in that code and what it does, according to the article, is create a couple of files in the sysfs.

    Perhaps check the link and post the one to the code your implementing?
    That actually is the code I am trying to implement. For reference

    Code:
    #include <linux/module.h>
    #include <linux/kernel.h>
    #include <linux/init.h>
    #include <linux/device.h>
    
    /* the parameter value shows up in /sys/module/sysfs/parameters/ 
     * it can be read an changed, however it is not possible to do 
     * some "additional action" when it is read/written
     */
    static int example_value;
    module_param_named(value, example_value, int, S_IRUGO | S_IWUSR);
    MODULE_PARM_DESC(value, "a test value");
    
    /* a file called busval will show up under /sys/bus/my_pseudo_bus/
     * bus_show and bus_store is executed when it is read and written
     * respectively
     */
    static int bus_value = 1;
    
    static struct bus_type my_pseudo_bus = {
    	.name = "my_pseudo_bus",
    };
    
    
    static ssize_t bus_show(struct bus_type *bus, char *buf) {
    	return scnprintf(buf, PAGE_SIZE, "%d\n", bus_value);
    }
    
    static ssize_t bus_store(struct bus_type *bus, const char *buf, size_t count) {
    	sscanf(buf, "%d", &bus_value);
            return sizeof(int);
    }
    BUS_ATTR(busval, S_IRUGO | S_IWUSR, bus_show, bus_store);
    
    static int __init sysfsexample_module_init(void)
    {
    	int ret = -1;
    	
    	ret = bus_register(&my_pseudo_bus);
    	if (ret < 0) {
                 	printk(KERN_WARNING "sysfs: error register bus: %d\n", ret);
    		return ret;
         	}
    
    	ret = bus_create_file(&my_pseudo_bus, &bus_attr_busval);
    	if (ret < 0) {
    		printk(KERN_WARNING "sysfs: error creating busfile\n");
    		bus_unregister(&my_pseudo_bus);	
    	}
    
    	return 0;
    }
    
    static void __exit sysfsexample_module_exit(void)
    {
    	bus_remove_file(&my_pseudo_bus, &bus_attr_busval);
    	bus_unregister(&my_pseudo_bus);
    }
    
    module_init(sysfsexample_module_init);
    module_exit(sysfsexample_module_exit);
    MODULE_LICENSE("GPL");
    I can't seem to be able to write to the busval sysfs file that is created unlike what the comments in the code segment above says.

  4. #4
    Linux Engineer
    Join Date
    Dec 2013
    Posts
    1,201
    What does :
    Code:
    ls -l /sys/bus/my_pseudo_bus/busval
    output?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •