Reading value from /sys/class/... from C code


hmc

Active Member
Joined
Dec 19, 2011
Messages
787
Location
Bavaria, Germany
Hi guys,

I need some advice from you please!

I need to read the integer value (0 .. 255) from

/sys/class/leds/pandora::keypad_bl/brightness

into a variable in my C code (the keyboard light configbutton plugin).

How can I do this properly?

I have tried two approaches (the target variable is kbl_status)

if ((fp = fopen("/sys/class/leds/pandora::keypad_bl/brightness", "r")) == NULL)
{
kbl_status = 0;
} else
{
fscanf(fp,"%d",&kbl_status);
fclose(fp);
}
This approach fails with:

Code:
*** glibc detected *** configbutton: double free or corruption (top): 0x00088ac8 ***
Aborted
What does this mean? %-)

The other approach is: I have a shell script, that returns the status value.

Now I want to call this shell script from teh Plugin code:

kbl_status = system("/usr/pandora/scripts/op_kbd_light.sh");
This call gives me a segfault, as soon as the plugin is loaded by configbutton. (Verified by replacing this line by "kbl_status=0", which gives no segfault).However, in the console output of configbutton I see that the shell script is actually executed.

I also verified that the script really returns the correct value:

$ /usr/pandora/scripts/op_kbd_light.sh
Keyboard Light On, Brightness: 255
$ echo $?
255
$
I'd greatly appreaciate any hint :)
in case it's relevant or you're interested: I have attached the entire plugin's code. (remove ".txt" extension and then untar)

Thanks!

kblight_plugin.tgz.txt
 

Attachments

  • kblight_plugin.tgz.txt
    15.8 KB · Views: 223
Last edited by a moderator:
It could be an error in a different location corrupting your memory, and thereafter causing the segfault.

I did not see any attached code.

I recommend running configbutton with that plugin in valgrind, any memory corruption should show up.
 
Last edited by a moderator:
the message typically means that the glib detected that an reference to memory was requested to be freed more than once.

I would suggest creating a standalone application with your code so that it can easily be stepped through and analyzed .
 
I see the problem.

You close fp on both line 82 and line 96. That's bad, mmmkay?
great. Thanks! :)

That's it.

The second fclose(fp) was a left-over from another kind of implementation. Often the problem is too simple. A half-layman as I am suspects a much more complicated problem, so doesn't look for so simple potential issues.

Now it works. I'll optimize it, and my plan is to create an installed in a PND for the configbutton plugin.

Thans again for helping me out of this problem.
 
Is valgrind available for the Pandora in any way?
I compiled it but the glibc we have is stripped. It needs to somehow be rebuilt without stripping, or at least a debug package. Just getting valgrind to build was too much of a pain in my butt to keep sitting and tinkering.
 
PanDebian is really good for threaded gdb, valgrind, etc - they're all just in the package manager.

If you mount --bind the rootfs to a folder in PanDebian's chroot and add rootfs/usr/lib to your ld.so.conf you can transparently use host libraries (like the SGX driver).
 
Back
Top