cartman
cartman

Reputation: 782

how a daemonize a c/c++ program on Linux

I wrote a daemon program on Linux according to the guide at http://linux.die.net/man/1/daemonize, but the process crashed several times and I cannot find the reason. It has troubled me for a few days.

Today I happened to have read 'UNIX network Programming volume 1, Third Edition' by W.Richard Stevens. And in this book, it shows an example to write daemon program. After reading the example, I realized 'Disassociate from the control terminal' is missing from my code.

Now my question is to daemonize a process, why we need disassociate from the control terminal? And does it related to the crash of the process? Is there any other place is missing in my code for daemonize?

Appreciate your replies.

Here is my code:

bool daemonize()
{
    // http://linux.die.net/man/1/daemonize

    // change working dir to root
    (void) uchdir("/");

    // close stdin, stderr, stdout
    if (int fdnull = open("/dev/null", O_RDWR))
    {   
        dup2 (fdnull, STDIN_FILENO);
        dup2 (fdnull, STDOUT_FILENO);
        dup2 (fdnull, STDERR_FILENO);
        close(fdnull);
    }   
    else
    {   
        Log (ERR, "Failed to open /dev/null");
        return false;
    }   

    // detach from previous process group
    if (setsid () == -1)      /* request a new session (job control) */
    {   
        Log (ERR, "Failed to detach from previous process group");
        return false;
    }   

    // inhibit others completely and group write
    umask(027);

    // it's dameonized!
    return true;
}

Upvotes: 5

Views: 41697

Answers (3)

Ako
Ako

Reputation: 1005

I found this github repository useful, it has what you need to build a daemon:

Simple example of daemon for Linux

And here is a stack overflow thread why double fork is nessesary.

Upvotes: 4

datenwolf
datenwolf

Reputation: 162327

Not addressing your actual question but…

I wrote a daemon program on Linux according to the guide at [http://linux.die.net/man/1/daemonize][1],

please don't!

Programs that daemon-ize are the bane of any sensible init system and service tracker. The problem is, that after a program detaches from its controlling terminal and parent process it gets difficult to keep track about its state and regain control over it. The usual way of using PID files is prone to race conditions, which is especially bad if the intention is to automatically respawn such a process.

The use of daemon led to the development of several hacks, some outright ugly some kind of okay, but none of them being beautiful. Do yourself and the rest of the world a favour and don't daemonize your program.

Upvotes: 1

Striezel
Striezel

Reputation: 3758

The basic steps to deamonize a C or C++ program have already been mentioned in this question: Creating a daemon in Linux

Yes, the question there was for C and not for C++, but since the system calls you need to daemonize a program are C functions in both cases, that really does not make a difference.

Upvotes: 6

Related Questions