Re: More info about kobj_attribute store and show functions
Sorry, didn't finish to write the email and Gmail sent. I will re-send. Sorry. -- Lucas Tanure +55 (19) 988176559
(Forget the last e-mail, please, stupid Gmail) Hi, I'm looking for some information about : struct kobj_attribute { struct attribute attr; 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); }; My function to handle the operation: static ssize_t my_store(struct kobject *kobj, struct kobj_attribute *attr, const char *buf, size_t count) { char *buffer; buffer = (char*) kcalloc(count, sizeof(char), GFP_KERNEL); copy_from_user(buffer, buf, count); ..... ..... etc.... } So, this *const char *buf, *where ti come from ? Kernel space? My copy_from_user it's not working. Should return zero. There any Macro or function to test if the pointer *buf *is a kernel pointer ? Thanks Lucas Tanure
On Wed, Sep 03, 2014 at 09:10:41PM -0300, Lucas Tanure wrote:
(Forget the last e-mail, please, stupid Gmail)
Hi,
I'm looking for some information about :
struct kobj_attribute { struct attribute attr; 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); };
My function to handle the operation:
static ssize_t my_store(struct kobject *kobj, struct kobj_attribute *attr, const char *buf, size_t count) { char *buffer;
buffer = (char*) kcalloc(count, sizeof(char), GFP_KERNEL);
copy_from_user(buffer, buf, count); ..... ..... etc.... }
So, this *const char *buf, *where ti come from ? Kernel space?
I'm guessing you didn't read the kobject documentation that is in the kernel tree?
Greg, I'm still trying to understand the sysfs. I indeed read the documentation, I was using your "Sample kobject implementation" as base of my code. But as 4h of work there some points that I didn't understand yet. Thanks -- Lucas Tanure +55 (19) 988176559 On Wed, Sep 3, 2014 at 9:23 PM, Greg KH <greg@kroah.com> wrote:
On Wed, Sep 03, 2014 at 09:10:41PM -0300, Lucas Tanure wrote:
(Forget the last e-mail, please, stupid Gmail)
Hi,
I'm looking for some information about :
struct kobj_attribute { struct attribute attr; 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); };
My function to handle the operation:
static ssize_t my_store(struct kobject *kobj, struct kobj_attribute *attr, const char *buf, size_t count) { char *buffer;
buffer = (char*) kcalloc(count, sizeof(char), GFP_KERNEL);
copy_from_user(buffer, buf, count); ..... ..... etc.... }
So, this *const char *buf, *where ti come from ? Kernel space?
I'm guessing you didn't read the kobject documentation that is in the kernel tree?
A: No. Q: Should I include quotations after my reply? http://daringfireball.net/2007/07/on_top On Wed, Sep 03, 2014 at 09:39:50PM -0300, Lucas Tanure wrote:
Greg,
I'm still trying to understand the sysfs.
Why? What are you using "raw kobjects" for? You should almost never need them.
I indeed read the documentation, I was using your "Sample kobject implementation" as base of my code.
Great, then it should "just work", right? :) Care to post your non-working code? thanks, greg k-h
Greg, I was looking again to documentation, but didn't find a explanation about the show and store functions. I'm missing something ? Yes, everything work if I simple use the buffer from store function, but I was looking for where it came from. Like, It was from user space? So, I need to take care like simple_write_to_buffer does with user space memory ? I said that because of file file_operations read and write, that I know that comes from user space. But kobj_attribute I didn't understand yet. What I meant posting one piece of my code was just show what I doing in high level. But, for some reason gmail sent in the middle of my writing. static ssize_t id_store(struct kobject *kobj, struct kobj_attribute *attr, const char *buf, size_t count) { char *buffer; buffer = kcalloc(cnt, sizeof(char), GFP_KERNEL); if (buffer) { if (copy_from_user(buffer, buf, cnt) == cnt) { if (strcmp(buffer, "Tanure") == 0) return cnt; } } } return -EINVAL; } The copy_from_user returns 6 when I write my name in the file. So, 6 means that copy_from_user fails to copy 6 bytes from buf. So there is my question, buf is a user pointer ? Or Can I just copy without problems ? All this e-mails got me in trouble here. I got kicked from eudyptula challenge, what I still begging to be able to finish. Thanks Greg for all the help. -- Lucas Tanure +55 (19) 988176559 On Wed, Sep 3, 2014 at 9:56 PM, Greg KH <greg@kroah.com> wrote:
A: No. Q: Should I include quotations after my reply?
http://daringfireball.net/2007/07/on_top
On Wed, Sep 03, 2014 at 09:39:50PM -0300, Lucas Tanure wrote:
Greg,
I'm still trying to understand the sysfs.
Why? What are you using "raw kobjects" for? You should almost never need them.
I indeed read the documentation, I was using your "Sample kobject implementation" as base of my code.
Great, then it should "just work", right? :)
Care to post your non-working code?
thanks,
greg k-h
On Wed, Sep 03, 2014 at 10:39:06PM -0300, Lucas Tanure wrote:
Greg,
I was looking again to documentation, but didn't find a explanation about the show and store functions. I'm missing something ?
Yes, everything work if I simple use the buffer from store function, but I was looking for where it came from. Like, It was from user space? So, I need to take care like simple_write_to_buffer does with user space memory ?
If it came from userspace, it would have been marked with a __user marking, so it's a kernel pointer.
static ssize_t id_store(struct kobject *kobj, struct kobj_attribute *attr, const char *buf, size_t count) { char *buffer; buffer = kcalloc(cnt, sizeof(char), GFP_KERNEL); if (buffer) { if (copy_from_user(buffer, buf, cnt) == cnt) { if (strcmp(buffer, "Tanure") == 0) return cnt; } } } return -EINVAL; }
Just work with the buffer directly, sysfs is supposed to be "easy" to use. greg k-h
From: kernelnewbies-bounces+jharan=bytemobile.com@kernelnewbies.org [mailto:kernelnewbies-bounces+jharan=bytemobile.com@kernelnewbies.org] On Behalf Of Lucas Tanure Sent: Wednesday, September 03, 2014 5:11 PM To: kernelnewbies Subject: Re: More info about kobj_attribute store and show functions (Forget the last e-mail, please, stupid Gmail) Hi, I'm looking for some information about : struct kobj_attribute { struct attribute attr; 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); }; My function to handle the operation: static ssize_t my_store(struct kobject *kobj, struct kobj_attribute *attr, const char *buf, size_t count) { char *buffer; buffer = (char*) kcalloc(count, sizeof(char), GFP_KERNEL); copy_from_user(buffer, buf, count); ..... ..... etc.... } So, this const char *buf, where ti come from ? Kernel space? My copy_from_user it's not working. Should return zero. There any Macro or function to test if the pointer buf is a kernel pointer ? Thanks Lucas Tanure I haven’t written too many of these things, but the last time I wrote a store function I didn’t need to do the copy_to_user(). buf seemed to point to directly accessible kernel memory. I would think a simple memcpy above would do the trick. Jeff
participants (3)
-
Greg KH -
Jeff Haran -
Lucas Tanure