Imported Upstream version 0.3.3
[anytun.git] / src / anytun-controld.cpp
index 1dbc6f8..767db53 100644 (file)
 #include "resolver.h"
 
 #include "syncServer.h"
-#include "daemon.hpp"
+#include "daemonService.h"
+#include <vector>
+
+std::list<std::string> config_;
 
 void syncOnConnect(SyncTcpConnection * connptr)
 {
-  std::ifstream file(gOpt.getFileName().c_str());
-  if(file.is_open()) {
-    std::string line;
-    while (!file.eof()) {
-      getline (file,line);
-      connptr->Send(line);
-    }
-    file.close();
+       for(std::list<std::string>::const_iterator it=config_.begin(); it!=config_.end();++it)
+       {
+    connptr->Send(*it);
   }
 }
 
@@ -79,25 +77,17 @@ void syncListener()
 
 int main(int argc, char* argv[])
 {
-  bool daemonized=false;
+  DaemonService service;
   try 
   {
     try 
     {
-      bool result = gOpt.parse(argc, argv);
-      if(!result) {
-        gOpt.printUsage();
+      if(!gOpt.parse(argc, argv))
         exit(0);
-      }
+
       StringList targets = gOpt.getLogTargets();
-      if(targets.empty()) {
-        cLog.addTarget("syslog:3,anytun-controld,daemon");
-      }
-      else {
-        StringList::const_iterator it;
-        for(it = targets.begin();it != targets.end(); ++it)
-          cLog.addTarget(*it);
-      }
+      for(StringList::const_iterator it = targets.begin();it != targets.end(); ++it)
+        cLog.addTarget(*it);
     }
     catch(syntax_error& e)
     {
@@ -112,25 +102,28 @@ int main(int argc, char* argv[])
 
     std::ifstream file( gOpt.getFileName().c_str() );
     if( file.is_open() )
+    {
+       std::string line;
+       while (!file.eof()) {
+       getline (file,line);
+        config_.push_back(line);
+                       }
       file.close();
-    else {
+    else {
       std::cout << "ERROR: unable to open file!" << std::endl;
       exit(-1);
     }
     
-    PrivInfo privs(gOpt.getUsername(), gOpt.getGroupname());
-    if(gOpt.getDaemonize()) {
-      daemonize();
-      daemonized = true;
-    }
+    service.initPrivs(gOpt.getUsername(), gOpt.getGroupname());
+    if(gOpt.getDaemonize())
+      service.daemonize();
 
-    gSignalController.init();
-    gResolver.init();
-    
     if(gOpt.getChrootDir() != "")
-      do_chroot(gOpt.getChrootDir());
-    
-    privs.drop();
+      service.chroot(gOpt.getChrootDir());
+    service.dropPrivs();
+
+    gSignalController.init(service);
+    gResolver.init();
 
     boost::thread * syncListenerThread;
     syncListenerThread = new boost::thread(boost::bind(syncListener));
@@ -141,14 +134,14 @@ int main(int argc, char* argv[])
   }
   catch(std::runtime_error& e)
   {
-    if(daemonized)
+    if(service.isDaemonized())
       cLog.msg(Log::PRIO_ERROR) << "uncaught runtime error, exiting: " << e.what();
     else
       std::cout << "uncaught runtime error, exiting: " << e.what() << std::endl;
   }
   catch(std::exception& e)
   {
-    if(daemonized)
+    if(service.isDaemonized())
       cLog.msg(Log::PRIO_ERROR) << "uncaught exception, exiting: " << e.what();
     else
       std::cout << "uncaught exception, exiting: " << e.what() << std::endl;