- use InterlockedExchange for setting shutdown flag
authorGed Murphy <gedmurphy@reactos.org>
Fri, 18 Nov 2005 22:55:30 +0000 (22:55 +0000)
committerGed Murphy <gedmurphy@reactos.org>
Fri, 18 Nov 2005 22:55:30 +0000 (22:55 +0000)
- fix bug in select() which was stopping the service from shutting down
- add more event logs

svn path=/trunk/; revision=19332

reactos/services/tcpsvcs/echo.c
reactos/services/tcpsvcs/skelserver.c
reactos/services/tcpsvcs/tcpsvcs.c

index 31efb45..29eb7ec 100644 (file)
@@ -88,5 +88,8 @@ BOOL EchoIncomingPackets(SOCKET Sock)
     if (! bShutDown)
         LogEvent(_T("Echo: Connection closed by peer.\n"), 0, FALSE);
 
     if (! bShutDown)
         LogEvent(_T("Echo: Connection closed by peer.\n"), 0, FALSE);
 
+       if (bShutDown)
+               LogEvent(_T("Echo: thread recieved shutdown signal\n"), 0, FALSE);
+
     return TRUE;
 }
     return TRUE;
 }
index 822bd0d..99a6aaa 100644 (file)
@@ -30,7 +30,7 @@ DWORD WINAPI StartServer(LPVOID lpParam)
     ListeningSocket = SetUpListener(HostIP, htons(pServices->Port));
     if (ListeningSocket == INVALID_SOCKET)
     {
     ListeningSocket = SetUpListener(HostIP, htons(pServices->Port));
     if (ListeningSocket == INVALID_SOCKET)
     {
-               LogEvent("Socket error when setting up listener", 0, TRUE);
+               LogEvent("Socket error when setting up listener\n", 0, TRUE);
         return 3;
     }
 
         return 3;
     }
 
@@ -94,8 +94,6 @@ VOID AcceptConnections(SOCKET ListeningSocket,
     INT TimeOut = 2000; // 2 seconds
 
 //DebugBreak();
     INT TimeOut = 2000; // 2 seconds
 
 //DebugBreak();
-    /* monitor for incomming connections */
-    FD_ZERO(&ReadFDS);
 
     /* set timeout values */
     TimeVal.tv_sec  = TimeOut / 1000;
 
     /* set timeout values */
     TimeVal.tv_sec  = TimeOut / 1000;
@@ -103,45 +101,52 @@ VOID AcceptConnections(SOCKET ListeningSocket,
 
     while (! bShutDown) // (i<MAX_CLIENTS && !bShutDown)
     {
 
     while (! bShutDown) // (i<MAX_CLIENTS && !bShutDown)
     {
+               INT SelRet = 0;
+
+               FD_ZERO(&ReadFDS);
                FD_SET(ListeningSocket, &ReadFDS);
                FD_SET(ListeningSocket, &ReadFDS);
-        if (select(0, &ReadFDS, NULL, NULL, &TimeVal) == SOCKET_ERROR)
+
+               SelRet = select(0, &ReadFDS, NULL, NULL, &TimeVal);
+        if (SelRet == SOCKET_ERROR)
         {
             LogEvent(_T("select failed\n"), 0, TRUE);
             return;
         }
         {
             LogEvent(_T("select failed\n"), 0, TRUE);
             return;
         }
-
-        /* don't call FD_ISSET if bShutDown flag is set */
-        if ((! bShutDown) || (FD_ISSET(ListeningSocket, &ReadFDS)))
-        {
-            Sock = accept(ListeningSocket, (SOCKADDR*)&Client, &nAddrSize);
-            if (Sock != INVALID_SOCKET)
-            {
-                _stprintf(buf, _T("Accepted connection to %s server from %s:%d\n"),
-                    Name, inet_ntoa(Client.sin_addr), ntohs(Client.sin_port));
-                LogEvent(buf, 0, FALSE);
-                _stprintf(buf, _T("Creating new thread for %s\n"), Name);
-                LogEvent(buf, 0, FALSE);
-
-                hThread = CreateThread(0, 0, Service, (void*)Sock, 0, &ThreadID);
-
-                /* Check the return value for success. */
-                if (hThread == NULL)
-                {
-                    _stprintf(buf, _T("Failed to start worker thread for "
-                        "the %s server....\n"), Name);
-                    LogEvent(buf, 0, TRUE);
-                }
-
-                WaitForSingleObject(hThread, INFINITE);
-                
-                CloseHandle(hThread);
-            }
-            else
-            {
-                LogEvent(_T("accept failed\n"), 0, TRUE);
-                return;
-            }
-        }
+               else if (SelRet > 0)
+               {
+                       /* don't call FD_ISSET if bShutDown flag is set */
+                       if ((! bShutDown) || (FD_ISSET(ListeningSocket, &ReadFDS)))
+                       {
+                               Sock = accept(ListeningSocket, (SOCKADDR*)&Client, &nAddrSize);
+                               if (Sock != INVALID_SOCKET)
+                               {
+                                       _stprintf(buf, _T("Accepted connection to %s server from %s:%d\n"),
+                                               Name, inet_ntoa(Client.sin_addr), ntohs(Client.sin_port));
+                                       LogEvent(buf, 0, FALSE);
+                                       _stprintf(buf, _T("Creating new thread for %s\n"), Name);
+                                       LogEvent(buf, 0, FALSE);
+
+                                       hThread = CreateThread(0, 0, Service, (void*)Sock, 0, &ThreadID);
+
+                                       /* Check the return value for success. */
+                                       if (hThread == NULL)
+                                       {
+                                               _stprintf(buf, _T("Failed to start worker thread for "
+                                                       "the %s server....\n"), Name);
+                                               LogEvent(buf, 0, TRUE);
+                                       }
+
+                                       WaitForSingleObject(hThread, INFINITE);
+                       
+                                       CloseHandle(hThread);
+                               }
+                               else
+                               {
+                                       LogEvent(_T("accept failed\n"), 0, TRUE);
+                                       return;
+                               }
+                       }
+               }
     }
 }
 
     }
 }
 
