summaryrefslogtreecommitdiff
path: root/one.cpp
diff options
context:
space:
mode:
authorAdam Ierymenko <adam.ierymenko@gmail.com>2017-03-17 13:55:26 -0700
committerAdam Ierymenko <adam.ierymenko@gmail.com>2017-03-17 13:55:26 -0700
commitc6a39ed927161736e44aeaa67c6783024c1fb86a (patch)
tree5f92551e3b4cbc58409c8daaca0755c75c61fa6e /one.cpp
parent0daff26fba640f2ef167ad66974dc1c4a6c79c01 (diff)
downloadinfinitytier-c6a39ed927161736e44aeaa67c6783024c1fb86a.tar.gz
infinitytier-c6a39ed927161736e44aeaa67c6783024c1fb86a.zip
Fixes for possible ARM issues, cleanup, fix for spurious meaningless exceptions on NETWORK_CONFIG_REQUEST
Diffstat (limited to 'one.cpp')
-rw-r--r--one.cpp85
1 files changed, 54 insertions, 31 deletions
diff --git a/one.cpp b/one.cpp
index 95230bf1..25a50dbb 100644
--- a/one.cpp
+++ b/one.cpp
@@ -72,6 +72,7 @@
#include "osdep/OSUtils.hpp"
#include "osdep/Http.hpp"
+#include "osdep/Thread.hpp"
#include "service/OneService.hpp"
@@ -1209,6 +1210,52 @@ static void printHelp(const char *cn,FILE *out)
fprintf(out," -q - Query API (zerotier-cli)" ZT_EOL_S);
}
+class _OneServiceRunner
+{
+public:
+ _OneServiceRunner(const char *pn,const std::string &hd,unsigned int p) : progname(pn),returnValue(0),port(p),homeDir(hd) {}
+ void threadMain()
+ throw()
+ {
+ try {
+ for(;;) {
+ zt1Service = OneService::newInstance(homeDir.c_str(),port);
+ switch(zt1Service->run()) {
+ case OneService::ONE_STILL_RUNNING: // shouldn't happen, run() won't return until done
+ case OneService::ONE_NORMAL_TERMINATION:
+ break;
+ case OneService::ONE_UNRECOVERABLE_ERROR:
+ fprintf(stderr,"%s: fatal error: %s" ZT_EOL_S,progname,zt1Service->fatalErrorMessage().c_str());
+ returnValue = 1;
+ break;
+ case OneService::ONE_IDENTITY_COLLISION: {
+ delete zt1Service;
+ zt1Service = (OneService *)0;
+ std::string oldid;
+ OSUtils::readFile((homeDir + ZT_PATH_SEPARATOR_S + "identity.secret").c_str(),oldid);
+ if (oldid.length()) {
+ OSUtils::writeFile((homeDir + ZT_PATH_SEPARATOR_S + "identity.secret.saved_after_collision").c_str(),oldid);
+ OSUtils::rm((homeDir + ZT_PATH_SEPARATOR_S + "identity.secret").c_str());
+ OSUtils::rm((homeDir + ZT_PATH_SEPARATOR_S + "identity.public").c_str());
+ }
+ } continue; // restart!
+ }
+ break; // terminate loop -- normally we don't keep restarting
+ }
+
+ delete zt1Service;
+ zt1Service = (OneService *)0;
+ } catch ( ... ) {
+ fprintf(stderr,"%s: unexpected exception starting main OneService instance" ZT_EOL_S,progname);
+ returnValue = 1;
+ }
+ }
+ const char *progname;
+ unsigned int returnValue;
+ unsigned int port;
+ const std::string &homeDir;
+};
+
#ifdef __WINDOWS__
int _tmain(int argc, _TCHAR* argv[])
#else
@@ -1421,8 +1468,8 @@ int main(int argc,char **argv)
} else {
// Running from service manager
_winPokeAHole();
- ZeroTierOneService zt1Service;
- if (CServiceBase::Run(zt1Service) == TRUE) {
+ ZeroTierOneService zt1WindowsService;
+ if (CServiceBase::Run(zt1WindowsService) == TRUE) {
return 0;
} else {
fprintf(stderr,"%s: unable to start service (try -h for help)" ZT_EOL_S,argv[0]);
@@ -1448,35 +1495,11 @@ int main(int argc,char **argv)
}
#endif // __UNIX_LIKE__
- unsigned int returnValue = 0;
-
- for(;;) {
- zt1Service = OneService::newInstance(homeDir.c_str(),port);
- switch(zt1Service->run()) {
- case OneService::ONE_STILL_RUNNING: // shouldn't happen, run() won't return until done
- case OneService::ONE_NORMAL_TERMINATION:
- break;
- case OneService::ONE_UNRECOVERABLE_ERROR:
- fprintf(stderr,"%s: fatal error: %s" ZT_EOL_S,argv[0],zt1Service->fatalErrorMessage().c_str());
- returnValue = 1;
- break;
- case OneService::ONE_IDENTITY_COLLISION: {
- delete zt1Service;
- zt1Service = (OneService *)0;
- std::string oldid;
- OSUtils::readFile((homeDir + ZT_PATH_SEPARATOR_S + "identity.secret").c_str(),oldid);
- if (oldid.length()) {
- OSUtils::writeFile((homeDir + ZT_PATH_SEPARATOR_S + "identity.secret.saved_after_collision").c_str(),oldid);
- OSUtils::rm((homeDir + ZT_PATH_SEPARATOR_S + "identity.secret").c_str());
- OSUtils::rm((homeDir + ZT_PATH_SEPARATOR_S + "identity.public").c_str());
- }
- } continue; // restart!
- }
- break; // terminate loop -- normally we don't keep restarting
- }
+ _OneServiceRunner thr(argv[0],homeDir,port);
+ thr.threadMain();
+ //Thread::join(Thread::start(&thr));
- delete zt1Service;
- zt1Service = (OneService *)0;
+ OSUtils::rm(pidPath.c_str());
- return returnValue;
+ return thr.returnValue;
}