]> git.sur5r.net Git - openldap/blob - servers/slapd/nt_svc.c
Bug fix for new sockbuf code under NT. Added mutex protection against
[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
22 int main( int argc, LPTSTR *argv )
23 {
24         int             length;
25         char    filename[MAX_PATH], *fname_start;
26         extern int is_NT_Service;
27
28         /*
29          * Because the service was registered as SERVICE_WIN32_OWN_PROCESS,
30          * the lpServiceName element of the SERVICE_TABLE_ENTRY will be
31          * ignored. Since we don't even know the name of the service at
32          * this point (since it could have been installed under a name
33          * different than SERVICE_NAME), we might as well just provide
34          * the parameter as "".
35          */
36
37         SERVICE_TABLE_ENTRY             DispatchTable[] = {
38                 {       "",     (LPSERVICE_MAIN_FUNCTION) ServiceMain   },
39                 {       NULL,                   NULL    }
40         };
41
42         /*
43         // set the service's current directory to being the installation directory for the service.
44         // this way we don't have to write absolute paths in the configuration files
45         */
46         GetModuleFileName( NULL, filename, sizeof( filename ) );
47         fname_start = strrchr( filename, *LDAP_DIRSEP );
48         *fname_start = '\0';
49         SetCurrentDirectory( filename );
50
51         if ( argc > 1 ) {
52                 if ( _stricmp( "install", argv[1] ) == 0 ) 
53                 {
54                         char *svcName = SERVICE_NAME;
55                         char *displayName = "OpenLDAP Directory Service";
56                         BOOL auto_start = FALSE;
57
58                         if ( (argc > 2) && (argv[2] != NULL) )
59                                 svcName = argv[2];
60
61                         if ( argc > 3 && argv[3])
62                                 displayName = argv[3];
63
64                         if ( argc > 4 && stricmp(argv[4], "auto") == 0)
65                                 auto_start = TRUE;
66
67                         if ( (length = GetModuleFileName(NULL, filename, sizeof( filename ))) == 0 ) 
68                         {
69                                 fputs( "unable to retrieve file name for the service.\n", stderr  );
70                                 return EXIT_FAILURE;
71                         }
72                         if ( !srv_install(svcName, displayName, filename, auto_start) ) 
73                         {
74                                 fputs( "service failed installation ...\n", stderr  );
75                                 return EXIT_FAILURE;
76                         }
77                         fputs( "service has been installed ...\n", stderr  );
78                         return EXIT_SUCCESS;
79                 }
80
81                 if ( _stricmp( "remove", argv[1] ) == 0 ) 
82                 {
83                         char *svcName = SERVICE_NAME;
84                         if ( (argc > 2) && (argv[2] != NULL) )
85                                 svcName = argv[2];
86                         if ( (length = GetModuleFileName(NULL, filename, sizeof( filename ))) == 0 ) 
87                         {
88                                 fputs( "unable to retrieve file name for the service.\n", stderr  );
89                                 return EXIT_FAILURE;
90                         }
91                         if ( !srv_remove(svcName, filename) ) 
92                         {
93                                 fputs( "failed to remove the service ...\n", stderr  );
94                                 return EXIT_FAILURE;
95                         }
96                         fputs( "service has been removed ...\n", stderr );
97                         return EXIT_SUCCESS;
98                 }
99         }
100
101         puts( "starting slapd..." );
102         if ( !StartServiceCtrlDispatcher(DispatchTable) )
103         {
104                 is_NT_Service = 0;
105                 ServiceMain( argc, argv );
106         }
107
108         return EXIT_SUCCESS;
109 }
110
111 #endif