index 1787f0a..188fbfa 100644 (file)
@@ -75,24 +75,25 @@ main(int argc, char *argv[])
 VOID WINAPI
 ServiceMain(DWORD argc, LPTSTR argv[])
 {
 VOID WINAPI
 ServiceMain(DWORD argc, LPTSTR argv[])
 {
-    TCHAR LogFilePath[MAX_PATH];
+       TCHAR LogFilePath[MAX_PATH];
 
     if(! GetSystemDirectory(LogFilePath, MAX_PATH))
         return;
 
     _tcscat(LogFilePath, LogFileName);
 
 
     if(! GetSystemDirectory(LogFilePath, MAX_PATH))
         return;
 
     _tcscat(LogFilePath, LogFileName);
 
-       hLogFile = fopen(LogFilePath, _T("w"));
+       hLogFile = fopen(LogFilePath, _T("a+"));
     if (hLogFile == NULL)
     {
         TCHAR buf[50];
 
     if (hLogFile == NULL)
     {
         TCHAR buf[50];
 
-        _stprintf(buf, _T("Could not open log file: %s"), LogFilePath);
+        _stprintf(buf, _T("Could not open log file: %s\n"), LogFilePath);
         MessageBox(NULL, buf, NULL, MB_OK);
         return;
     }
 
         MessageBox(NULL, buf, NULL, MB_OK);
         return;
     }
 
-    LogEvent(_T("Entering ServiceMain"), 0, FALSE);
+
+    LogEvent(_T("Entering ServiceMain\n"), 0, FALSE);
 
     hServStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
     hServStatus.dwCurrentState = SERVICE_START_PENDING;
 
     hServStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
     hServStatus.dwCurrentState = SERVICE_START_PENDING;
@@ -107,9 +108,9 @@ ServiceMain(DWORD argc, LPTSTR argv[])
     if (hSStat == 0)
         LogEvent(_T("Failed to register service\n"), -1, TRUE);
 
     if (hSStat == 0)
         LogEvent(_T("Failed to register service\n"), -1, TRUE);
 
-       LogEvent(_T("Control handler registered successfully"), 0, FALSE);
+       LogEvent(_T("Control handler registered successfully\n"), 0, FALSE);
        SetServiceStatus (hSStat, &hServStatus);
        SetServiceStatus (hSStat, &hServStatus);
-       LogEvent(_T("Service status set to SERVICE_START_PENDING"), 0, FALSE);
+       LogEvent(_T("Service status set to SERVICE_START_PENDING\n"), 0, FALSE);
 
     if (CreateServers() != 0)
     {
 
     if (CreateServers() != 0)
     {
@@ -119,13 +120,15 @@ ServiceMain(DWORD argc, LPTSTR argv[])
         return;
     }
 
         return;
     }
 
-       LogEvent(_T("Service threads shut down. Set SERVICE_STOPPED status"), 0, FALSE);
+       LogEvent(_T("Service threads shut down. Set SERVICE_STOPPED status\n"), 0, FALSE);
        /*  We will only return here when the ServiceSpecific function
                completes, indicating system shutdown. */
        UpdateStatus (SERVICE_STOPPED, 0);
        /*  We will only return here when the ServiceSpecific function
                completes, indicating system shutdown. */
        UpdateStatus (SERVICE_STOPPED, 0);
-       LogEvent(_T("Service status set to SERVICE_STOPPED"), 0, FALSE);
-       LogEvent(_T("Leaving ServiceMain"), 0, FALSE);
-       fclose(hLogFile);  /*  Clean up everything, in general */
+       LogEvent(_T("Service status set to SERVICE_STOPPED\n"), 0, FALSE);
+       LogEvent(_T("Leaving ServiceMain\n"), 0, FALSE);
+       
+       fclose(hLogFile);
+
        return;
 
 }
        return;
 
 }
