]> git.sur5r.net Git - openldap/blobdiff - libraries/liblutil/ntservice.c
Really fix the fix to the past commit
[openldap] / libraries / liblutil / ntservice.c
index ce036b4944e110bd6cffb25ab52b47c1ae0cee31..bc4ae422dc3642b232c7736ffcbadfe210056d30 100644 (file)
@@ -1,9 +1,14 @@
 /* $OpenLDAP$ */
 /*
- * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
+ * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
  */
-/* ntservice.c */
+
+/*
+ * NT Service manager utilities for OpenLDAP services
+ *     these should NOT be slapd specific, but are
+ */
+
 #include "portable.h"
 
 #ifdef HAVE_NT_SERVICE_MANAGER
@@ -43,8 +48,7 @@ ldap_pvt_thread_t             start_status_tid,       stop_status_tid;
 
 void (*stopfunc)(int);
 
-/* in nt_err.c */
-char *GetLastErrorString( void );
+static char *GetLastErrorString( void );
 
 int srv_install(LPCTSTR lpszServiceName, LPCTSTR lpszDisplayName,
                LPCTSTR lpszBinaryPathName, BOOL auto_start)
@@ -146,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 )
 {
@@ -333,9 +388,9 @@ void LogSlapdStartedEvent( char *svc, int slap_debug, char *configfile, char *ur
 
        Inserts[i] = (char *)malloc( 20 );
        itoa( slap_debug, Inserts[i++], 10 );
-       Inserts[i++] = ldap_pvt_strdup( configfile );
-       Inserts[i++] = ldap_pvt_strdup( urls ? urls : "ldap:///" );
-       Inserts[i++] = ldap_pvt_strdup( is_NT_Service ? "svc" : "cmd" );
+       Inserts[i++] = strdup( configfile );
+       Inserts[i++] = strdup( urls ? urls : "ldap:///" );
+       Inserts[i++] = strdup( is_NT_Service ? "svc" : "cmd" );
 
        ReportEvent( hEventLog, EVENTLOG_INFORMATION_TYPE, 0,
                MSG_SLAPD_STARTED, NULL, i, 0, (LPCSTR *) Inserts, NULL );
@@ -429,4 +484,21 @@ void ReportSlapdShutdownComplete(  )
        }
 }
 
+static char *GetErrorString( int err )
+{
+       static char msgBuf[1024];
+
+       FormatMessage(
+               FORMAT_MESSAGE_FROM_SYSTEM,
+               NULL,
+               err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
+               msgBuf, 1024, NULL );
+
+       return msgBuf;
+}
+
+static char *GetLastErrorString( void )
+{
+       return GetErrorString( GetLastError() );
+}
 #endif