PageRenderTime 27ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/mordor/daemon.h

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