1 /* $NetBSD: nt_svc.c,v 1.3 2021/08/14 16:14:58 christos Exp $ */
2
3 /* $OpenLDAP$ */
4 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 *
6 * Copyright 1998-2021 The OpenLDAP Foundation.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted only as authorized by the OpenLDAP
11 * Public License.
12 *
13 * A copy of this license is available in the file LICENSE in the
14 * top-level directory of the distribution or, alternatively, at
15 * <http://www.OpenLDAP.org/license.html>.
16 */
17
18 #include <sys/cdefs.h>
19 __RCSID("$NetBSD: nt_svc.c,v 1.3 2021/08/14 16:14:58 christos Exp $");
20
21 #include "portable.h"
22 #include <stdio.h>
23 #include <ac/string.h>
24 #include "slap.h"
25 #include "lutil.h"
26
27 #ifdef HAVE_NT_SERVICE_MANAGER
28
29 /* in main.c */
30 void WINAPI ServiceMain( DWORD argc, LPTSTR *argv );
31
32 /* in ntservice.c */
main(int argc,LPTSTR * argv)33 int main( int argc, LPTSTR *argv )
34 {
35 int length;
36 char filename[MAX_PATH], *fname_start;
37
38 /*
39 * Because the service was registered as SERVICE_WIN32_OWN_PROCESS,
40 * the lpServiceName element of the SERVICE_TABLE_ENTRY will be
41 * ignored.
42 */
43
44 SERVICE_TABLE_ENTRY DispatchTable[] = {
45 { "", (LPSERVICE_MAIN_FUNCTION) ServiceMain },
46 { NULL, NULL }
47 };
48
49 /*
50 * set the service's current directory to the installation directory
51 * for the service. this way we don't have to write absolute paths
52 * in the configuration files
53 */
54 GetModuleFileName( NULL, filename, sizeof( filename ) );
55 fname_start = strrchr( filename, *LDAP_DIRSEP );
56
57 if ( argc > 1 ) {
58 if ( _stricmp( "install", argv[1] ) == 0 )
59 {
60 char *svcName = SERVICE_NAME;
61 char *displayName = "OpenLDAP Directory Service";
62 BOOL auto_start = FALSE;
63
64 if ( (argc > 2) && (argv[2] != NULL) )
65 svcName = argv[2];
66
67 if ( argc > 3 && argv[3])
68 displayName = argv[3];
69
70 if ( argc > 4 && stricmp(argv[4], "auto") == 0)
71 auto_start = TRUE;
72
73 strcat(filename, " service");
74 if ( !lutil_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 ( !lutil_srv_remove(svcName, filename) )
89 {
90 fputs( "failed to remove the service ...\n", stderr );
91 return EXIT_FAILURE;
92 }
93 fputs( "service has been removed ...\n", stderr );
94 return EXIT_SUCCESS;
95 }
96 if ( _stricmp( "service", argv[1] ) == 0 )
97 {
98 is_NT_Service = 1;
99 *fname_start = '\0';
100 SetCurrentDirectory( filename );
101 }
102 }
103
104 if (is_NT_Service)
105 {
106 StartServiceCtrlDispatcher(DispatchTable);
107 } else
108 {
109 ServiceMain( argc, argv );
110 }
111
112 return EXIT_SUCCESS;
113 }
114
115 #endif
116