]> git.ipfire.org Git - thirdparty/pdns.git/blobdiff - pdns/receiver.cc
Fix compilation on systems that do not define HOST_NAME_MAX
[thirdparty/pdns.git] / pdns / receiver.cc
index 209db7af89410d2d7082543cbc1857ce462d157d..da65e8bf2591a9e3a5aaac613ec280a751b8c8a1 100644 (file)
@@ -101,7 +101,7 @@ const char *funnytext=
 This file is where it all happens - main is here, as are the two pivotal threads qthread() and athread()
 */
 
-void daemonize(void)
+static void daemonize(void)
 {
   if(fork())
     exit(0); // bye bye
@@ -137,7 +137,7 @@ static void writePid(void)
   string fname=::arg()["socket-dir"];
   if (::arg()["socket-dir"].empty()) {
     if (::arg()["chroot"].empty())
-      fname = LOCALSTATEDIR;
+      fname = std::string(LOCALSTATEDIR) + "/pdns";
     else
       fname = ::arg()["chroot"] + "/";
   } else if (!::arg()["socket-dir"].empty() && !::arg()["chroot"].empty()) {
@@ -149,12 +149,12 @@ static void writePid(void)
   if(of)
     of<<getpid()<<endl;
   else
-    g_log<<Logger::Error<<"Writing pid for "<<getpid()<<" to "<<fname<<" failed: "<<strerror(errno)<<endl;
+    g_log<<Logger::Error<<"Writing pid for "<<getpid()<<" to "<<fname<<" failed: "<<stringerror()<<endl;
 }
 
 int g_fd1[2], g_fd2[2];
 FILE *g_fp;
-pthread_mutex_t g_guardian_lock = PTHREAD_MUTEX_INITIALIZER;
+std::mutex g_guardian_lock;
 
 // The next two methods are not in dynhandler.cc because they use a few items declared in this file.
 static string DLCycleHandler(const vector<string>&parts, pid_t ppid)
@@ -176,7 +176,7 @@ static string DLRestHandler(const vector<string>&parts, pid_t ppid)
   }
   line.append(1,'\n');
   
-  Lock l(&g_guardian_lock);
+  std::lock_guard<std::mutex> l(g_guardian_lock);
 
   try {
     writen2(g_fd1[1],line.c_str(),line.size()+1);
@@ -216,14 +216,14 @@ static int guardian(int argc, char **argv)
   bool first=true;
   cpid=0;
 
-  pthread_mutex_lock(&g_guardian_lock);
+  g_guardian_lock.lock();
 
   for(;;) {
     int pid;
     setStatus("Launching child");
     
     if(pipe(g_fd1)<0 || pipe(g_fd2)<0) {
-      g_log<<Logger::Critical<<"Unable to open pipe for coprocess: "<<strerror(errno)<<endl;
+      g_log<<Logger::Critical<<"Unable to open pipe for coprocess: "<<stringerror()<<endl;
       exit(1);
     }
 
@@ -268,7 +268,7 @@ static int guardian(int argc, char **argv)
         close(g_fd2[1]);
       }
       if(execvp(argv[0], newargv)<0) {
-        g_log<<Logger::Error<<"Unable to execvp '"<<argv[0]<<"': "<<strerror(errno)<<endl;
+        g_log<<Logger::Error<<"Unable to execvp '"<<argv[0]<<"': "<<stringerror()<<endl;
         char **p=newargv;
         while(*p)
           g_log<<Logger::Error<<*p++<<endl;
@@ -292,14 +292,14 @@ static int guardian(int argc, char **argv)
 
         writePid();
       }
-      pthread_mutex_unlock(&g_guardian_lock);  
+      g_guardian_lock.unlock();
       int status;
       cpid=pid;
       for(;;) {
         int ret=waitpid(pid,&status,WNOHANG);
 
         if(ret<0) {
-          g_log<<Logger::Error<<"In guardian loop, waitpid returned error: "<<strerror(errno)<<endl;
+          g_log<<Logger::Error<<"In guardian loop, waitpid returned error: "<<stringerror()<<endl;
           g_log<<Logger::Error<<"Dying"<<endl;
           exit(1);
         }
@@ -314,7 +314,7 @@ static int guardian(int argc, char **argv)
         }
       }
 
-      pthread_mutex_lock(&g_guardian_lock);
+      g_guardian_lock.lock();
       close(g_fd1[1]);
       fclose(g_fp);
       g_fp=0;
@@ -348,13 +348,13 @@ static int guardian(int argc, char **argv)
       g_log<<Logger::Error<<"No clue what happened! Respawning"<<endl;
     }
     else {
-      g_log<<Logger::Error<<"Unable to fork: "<<strerror(errno)<<endl;
+      g_log<<Logger::Error<<"Unable to fork: "<<stringerror()<<endl;
       exit(1);
     }
   }
 }
 
