This is a just a sample real time event dispatcher program from <a href="http://people.redhat.com/sgrubb/audit/audit-rt-events.txt">http://people.redhat.com/sgrubb/audit/audit-rt-events.txt</a>.<br>I am just trying to write the data coming from daemon in some regular file instead of 
syslog.But it seems that even the file is not getting created.Is there some kind of restriction that we can't call functions like fopen(),open(),etc ?<br>Please tell what's the problem and how it can be solved.<br>
And if possible just give an sample example so that i can understand it better.<br>The added lines of code i have marked with "+".<br><br>Here is the code..<br><br>#include <stdio.h><br>#include <sys/types.h>
<br>#include <sys/uio.h><br>#include <unistd.h><br>#include <stdlib.h><br>#include <signal.h><br>#include <fcntl.h><br>#include <errno.h><br>#include <string.h><br>#include <locale.h
><br>#include "libaudit.h"<br><br><br>// Local data<br>static volatile int signaled = 0;<br>static int pipe_fd;<br>static const char *pgm = "skeleton";<br><br>// Local functions<br>static int event_loop(void);
<br><br>// SIGTERM handler<br>static void term_handler( int sig )<br>{<br>    signaled = 1;<br>}<br><br><br>/*<br> * main is started by auditd. See dispatcher in auditd.conf<br> */<br>int main(int argc, char *argv[])<br>{
<br>    struct sigaction sa;<br><br>    setlocale (LC_ALL, "");<br>    openlog(pgm, LOG_PID, LOG_DAEMON);<br>    syslog(LOG_NOTICE, "starting...");<br><br>#ifndef DEBUG<br>    // Make sure we are root<br>
    if (getuid() != 0) {<br>        syslog(LOG_ERR, "You must be root to run this program.");<br>        return 4;<br>    }<br>#endif<br><br>    // register sighandlers<br>    sa.sa_flags = 0 ;<br>    sa.sa_handler
 = term_handler;<br>    sigemptyset( &sa.sa_mask ) ;<br>    sigaction( SIGTERM, &sa, NULL );<br>    sa.sa_handler = term_handler;<br>    sigemptyset( &sa.sa_mask ) ;<br>    sigaction( SIGCHLD, &sa, NULL );
<br>    sa.sa_handler = SIG_IGN;<br>    sigaction( SIGHUP, &sa, NULL );<br>    (void)chdir("/");<br><br>    // change over to pipe_fd<br>    pipe_fd = dup(0);<br>    close(0);<br>    open("/dev/null", O_RDONLY);
<br>    fcntl(pipe_fd, F_SETFD, FD_CLOEXEC);<br><br>    // Start the program<br>    return event_loop();<br>}<br><br>static int event_loop(void)<br>{<br>    void* data;<br>+    FILE* fp=NULL;<br>    struct iovec vec[2];<br>
    struct audit_dispatcher_header hdr;<br><br>    // allocate data structures<br>    data = malloc(MAX_AUDIT_MESSAGE_LENGTH);<br>    if (data == NULL) {<br>        syslog(LOG_ERR, "Cannot allocate buffer");<br>
        return 1;<br>    }<br>    memset(data, 0, MAX_AUDIT_MESSAGE_LENGTH);<br>    memset(&hdr, 0, sizeof(hdr));<br><br>    do {<br>        int rc;<br>        struct timeval tv;<br>        fd_set fd;<br><br>        tv.tv_sec
 = 1;<br>        tv.tv_usec = 0;<br>        FD_ZERO(&fd);<br>        FD_SET(pipe_fd, &fd);<br>        rc = select(pipe_fd+1, &fd, NULL, NULL, &tv);<br>        if (rc == 0) <br>            continue;<br>         else if (rc == -1)
<br>            break;<br><br>        /* Get header first. it is fixed size */<br>        vec[0].iov_base = (void*)&hdr;<br>        vec[0].iov_len = sizeof(hdr);<br><br>            // Next payload <br>        vec[1].iov_base = data;
<br>        vec[1].iov_len = MAX_AUDIT_MESSAGE_LENGTH; <br><br>        rc = readv(pipe_fd, vec, 2);<br>        if (rc == 0 || rc == -1) {<br>            syslog(LOG_ERR, "rc == %d(%s)", rc, strerror(errno));<br>            break;
<br>        }<br><br>        // handle events here. Just for illustration, we print<br>        // to syslog, but you will want to do something else.<br><br><br>+        //I want to write the data in some regular file instead of syslog but in doing that it seems that even
<br>+        // the file doesn't get created or open.Given below is just a simple code<br>+        fp=fopen("tempfile.txt","w+");<br>+        fwrite((char*)data,sizeof(char),30,fp);<br>+        fclose(fp);
<br><br>        syslog(LOG_NOTICE,"type=%d, payload size=%d", <br>            hdr.type, hdr.size);<br>        syslog(LOG_NOTICE,"data=\"%.*s\"", hdr.size,<br>            (char *)data);<br><br>
    } while(!signaled);<br><br>    return 0;<br>}<br><br>