source: box/trunk/lib/server/Daemon.h @ 3059

Revision 3059, 3.1 KB checked in by chris, 4 months ago (diff)

Split option processing out of Daemon::Main() to ease use of Daemon class in tests.

  • Property svn:eol-style set to native
Line 
1// --------------------------------------------------------------------------
2//
3// File
4//              Name:    Daemon.h
5//              Purpose: Basic daemon functionality
6//              Created: 2003/07/29
7//
8// --------------------------------------------------------------------------
9
10/*  NOTE: will log to local6: include a line like
11        local6.info                                             /var/log/box
12        in /etc/syslog.conf
13*/
14
15
16#ifndef DAEMON__H
17#define DAEMON__H
18
19#include <string>
20
21#include "BoxTime.h"
22#include "Configuration.h"
23
24class ConfigurationVerify;
25
26// --------------------------------------------------------------------------
27//
28// Class
29//              Name:    Daemon
30//              Purpose: Basic daemon functionality
31//              Created: 2003/07/29
32//
33// --------------------------------------------------------------------------
34class Daemon
35{
36public:
37        Daemon();
38        virtual ~Daemon();
39private:
40        Daemon(const Daemon &rToCopy);
41public:
42
43        virtual int Main(const std::string& rDefaultConfigFile, int argc,
44                const char *argv[]);
45        virtual int ProcessOptions(int argc, const char *argv[]);
46
47        /* override this Main() if you want custom option processing: */
48        virtual int Main(const std::string &rConfigFile);
49       
50        virtual void Run();
51        const Configuration &GetConfiguration() const;
52        const std::string &GetConfigFileName() const {return mConfigFileName;}
53
54        virtual const char *DaemonName() const;
55        virtual std::string DaemonBanner() const;
56        virtual const ConfigurationVerify *GetConfigVerify() const;
57        virtual void Usage();
58
59        virtual bool Configure(const std::string& rConfigFileName);
60        virtual bool Configure(const Configuration& rConfig);
61
62        bool StopRun() {return mReloadConfigWanted | mTerminateWanted;}
63        bool IsReloadConfigWanted() {return mReloadConfigWanted;}
64        bool IsTerminateWanted() {return mTerminateWanted;}
65
66        // To allow derived classes to get these signals in other ways
67        void SetReloadConfigWanted() {mReloadConfigWanted = true;}
68        void SetTerminateWanted() {mTerminateWanted = true;}
69       
70        virtual void EnterChild();
71       
72        static void SetProcessTitle(const char *format, ...);
73        void SetRunInForeground(bool foreground)
74        {
75                mRunInForeground = foreground;
76        }
77        void SetSingleProcess(bool value)
78        {
79                mSingleProcess = value;
80        }
81
82protected:
83        virtual void SetupInInitialProcess();
84        box_time_t GetLoadedConfigModifiedTime() const;
85        bool IsSingleProcess() { return mSingleProcess; }
86        virtual std::string GetOptionString();
87        virtual int ProcessOption(signed int option);
88       
89private:
90        static void SignalHandler(int sigraised);
91        box_time_t GetConfigFileModifiedTime() const;
92       
93        std::string mConfigFileName;
94        std::auto_ptr<Configuration> mapConfiguration;
95        box_time_t mLoadedConfigModifiedTime;
96        bool mReloadConfigWanted;
97        bool mTerminateWanted;
98        bool mSingleProcess;
99        bool mRunInForeground;
100        bool mKeepConsoleOpenAfterFork;
101        bool mHaveConfigFile;
102        int mLogLevel; // need an int to do math with
103        std::string mLogFile;
104        Log::Level mLogFileLevel;
105        std::auto_ptr<FileLogger> mapLogFileLogger;
106        static Daemon *spDaemon;
107        std::string mAppName;
108};
109
110#define DAEMON_VERIFY_SERVER_KEYS \
111        ConfigurationVerifyKey("PidFile", ConfigTest_Exists), \
112        ConfigurationVerifyKey("LogFacility", 0), \
113        ConfigurationVerifyKey("User", ConfigTest_LastEntry)
114
115#endif // DAEMON__H
116
Note: See TracBrowser for help on using the repository browser.