user3461290
user3461290

Reputation: 41

C linux daemon does not write into file after opening FIFO

I have following program in C, which should run as a deamon and whenever is something written into FIFO, it should write it into a file.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <signal.h>
#include <syslog.h>

#define BUF_LENGTH 255

volatile int signal_flag = 1;

void signal_handler(int sig)
{
    signal_flag = 1;
}

char *getTimeString()
{
    time_t rawtime;
    struct tm * timeinfo;

    time ( &rawtime );
    timeinfo = localtime ( &rawtime );
    char *timeStr = asctime (timeinfo);
    timeStr[strlen(timeStr) - 1] = 0;

    return timeStr;
}

void printUsage()
{
    printf("Usage: syslog_daemon PATH INTERVAL\n");
}

int main(int argc, char *argv[])
{
    /* print usage */
    if(argc != 3)
    {
        printUsage();
        exit(EXIT_SUCCESS);
    }

    /* process arguments */
    char *logFilePath = argv[1];
    int interval = atoi(argv[2]);

    /* establish the signal handler */
    struct sigaction action;

    sigemptyset(&action.sa_mask);
    action.sa_flags = 0;
    action.sa_handler = signal_handler;
    sigaction(SIGALRM, &action, NULL);

    /* initialize variables */
    int fd;
    /*char buf[BUF_LENGTH];
    int length;*/
    int msgs = 0;

    /* Create FIFO if not created */
    if (mkfifo("/tmp/pb173_syslog", 0766) == -1 && errno != EEXIST)
    {
        fprintf(stderr, "Making FIFO failed with error %d\n", errno);
        exit(EXIT_FAILURE);
    }

    /* Run */
    daemon(1, 1);
    while(1)
    {           
        /* Open FIFO */     
        fd = open("/tmp/pb173_syslog", O_RDONLY);
        close(fd);

        /* Open and write into file */
        FILE *f = fopen(logFilePath, "a");
        fprintf(f, "Daemon write: %d\n", msgs);
        fclose(f);


        /* Process SIGALRM and write syslog */
        if(signal_flag)
        {                   
            openlog("syslog_daemon v2", LOG_CONS, LOG_DAEMON);
            syslog(LOG_INFO, "Messages written: %d\n", msgs);
            closelog();

            msgs++;

            signal_flag = 0;
            alarm(interval);
        }
    }

    return 0;
}

But this program does not write anything into the file. It seems, that when the FIFO is open, it cannot write anywhere. But if I don't open the FIFO, the program writes into the file without any problems. Does anyone know what is the problem? Thanks for any help.

Upvotes: 0

Views: 757

Answers (1)

afenster
afenster

Reputation: 3608

It hangs on open trying to open a FIFO which does not have the second endpoint (the writer) connected. You may want to use O_NONBLOCK.

Here is a quote from strace output that shows where it hangs:

$ strace -p 23114
Process 23114 attached - interrupt to quit
open("/tmp/pb173_syslog", O_RDONLY

If you write something to the FIFO (e.g. echo test > /tmp/pb173_syslog) it unblocks and starts working.

Upvotes: 3

Related Questions