Dr. Klahn
asked on
C/linux device control issue
I'm chasing what I think should be an obvious issue, but I can't find it.
The host is a Pogoplug E02 running Debian jessie.
Logged into the system as root, the following two commands turn the green LED on and off as expected.
Below is a little program adapted from one at https://github.com/suetanvil/ledflash/blob/master/ledflash.c. To my mind this code should blink the green LED on and off a few times:
The program's effect is exactly nothing. No error on the LED device file open, no blinking LED. Clearly something is different between echoing the command from a console prompt, and writing it to the device from a program. I don't know what it might be, though.
Any ideas?
The host is a Pogoplug E02 running Debian jessie.
Logged into the system as root, the following two commands turn the green LED on and off as expected.
root@www:# echo "default-on" > "/sys/class/leds/status:green:health/trigger"
root@www:# echo "none" > "/sys/class/leds/status:green:health/trigger"
Below is a little program adapted from one at https://github.com/suetanvil/ledflash/blob/master/ledflash.c. To my mind this code should blink the green LED on and off a few times:
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <syslog.h>
#include <string.h>
#include <sys/stat.h>
# define FLASHFILE "/sys/class/leds/status:green:health/trigger"
# define ON "default-on"
# define OFF "none"
void main() {
FILE *ledfile;
int status;
ledfile = fopen(FLASHFILE, "w");
if (ledfile == NULL) {
printf("Unable to open '" FLASHFILE "' for write\n");
printf("Error: %d (%s)\n", errno, strerror(errno));
exit(0);
}
status = fputs("none", ledfile);
sleep(1);
status = fputs("default-on", ledfile);
sleep(1);
status = fputs("none", ledfile);
sleep(1);
status = fputs("default-on", ledfile);
sleep(1);
status = fputs("none", ledfile);
sleep(1);
status = fputs("default-on", ledfile);
sleep(1);
status = fputs("none", ledfile);
sleep(1);
status = fputs("default-on", ledfile);
sleep(1);
status = fputs("none", ledfile);
sleep(1);
status = fputs("default-on", ledfile);
sleep(1);
status = fputs("none", ledfile);
exit(0);
}
The program's effect is exactly nothing. No error on the LED device file open, no blinking LED. Clearly something is different between echoing the command from a console prompt, and writing it to the device from a program. I don't know what it might be, though.
Any ideas?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Oh yeah, automatic file caching. I had not thought of that. I suspect that's the issue. Since the write solution is working, I'll stick with that rather than force buffer flushes which strikes me as inelegant code.
ASKER
Sarabande correctly points out that fputs is subject to automatic file caching and that buffer flushes are necessary for I/O to complete at the time of issuance.
ASKER
I tried echo -n and the commands work. I also modified the test program to send \n in the fputs, and that still does not work.
Now here is a mystery. The following code does work.
Open in new window
I'm sure there is a good, rational reason for this but I can't imagine what it is. I have no problem with changing the code but out of curiousity I'll leave the question open for a while yet in case someone can shed light on this.