]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
Change the waitFor*Data routines to take msec instead of usec.
authorMiod Vallat <miod.vallat@powerdns.com>
Mon, 4 Aug 2025 07:24:34 +0000 (09:24 +0200)
committerMiod Vallat <miod.vallat@powerdns.com>
Mon, 4 Aug 2025 09:48:51 +0000 (11:48 +0200)
They wrap poll() which has millisecond timeout resolution anyway.

Signed-off-by: Miod Vallat <miod.vallat@powerdns.com>
17 files changed:
modules/pipebackend/coprocess.cc
modules/remotebackend/httpconnector.cc
modules/remotebackend/pipeconnector.cc
modules/remotebackend/unixconnector.cc
pdns/comfun.cc
pdns/dnsbulktest.cc
pdns/dnsdistdist/delaypipe.cc
pdns/dnsdistdist/dnsdist-actions-factory.cc
pdns/dnsreplay.cc
pdns/dnstcpbench.cc
pdns/inflighter.cc
pdns/iputils.cc
pdns/misc.cc
pdns/misc.hh
pdns/resolver.cc
pdns/sdig.cc
pdns/tcpiohandler.cc

index 88be0220949d2ebc8c899596d1b1c051426317c4..d1ea2451d50be68487d109647b5a0b62766e9411 100644 (file)
@@ -186,7 +186,7 @@ void CoProcess::receive(string& received)
       }
       if (saved == EAGAIN) {
         if (d_timeout) {
-          int ret = waitForData(d_fd2[0], 0, d_timeout * 1000);
+          int ret = waitForData(d_fd2[0], 0, d_timeout);
           if (ret < 0)
             throw PDNSException("Error waiting on data from coprocess: " + string(strerror(saved)));
           if (!ret)
index 457fdd57f5c4d92c1d2cf257f8e4351f59c1968a..7903d7a7fecd7726649fa00bb68727499a95c1a2 100644 (file)
@@ -356,7 +356,7 @@ int HTTPConnector::send_message(const Json& input)
   if (this->d_socket != nullptr) {
     fd = this->d_socket->getHandle();
     // there should be no data waiting
-    if (waitForRWData(fd, true, 0, 1000) < 1) {
+    if (waitForRWData(fd, true, 0, 1) < 1) {
       try {
         d_socket->writenWithTimeout(out.str().c_str(), out.str().size(), timeout);
         rv = 1;
index 21055c073423e7620274903f61e2129fb9209fa5..f67884c003df17b6c2e00365110e4c24b9423826 100644 (file)
@@ -173,7 +173,7 @@ int PipeConnector::recv_message(Json& output)
   while (1) {
     receive.clear();
     if (d_timeout != 0) {
-      int ret = waitForData(fileno(d_fp.get()), 0, d_timeout * 1000);
+      int ret = waitForData(fileno(d_fp.get()), 0, d_timeout);
       if (ret < 0) {
         throw PDNSException("Error waiting on data from coprocess: " + stringerror());
       }
index 5ac137d4c457dc679da0b95705593ce8d72777c8..e6501ec4a31a4b7dc795e31b22819422d157678c 100644 (file)
@@ -77,7 +77,7 @@ int UnixsocketConnector::recv_message(Json& output)
   s_output = "";
 
   while ((t.tv_sec - t0.tv_sec) * 1000 + (t.tv_usec - t0.tv_usec) / 1000 < this->timeout) {
-    int avail = waitForData(this->fd, 0, this->timeout * 500); // use half the timeout as poll timeout
+    int avail = waitForData(this->fd, 0, this->timeout / 2); // use half the timeout as poll timeout
     if (avail < 0) { // poll error
       return -1;
     }
index 01a1fff2cc3fa6fbb94dd571904c533f972a5e64..3aafd8142b25b15c5d1a157e26a129a778b1972e 100644 (file)
@@ -110,7 +110,7 @@ struct SendReceive
   
   bool receive(Identifier& id, DNSResult& dr)
   {
-    if(waitForData(d_socket, 0, 500000) > 0) {
+    if(waitForData(d_socket, 0, 500) > 0) {
       char buf[512];
       ComboAddress from;
       from.sin4.sin_family = AF_INET;
@@ -244,7 +244,7 @@ struct SendReceiveRes
   
   bool receive(Identifier& id, RESResult& dr)
   {
-    if(waitForData(d_socket, 0, 500000) > 0) {
+    if(waitForData(d_socket, 0, 500) > 0) {
       char buf[512];
       ComboAddress from;
       from.sin4.sin_family = AF_INET;
index 9c9abd063d3a9eaa5cf80d5a52aaa6f56b9bef0d..49794828b36c9c51e1540a25e8e94835ee894d60 100644 (file)
@@ -141,7 +141,7 @@ struct SendReceive
 
   bool receive(Identifier& iden, DNSResult& dnsResult)
   {
-    if (waitForData(d_socket.getHandle(), 0, 500000) > 0) {
+    if (waitForData(d_socket.getHandle(), 0, 500) > 0) {
       // NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init): no need to initialize the buffer
       std::array<char, 512> buf;
 
index fdd828862cc55f9ce4889e2348769537480f2019..6b66467c3b72f8de4b16aa55e341d3d0ed5fbcf4 100644 (file)
@@ -52,7 +52,7 @@ template <class T>
 int ObjectPipe<T>::readTimeout(T* t, double msec)
 {
   while (true) {
-    int ret = waitForData(d_receiver.getDescriptor(), 0, 1000 * msec);
+    int ret = waitForData(d_receiver.getDescriptor(), 0, msec);
     if (ret < 0) {
       if (errno == EINTR) {
         continue;
index 07e8ea9b19d722fd3135953eb459df80fb0e7edc..db27eb063a1418ab8283f9a25b3c2be4ddef4a7a 100644 (file)
@@ -272,7 +272,7 @@ void TeeAction::worker()
   ssize_t res = 0;
   const dnsheader_aligned dnsheader(packet.data());
   for (;;) {
-    res = waitForData(d_socket.getHandle(), 0, 250000);
+    res = waitForData(d_socket.getHandle(), 0, 250);
     if (d_pleaseQuit) {
       break;
     }
index f7290fa93ddd43e8ac2983fec2c2b5d0eaae82f7..50325eff39f3423a5bfa9822b91452dffceb788e 100644 (file)
@@ -393,7 +393,7 @@ try
 {
   PacketBuffer packet;
   ComboAddress remote;
-  int res=waitForData(s_socket->getHandle(), g_timeoutMsec/1000, 1000*(g_timeoutMsec%1000));
+  int res=waitForData(s_socket->getHandle(), g_timeoutMsec/1000, g_timeoutMsec%1000);
 
   if(res < 0 || res==0)
     return;
index 996dbbd9c579af46790a9a5db788e6fa97b930e6..f0b907cb3af4f18a74e93224680c5b67eb26194e 100644 (file)
@@ -92,7 +92,7 @@ try
 
     udpsock.sendTo(string(packet.begin(), packet.end()), g_dest);
     ComboAddress origin;
-    res = waitForData(udpsock.getHandle(), 0, 1000 * g_timeoutMsec);
+    res = waitForData(udpsock.getHandle(), 0, static_cast<int>(g_timeoutMsec));
     if(res < 0)
       throw NetworkError("Error waiting for response");
     if(!res) {
@@ -129,7 +129,7 @@ try
 
   sock.writen(tcppacket);
 
-  res = waitForData(sock.getHandle(), 0, 1000 * g_timeoutMsec);
+  res = waitForData(sock.getHandle(), 0, static_cast<int>(g_timeoutMsec));
   if(res < 0)
     throw NetworkError("Error waiting for response");
   if(!res) {
index 6bdc6d74b0e51507d5a9df105346c9ab1267fc83..efc30b3cf5e32929973cc0659381c4c975904825 100644 (file)
@@ -230,7 +230,7 @@ struct SendReceive
   
   bool receive(Identifier& id, int& i)
   {
-    if(waitForData(d_socket, 0, 500000) > 0) {
+    if(waitForData(d_socket, 0, 500) > 0) {
       char buf[512];
     
       int len = recv(d_socket, buf, sizeof(buf), 0);
index 6beebddad1a557449acdd3896720070b908a45ac..e578f0110a46c00700a5a53c0735f8a046906b55 100644 (file)
@@ -77,7 +77,7 @@ int SConnectWithTimeout(int sockfd, const ComboAddress& remote, const struct tim
       /* we wait until the connection has been established */
       bool error = false;
       bool disconnected = false;
-      int res = waitForRWData(sockfd, false, static_cast<int>(timeout.tv_sec), static_cast<int>(timeout.tv_usec), &error, &disconnected);
+      int res = waitForRWData(sockfd, false, timeout, &error, &disconnected);
       if (res == 1) {
         if (error) {
           savederrno = 0;
index f8ccfa82dcbc8c53eec75196ff72c958147c2af8..e3cf96097c5249e87d6f7d6ad4cd5fa461938cb9 100644 (file)
@@ -143,7 +143,7 @@ size_t readn2WithTimeout(int fd, void* buffer, size_t len, const struct timeval&
     else {
       if (errno == EAGAIN) {
         struct timeval w = ((totalTimeout.tv_sec == 0 && totalTimeout.tv_usec == 0) || idleTimeout <= remainingTime) ? idleTimeout : remainingTime;
-        int res = waitForData(fd, w.tv_sec, w.tv_usec);
+        int res = waitForData(fd, w);
         if (res > 0) {
           /* there is data available */
         }
@@ -187,7 +187,7 @@ size_t writen2WithTimeout(int fd, const void * buffer, size_t len, const struct
       throw runtime_error("EOF while writing message");
     else {
       if (errno == EAGAIN) {
-        int res = waitForRWData(fd, false, timeout.tv_sec, timeout.tv_usec);
+        int res = waitForRWData(fd, false, timeout);
         if (res > 0) {
           /* there is room available */
         }
@@ -309,12 +309,17 @@ string nowTime()
 }
 
 // returns -1 in case if error, 0 if no data is available, 1 if there is. In the first two cases, errno is set
-int waitForData(int fileDesc, int seconds, int useconds)
+int waitForData(int fileDesc, int seconds, int mseconds)
 {
-  return waitForRWData(fileDesc, true, seconds, useconds);
+  return waitForRWData(fileDesc, true, seconds, mseconds);
 }
 
-int waitForRWData(int fileDesc, bool waitForRead, int seconds, int useconds, bool* error, bool* disconnected)
+int waitForData(int fileDesc, struct timeval timeout)
+{
+  return waitForRWData(fileDesc, true, timeout);
+}
+
+int waitForRWData(int fileDesc, bool waitForRead, int seconds, int mseconds, bool* error, bool* disconnected)
 {
   struct pollfd pfd{};
   memset(&pfd, 0, sizeof(pfd));
@@ -327,7 +332,7 @@ int waitForRWData(int fileDesc, bool waitForRead, int seconds, int useconds, boo
     pfd.events = POLLOUT;
   }
 
-  int ret = poll(&pfd, 1, seconds * 1000 + useconds/1000);
+  int ret = poll(&pfd, 1, seconds * 1000 + mseconds);
   if (ret > 0) {
     if ((error != nullptr) && (pfd.revents & POLLERR) != 0) {
       *error = true;
@@ -340,8 +345,13 @@ int waitForRWData(int fileDesc, bool waitForRead, int seconds, int useconds, boo
   return ret;
 }
 
+int waitForRWData(int fileDesc, bool waitForRead, struct timeval timeout, bool* error, bool* disconnected)
+{
+  return waitForRWData(fileDesc, waitForRead, static_cast<int>(timeout.tv_sec), static_cast<int>(timeout.tv_usec / 1000), error, disconnected);
+}
+
 // returns -1 in case of error, 0 if no data is available, 1 if there is. In the first two cases, errno is set
-int waitForMultiData(const set<int>& fds, const int seconds, const int useconds, int* fdOut) {
+int waitForMultiData(const set<int>& fds, const int seconds, const int mseconds, int* fdOut) {
   set<int> realFDs;
   for (const auto& fd : fds) {
     if (fd >= 0 && realFDs.count(fd) == 0) {
@@ -360,7 +370,7 @@ int waitForMultiData(const set<int>& fds, const int seconds, const int useconds,
 
   int ret;
   if(seconds >= 0)
-    ret = poll(pfds.data(), realFDs.size(), seconds * 1000 + useconds/1000);
+    ret = poll(pfds.data(), realFDs.size(), seconds * 1000 + mseconds);
   else
     ret = poll(pfds.data(), realFDs.size(), -1);
   if(ret <= 0)
@@ -379,7 +389,7 @@ int waitForMultiData(const set<int>& fds, const int seconds, const int useconds,
 }
 
 // returns -1 in case of error, 0 if no data is available, 1 if there is. In the first two cases, errno is set
-int waitFor2Data(int fd1, int fd2, int seconds, int useconds, int* fdPtr)
+int waitFor2Data(int fd1, int fd2, int seconds, int mseconds, int* fdPtr)
 {
   std::array<pollfd,2> pfds{};
   memset(pfds.data(), 0, pfds.size() * sizeof(struct pollfd));
@@ -392,7 +402,7 @@ int waitFor2Data(int fd1, int fd2, int seconds, int useconds, int* fdPtr)
 
   int ret{};
   if (seconds >= 0) {
-    ret = poll(pfds.data(), nsocks, seconds * 1000 + useconds / 1000);
+    ret = poll(pfds.data(), nsocks, seconds * 1000 + mseconds);
   }
   else {
     ret = poll(pfds.data(), nsocks, -1);
index 3c193f150b82670982eed064f57df68bfe4b36cc..7752b5f26ff3e327dd728d488f46c16118c668d9 100644 (file)
@@ -106,10 +106,12 @@ void stripLine(string &line);
 std::optional<string> getHostname();
 std::string getCarbonHostName();
 string urlEncode(const string &text);
-int waitForData(int fileDesc, int seconds, int useconds = 0);
-int waitFor2Data(int fd1, int fd2, int seconds, int useconds, int* fd);
-int waitForMultiData(const set<int>& fds, const int seconds, const int useconds, int* fd);
-int waitForRWData(int fileDesc, bool waitForRead, int seconds, int useconds, bool* error = nullptr, bool* disconnected = nullptr);
+int waitForData(int fileDesc, int seconds, int mseconds = 0);
+int waitForData(int fileDesc, struct timeval timeout);
+int waitFor2Data(int fd1, int fd2, int seconds, int mseconds, int* fd);
+int waitForMultiData(const set<int>& fds, const int seconds, const int mseconds, int* fd);
+int waitForRWData(int fileDesc, bool waitForRead, int seconds, int mseconds, bool* error = nullptr, bool* disconnected = nullptr);
+int waitForRWData(int fileDesc, bool waitForRead, struct timeval timeout, bool* error = nullptr, bool* disconnected = nullptr);
 bool getTSIGHashEnum(const DNSName& algoName, TSIGHashEnum& algoEnum);
 DNSName getTSIGAlgoName(TSIGHashEnum& algoEnum);
 
index 530a562ac5dbec5175caa8024e991e3cabce061e..cb2d8748487b684154df4b332e681fbf328e53d5 100644 (file)
@@ -313,7 +313,7 @@ int Resolver::resolve(const ComboAddress& to, const DNSName &domain, int type, R
   try {
     int sock = -1;
     int id = sendResolve(to, local, domain, type, &sock);
-    int err=waitForData(sock, 0, 3000000);
+    int err=waitForData(sock, 3, 0);
 
     if(!err) {
       throw ResolverException("Timeout waiting for answer");
index 887dafb38eca6bdf9b465a8d51eeef22e14a3acb..90a23e54d8542c06df6f1ee768c911dfae1ef654 100644 (file)
@@ -526,7 +526,7 @@ try {
     Socket sock(dest.sin4.sin_family, SOCK_DGRAM);
     question = proxyheader + question;
     sock.sendTo(question, dest);
-    int result = waitForData(sock.getHandle(), timeout.tv_sec, timeout.tv_usec);
+    int result = waitForData(sock.getHandle(), timeout);
     if (result < 0)
       throw std::runtime_error("Error waiting for data: " + stringerror());
     if (!result)
index 660b85f02424e4d760aa27bc23b68230f65a3827..6c1549f5643f89e7da32655e0485838339a50e20 100644 (file)
@@ -286,7 +286,7 @@ public:
   {
     auto state = convertIORequestToIOState(res);
     if (state == IOState::NeedRead) {
-      res = waitForData(d_socket, timeout.tv_sec, timeout.tv_usec);
+      res = waitForData(d_socket, timeout);
       if (res == 0) {
         throw std::runtime_error("Timeout while reading from TLS connection");
       }
@@ -295,7 +295,7 @@ public:
       }
     }
     else if (state == IOState::NeedWrite) {
-      res = waitForRWData(d_socket, false, timeout.tv_sec, timeout.tv_usec);
+      res = waitForRWData(d_socket, false, timeout);
       if (res == 0) {
         throw std::runtime_error("Timeout while writing to TLS connection");
       }
@@ -1308,13 +1308,13 @@ public:
         return;
       }
       else if (state == IOState::NeedRead) {
-        int result = waitForData(d_socket, remainingTime.tv_sec, remainingTime.tv_usec);
+        int result = waitForData(d_socket, remainingTime);
         if (result <= 0) {
           throw std::runtime_error("Error reading from TLS connection: " + std::to_string(result));
         }
       }
       else if (state == IOState::NeedWrite) {
-        int result = waitForRWData(d_socket, false, remainingTime.tv_sec, remainingTime.tv_usec);
+        int result = waitForRWData(d_socket, false, remainingTime);
         if (result <= 0) {
           throw std::runtime_error("Error reading from TLS connection: " + std::to_string(result));
         }
@@ -1489,7 +1489,7 @@ public:
           throw std::runtime_error("Fatal error reading from TLS connection: " + std::string(gnutls_strerror(res)));
         }
         else if (res == GNUTLS_E_AGAIN) {
-          int result = waitForData(d_socket, readTimeout.tv_sec, readTimeout.tv_usec);
+          int result = waitForData(d_socket, readTimeout);
           if (result <= 0) {
             throw std::runtime_error("Error while waiting to read from TLS connection: " + std::to_string(result));
           }
@@ -1532,7 +1532,7 @@ public:
           throw std::runtime_error("Fatal error writing to TLS connection: " + std::string(gnutls_strerror(res)));
         }
         else if (res == GNUTLS_E_AGAIN) {
-          int result = waitForRWData(d_socket, false, writeTimeout.tv_sec, writeTimeout.tv_usec);
+          int result = waitForRWData(d_socket, false, writeTimeout);
           if (result <= 0) {
             throw std::runtime_error("Error waiting to write to TLS connection: " + std::to_string(result));
           }