You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@httpd.apache.org by Paul Sutton <pa...@ukweb.com> on 1997/10/11 17:43:25 UTC

Win32 service errors

Umm, I cannot recreated PR#1207 (Apache still running after the service
shows it as stopped). But there is very little error checking (i.e. none)
when Apache is run to install or remove a the service.

This patch adds some (pretty basic) error checking, and reports errors on
stderr. That's ok since the install/uninstall functions are called from
the command line (apache -i or apache -u).

//pcs

Index: service.c
===================================================================
RCS file: /export/home/cvs/apachen/src/os/win32/service.c,v
retrieving revision 1.3
diff -u -r1.3 service.c
--- service.c	1997/08/13 08:37:35	1.3
+++ service.c	1997/10/11 15:19:11
@@ -10,7 +10,40 @@
 #include "conf.h"
 #include "multithread.h"
 #include "service.h"
-
+
+/*
+ * ReportWin32Error() - map the last Win32 error onto a string
+ *
+ * This function can be called after a Win32 function has returned an error
+ * status. This function writes an error line to the file pointed to by
+ * fp (which could be stdout or stderr) consisting of the passed-in prefix
+ * string, a colon, and the system error text corresponding to the last
+ * Win32 function error.
+ *
+ * If the file pointer argument is NULL, nothing is logged.
+ */
+
+void ReportWin32Error(FILE *fp, char *prefix) {
+    LPVOID lpMsgBuf;
+
+    if (!fp) return;
+
+    FormatMessage( 
+        FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
+        NULL,
+        GetLastError(),
+        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
+        (LPTSTR) &lpMsgBuf,
+        0,
+        NULL 
+    );
+
+    fprintf(fp, "%s: %s\n", prefix, lpMsgBuf);
+
+    // Free the buffer.
+    LocalFree( lpMsgBuf );
+}
+
 
 static struct
 {
@@ -116,8 +149,7 @@
     return;
 }
 
-void
-service_set_status(int status)
+void service_set_status(int status)
 {
     ReportStatusToSCMgr(status, NO_ERROR, 3000);
 }
@@ -166,7 +198,6 @@
     }
 
     ReportStatusToSCMgr(state, NO_ERROR, 0);
-
 }
 
 
@@ -219,7 +250,7 @@
 
     TCHAR szPath[512];
 
-    if ( GetModuleFileName( NULL, szPath, 512 ) == 0 )
+    if (GetModuleFileName( NULL, szPath, 512 ) == 0)
     {
         exit(1);
         return;
@@ -230,8 +261,10 @@
                         NULL,                   // database (NULL == default)
                         SC_MANAGER_ALL_ACCESS   // access required
                         );
-    if ( schSCManager )
-    {
+   if (!schSCManager) {
+       ReportWin32Error(stderr, "Cannot open service manager");
+    }
+    else {
         schService = CreateService(
             schSCManager,               // SCManager database
             globdat.name,        // name of service
@@ -247,12 +280,11 @@
             NULL,                       // LocalSystem account
             NULL);                      // no password
 
-        if ( schService )
-        {
+        if (schService) {
             CloseServiceHandle(schService);
         }
-        else
-        {
+        else {
+            ReportWin32Error(stderr, "Cannot create service");
         }
 
         CloseServiceHandle(schSCManager);
@@ -273,25 +305,28 @@
                         NULL,                   // database (NULL == default)
                         SC_MANAGER_ALL_ACCESS   // access required
                         );
-    if ( schSCManager )
-    {
+    if (!schSCManager) {
+        ReportWin32Error(stderr, "Cannot open service manager");
+    }
+    else {
         schService = OpenService(schSCManager, globdat.name, SERVICE_ALL_ACCESS);
-
-        /* try to stop the service */
-        if(ControlService(schService, SERVICE_CONTROL_STOP, &globdat.ssStatus))
-        {
-            Sleep(1000);
-            while(QueryServiceStatus(schService, &globdat.ssStatus))
-            {
-                if(globdat.ssStatus.dwCurrentState == SERVICE_STOP_PENDING)
-                    Sleep(1000);
-                else
-                    break;
+
+        if (schService == NULL) {
+            /* Could not open the service */
+            ReportWin32Error(stderr, "Error accessing service");
+        }
+        else {
+            /* try to stop the service */
+            if (ControlService(schService, SERVICE_CONTROL_STOP, &globdat.ssStatus)) {
+                Sleep(1000);
+                while(QueryServiceStatus(schService, &globdat.ssStatus)) {
+                    if(globdat.ssStatus.dwCurrentState == SERVICE_STOP_PENDING)
+                        Sleep(1000);
+                    else
+                        break;
+                }
             }
-        }
 
-        if (schService)
-        {
             // now remove the service
             DeleteService(schService);
             CloseServiceHandle(schService);
@@ -301,7 +336,7 @@
     }
 
 }
-
+
 #endif /* WIN32 */