]> git.sur5r.net Git - openldap/blob - servers/slapd/nt_svc.c
Changes:
[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 main( int argc, LPTSTR *argv )
20 {
21         int             length;
22         char    filename[MAX_PATH], *fname_start;
23
24         /*
25          * Because the service was registered as SERVICE_WIN32_OWN_PROCESS,
26          * the lpServiceName element of the SERVICE_TABLE_ENTRY will be
27          * ignored.
28          */
29
30         SERVICE_TABLE_ENTRY             DispatchTable[] = {
31                 {       "",     (LPSERVICE_MAIN_FUNCTION) ServiceMain   },
32                 {       NULL,                   NULL    }
33         };
34
35         /*
36          * set the service's current directory to the installation directory
37          * for the service. this way we don't have to write absolute paths
38          * in the configuration files
39          */
40         GetModuleFileName( NULL, filename, sizeof( filename ) );
41         fname_start = strrchr( filename, *LDAP_DIRSEP );
42
43         if ( argc > 1 ) {
44                 if ( _stricmp( "install", argv[1] ) == 0 ) 
45                 {
46                         char *svcName = SERVICE_NAME;
47                         char *displayName = "OpenLDAP Directory Service";
48                         BOOL auto_start = FALSE;
49
50                         if ( (argc > 2) && (argv[2] != NULL) )
51                                 svcName = argv[2];
52
53                         if ( argc > 3 && argv[3])
54                                 displayName = argv[3];
55
56                         if ( argc > 4 && stricmp(argv[4], "auto") == 0)
57                                 auto_start = TRUE;
58
59                         strcat(filename, " service");
60                         if ( !lutil_srv_install(svcName, displayName, filename, auto_start) ) 
61                         {
62                                 fputs( "service failed installation ...\n", stderr  );
63                                 return EXIT_FAILURE;
64                         }
65                         fputs( "service has been installed ...\n", stderr  );
66                         return EXIT_SUCCESS;
67                 }
68
69                 if ( _stricmp( "remove", argv[1] ) == 0 ) 
70                 {
71                         char *svcName = SERVICE_NAME;
72                         if ( (argc > 2) && (argv[2] != NULL) )
73                                 svcName = argv[2];
74                         if ( !lutil_srv_remove(svcName, filename) ) 
75                         {
76                                 fputs( "failed to remove the service ...\n", stderr  );
77                                 return EXIT_FAILURE;
78                         }
79                         fputs( "service has been removed ...\n", stderr );
80                         return EXIT_SUCCESS;
81                 }
82                 if ( _stricmp( "service", argv[1] ) == 0 )
83                 {
84                         is_NT_Service = 1;
85                         *fname_start = '\0';
86                         SetCurrentDirectory( filename );
87                 }
88         }
89
90         if (is_NT_Service)
91         {
92                 StartServiceCtrlDispatcher(DispatchTable);
93         } else
94         {
95                 ServiceMain( argc, argv );
96         }
97
98         return EXIT_SUCCESS;
99 }
100
101 #endif