/* $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"
void (*stopfunc)(int);
-/* in nt_err.c */
-char *GetLastErrorString( void );
+static char *GetLastErrorString( void );
-int srv_install(LPCTSTR lpszServiceName, LPCTSTR lpszBinaryPathName)
+int srv_install(LPCTSTR lpszServiceName, LPCTSTR lpszDisplayName,
+ LPCTSTR lpszBinaryPathName, BOOL auto_start)
{
HKEY hKey;
DWORD dwValue, dwDisposition;
if ((schService = CreateService(
schSCManager,
lpszServiceName,
- TEXT("OpenLDAP Directory Service"),
- SC_MANAGER_CREATE_SERVICE,
+ lpszDisplayName,
+ SERVICE_ALL_ACCESS,
SERVICE_WIN32_OWN_PROCESS,
- SERVICE_DEMAND_START,
+ auto_start ? SERVICE_AUTO_START : SERVICE_DEMAND_START,
SERVICE_ERROR_NORMAL,
lpszBinaryPathName,
NULL, NULL, NULL, NULL, NULL)) != NULL)
}
+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 )
{
DWORD valLen = sizeof( vValue );
if ( svc != NULL )
- sprintf ( path, "SOFTWARE\\OpenLDAP\\%s\\Parameters", svc );
+ sprintf ( path, "SOFTWARE\\%s", svc );
else
strcpy (path, "SOFTWARE\\OpenLDAP\\Parameters" );
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 );
}
}
+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