clinuxsyslogppp

Watching new entries on Linux Syslog from a C program


I want to write a program that monitors syslog and performs an action when PPP authentication fails.

I think "tail -f /var/log/syslog" could help, but I'm not sure how to use it... probably using pipes?

I have found something similar written in bash, but I'm not sure how to implement it in C.

This is the bash method:

First create a named pipe using mkfifo:

$ mkfifo -p /home/mezgani/syslog.pipe

Make syslog.conf to points to this file:

*.info |/home/mezgani/syslog.pipe

Restart syslog:

$ sudo pkill -HUP syslogd

Create processing script that read the pipe

$ cat > foo
#!/bin/bash
cat /home/mezgani/syslog.pipe | while read input
do
    # some stuff
    echo ${input}
    # ….
done

Solution

  • Finally I could found the solution!!

    The solution was using named pipes!

    First, I need to create a named pipe: mkfifo /pipe

    Then, I feed the pipe with the log info: tail -f /var/log/syslog > /pipe

    And then, I read the pipe from the C program using OPEN

    int pipefd;
    pipefd = open("/tmp/myFIFO", O_WRONLY);