daemonize.c 541 Bytes
#include <stdio.h>      /* for printf() and fprintf() */
#include <unistd.h>     /* for getopt */
#include <stdlib.h>


void daemonize(void) {
    pid_t pid;

    if (0 > ((pid = fork()))) {
        perror("deamoinze[fork]");
        exit(EXIT_FAILURE);
    } else if (0 != pid) {
        exit(EXIT_SUCCESS);
    }

    /* make new child session leader */
    setsid();

    /* connect all standard streams to /dev/null */
    freopen("/dev/null", "w", stderr);
    freopen("/dev/null", "r", stdin);
    freopen("/dev/null", "w", stdout);
}