1999-09-09 03:06:24 +08:00
|
|
|
/* $OpenLDAP$ */
|
2003-11-26 07:17:08 +08:00
|
|
|
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
|
|
|
|
*
|
2016-01-30 03:32:05 +08:00
|
|
|
* Copyright 1998-2016 The OpenLDAP Foundation.
|
2003-11-26 07:17:08 +08:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted only as authorized by the OpenLDAP
|
|
|
|
* Public License.
|
|
|
|
*
|
|
|
|
* A copy of this license is available in the file LICENSE in the
|
|
|
|
* top-level directory of the distribution or, alternatively, at
|
|
|
|
* <http://www.OpenLDAP.org/license.html>.
|
1999-08-18 03:00:59 +08:00
|
|
|
*/
|
2000-06-04 01:24:21 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* NT Service manager utilities for OpenLDAP services
|
|
|
|
*/
|
|
|
|
|
1999-06-17 01:53:17 +08:00
|
|
|
#include "portable.h"
|
1999-06-22 06:59:23 +08:00
|
|
|
|
1999-11-02 05:25:22 +08:00
|
|
|
#ifdef HAVE_NT_SERVICE_MANAGER
|
|
|
|
|
1999-06-22 06:59:23 +08:00
|
|
|
#include <ac/stdlib.h>
|
|
|
|
#include <ac/string.h>
|
|
|
|
|
1999-06-17 01:53:17 +08:00
|
|
|
#include <stdio.h>
|
1999-06-22 06:59:23 +08:00
|
|
|
|
1999-06-17 01:53:17 +08:00
|
|
|
#include <windows.h>
|
1999-06-22 06:59:23 +08:00
|
|
|
#include <winsvc.h>
|
|
|
|
|
1999-08-04 02:14:24 +08:00
|
|
|
#include <ldap.h>
|
1999-06-22 06:59:23 +08:00
|
|
|
|
1999-06-17 01:53:17 +08:00
|
|
|
#include "ldap_pvt_thread.h"
|
1999-06-22 06:59:23 +08:00
|
|
|
|
1999-06-17 11:54:25 +08:00
|
|
|
#include "ldap_defaults.h"
|
1999-06-17 01:53:17 +08:00
|
|
|
|
|
|
|
#include "slapdmsg.h"
|
|
|
|
|
|
|
|
#define SCM_NOTIFICATION_INTERVAL 5000
|
|
|
|
#define THIRTY_SECONDS (30 * 1000)
|
|
|
|
|
2003-03-03 14:43:12 +08:00
|
|
|
int is_NT_Service; /* is this is an NT service? */
|
1999-06-17 01:53:17 +08:00
|
|
|
|
2003-03-03 19:46:05 +08:00
|
|
|
SERVICE_STATUS lutil_ServiceStatus;
|
|
|
|
SERVICE_STATUS_HANDLE hlutil_ServiceStatus;
|
1999-06-17 01:53:17 +08:00
|
|
|
|
|
|
|
ldap_pvt_thread_cond_t started_event, stopped_event;
|
|
|
|
ldap_pvt_thread_t start_status_tid, stop_status_tid;
|
|
|
|
|
|
|
|
void (*stopfunc)(int);
|
|
|
|
|
2000-05-13 08:48:17 +08:00
|
|
|
static char *GetLastErrorString( void );
|
1999-06-17 01:53:17 +08:00
|
|
|
|
2003-03-03 19:46:05 +08:00
|
|
|
int lutil_srv_install(LPCTSTR lpszServiceName, LPCTSTR lpszDisplayName,
|
|
|
|
LPCTSTR lpszBinaryPathName, int auto_start)
|
1999-06-17 01:53:17 +08:00
|
|
|
{
|
|
|
|
HKEY hKey;
|
|
|
|
DWORD dwValue, dwDisposition;
|
|
|
|
SC_HANDLE schSCManager, schService;
|
2003-03-03 15:30:12 +08:00
|
|
|
char *sp = strchr( lpszBinaryPathName, ' ');
|
1999-06-17 01:53:17 +08:00
|
|
|
|
2003-03-03 15:30:12 +08:00
|
|
|
if ( sp ) *sp = '\0';
|
1999-06-17 01:53:17 +08:00
|
|
|
fprintf( stderr, "The install path is %s.\n", lpszBinaryPathName );
|
2003-03-03 15:30:12 +08:00
|
|
|
if ( sp ) *sp = ' ';
|
1999-06-17 01:53:17 +08:00
|
|
|
if ((schSCManager = OpenSCManager( NULL, NULL, SC_MANAGER_CONNECT|SC_MANAGER_CREATE_SERVICE ) ) != NULL )
|
|
|
|
{
|
|
|
|
if ((schService = CreateService(
|
|
|
|
schSCManager,
|
|
|
|
lpszServiceName,
|
2000-05-12 04:50:20 +08:00
|
|
|
lpszDisplayName,
|
|
|
|
SERVICE_ALL_ACCESS,
|
1999-06-17 01:53:17 +08:00
|
|
|
SERVICE_WIN32_OWN_PROCESS,
|
2000-05-12 04:50:20 +08:00
|
|
|
auto_start ? SERVICE_AUTO_START : SERVICE_DEMAND_START,
|
1999-06-17 01:53:17 +08:00
|
|
|
SERVICE_ERROR_NORMAL,
|
|
|
|
lpszBinaryPathName,
|
|
|
|
NULL, NULL, NULL, NULL, NULL)) != NULL)
|
|
|
|
{
|
|
|
|
char regpath[132];
|
|
|
|
CloseServiceHandle(schService);
|
|
|
|
CloseServiceHandle(schSCManager);
|
|
|
|
|
2002-07-24 02:26:33 +08:00
|
|
|
snprintf( regpath, sizeof regpath,
|
|
|
|
"SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\%s",
|
1999-06-17 01:53:17 +08:00
|
|
|
lpszServiceName );
|
1999-06-19 05:29:02 +08:00
|
|
|
/* Create the registry key for event logging to the Windows NT event log. */
|
1999-06-17 01:53:17 +08:00
|
|
|
if ( RegCreateKeyEx(HKEY_LOCAL_MACHINE,
|
|
|
|
regpath, 0,
|
|
|
|
"REG_SZ", REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey,
|
|
|
|
&dwDisposition) != ERROR_SUCCESS)
|
|
|
|
{
|
1999-10-28 15:18:48 +08:00
|
|
|
fprintf( stderr, "RegCreateKeyEx() failed. GetLastError=%lu (%s)\n", GetLastError(), GetLastErrorString() );
|
1999-06-17 01:53:17 +08:00
|
|
|
RegCloseKey(hKey);
|
|
|
|
return(0);
|
|
|
|
}
|
2003-03-03 15:30:12 +08:00
|
|
|
if ( sp ) *sp = '\0';
|
1999-06-17 01:53:17 +08:00
|
|
|
if ( RegSetValueEx(hKey, "EventMessageFile", 0, REG_EXPAND_SZ, lpszBinaryPathName, strlen(lpszBinaryPathName) + 1) != ERROR_SUCCESS)
|
|
|
|
{
|
1999-10-28 15:18:48 +08:00
|
|
|
fprintf( stderr, "RegSetValueEx(EventMessageFile) failed. GetLastError=%lu (%s)\n", GetLastError(), GetLastErrorString() );
|
1999-06-17 01:53:17 +08:00
|
|
|
RegCloseKey(hKey);
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
dwValue = EVENTLOG_ERROR_TYPE | EVENTLOG_WARNING_TYPE | EVENTLOG_INFORMATION_TYPE;
|
|
|
|
if ( RegSetValueEx(hKey, "TypesSupported", 0, REG_DWORD, (LPBYTE) &dwValue, sizeof(DWORD)) != ERROR_SUCCESS)
|
|
|
|
{
|
1999-10-28 15:18:48 +08:00
|
|
|
fprintf( stderr, "RegCreateKeyEx(TypesSupported) failed. GetLastError=%lu (%s)\n", GetLastError(), GetLastErrorString() );
|
1999-06-17 01:53:17 +08:00
|
|
|
RegCloseKey(hKey);
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
RegCloseKey(hKey);
|
|
|
|
return(1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
1999-10-28 15:18:48 +08:00
|
|
|
fprintf( stderr, "CreateService() failed. GetLastError=%lu (%s)\n", GetLastError(), GetLastErrorString() );
|
1999-06-17 01:53:17 +08:00
|
|
|
CloseServiceHandle(schSCManager);
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
1999-10-28 15:18:48 +08:00
|
|
|
fprintf( stderr, "OpenSCManager() failed. GetLastError=%lu (%s)\n", GetLastError(), GetLastErrorString() );
|
1999-06-17 01:53:17 +08:00
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-03-03 19:46:05 +08:00
|
|
|
int lutil_srv_remove(LPCTSTR lpszServiceName, LPCTSTR lpszBinaryPathName)
|
1999-06-17 01:53:17 +08:00
|
|
|
{
|
|
|
|
SC_HANDLE schSCManager, schService;
|
|
|
|
|
|
|
|
fprintf( stderr, "The installed path is %s.\n", lpszBinaryPathName );
|
|
|
|
if ((schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CONNECT|SC_MANAGER_CREATE_SERVICE)) != NULL )
|
|
|
|
{
|
|
|
|
if ((schService = OpenService(schSCManager, lpszServiceName, DELETE)) != NULL)
|
|
|
|
{
|
|
|
|
if ( DeleteService(schService) == TRUE)
|
|
|
|
{
|
|
|
|
CloseServiceHandle(schService);
|
|
|
|
CloseServiceHandle(schSCManager);
|
|
|
|
return(1);
|
|
|
|
} else {
|
1999-10-28 15:18:48 +08:00
|
|
|
fprintf( stderr, "DeleteService() failed. GetLastError=%lu (%s)\n", GetLastError(), GetLastErrorString() );
|
1999-06-17 01:53:17 +08:00
|
|
|
fprintf( stderr, "The %s service has not been removed.\n", lpszBinaryPathName);
|
|
|
|
CloseServiceHandle(schService);
|
|
|
|
CloseServiceHandle(schSCManager);
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
} else {
|
1999-10-28 15:18:48 +08:00
|
|
|
fprintf( stderr, "OpenService() failed. GetLastError=%lu (%s)\n", GetLastError(), GetLastErrorString() );
|
1999-06-17 01:53:17 +08:00
|
|
|
CloseServiceHandle(schSCManager);
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
1999-10-28 15:18:48 +08:00
|
|
|
fprintf( stderr, "OpenSCManager() failed. GetLastError=%lu (%s)\n", GetLastError(), GetLastErrorString() );
|
1999-06-17 01:53:17 +08:00
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-03-03 19:46:05 +08:00
|
|
|
#if 0 /* unused */
|
2000-06-15 10:21:01 +08:00
|
|
|
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);
|
|
|
|
}
|
2003-03-03 19:46:05 +08:00
|
|
|
#endif
|
1999-06-17 01:53:17 +08:00
|
|
|
|
|
|
|
static void *start_status_routine( void *ptr )
|
|
|
|
{
|
|
|
|
DWORD wait_result;
|
|
|
|
int done = 0;
|
|
|
|
|
|
|
|
while ( !done )
|
|
|
|
{
|
|
|
|
wait_result = WaitForSingleObject( started_event, SCM_NOTIFICATION_INTERVAL );
|
|
|
|
switch ( wait_result )
|
|
|
|
{
|
|
|
|
case WAIT_ABANDONED:
|
|
|
|
case WAIT_OBJECT_0:
|
1999-06-19 05:29:02 +08:00
|
|
|
/* the object that we were waiting for has been destroyed (ABANDONED) or
|
|
|
|
* signalled (TIMEOUT_0). We can assume that the startup process is
|
|
|
|
* complete and tell the Service Control Manager that we are now runnng */
|
2003-03-03 19:46:05 +08:00
|
|
|
lutil_ServiceStatus.dwCurrentState = SERVICE_RUNNING;
|
|
|
|
lutil_ServiceStatus.dwWin32ExitCode = NO_ERROR;
|
|
|
|
lutil_ServiceStatus.dwCheckPoint++;
|
|
|
|
lutil_ServiceStatus.dwWaitHint = 1000;
|
|
|
|
SetServiceStatus(hlutil_ServiceStatus, &lutil_ServiceStatus);
|
1999-06-17 01:53:17 +08:00
|
|
|
done = 1;
|
|
|
|
break;
|
|
|
|
case WAIT_TIMEOUT:
|
1999-06-19 05:29:02 +08:00
|
|
|
/* We've waited for the required time, so send an update to the Service Control
|
|
|
|
* Manager saying to wait again. */
|
2003-03-03 19:46:05 +08:00
|
|
|
lutil_ServiceStatus.dwCheckPoint++;
|
|
|
|
lutil_ServiceStatus.dwWaitHint = SCM_NOTIFICATION_INTERVAL * 2;
|
|
|
|
SetServiceStatus(hlutil_ServiceStatus, &lutil_ServiceStatus);
|
1999-06-17 01:53:17 +08:00
|
|
|
break;
|
|
|
|
case WAIT_FAILED:
|
1999-06-22 06:59:23 +08:00
|
|
|
/* theres been some problem with WaitForSingleObject so tell the Service
|
1999-06-19 05:29:02 +08:00
|
|
|
* Control Manager to wait 30 seconds before deploying its assasin and
|
|
|
|
* then leave the thread. */
|
2003-03-03 19:46:05 +08:00
|
|
|
lutil_ServiceStatus.dwCheckPoint++;
|
|
|
|
lutil_ServiceStatus.dwWaitHint = THIRTY_SECONDS;
|
|
|
|
SetServiceStatus(hlutil_ServiceStatus, &lutil_ServiceStatus);
|
1999-06-17 01:53:17 +08:00
|
|
|
done = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ldap_pvt_thread_exit(NULL);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void *stop_status_routine( void *ptr )
|
|
|
|
{
|
|
|
|
DWORD wait_result;
|
|
|
|
int done = 0;
|
|
|
|
|
|
|
|
while ( !done )
|
|
|
|
{
|
|
|
|
wait_result = WaitForSingleObject( stopped_event, SCM_NOTIFICATION_INTERVAL );
|
|
|
|
switch ( wait_result )
|
|
|
|
{
|
|
|
|
case WAIT_ABANDONED:
|
|
|
|
case WAIT_OBJECT_0:
|
1999-06-19 05:29:02 +08:00
|
|
|
/* the object that we were waiting for has been destroyed (ABANDONED) or
|
|
|
|
* signalled (TIMEOUT_0). The shutting down process is therefore complete
|
|
|
|
* and the final SERVICE_STOPPED message will be sent to the service control
|
|
|
|
* manager prior to the process terminating. */
|
1999-06-17 01:53:17 +08:00
|
|
|
done = 1;
|
|
|
|
break;
|
|
|
|
case WAIT_TIMEOUT:
|
1999-06-19 05:29:02 +08:00
|
|
|
/* We've waited for the required time, so send an update to the Service Control
|
|
|
|
* Manager saying to wait again. */
|
2003-03-03 19:46:05 +08:00
|
|
|
lutil_ServiceStatus.dwCheckPoint++;
|
|
|
|
lutil_ServiceStatus.dwWaitHint = SCM_NOTIFICATION_INTERVAL * 2;
|
|
|
|
SetServiceStatus(hlutil_ServiceStatus, &lutil_ServiceStatus);
|
1999-06-17 01:53:17 +08:00
|
|
|
break;
|
|
|
|
case WAIT_FAILED:
|
1999-06-22 06:59:23 +08:00
|
|
|
/* theres been some problem with WaitForSingleObject so tell the Service
|
1999-06-19 05:29:02 +08:00
|
|
|
* Control Manager to wait 30 seconds before deploying its assasin and
|
|
|
|
* then leave the thread. */
|
2003-03-03 19:46:05 +08:00
|
|
|
lutil_ServiceStatus.dwCheckPoint++;
|
|
|
|
lutil_ServiceStatus.dwWaitHint = THIRTY_SECONDS;
|
|
|
|
SetServiceStatus(hlutil_ServiceStatus, &lutil_ServiceStatus);
|
1999-06-17 01:53:17 +08:00
|
|
|
done = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ldap_pvt_thread_exit(NULL);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2004-07-18 01:46:54 +08:00
|
|
|
static void WINAPI lutil_ServiceCtrlHandler( IN DWORD Opcode)
|
1999-06-17 01:53:17 +08:00
|
|
|
{
|
|
|
|
switch (Opcode)
|
|
|
|
{
|
|
|
|
case SERVICE_CONTROL_STOP:
|
|
|
|
case SERVICE_CONTROL_SHUTDOWN:
|
|
|
|
|
2003-03-03 19:46:05 +08:00
|
|
|
lutil_ServiceStatus.dwCurrentState = SERVICE_STOP_PENDING;
|
|
|
|
lutil_ServiceStatus.dwCheckPoint++;
|
|
|
|
lutil_ServiceStatus.dwWaitHint = SCM_NOTIFICATION_INTERVAL * 2;
|
|
|
|
SetServiceStatus(hlutil_ServiceStatus, &lutil_ServiceStatus);
|
1999-06-17 01:53:17 +08:00
|
|
|
|
|
|
|
ldap_pvt_thread_cond_init( &stopped_event );
|
|
|
|
if ( stopped_event == NULL )
|
|
|
|
{
|
1999-06-19 05:29:02 +08:00
|
|
|
/* the event was not created. We will ask the service control manager for 30
|
|
|
|
* seconds to shutdown */
|
2003-03-03 19:46:05 +08:00
|
|
|
lutil_ServiceStatus.dwCheckPoint++;
|
|
|
|
lutil_ServiceStatus.dwWaitHint = THIRTY_SECONDS;
|
|
|
|
SetServiceStatus(hlutil_ServiceStatus, &lutil_ServiceStatus);
|
1999-06-17 01:53:17 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
1999-06-19 05:29:02 +08:00
|
|
|
/* start a thread to report the progress to the service control manager
|
|
|
|
* until the stopped_event is fired. */
|
1999-06-17 01:53:17 +08:00
|
|
|
if ( ldap_pvt_thread_create( &stop_status_tid, 0, stop_status_routine, NULL ) == 0 )
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
else {
|
1999-06-19 05:29:02 +08:00
|
|
|
/* failed to create the thread that tells the Service Control Manager that the
|
|
|
|
* service stopping is proceeding.
|
|
|
|
* tell the Service Control Manager to wait another 30 seconds before deploying its
|
|
|
|
* assasin. */
|
2003-03-03 19:46:05 +08:00
|
|
|
lutil_ServiceStatus.dwCheckPoint++;
|
|
|
|
lutil_ServiceStatus.dwWaitHint = THIRTY_SECONDS;
|
|
|
|
SetServiceStatus(hlutil_ServiceStatus, &lutil_ServiceStatus);
|
1999-06-17 01:53:17 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
stopfunc( -1 );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SERVICE_CONTROL_INTERROGATE:
|
2003-03-03 19:46:05 +08:00
|
|
|
SetServiceStatus(hlutil_ServiceStatus, &lutil_ServiceStatus);
|
1999-06-17 01:53:17 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2003-03-03 19:46:05 +08:00
|
|
|
void *lutil_getRegParam( char *svc, char *value )
|
1999-06-17 01:53:17 +08:00
|
|
|
{
|
1999-06-19 05:29:02 +08:00
|
|
|
HKEY hkey;
|
1999-06-17 01:53:17 +08:00
|
|
|
char path[255];
|
|
|
|
DWORD vType;
|
|
|
|
static char vValue[1024];
|
1999-06-19 05:29:02 +08:00
|
|
|
DWORD valLen = sizeof( vValue );
|
1999-06-17 01:53:17 +08:00
|
|
|
|
2000-05-13 05:26:30 +08:00
|
|
|
if ( svc != NULL )
|
2002-07-24 02:26:33 +08:00
|
|
|
snprintf ( path, sizeof path, "SOFTWARE\\%s", svc );
|
1999-06-17 01:53:17 +08:00
|
|
|
else
|
2002-07-24 02:26:33 +08:00
|
|
|
snprintf ( path, sizeof path, "SOFTWARE\\OpenLDAP\\Parameters" );
|
1999-06-19 05:29:02 +08:00
|
|
|
|
|
|
|
if ( RegOpenKeyEx( HKEY_LOCAL_MACHINE, path, 0, KEY_READ, &hkey ) != ERROR_SUCCESS )
|
1999-06-17 01:53:17 +08:00
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
1999-06-19 05:29:02 +08:00
|
|
|
if ( RegQueryValueEx( hkey, value, NULL, &vType, vValue, &valLen ) != ERROR_SUCCESS )
|
1999-06-17 01:53:17 +08:00
|
|
|
{
|
1999-06-19 05:29:02 +08:00
|
|
|
RegCloseKey( hkey );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
RegCloseKey( hkey );
|
|
|
|
|
|
|
|
switch ( vType )
|
|
|
|
{
|
|
|
|
case REG_BINARY:
|
|
|
|
case REG_DWORD:
|
|
|
|
return (void*)&vValue;
|
|
|
|
case REG_SZ:
|
|
|
|
return (void*)&vValue;
|
1999-06-17 01:53:17 +08:00
|
|
|
}
|
|
|
|
return (void*)NULL;
|
|
|
|
}
|
|
|
|
|
2003-03-03 19:46:05 +08:00
|
|
|
void lutil_LogStartedEvent( char *svc, int slap_debug, char *configfile, char *urls )
|
1999-06-17 01:53:17 +08:00
|
|
|
{
|
|
|
|
char *Inserts[5];
|
|
|
|
WORD i = 0, j;
|
|
|
|
HANDLE hEventLog;
|
|
|
|
|
|
|
|
hEventLog = RegisterEventSource( NULL, svc );
|
|
|
|
|
|
|
|
Inserts[i] = (char *)malloc( 20 );
|
|
|
|
itoa( slap_debug, Inserts[i++], 10 );
|
2000-06-07 13:17:29 +08:00
|
|
|
Inserts[i++] = strdup( configfile );
|
|
|
|
Inserts[i++] = strdup( urls ? urls : "ldap:///" );
|
1999-06-17 01:53:17 +08:00
|
|
|
|
1999-07-22 04:54:23 +08:00
|
|
|
ReportEvent( hEventLog, EVENTLOG_INFORMATION_TYPE, 0,
|
2003-03-03 19:46:05 +08:00
|
|
|
MSG_SVC_STARTED, NULL, i, 0, (LPCSTR *) Inserts, NULL );
|
1999-06-17 01:53:17 +08:00
|
|
|
|
|
|
|
for ( j = 0; j < i; j++ )
|
|
|
|
ldap_memfree( Inserts[j] );
|
|
|
|
DeregisterEventSource( hEventLog );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2003-03-03 19:46:05 +08:00
|
|
|
void lutil_LogStoppedEvent( char *svc )
|
1999-06-17 01:53:17 +08:00
|
|
|
{
|
|
|
|
HANDLE hEventLog;
|
|
|
|
|
|
|
|
hEventLog = RegisterEventSource( NULL, svc );
|
1999-07-22 04:54:23 +08:00
|
|
|
ReportEvent( hEventLog, EVENTLOG_INFORMATION_TYPE, 0,
|
2003-03-03 19:46:05 +08:00
|
|
|
MSG_SVC_STOPPED, NULL, 0, 0, NULL, NULL );
|
1999-06-17 01:53:17 +08:00
|
|
|
DeregisterEventSource( hEventLog );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-03-03 19:46:05 +08:00
|
|
|
void lutil_CommenceStartupProcessing( char *lpszServiceName,
|
1999-06-17 01:53:17 +08:00
|
|
|
void (*stopper)(int) )
|
|
|
|
{
|
2003-03-03 19:46:05 +08:00
|
|
|
hlutil_ServiceStatus = RegisterServiceCtrlHandler( lpszServiceName, (LPHANDLER_FUNCTION)lutil_ServiceCtrlHandler);
|
1999-06-17 01:53:17 +08:00
|
|
|
|
|
|
|
stopfunc = stopper;
|
|
|
|
|
|
|
|
/* initialize the Service Status structure */
|
2003-03-03 19:46:05 +08:00
|
|
|
lutil_ServiceStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
|
|
|
|
lutil_ServiceStatus.dwCurrentState = SERVICE_START_PENDING;
|
|
|
|
lutil_ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
|
|
|
|
lutil_ServiceStatus.dwWin32ExitCode = NO_ERROR;
|
|
|
|
lutil_ServiceStatus.dwServiceSpecificExitCode = 0;
|
|
|
|
lutil_ServiceStatus.dwCheckPoint = 1;
|
|
|
|
lutil_ServiceStatus.dwWaitHint = SCM_NOTIFICATION_INTERVAL * 2;
|
1999-06-17 01:53:17 +08:00
|
|
|
|
2003-03-03 19:46:05 +08:00
|
|
|
SetServiceStatus(hlutil_ServiceStatus, &lutil_ServiceStatus);
|
1999-06-17 01:53:17 +08:00
|
|
|
|
1999-06-19 05:29:02 +08:00
|
|
|
/* start up a thread to keep sending SERVICE_START_PENDING to the Service Control Manager
|
|
|
|
* until the slapd listener is completed and listening. Only then should we send
|
|
|
|
* SERVICE_RUNNING to the Service Control Manager. */
|
1999-06-17 01:53:17 +08:00
|
|
|
ldap_pvt_thread_cond_init( &started_event );
|
|
|
|
if ( started_event == NULL)
|
|
|
|
{
|
1999-06-19 05:29:02 +08:00
|
|
|
/* failed to create the event to determine when the startup process is complete so
|
|
|
|
* tell the Service Control Manager to wait another 30 seconds before deploying its
|
|
|
|
* assasin */
|
2003-03-03 19:46:05 +08:00
|
|
|
lutil_ServiceStatus.dwCheckPoint++;
|
|
|
|
lutil_ServiceStatus.dwWaitHint = THIRTY_SECONDS;
|
|
|
|
SetServiceStatus(hlutil_ServiceStatus, &lutil_ServiceStatus);
|
1999-06-17 01:53:17 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
1999-06-19 05:29:02 +08:00
|
|
|
/* start a thread to report the progress to the service control manager
|
|
|
|
* until the started_event is fired. */
|
1999-06-17 01:53:17 +08:00
|
|
|
if ( ldap_pvt_thread_create( &start_status_tid, 0, start_status_routine, NULL ) == 0 )
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
else {
|
1999-06-19 05:29:02 +08:00
|
|
|
/* failed to create the thread that tells the Service Control Manager that the
|
|
|
|
* service startup is proceeding.
|
|
|
|
* tell the Service Control Manager to wait another 30 seconds before deploying its
|
|
|
|
* assasin. */
|
2003-03-03 19:46:05 +08:00
|
|
|
lutil_ServiceStatus.dwCheckPoint++;
|
|
|
|
lutil_ServiceStatus.dwWaitHint = THIRTY_SECONDS;
|
|
|
|
SetServiceStatus(hlutil_ServiceStatus, &lutil_ServiceStatus);
|
1999-06-17 01:53:17 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-03-03 19:46:05 +08:00
|
|
|
void lutil_ReportShutdownComplete( )
|
1999-06-17 01:53:17 +08:00
|
|
|
{
|
|
|
|
if ( is_NT_Service )
|
|
|
|
{
|
1999-06-19 05:29:02 +08:00
|
|
|
/* stop sending SERVICE_STOP_PENDING messages to the Service Control Manager */
|
1999-06-17 01:53:17 +08:00
|
|
|
ldap_pvt_thread_cond_signal( &stopped_event );
|
|
|
|
ldap_pvt_thread_cond_destroy( &stopped_event );
|
|
|
|
|
1999-06-19 05:29:02 +08:00
|
|
|
/* wait for the thread sending the SERVICE_STOP_PENDING messages to the Service Control Manager to die.
|
|
|
|
* if the wait fails then put ourselves to sleep for half the Service Control Manager update interval */
|
1999-06-17 01:53:17 +08:00
|
|
|
if (ldap_pvt_thread_join( stop_status_tid, (void *) NULL ) == -1)
|
|
|
|
ldap_pvt_thread_sleep( SCM_NOTIFICATION_INTERVAL / 2 );
|
|
|
|
|
2003-03-03 19:46:05 +08:00
|
|
|
lutil_ServiceStatus.dwCurrentState = SERVICE_STOPPED;
|
|
|
|
lutil_ServiceStatus.dwCheckPoint++;
|
|
|
|
lutil_ServiceStatus.dwWaitHint = SCM_NOTIFICATION_INTERVAL;
|
|
|
|
SetServiceStatus(hlutil_ServiceStatus, &lutil_ServiceStatus);
|
1999-06-17 01:53:17 +08:00
|
|
|
}
|
|
|
|
}
|
1999-11-02 05:25:22 +08:00
|
|
|
|
2000-05-13 08:48:17 +08:00
|
|
|
static char *GetErrorString( int err )
|
2000-05-13 07:53:14 +08:00
|
|
|
{
|
|
|
|
static char msgBuf[1024];
|
|
|
|
|
|
|
|
FormatMessage(
|
|
|
|
FORMAT_MESSAGE_FROM_SYSTEM,
|
|
|
|
NULL,
|
|
|
|
err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
|
|
|
msgBuf, 1024, NULL );
|
|
|
|
|
|
|
|
return msgBuf;
|
|
|
|
}
|
|
|
|
|
2000-05-13 08:48:17 +08:00
|
|
|
static char *GetLastErrorString( void )
|
2000-05-13 07:53:14 +08:00
|
|
|
{
|
|
|
|
return GetErrorString( GetLastError() );
|
|
|
|
}
|
1999-11-02 05:25:22 +08:00
|
|
|
#endif
|