@@ -133,18 +136,23 @@ ServiceMain(DWORD argc, LPTSTR argv[])
 VOID WINAPI
 ServerCtrlHandler(DWORD Control)
 {
 VOID WINAPI
 ServerCtrlHandler(DWORD Control)
 {
+       TCHAR buf[256];
+
     switch (Control)
     {
         case SERVICE_CONTROL_SHUTDOWN: /* fall through */
         case SERVICE_CONTROL_STOP:
     switch (Control)
     {
         case SERVICE_CONTROL_SHUTDOWN: /* fall through */
         case SERVICE_CONTROL_STOP:
-            bShutDown = TRUE;
+                       LogEvent(_T("stopping service\n"), 0, FALSE);
+            InterlockedExchange((LONG *)&bShutDown, TRUE);
             UpdateStatus(SERVICE_STOP_PENDING, -1);
             break;
             UpdateStatus(SERVICE_STOP_PENDING, -1);
             break;
-        case SERVICE_CONTROL_PAUSE:
-            bPause = TRUE;
+        case SERVICE_CONTROL_PAUSE: /* not yet implemented */
+                       LogEvent(_T("pausing service\n"), 0, FALSE);
+            InterlockedExchange((LONG *)&bPause, TRUE);
             break;
         case SERVICE_CONTROL_CONTINUE:
             break;
         case SERVICE_CONTROL_CONTINUE:
-            bPause = FALSE;
+                       LogEvent(_T("continuing service\n"), 0, FALSE);
+            InterlockedExchange((LONG *)&bPause, FALSE);
             break;
         case SERVICE_CONTROL_INTERROGATE:
             break;
             break;
         case SERVICE_CONTROL_INTERROGATE:
             break;
@@ -169,7 +177,7 @@ void UpdateStatus (int NewStatus, int Check)
         hServStatus.dwCurrentState = NewStatus;
 
        if (! SetServiceStatus (hSStat, &hServStatus))
         hServStatus.dwCurrentState = NewStatus;
 
        if (! SetServiceStatus (hSStat, &hServStatus))
-               LogEvent(_T("Cannot set service status"), -1, TRUE);
+               LogEvent(_T("Cannot set service status\n"), -1, TRUE);
 
        return;
 }
 
        return;
 }
@@ -180,7 +188,7 @@ CreateServers()
     DWORD dwThreadId[NUM_SERVICES];
     HANDLE hThread[NUM_SERVICES];
        WSADATA wsaData;
     DWORD dwThreadId[NUM_SERVICES];
     HANDLE hThread[NUM_SERVICES];
        WSADATA wsaData;
-       TCHAR buf[256]; // temp for holding LogEvent text
+       TCHAR buf[256];
     INT i;
     DWORD RetVal;
 
     INT i;
     DWORD RetVal;
 
@@ -251,6 +259,8 @@ LogEvent (LPCTSTR UserMessage, DWORD ExitCode, BOOL PrintErrorMsg)
        LPTSTR lpvSysMsg;
        TCHAR MessageBuffer[512];
 
        LPTSTR lpvSysMsg;
        TCHAR MessageBuffer[512];
 
+
+
        if (PrintErrorMsg)
     {
                eMsgLen = FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER |
        if (PrintErrorMsg)
     {
                eMsgLen = FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER |
@@ -258,13 +268,13 @@ LogEvent (LPCTSTR UserMessage, DWORD ExitCode, BOOL PrintErrorMsg)
                        ErrNum, MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
                        (LPTSTR)&lpvSysMsg, 0, NULL);
 
                        ErrNum, MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
                        (LPTSTR)&lpvSysMsg, 0, NULL);
 
-               _stprintf(MessageBuffer, _T("\n%s %s ErrNum = %lu. ExitCode = %lu."),
+               _stprintf(MessageBuffer, _T("%s %s ErrNum = %lu. ExitCode = %lu."),
                        UserMessage, lpvSysMsg, ErrNum, ExitCode);
                HeapFree(GetProcessHeap (), 0, lpvSysMsg);
        }
     else
     {
                        UserMessage, lpvSysMsg, ErrNum, ExitCode);
                HeapFree(GetProcessHeap (), 0, lpvSysMsg);
        }
     else
     {
-               _stprintf(MessageBuffer, _T("\n%s"), UserMessage);
+               _stprintf(MessageBuffer, _T("%s"), UserMessage);
        }
 
        fputs (MessageBuffer, hLogFile);
        }
 
        fputs (MessageBuffer, hLogFile);