Sysfs class attribute problem

Pranay Kumar Srivastava Pranay.Shrivastava at hcl.com
Thu Jun 21 04:23:36 EDT 2012



> -----Original Message-----
> From: kernelnewbies-bounces at kernelnewbies.org [mailto:kernelnewbies-
> bounces at kernelnewbies.org] On Behalf Of kernelnewbies-
> request at kernelnewbies.org
> Sent: Wednesday, June 20, 2012 11:33 PM
> To: kernelnewbies at kernelnewbies.org
> Subject: Kernelnewbies Digest, Vol 19, Issue 39
> 
> Send Kernelnewbies mailing list submissions to
> 	kernelnewbies at kernelnewbies.org
> 
> To subscribe or unsubscribe via the World Wide Web, visit
> 	http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
> or, via email, send a message with subject or body 'help' to
> 	kernelnewbies-request at kernelnewbies.org
> 
> You can reach the person managing the list at
> 	kernelnewbies-owner at kernelnewbies.org
> 
> When replying, please edit your Subject line so it is more specific
> than "Re: Contents of Kernelnewbies digest..."
> 
> 
> Today's Topics:
> 
>    1. RE: A confusion about invoking my syscall (Jeff Haran)
>    2. Re: Sysfs class attribute problem (Jeshwanth Kumar N K Jeshu)
> 
> 
> ----------------------------------------------------------------------
> 
> Message: 1
> Date: Wed, 20 Jun 2012 16:58:30 +0000
> From: Jeff Haran <jharan at bytemobile.com>
> Subject: RE: A confusion about invoking my syscall
> To: ?? <wangzhe5004 at gmail.com>
> Cc: kernelnewbies <kernelnewbies at kernelnewbies.org>
> Message-ID:
> 	<4AB110E2FA959F41AC8480E84516371105B80D at HQ-EX01.bytemobile.com>
> Content-Type: text/plain; charset="iso-2022-jp"
> 
> 
> 
> From: ?? [mailto:wangzhe5004 at gmail.com]
> Sent: Wednesday, June 20, 2012 1:16 AM
> To: Jeff Haran
> Cc: kernelnewbies
> Subject: Re: A confusion about invoking my syscall
> 
> 
> 2012/6/20 Jeff Haran
> <jharan at bytemobile.com<mailto:jharan at bytemobile.com>>
> 
> 
> From: ?? [mailto:wangzhe5004 at gmail.com<mailto:wangzhe5004 at gmail.com>]
> Sent: Monday, June 18, 2012 9:32 PM
> To: Jeff Haran
> Cc: kernelnewbies
> Subject: Re: A confusion about invoking my syscall
> 
> 
> 2012/6/19 Jeff Haran
> <jharan at bytemobile.com<mailto:jharan at bytemobile.com>>
> 
> 
> From: kernelnewbies-bounces at kernelnewbies.org<mailto:kernelnewbies-
> bounces at kernelnewbies.org> [mailto:kernelnewbies-
> bounces at kernelnewbies.org<mailto:kernelnewbies-
> bounces at kernelnewbies.org>] On Behalf Of ??
> Sent: Monday, June 18, 2012 6:40 PM
> To: kernelnewbies
> Subject: A confusion about invoking my syscall
> 
> Hello everyone:
> 
>          I append a simple syscall in kernel. and the function is as
> follows:
> 
>   asmlinkage  long sys_mysyscall(long data)
>  {
>           printk("This is my syscall!\n");
>           return data;
>   }
> 
> and i test it sucessfully in user space . and the test program:
> 
>    #include <linux/unistd.h>
>    #include <syscall.h>
>    #include <sys/types.h>
>    #include <stdio.h>
> 
> 
> 
>    int main(void)
>    {
>    long n = 0,m = 0,pid1,pid2;
>    n = syscall(345,190);// #define __NR_mysyscall          345
>    printf("n = %ld\n",n);
>    pid1 = syscall(SYS_getpid);  //getpid
>    printf("pid = %ld\n",pid1);
>    pid2 = syscall(20);  //getpid
>    printf("pid = %ld\n",pid2);
>    return 0;
>   }
> and the result:
> n = 190
> pid = 4097
> pid = 4097
> 
> but if the test program is:
> #include <linux/unistd.h>
> #include <syscall.h>
> #include <sys/types.h>
> #include <stdio.h>
> 
> 
> 
> int main(void)
> {
>  long n = 0,m = 0,pid1,pid2;
>  n = syscall(345,190);// #define __NR_mysyscall          345
>  printf("n = %ld\n",n);
>  m = syscall(SYS_mysyscall,190);
>  printf("m = %ld\n",m);
>  pid1 = syscall(SYS_getpid);  //getpid
>  printf("pid = %ld\n",pid1);
>  pid2 = syscall(20);  //getpid
>  printf("pid = %ld\n",pid2);
>  return 0;
> }
> and the result:
> wanny at wanny-C-Notebook-XXXX:~/syscall/src$ gcc test1.c
> test1.c: In function ?main?:
> test1.c:13:14: error: ?SYS_mysyscall? undeclared (first use in this
> function)
> test1.c:13:14: note: each undeclared identifier is reported only once
> for each function it appears in
> 
> 
> why i can't invoke my syscall with "SYS_mysyscall"?
> 
> Thanks in advance!
> Because it appears you never defined the symbol SYS_mysyscall.
>  I think so,but where shoud i defne the  symbol SYS_mysyscall ?
>   and where is the symbol SYS_getpid defined?
> On my system /usr/include/bits/syscall.h, which is being included in
> your program because it includes syscall.h.
>            83 #define SYS_getpid __NR_getpid  ?so SYS_getpid is
> replaced by __NR_getpid. and __NR_getpid was defined in the
> kernel(arch/x86/include/asm/unistd_32.h). and my syscall was also
> defined there.#define SYS_mysyscall __NR_mysyscall, i don't kown why it
> doesn't works.
> 
> My sources contain no reference to SYS_mysyscall nor __NR_mysyscall, so
> I assume you?ve added them to the Linux include files that you built
> your module from.
> 
> User space programs like your main() program above generally aren?t
> going to include Linux source tree include files. When you include
> <syscall.h> from a user space program in a typical development
> environment, the compiler is by default going to look for syscall.h in
> /usr/include, not in the Linux source tree where presumably you?ve made
> your modifications. Of course you can always tell the compiler to look
> there using the -I command line option to gcc, if you want to. The
> usual practice however is to keep kernel code and user code include
> files completely separate. That means some duplication of effort, like
> having to define SYS_mysyscall in two different places, but that?s the
> usual practice because most people aren?t building kernels and thus
> haven?t installed the kernel source include files.
> 
> Jeff Haran
> 
> 
> -------------- next part --------------
> An HTML attachment was scrubbed...
> URL:
> http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/2012
> 0620/4287a36b/attachment-0001.html
> 
> ------------------------------
> 
> Message: 2
> Date: Wed, 20 Jun 2012 23:32:12 +0530
> From: Jeshwanth Kumar N K Jeshu <jeshkumar555 at gmail.com>
> Subject: Re: Sysfs class attribute problem
> To: anish singh <anish198519851985 at gmail.com>
> Cc: kernelnewbies at kernelnewbies.org
> Message-ID:
> 	<CAC-LjFsrQk4FQab-Yb=ZKExd_FT9dB0X_BvLK6R-
> oGuOpVXtPg at mail.gmail.com>
> Content-Type: text/plain; charset="iso-8859-1"
> 
> Hello
> 
> Thank you anish for the reply, the code is I pasted below.
> [code-starts]
> 
> #include <linux/init.h>
> #include <linux/module.h>
> #include <linux/kernel.h>
> #include <linux/fs.h>
> #include <linux/device.h>
> #include <linux/sysdev.h>
> #include <linux/major.h>
> #include <asm/uaccess.h>
> #include <linux/slab.h>
> #include <linux/cdev.h>
> #include <linux/kdev_t.h>
> 
> static char *gvar = "BeHonest";

