views:

199

answers:

4

I want to write a generic (C/C++) library that I will use to develop daemons in the Linux environment. Rather than reinventing the wheel, I thought I'd come in here to find out if there are any well known libraries in use.

The library could be either C or C++ - although I would prefer C++ (maybe something that was part of, or based on the excellent BOOST library?).

As an aside, in terms of library selection criteria, since daemons are pretty 'mission critical' components, it would be much better if the library that you propose is actively maintained by a group of developers (e.g. the BOOST library [again]), has an active community (or at least a mailing list to resort to when faced with tricky situations), rather than a lone individual somewhere out there ...

I saw this document, which is a good starting point, but it is slightly dated, so I'm wondering if there is anything better and more well known/used out there ... ?

BTW, I will be developing on Ubuntu (10.0.4)

+6  A: 

An alternative solution is to use a process monitor such as supervisord, which manages multiple services, restarts them when they crash, provides a minimalistic web page to view and control the status of processes, can manage groups of services, supports a general-purpose status-change event forwarding mechanism and other goodies. Such tools give you far more value than a daemon library would.

Marcelo Cantos
+1: Oh snap!, why didn't I think of that?!. This could save me a lot of time. I'll look into it and see if it fufils my requirements.
morpheous
Document, you're linking to is up to date, cause nothing changed since that times in daemon behavior. And, as Marcelo said, supervisord is right for that sorts of things -- it can daemonize even simple /bin/cat program (see docs for supervisord).
andreypopp
Take a look at this http://upstart.ubuntu.com/
Matt Joiner
@Matt: AFAIK, upstart doesn't monitor long-running processes or restart crashes services. I don't know upstart very well, so please correct me if I'm wrong.
Marcelo Cantos
A: 

If your daemon uses tcp/ip sockets, you can use the inet daemon (or xinetd). Your process is started on demand as a new incoming connection comes in. There may be scalability issues in the event of large scale deployment, however.

doron
A: 
#include <unistd.h>

has

int daemon(int nochdir, int noclose);

Which forks, detatches from the controlling terminal, reopens all of {stdin, stdout, stderr} to /dev/null, and changes the working directory to the root directory. (based on flags, of course)

nategoose
A: 

start-stop-daemon may help

KitsuneYMG