Daemon.h
1 /* 2 * Copyright (c) 2013-2025, The PurpleI2P Project 3 * 4 * This file is part of Purple i2pd project and licensed under BSD3 5 * 6 * See full license text in LICENSE file at top of project tree 7 */ 8 9 #ifndef DAEMON_H__ 10 #define DAEMON_H__ 11 12 #include <memory> 13 #include <string> 14 #include <string_view> 15 #include <ostream> 16 #include <sstream> 17 18 namespace i2p 19 { 20 namespace util 21 { 22 class Daemon_Singleton_Private; 23 class Daemon_Singleton 24 { 25 public: 26 27 virtual bool init (int argc, char* argv[], std::shared_ptr<std::ostream> logstream); 28 virtual bool init (int argc, char* argv[]); 29 virtual bool start (); 30 virtual bool stop (); 31 virtual void run () {}; 32 33 virtual int GetGracefulShutdownInterval () const { return 0; }; 34 void setDataDir (std::string_view path); 35 36 public: 37 38 bool isDaemon; 39 bool running; 40 41 protected: 42 43 Daemon_Singleton (); 44 virtual ~Daemon_Singleton (); 45 46 bool IsService () const; 47 48 // d-pointer for httpServer, httpProxy, etc. 49 class Daemon_Singleton_Private; 50 Daemon_Singleton_Private &d; 51 52 private: 53 54 std::string DaemonDataDir; 55 }; 56 57 void PrintMainWindowText (std::stringstream& s); // for GUI 58 59 #if defined(QT_GUI_LIB) // check if QT 60 #define Daemon i2p::util::DaemonQT::Instance() 61 // dummy, invoked from RunQT 62 class DaemonQT: public i2p::util::Daemon_Singleton 63 { 64 public: 65 66 static DaemonQT& Instance() 67 { 68 static DaemonQT instance; 69 return instance; 70 } 71 }; 72 73 #elif defined(_WIN32) 74 #define Daemon i2p::util::DaemonWin32::Instance() 75 class DaemonWin32 : public Daemon_Singleton 76 { 77 public: 78 79 static DaemonWin32& Instance() 80 { 81 static DaemonWin32 instance; 82 return instance; 83 } 84 85 bool init(int argc, char* argv[]); 86 bool start(); 87 bool stop(); 88 void run (); 89 90 int GetGracefulShutdownInterval () const; 91 92 public: 93 94 bool isGraceful; 95 96 private: 97 98 DaemonWin32 (): isGraceful(false) {} 99 }; 100 #elif (defined(ANDROID) && !defined(ANDROID_BINARY)) 101 #define Daemon i2p::util::DaemonAndroid::Instance() 102 // dummy, invoked from android/jni/DaemonAndroid.* 103 class DaemonAndroid: public i2p::util::Daemon_Singleton 104 { 105 public: 106 107 static DaemonAndroid& Instance() 108 { 109 static DaemonAndroid instance; 110 return instance; 111 } 112 }; 113 #else // Unix-like systems, including Linux 114 class DaemonUnix : public Daemon_Singleton 115 { 116 public: 117 118 static DaemonUnix& Instance() 119 { 120 static DaemonUnix instance; 121 return instance; 122 } 123 124 bool start(); 125 bool stop(); 126 void run (); 127 128 int GetGracefulShutdownInterval () const { return gracefulShutdownInterval; }; 129 130 private: 131 132 std::string pidfile; 133 int pidFH; 134 135 public: 136 137 int gracefulShutdownInterval; // in seconds 138 }; 139 #if !defined(__HAIKU__) 140 #define Daemon i2p::util::DaemonUnix::Instance() 141 #else 142 class DaemonHaiku: public DaemonUnix 143 { 144 public: 145 146 static DaemonHaiku& Instance () 147 { 148 static DaemonHaiku instance; 149 return instance; 150 } 151 152 bool start (); 153 void run (); 154 }; 155 #define Daemon i2p::util::DaemonHaiku::Instance() 156 #endif 157 158 #endif 159 } 160 } 161 162 #endif // DAEMON_H__