0

I want to run a system function within a program written in C. This system function is blocking and can take some time before it returns to stdout. The function to be called is snort, and normally is executed on a raspberry pi as followed:

sudo snort -q -A console -i eth0 -c /etc/snort/snort.conf

In the case snort triggers an alert, the parent program should read that line and turn on a LED. I currently am turning on leds as followed:

void triggerLed(void) {
   pinMode(7], OUTPUT);
   digitalWrite(7, HIGH);
}

int main(void) {
  //Execute this function call:  sudo snort -q -A console -i eth0 -c /etc/snort/snort.conf

  //while executing
      //On new line from readline()
           //if strcmp(line,"alert")
                 triggerLed();
           //endif
 //end while

}

How would you solve this? I tried monitoring syslog, snort however does not write to syslog as I cannot find any alerts.

fyi: Last week I asked this question on: Execute script on Snort alert . Unfortunately, due to a combination a vaguely formed question and a change of scope I rephrased the question here.

Community
  • 1
  • 1
Alex van Rijs
  • 803
  • 5
  • 17
  • 39

1 Answers1

0

The function you are looking for is system(3). You get the exit code of the process back. But if you intend to read the output (stdout) of the called process you have to implement a fork(3)/exec(3) combination, reconnecting the child's file descriptors (at least fd 1) and then reading from it.

Bernhard
  • 354
  • 1
  • 6
  • The problem is that the function does not output EOL or EOF, therefore STDOUT is buffered until it receives that character before STDIN is able to read it. – Alex van Rijs Mar 04 '15 at 11:28
  • Stdout is flushed as soon as there is an EOL ('\n'). EOF is _not_ a character. It is detected by read(2) which returns 0 in that case. – Bernhard Mar 06 '15 at 07:09