xref: /netbsd-src/external/bsd/openldap/dist/servers/slapd/nt_svc.c (revision b7b7574d3bf8eeb51a1fa3977b59142ec6434a55)
1 /*	$NetBSD: nt_svc.c,v 1.1.1.4 2014/05/28 09:58:47 tron Exp $	*/
2 
3 /* $OpenLDAP$ */
4 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5  *
6  * Copyright 1998-2014 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 "portable.h"
19 #include <stdio.h>
20 #include <ac/string.h>
21 #include "slap.h"
22 #include "lutil.h"
23 
24 #ifdef HAVE_NT_SERVICE_MANAGER
25 
26 /* in main.c */
27 void WINAPI ServiceMain( DWORD argc, LPTSTR *argv );
28 
29 /* in ntservice.c */
30 int main( int argc, LPTSTR *argv )
31 {
32 	int		length;
33 	char	filename[MAX_PATH], *fname_start;
34 
35 	/*
36 	 * Because the service was registered as SERVICE_WIN32_OWN_PROCESS,
37 	 * the lpServiceName element of the SERVICE_TABLE_ENTRY will be
38 	 * ignored.
39 	 */
40 
41 	SERVICE_TABLE_ENTRY		DispatchTable[] = {
42 		{	"",	(LPSERVICE_MAIN_FUNCTION) ServiceMain	},
43 		{	NULL,			NULL	}
44 	};
45 
46 	/*
47 	 * set the service's current directory to the installation directory
48 	 * for the service. this way we don't have to write absolute paths
49 	 * in the configuration files
50 	 */
51 	GetModuleFileName( NULL, filename, sizeof( filename ) );
52 	fname_start = strrchr( filename, *LDAP_DIRSEP );
53 
54 	if ( argc > 1 ) {
55 		if ( _stricmp( "install", argv[1] ) == 0 )
56 		{
57 			char *svcName = SERVICE_NAME;
58 			char *displayName = "OpenLDAP Directory Service";
59 			BOOL auto_start = FALSE;
60 
61 			if ( (argc > 2) && (argv[2] != NULL) )
62 				svcName = argv[2];
63 
64 			if ( argc > 3 && argv[3])
65 				displayName = argv[3];
66 
67 			if ( argc > 4 && stricmp(argv[4], "auto") == 0)
68 				auto_start = TRUE;
69 
70 			strcat(filename, " service");
71 			if ( !lutil_srv_install(svcName, displayName, filename, auto_start) )
72 			{
73 				fputs( "service failed installation ...\n", stderr  );
74 				return EXIT_FAILURE;
75 			}
76 			fputs( "service has been installed ...\n", stderr  );
77 			return EXIT_SUCCESS;
78 		}
79 
80 		if ( _stricmp( "remove", argv[1] ) == 0 )
81 		{
82 			char *svcName = SERVICE_NAME;
83 			if ( (argc > 2) && (argv[2] != NULL) )
84 				svcName = argv[2];
85 			if ( !lutil_srv_remove(svcName, filename) )
86 			{
87 				fputs( "failed to remove the service ...\n", stderr  );
88 				return EXIT_FAILURE;
89 			}
90 			fputs( "service has been removed ...\n", stderr );
91 			return EXIT_SUCCESS;
92 		}
93 		if ( _stricmp( "service", argv[1] ) == 0 )
94 		{
95 			is_NT_Service = 1;
96 			*fname_start = '\0';
97 			SetCurrentDirectory( filename );
98 		}
99 	}
100 
101 	if (is_NT_Service)
102 	{
103 		StartServiceCtrlDispatcher(DispatchTable);
104 	} else
105 	{
106 		ServiceMain( argc, argv );
107 	}
108 
109 	return EXIT_SUCCESS;
110 }
111 
112 #endif
113