]> granicus.if.org Git - pdns/commitdiff
CHANGES BEHAVIOUR: before we launch, check if we can connect to the controlsocket...
authorbert hubert <bert.hubert@netherlabs.nl>
Mon, 10 Jun 2013 14:11:20 +0000 (16:11 +0200)
committerPeter van Dijk <peter.van.dijk@netherlabs.nl>
Mon, 10 Jun 2013 14:13:42 +0000 (16:13 +0200)
pdns/common_startup.cc
pdns/dynlistener.cc
pdns/dynlistener.hh
pdns/receiver.cc

index 044b38f7b3753b23e63c156f2019ab61b00e7023..cf627e87c7a0b092d90b769dfa302bb34f15cb0c 100644 (file)
@@ -31,8 +31,6 @@ UDPNameserver *N;
 int avg_latency;
 TCPNameserver *TN;
 
-
-
 ArgvMap &arg()
 {
   return theArg;
index b46dcec7c763cc881ab90b36f659ab899346ae49..a8cb793d4fee789c5f5e99c769c22db42e7bda7d 100644 (file)
@@ -83,8 +83,33 @@ void DynListener::createSocketAndBind(int family, struct sockaddr*local, size_t
   }
 }
 
+/* this does a simplistic check, if we can connect, we consider it live. If we can't connect because
+   of access denied, we must consider it dead, nothing we can do about it.
+*/
+bool DynListener::testLive(const string& fname)
+{
+  struct sockaddr_un addr;
+  int fd = socket(AF_UNIX, SOCK_STREAM, 0);
+  if(fd < 0) { // we'll have bigger issues down the road
+    return false;
+  }
+
+  memset(&addr, 0, sizeof(addr));
+  addr.sun_family = AF_UNIX;
+  strncpy(addr.sun_path, fname.c_str(), fname.length());
+
+  int status = connect(fd, (struct sockaddr*)&addr, sizeof(addr));
+  int err=errno;
+  close(fd);
+  return status==0;
+}
+
 void DynListener::listenOnUnixDomain(const string& fname)
 {
+  if(testLive(fname)) {
+    L<<Logger::Critical<<"Previous controlsocket '"<<fname<<"' is in use"<<endl;
+    exit(1);
+  }
   int err=unlink(fname.c_str());
   if(err < 0 && errno!=ENOENT) {
     L<<Logger::Critical<<"Unable to remove (previous) controlsocket at '"<<fname<<"': "<<strerror(errno)<<endl;
@@ -219,8 +244,10 @@ string DynListener::getLine()
           continue;
         }
       }
+      errno=0;
       if(!fgets(&mesg[0], mesg.size(), fp.get())) {
-        L<<Logger::Error<<"Unable to receive line from controlsocket ("<<d_client<<"): "<<strerror(errno)<<endl;
+        if(errno)
+         L<<Logger::Error<<"Unable to receive line from controlsocket ("<<d_client<<"): "<<strerror(errno)<<endl;
         close(d_client);
         continue;
       }
index 769aa470d74989e9087fd1cb68c3f9f881c18f4e..fd28439594f4dd472c89f6ad09cb3dd65c111dfa 100644 (file)
@@ -84,5 +84,6 @@ private:
   ComboAddress d_socketaddress;
   static g_funkdb_t s_funcdb;
   static g_funk_t* s_restfunc;
+  bool testLive(const string& fname);
 };
 #endif /* PDNS_DYNLISTENER */
index 99b94f7b8024384e4fca5b18cd91a795191f4449..730981a5fbeaba33be9ae5aeb6af08dda0c1a329 100644 (file)
@@ -1,6 +1,6 @@
 /*
     PowerDNS Versatile Database Driven Nameserver
-    Copyright (C) 2002 - 2011  PowerDNS.COM BV
+    Copyright (C) 2002 - 2013  PowerDNS.COM BV
 
     This program is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License version 2
@@ -585,7 +585,6 @@ int main(int argc, char **argv)
   showProductVersion();
 
   try {
-
     mainthread();
   }
   catch(AhuException &AE) {