Your gvar doesn't point to any allocated or statically allocated memory.....

> // Store and Show functions......
>  static ssize_t attr1_store(struct class *cls, struct class_attribute
> *attr, const char *buf, size_t count);
>  static ssize_t attr1_show(struct class *cls, struct class_attribute
> *attr,
> char *buf);
> 
> static struct class_attribute pwm_class_attrs[] = {
>     __ATTR(attr1, S_IRUGO | S_IWUSR , attr1_show, attr1_store),
>     __ATTR_NULL
> };
> 
> static struct class pwm_class =
> {
>     .name = "dev_jes",
>     .owner = THIS_MODULE,
>     .class_attrs = pwm_class_attrs
> };
> 
> 
> static int hello_init(void)
> {
>     class_register(&pwm_class);
>     printk("In hello_init function \n");
> 
>     return 0;
> }
> 
> static ssize_t attr1_show(struct class *cls, struct class_attribute
> *attr,
> char *buf)
> {
> 
>     printk("In attr1_show function\n");
>     return sprintf(buf, "%s\n", gvar);
> }
> 
> static ssize_t attr1_store(struct class *cls, struct class_attribute
> *attr,
> const char *buf, size_t count)
> {
>             printk("the string is : %s\n",buf);
>             printk("In attr1_store function\n");
>             return 1;
>             //return sprintf(gvar, "%s\n", buf);  --- If I put this
> line I
> am getting ERROR :(
Ofcourse you'll....! gvar isn't allocated memory... you are trying to over write a read only memory area.
> }
> 
> 
> static void  hello_exit(void)
> {
>     class_unregister(&pwm_class);
>     printk("In hello_exit function \n");
> }
> 
> module_init(hello_init);
> module_exit(hello_exit);
> 
> MODULE_LICENSE("GPL");
> MODULE_AUTHOR("Jeshwanth");
> MODULE_DESCRIPTION("Learning sysfs");
> 
> [code-ends]
> 
> echo "jeshu" > /sys/class/dev_jes/attr1
> 
> And If I pass the command to atttribute I got result below in dmesg.
> 
> [ 3435.613057] the string is : jeshu
> [ 3435.613061]
> [ 3435.613066] In attr1_store function
> [ 3435.613072] the string is : eshu
> [ 3435.613074]
> [ 3435.613078] In attr1_store function
> [ 3435.613083] the string is : shu
> [ 3435.613085]
> [ 3435.613088] In attr1_store function
> [ 3435.613093] the string is : hu
> [ 3435.613095]
> [ 3435.613098] In attr1_store function
> [ 3435.613103] the string is : u
> [ 3435.613105]
> [ 3435.613108] In attr1_store function
> [ 3435.613113] the string is :
> [ 3435.613115]
> [ 3435.613118] In attr1_store function
> 
> 
> So please help me how to read the attribute in store function.. Thanks
> :)
> 
> 
> On Tue, Jun 19, 2012 at 4:02 PM, anish singh
> <anish198519851985 at gmail.com>wrote:
> 
> > On Tue, Jun 19, 2012 at 3:14 PM, jeshwanth Kumar N K
> > <jeshkumar555 at gmail.com> wrote:
> > > Hello all
> > >
> > > I am new to sysfs interface ans I read about in mochel's
> documentation.
> > And
> > > doing some experiements on it. Let's come to Tue problem, in my
> module I
> > > have a global variable type char* myglobal and it s static, I am
> putting
> > > that value in my one and only class attribute using show function.
> But
> > for
> > > the same if I a user put some value in to the using echo or
> something,
> > the
> > > attribute value is not changing. And one more thing s after I
> passed the
> > > value to the attribute my system not shutting down :( . Please help
> me
> > how
> > > to read values.
> > Why don't you post your code?As that will really help and moreover as
> > this is your code
> > you won't be breakign any law set by your company.
> > >
> > > // in store function I am doing this:
> > > Myglobal = bus;
> > >
> > > Sent from my HTC
> > >
> > >
> > >
> > >
> > > _______________________________________________
> > > Kernelnewbies mailing list
> > > Kernelnewbies at kernelnewbies.org
> > > http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
> > >
> >
> 
> 
> 
> --
> Regards
> Jeshwanth Kumar N K
> +91-7411483498
> -------------- next part --------------
> An HTML attachment was scrubbed...
> URL:
> http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/2012
> 0620/72d70af2/attachment.html
> 
> ------------------------------
> 
> _______________________________________________
> Kernelnewbies mailing list
> Kernelnewbies at kernelnewbies.org
> http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
> 
> 
> End of Kernelnewbies Digest, Vol 19, Issue 39
> *********************************************


::DISCLAIMER::
----------------------------------------------------------------------------------------------------------------------------------------------------

The contents of this e-mail and any attachment(s) are confidential and intended for the named recipient(s) only.
E-mail transmission is not guaranteed to be secure or error-free as information could be intercepted, corrupted,
lost, destroyed, arrive late or incomplete, or may contain viruses in transmission. The e mail and its contents
(with or without referred errors) shall therefore not attach any liability on the originator or HCL or its affiliates.
Views or opinions, if any, presented in this email are solely those of the author and may not necessarily reflect the
views or opinions of HCL or its affiliates. Any form of reproduction, dissemination, copying, disclosure, modification,
distribution and / or publication of this message without the prior written consent of authorized representative of
HCL is strictly prohibited. If you have received this email in error please delete it and notify the sender immediately.
Before opening any email and/or attachments, please check them for viruses and other defects.

----------------------------------------------------------------------------------------------------------------------------------------------------




More information about the Kernelnewbies mailing list