]> git.sur5r.net Git - openldap/blob - servers/slapd/nt_svc.c
More changes to let BDB build without LDBM.
[openldap] / servers / slapd / nt_svc.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6
7 #include "portable.h"
8 #include <stdio.h>
9 #include <ac/string.h>
10 #include "slap.h"
11
12 #ifdef HAVE_NT_SERVICE_MANAGER
13
14 /* in main.c */
15 void WINAPI ServiceMain( DWORD argc, LPTSTR *argv );
16
17 /* in ntservice.c */
18 int srv_install( char* service, char * displayName, char* filename,
19                  BOOL auto_start );
20 int srv_remove ( char* service, char* filename );
21 DWORD svc_installed (LPTSTR lpszServiceName, LPTSTR lpszBinaryPathName);
22 DWORD svc_running (LPTSTR lpszServiceName);
23
24 int main( int argc, LPTSTR *argv )
25 {
26         int             length;
27         char    filename[MAX_PATH], *fname_start;
28         extern int is_NT_Service;
29
30         /*
31          * Because the service was registered as SERVICE_WIN32_OWN_PROCESS,
32          * the lpServiceName element of the SERVICE_TABLE_ENTRY will be
33          * ignored. Since we don't even know the name of the service at
34          * this point (since it could have been installed under a name
35          * different than SERVICE_NAME), we might as well just provide
36          * the parameter as "".
37          */
38
39         SERVICE_TABLE_ENTRY             DispatchTable[] = {
40                 {       "",     (LPSERVICE_MAIN_FUNCTION) ServiceMain   },
41                 {       NULL,                   NULL    }
42         };
43
44         /*
45         // set the service's current directory to being the installation directory for the service.
46         // this way we don't have to write absolute paths in the configuration files
47         */
48         GetModuleFileName( NULL, filename, sizeof( filename ) );
49         fname_start = strrchr( filename, *LDAP_DIRSEP );
50         *fname_start = '\0';
51         SetCurrentDirectory( filename );
52
53         if ( argc > 1 ) {
54                 if ( _stricmp( "install", argv[1] ) == 0 ) 
55                 {
56                         char *svcName = SERVICE_NAME;
57                         char *displayName = "OpenLDAP Directory Service";
58                         BOOL auto_start = FALSE;
59
60                         if ( (argc > 2) && (argv[2] != NULL) )
61                                 svcName = argv[2];
62
63                         if ( argc > 3 && argv[3])
64                                 displayName = argv[3];
65
66                         if ( argc > 4 && stricmp(argv[4], "auto") == 0)
67                                 auto_start = TRUE;
68
69                         if ( (length = GetModuleFileName(NULL, filename, sizeof( filename ))) == 0 ) 
70                         {
71                                 fputs( "unable to retrieve file name for the service.\n", stderr  );
72                                 return EXIT_FAILURE;
73                         }
74                         if ( !srv_install(svcName, displayName, filename, auto_start) ) 
75                         {
76                                 fputs( "service failed installation ...\n", stderr  );
77                                 return EXIT_FAILURE;
78                         }
79                         fputs( "service has been installed ...\n", stderr  );
80                         return EXIT_SUCCESS;
81                 }
82
83                 if ( _stricmp( "remove", argv[1] ) == 0 ) 
84                 {
85                         char *svcName = SERVICE_NAME;
86                         if ( (argc > 2) && (argv[2] != NULL) )
87                                 svcName = argv[2];
88                         if ( (length = GetModuleFileName(NULL, filename, sizeof( filename ))) == 0 ) 
89                         {
90                                 fputs( "unable to retrieve file name for the service.\n", stderr  );
91                                 return EXIT_FAILURE;
92                         }
93                         if ( !srv_remove(svcName, filename) ) 
94                         {
95                                 fputs( "failed to remove the service ...\n", stderr  );
96                                 return EXIT_FAILURE;
97                         }
98                         fputs( "service has been removed ...\n", stderr );
99                         return EXIT_SUCCESS;
100                 }
101         }
102
103         puts( "starting slapd..." );
104         if (svc_installed(SERVICE_NAME, NULL) != 0
105                 || svc_running(SERVICE_NAME) == 1
106                 || StartServiceCtrlDispatcher(DispatchTable) == 0 )
107         {
108                 is_NT_Service = 0;
109                 ServiceMain( argc, argv );
110         }
111
112         return EXIT_SUCCESS;
113 }
114
115 #endif