Search code examples
c++clinuxdaemon

Need to write a daemon in linux, not sure what to use C++ or C


I have a little problem picking the right language to write my daemon, I am confused between C and C++, I want to use C++ because it is more expanded than C, but I want to use C because it is the starting point of everything in linux,

I want to go for C++ as I have many resources about it, so, does it make any difference if I pick C++ instead of C?

and what I will have good if I learn C more? I feel like if I go into C++ I will cover C within C++...

Regards


Solution

  • I can answer this one entirely in code. Writing a daemon roughly consists of doing this:

    /*
     * Daemon Initialisation:
     * 1. Fork()
     * 2. setsid()
     * 3. Fork() do we need to do this twice?
     * 4. Chdir /
     * 5. Umask(0)
     * 6. Close STDIN/OUT/ERR
     * 7. Optionally re-open stuff.
     *
     * Refs:
     * 1. http://www.faqs.org/faqs/unix-faq/programmer/faq/
     * 2. http://www.netzmafia.de/skripten/unix/linux-daemon-howto.html
     * 3. http://www.enderunix.org/docs/eng/daemon.php
     */
    
    /* Variables */
    /* Our process ID and Session ID */
    
    pid_t pid, sid;
    int fd = 0;
    
    /* Fork off the parent process */
    pid = fork();
    if (pid < 0)
    {
        exit(EXIT_FAILURE);
    }
    /* If we got a good PID, then
     * we can exit the parent process.
     */
    if (pid > 0)
    {
        exit(EXIT_SUCCESS);
    }
    
    /* Create a new SID for the child process */
    sid = setsid();
    
    if (sid < 0)
    {
        /* Log the failure */
        exit(EXIT_FAILURE);
    }
    
    /* Fork off the parent process, again */
    pid = fork();
    if (pid < 0)
    {
        exit(EXIT_FAILURE);
    }
    /* If we got a good PID, then
       we can exit the parent process. */
    if (pid > 0)
    {
        exit(EXIT_SUCCESS);
    }
    
    /* Change the current working directory */
    if ((chdir("/")) < 0)
    {
        /* Log the failure */
        exit(EXIT_FAILURE);
    }
    
    /* Change the file mode mask */
    umask(0);
    
    /* Close all file descriptors */
    
    for (fd = getdtablesize(); fd >= 0; --fd)
    {
        close(fd);
    }
    
    /* Open standard file descriptors from
     * elsewhere.
     * e.g. /dev/null -> stdin.
     *      /dev/console -> stderr?
     *      logfile as stdout?
     */
    fd = open("/dev/null", O_RDWR); /* open stdin */
    dup(fd); /* stdout */
    dup(fd); /* stderr */
    

    These are all C function calls, but of course, you can call them from C++.

    The reason I have this code sat around is because it's actually a function I pass function pointers to which executes my "daemon body". Why do I do it this way? To work out where my errors are running the code directly as a process (if necessary with root privs) then I "daemonise". It's quite hard to debug a daemon process otherwise...

    Edit: of course, using function pointers is a C way of thinking, but there's no reason you can't implement some form of class-based mechanism.

    So, honestly, it really doesn't matter. Pick whichever you prefer.