-#ifdef __GLIBC__ && !defined(__UCLIBC__)
+#if defined(__GLIBC__) && !defined(__UCLIBC__)
 #include <execinfo.h>
 static void tbhandler(int num)
 {
@@ -386,7 +386,7 @@ int main(int argc, char **argv)
   s_programname="pdns";
   s_starttime=time(0);
 
-#ifdef __GLIBC__ && !defined(__UCLIBC__)
+#if defined(__GLIBC__) && !defined(__UCLIBC__)
   signal(SIGSEGV,tbhandler);
   signal(SIGFPE,tbhandler);
   signal(SIGABRT,tbhandler);
@@ -415,7 +415,7 @@ int main(int argc, char **argv)
     string configname=::arg()["config-dir"]+"/"+s_programname+".conf";
     cleanSlashes(configname);
 
-    if(!::arg().mustDo("config") && !::arg().mustDo("no-config")) // "config" == print a configuration file
+    if(::arg()["config"] != "default" && !::arg().mustDo("no-config")) // "config" == print a configuration file
       ::arg().laxFile(configname.c_str());
     
     ::arg().laxParse(argc,argv); // reparse so the commandline still wins
@@ -450,7 +450,7 @@ int main(int argc, char **argv)
     
     // we really need to do work - either standalone or as an instance
 
-#ifdef __GLIBC__ && !defined(__UCLIBC__)
+#if defined(__GLIBC__) && !defined(__UCLIBC__)
     if(!::arg().mustDo("traceback-handler")) {
       g_log<<Logger::Warning<<"Disabling traceback handler"<<endl;
       signal(SIGSEGV,SIG_DFL);
@@ -488,7 +488,20 @@ int main(int argc, char **argv)
     BackendMakers().launch(::arg()["launch"]); // vrooooom!
 
     if(!::arg().getCommands().empty()) {
-      cerr<<"Fatal: non-option on the command line, perhaps a '--setting=123' statement missed the '='?"<<endl;
+      cerr<<"Fatal: non-option";
+      if (::arg().getCommands().size() > 1) {
+        cerr<<"s";
+      }
+      cerr<<" (";
+      bool first = true;
+      for (const auto& c : ::arg().getCommands()) {
+        if (!first) {
+          cerr<<", ";
+        }
+        first = false;
+        cerr<<c;
+      }
+      cerr<<") on the command line, perhaps a '--setting=123' statement missed the '='?"<<endl;
       exit(99);
     }
     
@@ -499,7 +512,25 @@ int main(int argc, char **argv)
     }
     
     if(::arg().mustDo("config")) {
-      cout<<::arg().configstring()<<endl;
+      string config = ::arg()["config"];
+      if (config == "default") {
+        cout<<::arg().configstring(false, true);
+      } else if (config == "diff") {
+          cout<<::arg().configstring(true, false);
+      } else if (config == "check") {
+        try {
+          if(!::arg().mustDo("no-config"))
+            ::arg().file(configname.c_str());
+          ::arg().parse(argc,argv);
+          exit(0);
+        }
+        catch(const ArgException &A) {
+          cerr<<"Fatal error: "<<A.reason<<endl;
+          exit(1);
+        }
+      } else {
+        cout<<::arg().configstring(true, true);
+      }
       exit(0);
     }
 
@@ -528,15 +559,15 @@ int main(int argc, char **argv)
 
     if(isGuarded(argv)) {
       g_log<<Logger::Warning<<"This is a guarded instance of pdns"<<endl;
-      dl=new DynListener; // listens on stdin 
+      dl=make_unique<DynListener>(); // listens on stdin 
     }
     else {
       g_log<<Logger::Warning<<"This is a standalone pdns"<<endl; 
       
       if(::arg().mustDo("control-console"))
-        dl=new DynListener();
+        dl=make_unique<DynListener>();
       else
-        dl=new DynListener(s_programname);
+        dl=std::unique_ptr<DynListener>(new DynListener(s_programname));
       
       writePid();
     }
@@ -556,7 +587,7 @@ int main(int argc, char **argv)
     DynListener::registerFunc("REMOTES", &DLRemotesHandler, "get top remotes");
     DynListener::registerFunc("SET",&DLSettingsHandler, "set config variables", "<var> <value>");
     DynListener::registerFunc("RETRIEVE",&DLNotifyRetrieveHandler, "retrieve slave domain", "<domain>");
-    DynListener::registerFunc("CURRENT-CONFIG",&DLCurrentConfigHandler, "retrieve the current configuration");
+    DynListener::registerFunc("CURRENT-CONFIG",&DLCurrentConfigHandler, "retrieve the current configuration", "[diff|default]");
     DynListener::registerFunc("LIST-ZONES",&DLListZones, "show list of zones", "[master|slave|native]");
     DynListener::registerFunc("TOKEN-LOGIN", &DLTokenLogin, "Login to a PKCS#11 token", "<module> <slot> <pin>");
 
@@ -575,7 +606,7 @@ int main(int argc, char **argv)
       if(gethostname(tmp, sizeof(tmp)-1) == 0) {
         ::arg().set("server-id")=tmp;
       } else {
-        g_log<<Logger::Warning<<"Unable to get the hostname, NSID and id.server values will be empty: "<<strerror(errno)<<endl;
+        g_log<<Logger::Warning<<"Unable to get the hostname, NSID and id.server values will be empty: "<<stringerror()<<endl;
       }
     }