LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-14-2011, 06:05 PM   #1
animesh
LQ Newbie
 
Registered: Aug 2011
Posts: 7

Rep: Reputation: Disabled
sysfs


Following is a simple sysfs code that creates the kobject test_test and adds a new attribute attr1 to it.

Code:
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/kobject.h>
#include <linux/sysfs.h>

static ssize_t attr1_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
{
	printk("goldeneagle: attr1_show\n");
	return 0;
}

static ssize_t attr1_store(struct kobject *kobj, struct kobj_attribute *attr, const char *buf, size_t cnt)
{
	printk("goldeneagle: attr1_store\n");
	return 0;
}

struct kobject *kobj;
const struct kobj_attribute kobj_attr =
	__ATTR(attr1, 0666, attr1_show, attr1_store);

static int test_char_init(void)
{
	int ret = 0;

	printk("\ngoldeneagle:%s\n", __func__);

	kobj = kobject_create_and_add("test_test", kernel_kobj);
	if (!kobj) {
		printk("goldeneagle: kobj could not be created and added\n");
		ret = -ENOMEM;
		goto fail;
	}

	ret = sysfs_create_file(kobj, &kobj_attr.attr);
	if (ret) {
		printk("goldeneagle: Failed to add new attribute\n");
		goto err;
	}
	return 0;
err:
	sysfs_remove_file(kobj, &kobj_attr.attr);
	kobject_put(kobj);
fail:
	return ret;
}

static void test_char_exit(void)
{
	printk("\ngoldeneagle:%s\n", __func__);
	sysfs_remove_file(kobj, &kobj_attr.attr);
	kobject_put(kobj);
}

module_init(test_char_init);
module_exit(test_char_exit);

MODULE_LICENSE("GPL");
When I try to write something to attr1 via "echo 0 > attr1"
I see attr1_store getting called infinite no. of time.

Please suggest what might be causing this issue !!!
 
Old 08-16-2011, 04:38 AM   #2
gary185
Member
 
Registered: Jul 2011
Posts: 113

Rep: Reputation: Disabled
warning * i'm not very good at following other peoples code

first thing i see right off is you are using printk
printk uses a cyclic buffer to hold messages managed by klogd and spit to terminal or logs
you should have something like

Code:
// create a file in your sysfs subdirectory to write to named for example "example"
static int example;

static ssize_t attr1_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
{
// however you print a string with sprintf i think it's %s
	 return sprintf(buf, "%s\n", example);
}

static ssize_t attr1_store(struct kobject *kobj, struct kobj_attribute *attr, const char *buf, size_t cnt)
{
//and then for this one
	 sscanf(buf, "%su", &example);
         return cnt;

}
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
sysfs filesystem fateme-linux Linux - Kernel 2 10-28-2007 02:07 AM
Sysfs use chessi Linux - Kernel 1 04-24-2007 04:59 PM
Sysfs info chessi Linux - Newbie 3 04-24-2007 04:53 PM
How to use sysfs? asahlot Linux - Kernel 1 01-21-2007 04:44 PM
2.6.x kernel,sysfs and rc.S tobyl Slackware 3 04-09-2004 02:24 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:42 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration