You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by be...@apache.org on 2011/06/05 11:23:51 UTC

svn commit: r1132316 - /incubator/mesos/trunk/third_party/libprocess/pid.cpp

Author: benh
Date: Sun Jun  5 09:23:51 2011
New Revision: 1132316

URL: http://svn.apache.org/viewvc?rev=1132316&view=rev
Log:
Another attempt at fixing pid parsing issues, this time using gethostbyname2_r to deal with possible re-entrancy issues.

Modified:
    incubator/mesos/trunk/third_party/libprocess/pid.cpp

Modified: incubator/mesos/trunk/third_party/libprocess/pid.cpp
URL: http://svn.apache.org/viewvc/incubator/mesos/trunk/third_party/libprocess/pid.cpp?rev=1132316&r1=1132315&r2=1132316&view=diff
==============================================================================
--- incubator/mesos/trunk/third_party/libprocess/pid.cpp (original)
+++ incubator/mesos/trunk/third_party/libprocess/pid.cpp Sun Jun  5 09:23:51 2011
@@ -1,9 +1,12 @@
+#include <errno.h>
 #include <netdb.h>
 #include <stdio.h>
 #include <string.h>
 
 #include <arpa/inet.h>
 
+#include <glog/logging.h>
+
 #include <iostream>
 #include <string>
 
@@ -77,6 +80,8 @@ istream& operator >> (istream& stream, U
     return stream;
   }
 
+  VLOG(1) << "Attempting to parse '" << str << "' into a PID";
+
   if (str.size() == 0) {
     stream.setstate(std::ios_base::badbit);
     return stream;
@@ -84,7 +89,8 @@ istream& operator >> (istream& stream, U
 
   string id;
   string host;
-  unsigned short port;
+  uint32_t ip;
+  uint16_t port;
 
   size_t index = str.find('@');
 
@@ -95,7 +101,7 @@ istream& operator >> (istream& stream, U
     return stream;
   }
 
-  str = str.substr(index + 1, str.size() - index);
+  str = str.substr(index + 1);
 
   index = str.find(':');
 
@@ -106,18 +112,44 @@ istream& operator >> (istream& stream, U
     return stream;
   }
 
-  hostent* he = gethostbyname2(host.c_str(), AF_INET);
-  if (he == NULL) {
+  hostent he, *hep;
+  char* temp;
+  size_t length;
+  int result;
+  int herrno;
+
+  // Allocate temporary buffer for gethostbyname2_r.
+  length = 1024;
+  temp = new char[length];
+
+  while ((result = gethostbyname2_r(host.c_str(), AF_INET, &he,
+				    temp, length, &hep, &herrno)) == ERANGE) {
+    // Enlarge the buffer.
+    delete temp;
+    length *= 2;
+    temp = new char[length];
+  }
+
+  if (result != 0 || hep == NULL) {
+    VLOG(1) << "Failed to parse host '" << host
+	    << "' because " << hstrerror(herrno);
     stream.setstate(std::ios_base::badbit);
+    delete temp;
     return stream;
   }
 
-  if (he->h_addr_list[0] == NULL) {
+  if (hep->h_addr_list[0] == NULL) {
+    VLOG(1) << "Got no addresses for '" << host << "'";
     stream.setstate(std::ios_base::badbit);
+    delete temp;
     return stream;
   }
 
-  str = str.substr(index + 1, str.size() - index);
+  ip = *((uint32_t*) hep->h_addr_list[0]);
+
+  delete temp;
+
+  str = str.substr(index + 1);
 
   if (sscanf(str.c_str(), "%hu", &port) != 1) {
     stream.setstate(std::ios_base::badbit);
@@ -125,7 +157,7 @@ istream& operator >> (istream& stream, U
   }
 
   pid.id = id;
-  pid.ip = *((uint32_t*) he->h_addr_list[0]);
+  pid.ip = ip;
   pid.port = port;
 
   return stream;