Permalink
Browse files

Merge pull request #484 from KiNgMaR/singleton

Allow more control over CZNC singleton
  • Loading branch information...
DarthGandalf committed Feb 8, 2014
2 parents 17d97bb + ec6607c commit b7592008afe265eec539c2de89ef2d1ebe62fabc
Showing with 36 additions and 20 deletions.
  1. +3 −1 include/znc/znc.h
  2. +16 −13 src/main.cpp
  3. +17 −6 src/znc.cpp
View
@@ -56,7 +56,7 @@ class CZNC {
CString ExpandConfigPath(const CString& sConfigFile, bool bAllowMkDir = true);
bool WriteNewConfig(const CString& sConfigFile);
bool WriteConfig();
- bool ParseConfig(const CString& sConfig);
+ bool ParseConfig(const CString& sConfig, CString& sError);
bool RehashConfig(CString& sError);
void BackupConfigOnce(const CString& sSuffix);
static CString GetVersion();
@@ -131,7 +131,9 @@ class CZNC {
// !Getters
// Static allocator
+ static void CreateInstance();
static CZNC& Get();
+ static void DestroyInstance();
CUser* FindUser(const CString& sUsername);
CModule* FindModule(const CString& sModName, const CString& sUsername);
CModule* FindModule(const CString& sModName, CUser* pUser);
View
@@ -77,7 +77,7 @@ static void die(int sig) {
CUtils::PrintMessage("Exiting on SIG [" + CString(sig) + "]");
- delete &CZNC::Get();
+ CZNC::DestroyInstance();
exit(sig);
}
@@ -193,14 +193,16 @@ int main(int argc, char** argv) {
return 1;
}
+ CZNC::CreateInstance();
+
CZNC* pZNC = &CZNC::Get();
pZNC->InitDirs(((argc) ? argv[0] : ""), sDataDir);
#ifdef HAVE_LIBSSL
if (bMakePem) {
pZNC->WritePemFile();
- delete pZNC;
+ CZNC::DestroyInstance();
return 0;
}
#endif /* HAVE_LIBSSL */
@@ -219,7 +221,7 @@ int main(int argc, char** argv) {
std::cout << "</Pass>" << std::endl;
CUtils::PrintMessage("After that start ZNC again, and you should be able to login with the new password.");
- delete pZNC;
+ CZNC::DestroyInstance();
return 0;
}
@@ -236,7 +238,7 @@ int main(int argc, char** argv) {
CUtils::PrintError("No modules found. Perhaps you didn't install ZNC properly?");
CUtils::PrintError("Read http://wiki.znc.in/Installation for instructions.");
if (!CUtils::GetBoolInput("Do you really want to run ZNC without any modules?", false)) {
- delete pZNC;
+ CZNC::DestroyInstance();
return 1;
}
}
@@ -247,7 +249,7 @@ int main(int argc, char** argv) {
CUtils::PrintError("You are running ZNC as root! Don't do that! There are not many valid");
CUtils::PrintError("reasons for this and it can, in theory, cause great damage!");
if (!bAllowRoot) {
- delete pZNC;
+ CZNC::DestroyInstance();
return 1;
}
CUtils::PrintError("You have been warned.");
@@ -258,21 +260,22 @@ int main(int argc, char** argv) {
if (bMakeConf) {
if (!pZNC->WriteNewConfig(sConfig)) {
- delete pZNC;
+ CZNC::DestroyInstance();
return 0;
}
/* Fall through to normal bootup */
}
- if (!pZNC->ParseConfig(sConfig)) {
+ CString sConfigError;
+ if (!pZNC->ParseConfig(sConfig, sConfigError)) {
CUtils::PrintError("Unrecoverable config error.");
- delete pZNC;
+ CZNC::DestroyInstance();
return 1;
}
if (!pZNC->OnBoot()) {
CUtils::PrintError("Exiting due to module boot errors.");
- delete pZNC;
+ CZNC::DestroyInstance();
return 1;
}
@@ -289,7 +292,7 @@ int main(int argc, char** argv) {
if (iPid == -1) {
CUtils::PrintStatus(false, strerror(errno));
- delete pZNC;
+ CZNC::DestroyInstance();
return 1;
}
@@ -309,7 +312,7 @@ int main(int argc, char** argv) {
*/
if (!pZNC->WaitForChildLock()) {
CUtils::PrintError("Child was unable to obtain lock on config file.");
- delete pZNC;
+ CZNC::DestroyInstance();
return 1;
}
@@ -378,7 +381,7 @@ int main(int argc, char** argv) {
// The above code adds 3 entries to args tops
// which means the array should be big enough
- delete pZNC;
+ CZNC::DestroyInstance();
execvp(args[0], args);
CUtils::PrintError("Unable to restart ZNC [" + CString(strerror(errno)) + "]");
} /* Fall through */
@@ -387,7 +390,7 @@ int main(int argc, char** argv) {
}
}
- delete pZNC;
+ CZNC::DestroyInstance();
return iRet;
}
View
@@ -1022,13 +1022,11 @@ void CZNC::BackupConfigOnce(const CString& sSuffix) {
CUtils::PrintStatus(false, strerror(errno));
}
-bool CZNC::ParseConfig(const CString& sConfig)
+bool CZNC::ParseConfig(const CString& sConfig, CString& sError)
{
- CString s;
-
m_sConfigFile = ExpandConfigPath(sConfig, false);
- return DoRehash(s);
+ return DoRehash(sError);
}
bool CZNC::RehashConfig(CString& sError)
@@ -1853,9 +1851,22 @@ bool CZNC::DelListener(CListener* pListener) {
return false;
}
+static CZNC* s_pZNC = NULL;
+
+void CZNC::CreateInstance() {
+ if (s_pZNC)
+ abort();
+
+ s_pZNC = new CZNC();
+}
+
CZNC& CZNC::Get() {
- static CZNC* pZNC = new CZNC;
- return *pZNC;
+ return *s_pZNC;
+}
+
+void CZNC::DestroyInstance() {
+ delete s_pZNC;
+ s_pZNC = NULL;
}
CZNC::TrafficStatsMap CZNC::GetTrafficStats(TrafficStatsPair &Users,

0 comments on commit b759200

Please sign in to comment.