/mordor/daemon.h
C Header | 88 lines | 24 code | 8 blank | 56 comment | 0 complexity | 975f27427de242cab4e500bbd230e43e MD5 | raw file
1#ifndef __MORDOR_SERVICE_H__ 2#define __MORDOR_SERVICE_H__ 3// Copyright (c) 2010 - Mozy, Inc. 4 5#include <string> 6#ifdef POSIX 7#include <sys/types.h> 8#endif 9#include <vector> 10 11#include <boost/function.hpp> 12#include <boost/signals2/signal.hpp> 13 14#include "version.h" 15 16namespace Mordor { 17namespace Daemon { 18 19/// @brief Run a process as a daemon 20/// 21/// Runs daemonMain as a daemon process, that can be externally controlled. 22/// It is cross platform, and will adapt for how this process is run. 23/// The words in parenthesis describe behavior for how the signal handler 24/// interacts with the platform's normal actions: 25/// * default: if no slots are connected to the signal, it gets passed 26/// to the default handler for that platform 27/// * always: the default handler for that platform is always invoked 28/// (after all slots are signalled) 29/// * ignored: the default handler for that platform is ignored 30/// * ExitProcess: there is no default handler for a Windows System Service 31/// so a stop request that is unhandled will terminate the process 32/// 33/// Windows Console: 34/// * argc/argv are passed through 35/// * Ctrl-C triggers onInterrupt (default) 36/// * Ctrl-Break and End Task trigger onTerminate (default) 37/// 38/// Windows System Service: 39/// * argc/argv are ignored; daemonMain receives the arguments from 40/// ServiceStart 41/// * SERVICE_CONTROL_STOP triggers onTerminate (ExitProcess) 42/// * SERVICE_CONTROL_PAUSE triggers onPause (ignored) 43/// * SERVICE_CONTROL_CONTINUE triggers onContinue (ignored) 44/// * SERVICE_CONTROL_PARAMCHANGE triggers onReload (ignored) 45/// 46/// POSIX: 47/// * argc/argv are passed through 48/// * SIGTERM triggers onTerminate (default) 49/// * SIGINT triggers onInterrupt (default) 50/// * SIGTSTP triggers onPause (always) 51/// * SIGCONT triggers onContinue (always) 52/// * SIGHUP triggers onReload (default) 53/// 54/// @note On Windows run automagically determines if it is being run as a 55/// system service or from the console. 56/// On OS X, you should be using launchd, and never daemonize. 57/// On Linux, run attempts to automagically determine if it needs to 58/// daemonize by seeing if it is being run directly from an script in 59/// /etc/init.d, or by start-stop-daemon (Debian) 60/// @note If enableWatchdog is true, the daemonMain will be run in a forked 61/// process, the parent process will restart it whenever it dies. 62/// onChildProcessExit is also provided to empower user to stop restarting 63/// and quit when necessary. (works for POSIX only) 64/// @note In all cases, the signals are invoked on a thread separate from 65/// the thread daemonMain is called on, or any that it created 66/// @note run should be called *exactly* once, since the signals are global 67/// for the process 68int run(int argc, char **argv, boost::function<int (int, char **)> daemonMain, 69 bool enableWatchdog = false); 70 71extern boost::signals2::signal<void ()> onTerminate; 72extern boost::signals2::signal<void ()> onInterrupt; 73extern boost::signals2::signal<void ()> onReload; 74extern boost::signals2::signal<void ()> onPause; 75extern boost::signals2::signal<void ()> onContinue; 76#ifdef POSIX 77/// Works together with watchdog, callback whenever child process exits. 78/// Watchdog will check the return value to restart child (if false) or 79/// just shutdown the program (if true). 80/// @note If this function is not hooked, watchdog will by default repeatly 81/// restart child process whenever it exits. If one-shot running is 82/// expected, please set enableWatchdog = false in run. 83extern boost::function<bool (pid_t, int)> onChildProcessExit; 84#endif 85 86}} 87 88#endif