From: Maxim Mamontov <faust.madf@gmail.com>
Date: Fri, 16 Sep 2011 16:11:58 +0000 (+0300)
Subject: Replace deprecated usleep with POSIX-compliant nanosleep
X-Git-Tag: 2.408-rc1~37
X-Git-Url: https://git.stg.codes/stg.git/commitdiff_plain/53c1823aaef2eb8d547a8eed8cfe12fe7204ca79?ds=sidebyside

Replace deprecated usleep with POSIX-compliant nanosleep
---

diff --git a/projects/rscriptd/listener.cpp b/projects/rscriptd/listener.cpp
index 9e03988c..d4f2dc93 100644
--- a/projects/rscriptd/listener.cpp
+++ b/projects/rscriptd/listener.cpp
@@ -102,14 +102,16 @@ running = false;
 
 printfd(__FILE__, "LISTENER::Stop()\n");
 
-usleep(500000);
+struct timespec ts = {0, 500000000};
+nanosleep(&ts, NULL);
 
 if (!processorStopped)
     {
     //5 seconds to thread stops itself
     for (int i = 0; i < 25 && !processorStopped; i++)
         {
-        usleep(200000);
+        struct timespec ts = {0, 200000000};
+        nanosleep(&ts, NULL);
         }
 
     //after 5 seconds waiting thread still running. now killing it
@@ -130,7 +132,8 @@ if (!receiverStopped)
     //5 seconds to thread stops itself
     for (int i = 0; i < 25 && !receiverStopped; i++)
         {
-        usleep(200000);
+        struct timespec ts = {0, 200000000};
+        nanosleep(&ts, NULL);
         }
 
     //after 5 seconds waiting thread still running. now killing it
@@ -196,7 +199,8 @@ processorStopped = false;
 
 while (running)
     {
-    usleep(500000);
+    struct timespec ts = {0, 500000000};
+    nanosleep(&ts, NULL);
     if (!pending.empty())
         ProcessPending();
     ProcessTimeouts();
diff --git a/projects/rscriptd/main.cpp b/projects/rscriptd/main.cpp
index 7e7f7f35..2739758c 100644
--- a/projects/rscriptd/main.cpp
+++ b/projects/rscriptd/main.cpp
@@ -430,7 +430,8 @@ WriteServLog("+++++++++++++++++++++++++++++++++++++++++++++");
 
 while (nonstop.GetStatus())
     {
-    usleep(100000);
+    struct timespec ts = {0, 100000000};
+    nanosleep(&ts, NULL);
     }
 
 listener->Stop();
diff --git a/projects/sgauth/main.cpp b/projects/sgauth/main.cpp
index 96c58203..2af78b71 100644
--- a/projects/sgauth/main.cpp
+++ b/projects/sgauth/main.cpp
@@ -239,7 +239,8 @@ clnp->Connect();
 
 while (1)
     {
-    usleep(200000);
+    struct timespec ts = {0, 200000000};
+    nanosleep(&ts, NULL);
 
     char state[20];
 
diff --git a/projects/sgauth/web.cpp b/projects/sgauth/web.cpp
index 6e2d4aca..ab169cc4 100644
--- a/projects/sgauth/web.cpp
+++ b/projects/sgauth/web.cpp
@@ -182,7 +182,8 @@ while (1)
         #ifdef WIN32
         Sleep(1000);
         #else
-        usleep(1000000);
+        struct timespec ts = {1, 0};
+        nanosleep(&ts, NULL);
         #endif
         exit(0);
         }
diff --git a/projects/sgauthstress/main.cpp b/projects/sgauthstress/main.cpp
index 2388254d..b0819c3c 100644
--- a/projects/sgauthstress/main.cpp
+++ b/projects/sgauthstress/main.cpp
@@ -157,7 +157,8 @@ std::cout << "Successfully loaded " << proto.UserCount() << " users" << std::end
 running = true;
 while (running)
     {
-    usleep(200000);
+    struct timespec ts = {0, 200000000};
+    nanosleep(&ts, NULL);
     }
 
 proto.Stop();
diff --git a/projects/stargazer/plugins/authorization/inetaccess/inetaccess.cpp b/projects/stargazer/plugins/authorization/inetaccess/inetaccess.cpp
index 5e468d0f..4986080e 100644
--- a/projects/stargazer/plugins/authorization/inetaccess/inetaccess.cpp
+++ b/projects/stargazer/plugins/authorization/inetaccess/inetaccess.cpp
@@ -30,7 +30,7 @@
 
 #include <sys/types.h>
 #include <sys/socket.h>
-#include <unistd.h> // usleep, close
+#include <unistd.h> // close
 
 #include <csignal>
 #include <cstdlib>
@@ -428,7 +428,8 @@ if (isRunningRun)
     //5 seconds to thread stops itself
     for (int i = 0; i < 25 && isRunningRun; i++)
         {
-        usleep(200000);
+        struct timespec ts = {0, 200000000};
+        nanosleep(&ts, NULL);
         }
 
     //after 5 seconds waiting thread still running. now killing it
@@ -441,7 +442,10 @@ if (isRunningRun)
             return -1;
             }
         for (int i = 0; i < 25 && isRunningRun; ++i)
-            usleep(200000);
+            {
+            struct timespec ts = {0, 200000000};
+            nanosleep(&ts, NULL);
+            }
         if (isRunningRun)
             {
             printfd(__FILE__, "Failed to stop recv thread\n");
@@ -461,7 +465,8 @@ if (isRunningRunTimeouter)
     //5 seconds to thread stops itself
     for (int i = 0; i < 25 && isRunningRunTimeouter; i++)
         {
-        usleep(200000);
+        struct timespec ts = {0, 200000000};
+        nanosleep(&ts, NULL);
         }
 
     //after 5 seconds waiting thread still running. now killing it
@@ -473,7 +478,10 @@ if (isRunningRunTimeouter)
             return -1;
             }
         for (int i = 0; i < 25 && isRunningRunTimeouter; ++i)
-            usleep(200000);
+            {
+            struct timespec ts = {0, 200000000};
+            nanosleep(&ts, NULL);
+            }
         if (isRunningRunTimeouter)
             {
             printfd(__FILE__, "Failed to stop timeouter thread\n");
@@ -525,7 +533,8 @@ int a = -1;
 string monFile = ia->stgSettings->GetMonitorDir() + "/inetaccess_t";
 while (ia->nonstop)
     {
-    usleep(20000);
+    struct timespec ts = {0, 20000000};
+    nanosleep(&ts, NULL);
     ia->Timeouter();
     // TODO change counter to timer and MONITOR_TIME_DELAY_SEC
     if (++a % (50 * 60) == 0 && ia->stgSettings->GetMonitoring())
diff --git a/projects/stargazer/plugins/authorization/stress/stress.cpp b/projects/stargazer/plugins/authorization/stress/stress.cpp
index 5d41a91d..7273ed48 100644
--- a/projects/stargazer/plugins/authorization/stress/stress.cpp
+++ b/projects/stargazer/plugins/authorization/stress/stress.cpp
@@ -176,7 +176,8 @@ if (isRunning)
         {
         if (!isRunning)
             break;
-        usleep(200000);
+        struct timespec ts = {0, 200000000};
+        nanosleep(&ts, NULL);
         }
 
     //after 5 seconds waiting thread still running. now killing it
diff --git a/projects/stargazer/plugins/capture/cap_nf/cap_nf.cpp b/projects/stargazer/plugins/capture/cap_nf/cap_nf.cpp
index 9ed50756..fb7d2cc5 100644
--- a/projects/stargazer/plugins/capture/cap_nf/cap_nf.cpp
+++ b/projects/stargazer/plugins/capture/cap_nf/cap_nf.cpp
@@ -148,7 +148,8 @@ if (portU && !stoppedUDP)
     CloseUDP();
     for (int i = 0; i < 25 && !stoppedUDP; ++i)
         {
-        usleep(200000);
+        struct timespec ts = {0, 200000000};
+        nanosleep(&ts, NULL);
         }
     if (stoppedUDP)
         {
@@ -170,7 +171,8 @@ if (portT && !stoppedTCP)
     CloseTCP();
     for (int i = 0; i < 25 && !stoppedTCP; ++i)
         {
-        usleep(200000);
+        struct timespec ts = {0, 200000000};
+        nanosleep(&ts, NULL);
         }
     if (stoppedTCP)
         {
diff --git a/projects/stargazer/plugins/capture/divert_freebsd/divert_cap.cpp b/projects/stargazer/plugins/capture/divert_freebsd/divert_cap.cpp
index 98bf1b6c..5927727e 100644
--- a/projects/stargazer/plugins/capture/divert_freebsd/divert_cap.cpp
+++ b/projects/stargazer/plugins/capture/divert_freebsd/divert_cap.cpp
@@ -130,7 +130,8 @@ for (i = 0; i < 25; i++)
     if (!isRunning)
         break;
 
-    usleep(200000);
+    struct timespec ts = {0, 200000000};
+    nanosleep(&ts, NULL);
     }
 
 //after 5 seconds waiting thread still running. now killing it
diff --git a/projects/stargazer/plugins/capture/ether_freebsd/ether_cap.cpp b/projects/stargazer/plugins/capture/ether_freebsd/ether_cap.cpp
index c415e691..108c4e0d 100644
--- a/projects/stargazer/plugins/capture/ether_freebsd/ether_cap.cpp
+++ b/projects/stargazer/plugins/capture/ether_freebsd/ether_cap.cpp
@@ -180,7 +180,8 @@ for (i = 0; i < 25; i++)
     if (!isRunning)
         break;
 
-    usleep(200000);
+    struct timespec ts = {0, 200000000};
+    nanosleep(&ts, NULL);
     }
 
 //after 5 seconds waiting thread still running. now killing it
@@ -213,12 +214,11 @@ char * iface;
 
 while (dc->nonstop)
     {
-    dc->BPFCapRead((char*)&hdr, 68 + 14, &iface);
+    if (dc->BPFCapRead((char*)&hdr, 68 + 14, &iface))
+        continue;
 
     if (!(hdr[12] == 0x8 && hdr[13] == 0x0))
-    {
         continue;
-    }
 
     dc->traffCnt->Process(*rpp);
     }
@@ -313,12 +313,16 @@ for (unsigned int i = 0; i < polld.size(); i++)
     {
     if (polld[i].revents & POLLIN)
         {
-        BPFCapRead(buffer, blen, capIface, &bpfData[i]);
+        if (BPFCapRead(buffer, blen, capIface, &bpfData[i]))
+            {
+            polld[i].revents = 0;
+            continue;
+            }
         polld[i].revents = 0;
         return 0;
         }
     }
-return 0;
+return -1;
 }
 //-----------------------------------------------------------------------------
 int BPF_CAP::BPFCapRead(char * buffer, int blen, char **, BPF_DATA * bd)
@@ -328,8 +332,9 @@ if (bd->canRead)
     bd->r = read(bd->fd, bd->buffer, BUFF_LEN);
     if (bd->r < 0)
         {
-        //printfd(__FILE__, " error read\n");
-        usleep(20000);
+        struct timespec ts = {0, 20000000};
+        nanosleep(&ts, NULL);
+        return -1;
         }
 
     bd->p = bd->buffer;
diff --git a/projects/stargazer/plugins/capture/ether_linux/ether_cap.cpp b/projects/stargazer/plugins/capture/ether_linux/ether_cap.cpp
index 97e9b171..276c8df0 100644
--- a/projects/stargazer/plugins/capture/ether_linux/ether_cap.cpp
+++ b/projects/stargazer/plugins/capture/ether_linux/ether_cap.cpp
@@ -115,7 +115,8 @@ nonstop = false;
 //5 seconds to thread stops itself
 for (int i = 0; i < 25 && isRunning; i++)
     {
-    usleep(200000);
+    struct timespec ts = {0, 200000000};
+    nanosleep(&ts, NULL);
     }
 //after 5 seconds waiting thread still running. now killing it
 if (isRunning)
@@ -126,7 +127,10 @@ if (isRunning)
         return -1;
         }
     for (int i = 0; i < 25 && isRunning; ++i)
-        usleep(200000);
+        {
+        struct timespec ts = {0, 200000000};
+        nanosleep(&ts, NULL);
+        }
     if (isRunning)
         {
         errorStr = "ETHER_CAP not stopped.";
diff --git a/projects/stargazer/plugins/capture/ipq_linux/ipq_cap.cpp b/projects/stargazer/plugins/capture/ipq_linux/ipq_cap.cpp
index 7f60c7eb..84939b4e 100644
--- a/projects/stargazer/plugins/capture/ipq_linux/ipq_cap.cpp
+++ b/projects/stargazer/plugins/capture/ipq_linux/ipq_cap.cpp
@@ -95,7 +95,8 @@ for (int i = 0; i < 25; i++)
     {
     if (!isRunning)
         break;
-    usleep(200000);
+    struct timespec ts = {0, 200000000};
+    nanosleep(&ts, NULL);
     }
 //after 5 seconds waiting thread still running. now killing it
 if (isRunning)
@@ -107,7 +108,8 @@ if (isRunning)
         }
     for (int i = 0; i < 25 && isRunning; ++i)
         {
-        usleep(200000);
+        struct timespec ts = {0, 200000000};
+        nanosleep(&ts, NULL);
         }
     if (isRunning)
         {
diff --git a/projects/stargazer/plugins/configuration/rpcconfig/rpcconfig.cpp b/projects/stargazer/plugins/configuration/rpcconfig/rpcconfig.cpp
index b82293d4..837c8649 100644
--- a/projects/stargazer/plugins/configuration/rpcconfig/rpcconfig.cpp
+++ b/projects/stargazer/plugins/configuration/rpcconfig/rpcconfig.cpp
@@ -1,5 +1,3 @@
-#include <unistd.h> // TODO: usleep
-
 #include <cstdlib>
 #include <csignal>
 
@@ -140,7 +138,10 @@ int RPC_CONFIG::Stop()
 {
 running = false;
 for (int i = 0; i < 5 && !stopped; ++i)
-    usleep(200000);
+    {
+    struct timespec ts = {0, 200000000};
+    nanosleep(&ts, NULL);
+    }
 //rpcServer->terminate();
 if (!stopped)
     {
@@ -150,7 +151,10 @@ if (!stopped)
         printfd(__FILE__, "Failed to kill thread\n");
         }
     for (int i = 0; i < 25 && !stopped; ++i)
-        usleep(200000);
+        {
+        struct timespec ts = {0, 200000000};
+        nanosleep(&ts, NULL);
+        }
     if (!stopped)
         {
         printfd(__FILE__, "Failed to stop RPC thread\n");
diff --git a/projects/stargazer/plugins/configuration/sgconfig/rsconf.cpp b/projects/stargazer/plugins/configuration/sgconfig/rsconf.cpp
index b4cbf544..421fc7bb 100644
--- a/projects/stargazer/plugins/configuration/sgconfig/rsconf.cpp
+++ b/projects/stargazer/plugins/configuration/sgconfig/rsconf.cpp
@@ -26,7 +26,7 @@
 *
 *******************************************************************/
 
-#include <unistd.h> // cloase, usleep
+#include <unistd.h> // close
 
 #include <cerrno>
 #include <csignal>
diff --git a/projects/stargazer/plugins/configuration/sgconfig/stgconfig.cpp b/projects/stargazer/plugins/configuration/sgconfig/stgconfig.cpp
index f36792ae..0a65c0d9 100644
--- a/projects/stargazer/plugins/configuration/sgconfig/stgconfig.cpp
+++ b/projects/stargazer/plugins/configuration/sgconfig/stgconfig.cpp
@@ -123,7 +123,8 @@ for (i = 0; i < 25; i++)
     if (!isRunning)
         break;
 
-    usleep(200000);
+    struct timespec ts = {0, 200000000};
+    nanosleep(&ts, NULL);
     }
 
 //after 5 seconds waiting thread still running. now killing it
diff --git a/projects/stargazer/plugins/other/radius/radius.cpp b/projects/stargazer/plugins/other/radius/radius.cpp
index 62c9305c..6a02ce4f 100644
--- a/projects/stargazer/plugins/other/radius/radius.cpp
+++ b/projects/stargazer/plugins/other/radius/radius.cpp
@@ -236,7 +236,8 @@ if (isRunning)
     //5 seconds to thread stops itself
     for (int i = 0; i < 25 && isRunning; i++)
         {
-        usleep(200000);
+        struct timespec ts = {0, 200000000};
+        nanosleep(&ts, NULL);
         }
 
     //after 5 seconds waiting thread still running. now killing it
diff --git a/projects/stargazer/plugins/other/rscript/rscript.cpp b/projects/stargazer/plugins/other/rscript/rscript.cpp
index 6fde814a..c1d66051 100644
--- a/projects/stargazer/plugins/other/rscript/rscript.cpp
+++ b/projects/stargazer/plugins/other/rscript/rscript.cpp
@@ -282,7 +282,8 @@ if (isRunning)
     //5 seconds to thread stops itself
     for (int i = 0; i < 25 && isRunning; i++)
         {
-        usleep(200000);
+        struct timespec ts = {0, 200000000};
+        nanosleep(&ts, NULL);
         }
 
     //after 5 seconds waiting thread still running. now killing it
diff --git a/stglibs/ia.lib/ia.cpp b/stglibs/ia.lib/ia.cpp
index 9883dbbb..abecef09 100644
--- a/stglibs/ia.lib/ia.cpp
+++ b/stglibs/ia.lib/ia.cpp
@@ -69,7 +69,7 @@ static int a = 0;
 
 if (a == 0)
     {
-    usleep(50000);
+    Sleep(50);
     a = 1;
     }
 
@@ -82,7 +82,9 @@ return NULL;
 //---------------------------------------------------------------------------
 void Sleep(int ms)
 {
-usleep(ms * 1000);
+long long res = ms * 1000000;
+struct timespec ts = {res / 1000000000, res % 1000000000};
+nanosleep(&ts, NULL);
 }
 //---------------------------------------------------------------------------
 long GetTickCount()
diff --git a/stglibs/pinger.lib/pinger.cpp b/stglibs/pinger.lib/pinger.cpp
index f3686806..df3793ab 100644
--- a/stglibs/pinger.lib/pinger.cpp
+++ b/stglibs/pinger.lib/pinger.cpp
@@ -93,7 +93,8 @@ int STG_PINGER::Stop()
             if (!isRunningRecver)
                 break;
 
-            usleep(200000);
+            struct timespec ts = {0, 200000000};
+            nanosleep(&ts, NULL);
             }
 
         //after 5 seconds waiting thread still running. now killing it
@@ -115,7 +116,8 @@ int STG_PINGER::Stop()
             if (!isRunningSender)
                 break;
 
-            usleep(200000);
+            struct timespec ts = {0, 200000000};
+            nanosleep(&ts, NULL);
             }
 
         //after 5 seconds waiting thread still running. now killing it
@@ -315,7 +317,8 @@ void * STG_PINGER::RunSendPing(void * d)
             #else
             currTime = time(NULL);
             #endif
-            usleep(20000);
+            struct timespec ts = {0, 20000000};
+            nanosleep(&ts, NULL);
             }
         }