1

I have created a network daemon but for some reason the code does not print to the log file. The file is created if not there and I do get information from the stdout so the process does get past those functions.

process_id of child process 7796

13254864UDP Server: waiting for connection...

I would like to log to a file NOT SYSLOG if at all possoble.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <stdint.h>

FILE* logging_file;

int main(int argc, char* argv[])
{
    logging_file = NULL;

    // INITILIZE DEAMON
    if (geteuid())
    {
        printf("You must run this as root\n");
        exit(1);
    }

    // Create child process
    process_id = fork();

    // Indication of fork() failure
    if (process_id < 0)
    {
        printf("Fork failed!\n");
        // Return failure in exit status
        exit(1);
    }

    // PARENT PROCESS. Need to kill it.
    if (process_id > 0)
    {
        printf("process_id of child process %d \n", process_id);
        // return success in exit status
        exit(0);
    }
    //unmask the file mode
    if (umask(022) < 0)
    {
        printf("Error chainging umask\n");
        exit(0);
    }

    //set new session
    if((sid = setsid()) < 0)
    {
        // Return failure
        printf("Error setting sid\n");
        exit(1);
    }

    // Change the current working directory to root.
    if (chdir("/"))
    {
        printf("Error Changing Directory");
        exit(1);
    }

    // Close stdin. stdout and stderr
    if(close(STDIN_FILENO) < 0)
    {
        printf("Error Closing STDIN_FILENO\n");
    }

    if(close(STDERR_FILENO) < 0)
    {
        printf("Error Closing STDERR_FILENO\n");
    }
    /*close(STDOUT_FILENO);*/

    // Open a log file in write mode.
    if ((logging_file = fopen("/var/log/udp_daemon.log", "a")) < 0)
    {
        fprintf(stdout, "Error Creating Log file\n");
    }

    fprintf(logging_file, "Started Deamon\n");
        fprintf(stdout,"%d",logging_file);

    //Network initilization

    fprintf(logging_file, "UDP Server: waiting for connection...\n");
    printf("UDP Server: waiting for connection...\n");
    //Main Loop with timers
    while (1)
    {
        // Implement and call some function that does core work for this daemon.

        //Receve Data from socket
        bytes_read = recvfrom(server_fd, buffer, MAXBUF-1, 0, cliaddr, &len);

        //If data is present
        if (bytes_read > 0) {

        }

        //short sleep before next intteration
        usleep(10);
    }
    fclose(logging_file);
    return (0);
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
GeneralZero
  • 290
  • 1
  • 3
  • 15

1 Answers1

3

The output to your file is likely being buffered. Try adding fflush(logging_file) after one of the fprintf calls that you want to see written (immediately) to your logging file.

Drew MacInnis
  • 8,267
  • 1
  • 22
  • 18
  • I was under the assumption that if you have a newline with fprintf it is aupose to flush automaticly. – GeneralZero Oct 17 '13 at 02:30
  • But that does not seem to be the case. Because fflush does work so i put setbuff(logging_file, NULL);. Do you happen to know why that is? Maybe because logging file is a FILE*? – GeneralZero Oct 17 '13 at 02:48
  • The type of buffering (by default) is usually determined by the type of the destination (interactive tty vs. file for example). See http://stackoverflow.com/questions/5229096/does-printf-always-flush-the-buffer-on-encountering-a-newline – Drew MacInnis Oct 17 '13 at 07:20