]> git.sur5r.net Git - openldap/blob - servers/slapd/nt_svc.c
736c4653a85ad7fccd9c0fa3da7a42abe841ff2b
[openldap] / servers / slapd / nt_svc.c
1 /* $OpenLDAP$ */
2 // nt_main.c
3 #include "portable.h"
4
5 #ifdef HAVE_NT_SERVICE_MANAGER
6
7 #include <stdio.h>
8 #include <ac/string.h>
9
10 #include "slap.h"
11
12 ldap_pvt_thread_cond_t  started_event,          stopped_event;
13 ldap_pvt_thread_t               start_status_tid,       stop_status_tid;
14
15
16 /* in main.c */
17 void WINAPI ServiceMain( DWORD argc, LPTSTR *argv );
18
19 /* in ntservice.c */
20 int srv_install( char* service, char* filename );
21 int srv_remove ( char* service, char* filename );
22
23 static void stubs()
24 {
25     ldap_add_s(NULL, NULL, NULL);
26     ldap_bind_s(NULL, NULL, NULL, 0);
27     ldap_delete_s(NULL, NULL);
28     ldap_first_attribute(NULL, NULL, NULL);
29     ldap_first_entry(NULL, NULL);
30     ldap_get_dn(NULL, NULL);
31     ldap_get_option(NULL, 0, NULL);
32     ldap_get_values_len(NULL, NULL, NULL);
33     ldap_init(NULL, 0);
34     ldap_modify_s(NULL, NULL, NULL);
35     ldap_modrdn_s(NULL, NULL, NULL);
36     ldap_msgfree(NULL);
37     ldap_next_attribute(NULL, NULL, NULL);
38     ldap_result(NULL, 0, 0, NULL, NULL);
39     ldap_search(NULL, NULL, 0, NULL, NULL, 0);
40     ldap_unbind(NULL);
41 }
42
43 int main( int argc, LPTSTR *argv )
44 {
45         int             length;
46         char    filename[MAX_PATH], *fname_start;
47         extern int is_NT_Service;
48
49         SERVICE_TABLE_ENTRY             DispatchTable[] = {
50                 {       SERVICE_NAME,   (LPSERVICE_MAIN_FUNCTION) ServiceMain   },
51                 {       NULL,                   NULL    }
52         };
53
54         /*
55         // set the service's current directory to being the installation directory for the service.
56         // this way we don't have to write absolute paths in the configuration files
57         */
58         GetModuleFileName( NULL, filename, sizeof( filename ) );
59         fname_start = strrchr( filename, *LDAP_DIRSEP );
60         *fname_start = '\0';
61         SetCurrentDirectory( filename );
62
63         if ( argc > 1 ) {
64                 if ( _stricmp( "install", argv[1] ) == 0 ) 
65                 {
66                         char *svcName = SERVICE_NAME;
67                         if ( (argc > 2) && (argv[2] != NULL) )
68                                 svcName = argv[2];
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, filename) ) 
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 ( !StartServiceCtrlDispatcher(DispatchTable) )
105         {
106                 is_NT_Service = 0;
107                 ServiceMain( argc, argv );
108         }
109
110         return EXIT_SUCCESS;
111 }
112
113 #endif