Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

General Daemon/Server Design - Best Practices (C/C++, Linux)

Tags:

c++

c

linux

daemon

These questions are quite general since they keep coming up for me in different situations. I'm hoping there are some basic principles/standard practices.

The typical requirements:

  1. A program that acts like a "server", running in linux in the background (and runs almost non-stop. restarting perhaps daily or weekly)
  2. Handles client connections via some socket protocol
  3. Has startup configuration files
  4. Outputs to one or more log files

My questions:

  1. Should I write the program as a "daemon"? What are things I should consider when choosing the daemon vs non-daemon route?
  2. Where in the linux folder hierarchy should the log files and configuration files go? Should I run it out of some user's home directory or a sub-folder in some user's home directory? Or perhaps I should make a new folder i.e. /my_server_abc/ and then run it from there, writing log files to that directory as well?

Thanks

like image 377
Switch Avatar asked Aug 20 '11 01:08

Switch


3 Answers

  1. Should I write the program as a "daemon"?

No.

Do not try to daemonize yourself. Use OS provisioned tools to make your application run in background from a startup script, like start-stop-daemon in Debian/Ubuntu. Systemd and upstart can also handle this for you in their startup scripts, as can most init systems these days.

Writing a daemon has some pitfalls you might not expect, and most modern init scripts don't expect you to do send your own process to background - which would complicate their jobs anyway. This allows for example generating reliable .pid files for tracking your application's process id. If you daemonize yourself, your init system has to rely on your application to correctly communicate your process id somehow since you generate new PID's the init system can't track. This complicates things both for them as for you.

like image 69
Bart M. Avatar answered Sep 28 '22 08:09

Bart M.


  1. Use libdaemon.
  2. Follow the Filesystem Hierarchy Standard.
like image 35
John Zwinck Avatar answered Sep 28 '22 09:09

John Zwinck


I know you are thinking c/c++ when asking this question but I think it's more general than that and the logic used when designing is independent of the language used for the implementation.

There is a python enhancement proposal (PEP 3143) that was used to describe the standard daemon process library that has now become part of the language. If you look in this section on correct daemon behavior it describes how a daemon should act. There are also considerations of the differences between a 'service' and a daemon.

I think that should pretty well answer your general questions on daemons and their behavior. Check out W. Richard Stevens' Home Page and you can find info on 'Unix Network Programming', Prentice Hall which has more information specific to c/c++ when coding daemons in a *nix environment and best practice.

like image 32
unclejamil Avatar answered Sep 28 '22 08:09

unclejamil