]> git.sur5r.net Git - openldap/blob - libraries/liblutil/ntservice.c
Add missing newline at EOF
[openldap] / libraries / liblutil / ntservice.c
1 /* ntservice.c */
2 #include "portable.h"
3
4 #include <ac/stdlib.h>
5 #include <ac/string.h>
6
7 #include <stdio.h>
8
9 #include <windows.h>
10 #include <winsvc.h>
11
12 #include "ldap.h"
13
14 #define ldap_debug slap_debug
15 extern int slap_debug;
16
17 #include "ldap_log.h"
18 #include "ldap_pvt_thread.h"
19
20
21 #include "ldap_defaults.h"
22
23 #include "slapdmsg.h"
24
25 #define SCM_NOTIFICATION_INTERVAL       5000
26 #define THIRTY_SECONDS                          (30 * 1000)
27
28 int       is_NT_Service = 1;    /* assume this is an NT service until determined that */
29                                                         /* startup was from the command line */
30
31 SERVICE_STATUS                  SLAPDServiceStatus;
32 SERVICE_STATUS_HANDLE   hSLAPDServiceStatus;
33
34 ldap_pvt_thread_cond_t  started_event,          stopped_event;
35 ldap_pvt_thread_t               start_status_tid,       stop_status_tid;
36
37 void (*stopfunc)(int);
38
39 /* in nt_err.c */
40 char *GetLastErrorString( void );
41
42 int srv_install(LPCTSTR lpszServiceName, LPCTSTR lpszBinaryPathName)
43 {
44         HKEY            hKey;
45         DWORD           dwValue, dwDisposition;
46         SC_HANDLE       schSCManager, schService;
47
48         fprintf( stderr, "The install path is %s.\n", lpszBinaryPathName );
49         if ((schSCManager = OpenSCManager( NULL, NULL, SC_MANAGER_CONNECT|SC_MANAGER_CREATE_SERVICE ) ) != NULL )
50         {
51                 if ((schService = CreateService( 
52                                                         schSCManager, 
53                                                         lpszServiceName, 
54                                                         TEXT("OpenLDAP Directory Service"), 
55                                                         SC_MANAGER_CREATE_SERVICE, 
56                                                         SERVICE_WIN32_OWN_PROCESS, 
57                                                         SERVICE_DEMAND_START, 
58                                                         SERVICE_ERROR_NORMAL, 
59                                                         lpszBinaryPathName, 
60                                                         NULL, NULL, NULL, NULL, NULL)) != NULL)
61                 {
62                         char regpath[132];
63                         CloseServiceHandle(schService);
64                         CloseServiceHandle(schSCManager);
65
66                         sprintf( regpath, "SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\%s",
67                                 lpszServiceName );
68                         /* Create the registry key for event logging to the Windows NT event log. */
69                         if ( RegCreateKeyEx(HKEY_LOCAL_MACHINE, 
70                                 regpath, 0, 
71                                 "REG_SZ", REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey, 
72                                 &dwDisposition) != ERROR_SUCCESS)
73                         {
74                                 fprintf( stderr, "RegCreateKeyEx() failed. GetLastError=%d (%s)\n", GetLastError(), GetLastErrorString() );
75                                 RegCloseKey(hKey);
76                                 return(0);
77                         }
78                         if ( RegSetValueEx(hKey, "EventMessageFile", 0, REG_EXPAND_SZ, lpszBinaryPathName, strlen(lpszBinaryPathName) + 1) != ERROR_SUCCESS)
79                         {
80                                 fprintf( stderr, "RegSetValueEx(EventMessageFile) failed. GetLastError=%d (%s)\n", GetLastError(), GetLastErrorString() );
81                                 RegCloseKey(hKey);
82                                 return(0);
83                         }
84
85                         dwValue = EVENTLOG_ERROR_TYPE | EVENTLOG_WARNING_TYPE | EVENTLOG_INFORMATION_TYPE;
86                         if ( RegSetValueEx(hKey, "TypesSupported", 0, REG_DWORD, (LPBYTE) &dwValue, sizeof(DWORD)) != ERROR_SUCCESS) 
87                         {
88                                 fprintf( stderr, "RegCreateKeyEx(TypesSupported) failed. GetLastError=%d (%s)\n", GetLastError(), GetLastErrorString() );
89                                 RegCloseKey(hKey);
90                                 return(0);
91                         }
92                         RegCloseKey(hKey);
93                         return(1);
94                 }
95                 else
96                 {
97                         fprintf( stderr, "CreateService() failed. GetLastError=%d (%s)\n", GetLastError(), GetLastErrorString() );
98                         CloseServiceHandle(schSCManager);
99                         return(0);
100                 }
101         }
102         else
103                 fprintf( stderr, "OpenSCManager() failed. GetLastError=%d (%s)\n", GetLastError(), GetLastErrorString() );
104         return(0);
105 }
106
107
108 int srv_remove(LPCTSTR lpszServiceName, LPCTSTR lpszBinaryPathName)
109 {
110         SC_HANDLE schSCManager, schService;
111
112         fprintf( stderr, "The installed path is %s.\n", lpszBinaryPathName );
113         if ((schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CONNECT|SC_MANAGER_CREATE_SERVICE)) != NULL ) 
114         {
115                 if ((schService = OpenService(schSCManager, lpszServiceName, DELETE)) != NULL) 
116                 {
117                         if ( DeleteService(schService) == TRUE) 
118                         {
119                                 CloseServiceHandle(schService);
120                                 CloseServiceHandle(schSCManager);
121                                 return(1);
122                         } else {
123                                 fprintf( stderr, "DeleteService() failed. GetLastError=%d (%s)\n", GetLastError(), GetLastErrorString() );
124                                 fprintf( stderr, "The %s service has not been removed.\n", lpszBinaryPathName);
125                                 CloseServiceHandle(schService);
126                                 CloseServiceHandle(schSCManager);
127                                 return(0);
128                         }
129                 } else {
130                         fprintf( stderr, "OpenService() failed. GetLastError=%d (%s)\n", GetLastError(), GetLastErrorString() );
131                         CloseServiceHandle(schSCManager);
132                         return(0);
133                 }
134         }
135         else
136                 fprintf( stderr, "OpenSCManager() failed. GetLastError=%d (%s)\n", GetLastError(), GetLastErrorString() );
137         return(0);
138 }
139
140
141
142 static void *start_status_routine( void *ptr )
143 {
144         DWORD   wait_result;
145         int             done = 0;
146
147         while ( !done )
148         {
149                 wait_result = WaitForSingleObject( started_event, SCM_NOTIFICATION_INTERVAL );
150                 switch ( wait_result )
151                 {
152                         case WAIT_ABANDONED:
153                         case WAIT_OBJECT_0:
154                                 /* the object that we were waiting for has been destroyed (ABANDONED) or
155                                  * signalled (TIMEOUT_0). We can assume that the startup process is
156                                  * complete and tell the Service Control Manager that we are now runnng */
157                                 SLAPDServiceStatus.dwCurrentState       = SERVICE_RUNNING;
158                                 SLAPDServiceStatus.dwWin32ExitCode      = NO_ERROR;
159                                 SLAPDServiceStatus.dwCheckPoint++;
160                                 SLAPDServiceStatus.dwWaitHint           = 1000;
161                                 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
162                                 done = 1;
163                                 break;
164                         case WAIT_TIMEOUT:
165                                 /* We've waited for the required time, so send an update to the Service Control 
166                                  * Manager saying to wait again. */
167                                 SLAPDServiceStatus.dwCheckPoint++;
168                                 SLAPDServiceStatus.dwWaitHint = SCM_NOTIFICATION_INTERVAL * 2;
169                                 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
170                                 break;
171                         case WAIT_FAILED:
172                                 /* theres been some problem with WaitForSingleObject so tell the Service
173                                  * Control Manager to wait 30 seconds before deploying its assasin and 
174                                  * then leave the thread. */
175                                 SLAPDServiceStatus.dwCheckPoint++;
176                                 SLAPDServiceStatus.dwWaitHint = THIRTY_SECONDS;
177                                 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
178                                 done = 1;
179                                 break;
180                 }
181         }
182         ldap_pvt_thread_exit(NULL);
183         return NULL;
184 }
185
186
187
188 static void *stop_status_routine( void *ptr )
189 {
190         DWORD   wait_result;
191         int             done = 0;
192
193         while ( !done )
194         {
195                 wait_result = WaitForSingleObject( stopped_event, SCM_NOTIFICATION_INTERVAL );
196                 switch ( wait_result )
197                 {
198                         case WAIT_ABANDONED:
199                         case WAIT_OBJECT_0:
200                                 /* the object that we were waiting for has been destroyed (ABANDONED) or
201                                  * signalled (TIMEOUT_0). The shutting down process is therefore complete 
202                                  * and the final SERVICE_STOPPED message will be sent to the service control
203                                  * manager prior to the process terminating. */
204                                 done = 1;
205                                 break;
206                         case WAIT_TIMEOUT:
207                                 /* We've waited for the required time, so send an update to the Service Control 
208                                  * Manager saying to wait again. */
209                                 SLAPDServiceStatus.dwCheckPoint++;
210                                 SLAPDServiceStatus.dwWaitHint = SCM_NOTIFICATION_INTERVAL * 2;
211                                 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
212                                 break;
213                         case WAIT_FAILED:
214                                 /* theres been some problem with WaitForSingleObject so tell the Service
215                                  * Control Manager to wait 30 seconds before deploying its assasin and 
216                                  * then leave the thread. */
217                                 SLAPDServiceStatus.dwCheckPoint++;
218                                 SLAPDServiceStatus.dwWaitHint = THIRTY_SECONDS;
219                                 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
220                                 done = 1;
221                                 break;
222                 }
223         }
224         ldap_pvt_thread_exit(NULL);
225         return NULL;
226 }
227
228
229
230 void WINAPI SLAPDServiceCtrlHandler( IN DWORD Opcode)
231 {
232         switch (Opcode)
233         {
234         case SERVICE_CONTROL_STOP:
235         case SERVICE_CONTROL_SHUTDOWN:
236
237                 Debug( LDAP_DEBUG_TRACE, "Service Shutdown ordered\n", 0, 0, 0 );
238                 SLAPDServiceStatus.dwCurrentState       = SERVICE_STOP_PENDING;
239                 SLAPDServiceStatus.dwCheckPoint++;
240                 SLAPDServiceStatus.dwWaitHint           = SCM_NOTIFICATION_INTERVAL * 2;
241                 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
242
243                 ldap_pvt_thread_cond_init( &stopped_event );
244                 if ( stopped_event == NULL )
245                 {
246                         /* the event was not created. We will ask the service control manager for 30
247                          * seconds to shutdown */
248                         SLAPDServiceStatus.dwCheckPoint++;
249                         SLAPDServiceStatus.dwWaitHint           = THIRTY_SECONDS;
250                         SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
251                 }
252                 else
253                 {
254                         /* start a thread to report the progress to the service control manager 
255                          * until the stopped_event is fired. */
256                         if ( ldap_pvt_thread_create( &stop_status_tid, 0, stop_status_routine, NULL ) == 0 )
257                         {
258                                 
259                         }
260                         else {
261                                 /* failed to create the thread that tells the Service Control Manager that the
262                                  * service stopping is proceeding. 
263                                  * tell the Service Control Manager to wait another 30 seconds before deploying its
264                                  * assasin.  */
265                                 SLAPDServiceStatus.dwCheckPoint++;
266                                 SLAPDServiceStatus.dwWaitHint = THIRTY_SECONDS;
267                                 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
268                         }
269                 }
270                 stopfunc( -1 );
271                 break;
272
273         case SERVICE_CONTROL_INTERROGATE:
274                 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
275                 break;
276         }
277         return;
278 }
279
280 void *getRegParam( char *svc, char *value )
281 {
282         HKEY hkey;
283         char path[255];
284         DWORD vType;
285         static char vValue[1024];
286         DWORD valLen = sizeof( vValue );
287
288         if ( svc != NULL )
289                 sprintf ( path, "SOFTWARE\\OpenLDAP\\%s\\Parameters", svc );
290         else
291                 strcpy (path, "SOFTWARE\\OpenLDAP\\Parameters" );
292         
293         if ( RegOpenKeyEx( HKEY_LOCAL_MACHINE, path, 0, KEY_READ, &hkey ) != ERROR_SUCCESS )
294         {
295                 /*Debug( LDAP_DEBUG_ANY, "RegOpenKeyEx() %s\n", GetLastErrorString(), 0, 0); */
296                 return NULL;
297         }
298
299         if ( RegQueryValueEx( hkey, value, NULL, &vType, vValue, &valLen ) != ERROR_SUCCESS )
300         {
301                 /*Debug( LDAP_DEBUG_ANY, "RegQueryValueEx() %s\n", GetLastErrorString(), 0, 0 );*/
302                 RegCloseKey( hkey );
303                 return NULL;
304         }
305         RegCloseKey( hkey );
306         
307         switch ( vType )
308         {
309         case REG_BINARY:
310         case REG_DWORD:
311                 return (void*)&vValue;
312         case REG_SZ:
313                 return (void*)&vValue;
314         }
315         return (void*)NULL;
316 }
317
318 void LogSlapdStartedEvent( char *svc, int slap_debug, char *configfile, short port, int udp )
319 {
320         char *Inserts[5];
321         WORD i = 0, j;
322         HANDLE hEventLog;
323         
324         hEventLog = RegisterEventSource( NULL, svc );
325
326         Inserts[i] = (char *)malloc( 20 );
327         itoa( slap_debug, Inserts[i++], 10 );
328         Inserts[i++] = ldap_pvt_strdup( configfile );
329         Inserts[i] = (char *)malloc( 20 );
330         itoa( port, Inserts[i++], 10 );
331         Inserts[i++] = ldap_pvt_strdup( udp ? "udp" : "tcp" );
332         Inserts[i++] = ldap_pvt_strdup( is_NT_Service ? "svc" : "cmd" );
333
334         ReportEvent( hEventLog, EVENTLOG_INFORMATION_TYPE, 0, MSG_SLAPD_STARTED, NULL, i, 0, Inserts, NULL );
335
336         for ( j = 0; j < i; j++ )
337                 ldap_memfree( Inserts[j] );
338         DeregisterEventSource( hEventLog );
339 }
340
341
342
343 void LogSlapdStoppedEvent( char *svc )
344 {
345         HANDLE hEventLog;
346         
347         hEventLog = RegisterEventSource( NULL, svc );
348         ReportEvent( hEventLog, EVENTLOG_INFORMATION_TYPE, 0, MSG_SLAPD_STOPPED, NULL, 0, 0, NULL, NULL );
349         DeregisterEventSource( hEventLog );
350 }
351
352
353 void CommenceStartupProcessing( LPCTSTR lpszServiceName,
354                                                            void (*stopper)(int) )
355 {
356         hSLAPDServiceStatus = RegisterServiceCtrlHandler( lpszServiceName, (LPHANDLER_FUNCTION)SLAPDServiceCtrlHandler);
357
358         stopfunc = stopper;
359
360         /* initialize the Service Status structure */
361         SLAPDServiceStatus.dwServiceType                                = SERVICE_WIN32_OWN_PROCESS;
362         SLAPDServiceStatus.dwCurrentState                               = SERVICE_START_PENDING;
363         SLAPDServiceStatus.dwControlsAccepted                   = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
364         SLAPDServiceStatus.dwWin32ExitCode                              = NO_ERROR;
365         SLAPDServiceStatus.dwServiceSpecificExitCode    = 0;
366         SLAPDServiceStatus.dwCheckPoint                                 = 1;
367         SLAPDServiceStatus.dwWaitHint                                   = SCM_NOTIFICATION_INTERVAL * 2;
368
369         SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
370
371         /* start up a thread to keep sending SERVICE_START_PENDING to the Service Control Manager
372          * until the slapd listener is completed and listening. Only then should we send 
373          * SERVICE_RUNNING to the Service Control Manager. */
374         ldap_pvt_thread_cond_init( &started_event );
375         if ( started_event == NULL)
376         {
377                 /* failed to create the event to determine when the startup process is complete so
378                  * tell the Service Control Manager to wait another 30 seconds before deploying its
379                  * assasin  */
380                 SLAPDServiceStatus.dwCheckPoint++;
381                 SLAPDServiceStatus.dwWaitHint = THIRTY_SECONDS;
382                 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
383         }
384         else
385         {
386                 /* start a thread to report the progress to the service control manager 
387                  * until the started_event is fired.  */
388                 if ( ldap_pvt_thread_create( &start_status_tid, 0, start_status_routine, NULL ) == 0 )
389                 {
390                         
391                 }
392                 else {
393                         /* failed to create the thread that tells the Service Control Manager that the
394                          * service startup is proceeding. 
395                          * tell the Service Control Manager to wait another 30 seconds before deploying its
396                          * assasin.  */
397                         SLAPDServiceStatus.dwCheckPoint++;
398                         SLAPDServiceStatus.dwWaitHint = THIRTY_SECONDS;
399                         SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
400                 }
401         }
402 }
403
404 void ReportSlapdShutdownComplete(  )
405 {
406         if ( is_NT_Service )
407         {
408                 /* stop sending SERVICE_STOP_PENDING messages to the Service Control Manager */
409                 ldap_pvt_thread_cond_signal( &stopped_event );
410                 ldap_pvt_thread_cond_destroy( &stopped_event );
411
412                 /* wait for the thread sending the SERVICE_STOP_PENDING messages to the Service Control Manager to die.
413                  * if the wait fails then put ourselves to sleep for half the Service Control Manager update interval */
414                 if (ldap_pvt_thread_join( stop_status_tid, (void *) NULL ) == -1)
415                         ldap_pvt_thread_sleep( SCM_NOTIFICATION_INTERVAL / 2 );
416
417                 SLAPDServiceStatus.dwCurrentState = SERVICE_STOPPED;
418                 SLAPDServiceStatus.dwCheckPoint++;
419                 SLAPDServiceStatus.dwWaitHint = SCM_NOTIFICATION_INTERVAL;
420                 SetServiceStatus(hSLAPDServiceStatus, &SLAPDServiceStatus);
421         }
422 }