From e61bb6aa3f3af43e137a1455ac7a874c72c14120 Mon Sep 17 00:00:00 2001 From: Gary Williams Date: Fri, 18 Jun 1999 21:29:02 +0000 Subject: [PATCH] close registry, and return static pointer (no free) from getRegParam --- libraries/liblutil/ntservice.c | 138 ++++++++++++++++----------------- 1 file changed, 65 insertions(+), 73 deletions(-) diff --git a/libraries/liblutil/ntservice.c b/libraries/liblutil/ntservice.c index 53c6841106..5b1acddd12 100644 --- a/libraries/liblutil/ntservice.c +++ b/libraries/liblutil/ntservice.c @@ -10,15 +10,14 @@ #include #include "ldap_defaults.h" -//#include "slap.h" #include "slapdmsg.h" #define SCM_NOTIFICATION_INTERVAL 5000 #define THIRTY_SECONDS (30 * 1000) -int is_NT_Service = 1; // assume this is an NT service until determined that - // startup was from the command line +int is_NT_Service = 1; /* assume this is an NT service until determined that */ + /* startup was from the command line */ SERVICE_STATUS SLAPDServiceStatus; SERVICE_STATUS_HANDLE hSLAPDServiceStatus; @@ -28,14 +27,14 @@ ldap_pvt_thread_t start_status_tid, stop_status_tid; void (*stopfunc)(int); -// in main.c +/* in main.c */ void WINAPI ServiceMain( DWORD argc, LPTSTR *argv ); -// in wsa_err.c +/* in wsa_err.c */ char *WSAGetLastErrorString( void ); -// in nt_err.c +/* in nt_err.c */ char *GetLastErrorString( void ); int srv_install(LPCTSTR lpszServiceName, LPCTSTR lpszBinaryPathName) @@ -64,7 +63,7 @@ int srv_install(LPCTSTR lpszServiceName, LPCTSTR lpszBinaryPathName) sprintf( regpath, "SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\%s", lpszServiceName ); - // Create the registry key for event logging to the Windows NT event log. + /* Create the registry key for event logging to the Windows NT event log. */ if ( RegCreateKeyEx(HKEY_LOCAL_MACHINE, regpath, 0, "REG_SZ", REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey, @@ -150,9 +149,9 @@ static void *start_status_routine( void *ptr ) { case WAIT_ABANDONED: case WAIT_OBJECT_0: - // 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 + /* 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 */ SLAPDServiceStatus.dwCurrentState = SERVICE_RUNNING; SLAPDServiceStatus.dwWin32ExitCode = NO_ERROR; SLAPDServiceStatus.dwCheckPoint++; @@ -161,16 +160,16 @@ static void *start_status_routine( void *ptr ) done = 1; break; case WAIT_TIMEOUT: - // We've waited for the required time, so send an update to the Service Control - // Manager saying to wait again. + /* We've waited for the required time, so send an update to the Service Control + * Manager saying to wait again. */ SLAPDServiceStatus.dwCheckPoint++; SLAPDServiceStatus.dwWaitHint = SCM_NOTIFICATION_INTERVAL * 2; SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus); break; case WAIT_FAILED: - // theres been some proble with WaitForSingleObject so tell the Service - // Control Manager to wait 30 seconds before deploying its assasin and - // then leave the thread. + /* theres been some proble with WaitForSingleObject so tell the Service + * Control Manager to wait 30 seconds before deploying its assasin and + * then leave the thread. */ SLAPDServiceStatus.dwCheckPoint++; SLAPDServiceStatus.dwWaitHint = THIRTY_SECONDS; SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus); @@ -196,23 +195,23 @@ static void *stop_status_routine( void *ptr ) { case WAIT_ABANDONED: case WAIT_OBJECT_0: - // 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. + /* 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. */ done = 1; break; case WAIT_TIMEOUT: - // We've waited for the required time, so send an update to the Service Control - // Manager saying to wait again. + /* We've waited for the required time, so send an update to the Service Control + * Manager saying to wait again. */ SLAPDServiceStatus.dwCheckPoint++; SLAPDServiceStatus.dwWaitHint = SCM_NOTIFICATION_INTERVAL * 2; SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus); break; case WAIT_FAILED: - // theres been some proble with WaitForSingleObject so tell the Service - // Control Manager to wait 30 seconds before deploying its assasin and - // then leave the thread. + /* theres been some proble with WaitForSingleObject so tell the Service + * Control Manager to wait 30 seconds before deploying its assasin and + * then leave the thread. */ SLAPDServiceStatus.dwCheckPoint++; SLAPDServiceStatus.dwWaitHint = THIRTY_SECONDS; SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus); @@ -242,25 +241,25 @@ void WINAPI SLAPDServiceCtrlHandler( IN DWORD Opcode) ldap_pvt_thread_cond_init( &stopped_event ); if ( stopped_event == NULL ) { - // the event was not created. We will ask the service control manager for 30 - // seconds to shutdown + /* the event was not created. We will ask the service control manager for 30 + * seconds to shutdown */ SLAPDServiceStatus.dwCheckPoint++; SLAPDServiceStatus.dwWaitHint = THIRTY_SECONDS; SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus); } else { - // start a thread to report the progress to the service control manager - // until the stopped_event is fired. + /* start a thread to report the progress to the service control manager + * until the stopped_event is fired. */ if ( ldap_pvt_thread_create( &stop_status_tid, 0, stop_status_routine, NULL ) == 0 ) { } else { - // 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. + /* 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. */ SLAPDServiceStatus.dwCheckPoint++; SLAPDServiceStatus.dwWaitHint = THIRTY_SECONDS; SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus); @@ -276,47 +275,40 @@ void WINAPI SLAPDServiceCtrlHandler( IN DWORD Opcode) return; } - void *getRegParam( char *svc, char *value ) { - HANDLE hkey; + HKEY hkey; char path[255]; - int i = 0, rc; - char vName[256]; - DWORD vNameLen = 255; DWORD vType; static char vValue[1024]; - DWORD valLen = 1024; + DWORD valLen = sizeof( vValue ); if ( svc != NULL ) sprintf ( path, "SOFTWARE\\OpenLDAP\\%s\\Parameters", svc ); else strcpy (path, "SOFTWARE\\OpenLDAP\\Parameters" ); - - if( (rc = RegOpenKeyEx( HKEY_LOCAL_MACHINE, - path, 0, KEY_READ, &hkey )) != ERROR_SUCCESS ) + + if ( RegOpenKeyEx( HKEY_LOCAL_MACHINE, path, 0, KEY_READ, &hkey ) != ERROR_SUCCESS ) { - //Debug( LDAP_DEBUG_ANY, "%s\n", GetLastErrorString(), 0, 0 ); + /*Debug( LDAP_DEBUG_ANY, "RegOpenKeyEx() %s\n", GetLastErrorString(), 0, 0); */ return NULL; } - while ( !RegEnumValue( hkey, i, vName, &vNameLen, NULL, - &vType, vValue, &valLen ) ) + if ( RegQueryValueEx( hkey, value, NULL, &vType, vValue, &valLen ) != ERROR_SUCCESS ) { - if ( !strcasecmp( value, vName ) ) - { - switch ( vType ) - { - case REG_BINARY: - case REG_DWORD: - return (void*)&vValue; - case REG_SZ: - return (void*)strdup( vValue ); - } - } - i++; - vNameLen = 255; - valLen = 1024; + /*Debug( LDAP_DEBUG_ANY, "RegQueryValueEx() %s\n", GetLastErrorString(), 0, 0 );*/ + RegCloseKey( hkey ); + return NULL; + } + RegCloseKey( hkey ); + + switch ( vType ) + { + case REG_BINARY: + case REG_DWORD: + return (void*)&vValue; + case REG_SZ: + return (void*)&vValue; } return (void*)NULL; } @@ -374,32 +366,32 @@ void CommenceStartupProcessing( LPCTSTR lpszServiceName, SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus); - // 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. + /* 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. */ ldap_pvt_thread_cond_init( &started_event ); if ( started_event == NULL) { - // 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 + /* 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 */ SLAPDServiceStatus.dwCheckPoint++; SLAPDServiceStatus.dwWaitHint = THIRTY_SECONDS; SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus); } else { - // start a thread to report the progress to the service control manager - // until the started_event is fired. + /* start a thread to report the progress to the service control manager + * until the started_event is fired. */ if ( ldap_pvt_thread_create( &start_status_tid, 0, start_status_routine, NULL ) == 0 ) { } else { - // 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. + /* 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. */ SLAPDServiceStatus.dwCheckPoint++; SLAPDServiceStatus.dwWaitHint = THIRTY_SECONDS; SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus); @@ -411,12 +403,12 @@ void ReportSlapdShutdownComplete( ) { if ( is_NT_Service ) { - // stop sending SERVICE_STOP_PENDING messages to the Service Control Manager + /* stop sending SERVICE_STOP_PENDING messages to the Service Control Manager */ ldap_pvt_thread_cond_signal( &stopped_event ); ldap_pvt_thread_cond_destroy( &stopped_event ); - // 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 + /* 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 */ if (ldap_pvt_thread_join( stop_status_tid, (void *) NULL ) == -1) ldap_pvt_thread_sleep( SCM_NOTIFICATION_INTERVAL / 2 ); -- 2.39.5