]> git.sur5r.net Git - openldap/commitdiff
Check if process is installed/running as service on NT beofre trying t
authorMark Valence <mrv@openldap.org>
Thu, 15 Jun 2000 02:21:01 +0000 (02:21 +0000)
committerMark Valence <mrv@openldap.org>
Thu, 15 Jun 2000 02:21:01 +0000 (02:21 +0000)
o start it as a service (gets around an annoying pause when starting u
p as a non-service).

libraries/liblutil/ntservice.c
servers/slapd/nt_svc.c

index 657974ae7c9b0152e0f250c2d2e2b723b416c83e..bc4ae422dc3642b232c7736ffcbadfe210056d30 100644 (file)
@@ -150,6 +150,57 @@ int srv_remove(LPCTSTR lpszServiceName, LPCTSTR lpszBinaryPathName)
 }
 
 
+DWORD
+svc_installed (LPTSTR lpszServiceName, LPTSTR lpszBinaryPathName)
+{
+       char buf[256];
+       HKEY key;
+       DWORD rc;
+       DWORD type;
+       long len;
+
+       strcpy(buf, TEXT("SYSTEM\\CurrentControlSet\\Services\\"));
+       strcat(buf, lpszServiceName);
+       if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, buf, 0, KEY_QUERY_VALUE, &key) != ERROR_SUCCESS)
+               return(-1);
+
+       rc = 0;
+       if (lpszBinaryPathName) {
+               len = sizeof(buf);
+               if (RegQueryValueEx(key, "ImagePath", NULL, &type, buf, &len) == ERROR_SUCCESS) {
+                       if (strcmp(lpszBinaryPathName, buf))
+                               rc = -1;
+               }
+       }
+       RegCloseKey(key);
+       return(rc);
+}
+
+
+DWORD
+svc_running (LPTSTR lpszServiceName)
+{
+       SC_HANDLE service;
+       SC_HANDLE scm;
+       DWORD rc;
+       SERVICE_STATUS ss;
+
+       if (!(scm = OpenSCManager(NULL, NULL, GENERIC_READ)))
+               return(GetLastError());
+
+       rc = 1;
+       service = OpenService(scm, lpszServiceName, SERVICE_QUERY_STATUS);
+       if (service) {
+               if (!QueryServiceStatus(service, &ss))
+                       rc = GetLastError();
+               else if (ss.dwCurrentState != SERVICE_STOPPED)
+                       rc = 0;
+               CloseServiceHandle(service);
+       }
+       CloseServiceHandle(scm);
+       return(rc);
+}
+
 
 static void *start_status_routine( void *ptr )
 {
index 5bd46f9636430d6679947360b556faa312c8b417..d81bc68a47ac585054e19388fb77c19b1ffc4a81 100644 (file)
@@ -18,6 +18,8 @@ void WINAPI ServiceMain( DWORD argc, LPTSTR *argv );
 int srv_install( char* service, char * displayName, char* filename,
                 BOOL auto_start );
 int srv_remove ( char* service, char* filename );
+DWORD svc_installed (LPTSTR lpszServiceName, LPTSTR lpszBinaryPathName);
+DWORD svc_running (LPTSTR lpszServiceName);
 
 int main( int argc, LPTSTR *argv )
 {
@@ -99,7 +101,9 @@ int main( int argc, LPTSTR *argv )
        }
 
        puts( "starting slapd..." );
-       if ( !StartServiceCtrlDispatcher(DispatchTable) )
+       if (svc_installed(SERVICE_NAME, NULL) != 0
+               || svc_running(SERVICE_NAME) == 1
+               || StartServiceCtrlDispatcher(DispatchTable) != 0 )
        {
                is_NT_Service = 0;
                ServiceMain( argc, argv );