]> git.sur5r.net Git - openldap/blob - servers/slapd/nt_svc.c
Modify ad_cmp() macro to support use as an ordering function.
[openldap] / servers / slapd / nt_svc.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2002 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 #include "lutil.h"
12
13 #ifdef HAVE_NT_SERVICE_MANAGER
14
15 /* in main.c */
16 void WINAPI ServiceMain( DWORD argc, LPTSTR *argv );
17
18 /* in ntservice.c */
19 int srv_install( char* service, char * displayName, char* filename,
20                  BOOL auto_start );
21 int srv_remove ( char* service, char* filename );
22 DWORD svc_installed (LPTSTR lpszServiceName, LPTSTR lpszBinaryPathName);
23 DWORD svc_running (LPTSTR lpszServiceName);
24
25 int main( int argc, LPTSTR *argv )
26 {
27         int             length;
28         char    filename[MAX_PATH], *fname_start;
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