Link to home
Start Free TrialLog in
Avatar of Dr. Klahn
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.

root@www:# echo "default-on" > "/sys/class/leds/status:green:health/trigger"
root@www:# echo "none" > "/sys/class/leds/status:green:health/trigger"

Open in new window


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);
}

Open in new window


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
Avatar of David Favor
David Favor
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Dr. Klahn
Dr. Klahn

ASKER

David, thanks for your comment.

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.

#include <errno.h>
#include <fcntl.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",10
#   define OFF         "none",4

void main() {
    int status;
    int fn;

  fn = open(FLASHFILE,O_WRONLY);
  if(fn < 1){
    printf("Unable to open '" FLASHFILE "' for write\n");
    printf("Error: %d (%s)\n\n", errno, strerror(errno));
    exit(0);
  }

  status = write(fn,"none",4);
  sleep(1);
  status = write(fn,"default-on",10);
  sleep(1);
  status = write(fn,"none",4);
  sleep(1);
  status = write(fn,"default-on",10);
  sleep(1);
  status = write(fn,"none",4);
  sleep(1);
  status = write(fn,"default-on",10);
  sleep(1);
  status = write(fn,"none",4);
  sleep(1);
  status = write(fn,"default-on",10);
  sleep(1);
  status = write(fn,"none",4);
  sleep(1);
  status = write(fn,"default-on",10);
  sleep(1);
  status = write(fn,"none",4);
  close(fn);

  exit(0);
}

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.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
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.