]> git.sur5r.net Git - openldap/blobdiff - libraries/liblutil/ntservice.c
Fix up abandon merge. Hallvard will holler if I get this wrong. :-)
[openldap] / libraries / liblutil / ntservice.c
index 73acdd37544d06a9e3eac216a942b795208a5378..4d6504211c4177aa9db279ad5692964818f3bcbe 100644 (file)
@@ -1,9 +1,14 @@
 /* $OpenLDAP$ */
 /*
- * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
+ * Copyright 1998-2002 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
 
 #include <ldap.h>
 
+/*
+ * The whole debug implementation is a bit of a hack.
+ * We have to define this LDAP_SLAPD_V macro here since the slap.h
+ * header file isn't included (and really shouldn't be).
+ * TODO: re-implement debug functions so that the debug level can
+ * be passed around instead of having to count on the existence of
+ * ldap_debug, slap_debug, etc.
+ */
 #define ldap_debug slap_debug
-extern int slap_debug;
+LDAP_SLAPD_V (int) slap_debug;
 
 #include "ldap_log.h"
 #include "ldap_pvt_thread.h"
@@ -43,7 +56,7 @@ ldap_pvt_thread_t             start_status_tid,       stop_status_tid;
 
 void (*stopfunc)(int);
 
-char *GetLastErrorString( void );
+static char *GetLastErrorString( void );
 
 int srv_install(LPCTSTR lpszServiceName, LPCTSTR lpszDisplayName,
                LPCTSTR lpszBinaryPathName, BOOL auto_start)
@@ -145,6 +158,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 )
 {
@@ -332,9 +396,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 );
@@ -428,7 +492,7 @@ void ReportSlapdShutdownComplete(  )
        }
 }
 
-char *GetErrorString( int err )
+static char *GetErrorString( int err )
 {
        static char msgBuf[1024];
 
@@ -441,7 +505,7 @@ char *GetErrorString( int err )
        return msgBuf;
 }
 
-char *GetLastErrorString( void )
+static char *GetLastErrorString( void )
 {
        return GetErrorString( GetLastError() );
 }