2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2005, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23 * related information and legal notices. It is expected that any future
24 * projects/authors will amend these files as needed.
28 * service.c: Windows services support
30 * Known contributors to this file:
31 * Ron Koenderink, 2004
37 #include "prototypes.h"
42 install_service(char *program_name, char *service_name, char *config_file)
45 HANDLE schSCManager,schService;
46 LPCTSTR lpszBinaryPathName;
47 SERVICE_DESCRIPTION sdBuf;
49 if (strrchr(program_name,'\\') == NULL) {
50 GetCurrentDirectory(sizeof(strDir), strDir);
52 strcat(strDir, program_name);
54 strcpy(strDir, program_name);
56 if (config_file != NULL) {
57 strcat(strDir, " -e ");
58 strcat(strDir, config_file);
61 if (service_name == NULL)
62 service_name = DEFAULT_SERVICE_NAME;
63 else if (service_name[0] == '\0')
64 service_name = DEFAULT_SERVICE_NAME;
66 schSCManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
68 if (schSCManager == NULL) {
69 fprintf(stderr, "install_service failed to open Service Control Manager\n");
73 lpszBinaryPathName = strDir;
75 schService = CreateService(schSCManager,
77 service_name, /* service name to display */
78 SERVICE_ALL_ACCESS, /* desired access */
79 SERVICE_WIN32_OWN_PROCESS, /* service type */
80 SERVICE_AUTO_START, /* start type */
81 SERVICE_ERROR_NORMAL, /* error control type */
82 lpszBinaryPathName, /* service's binary */
83 NULL, /* no load ordering group */
84 NULL, /* no tag identifier */
85 NULL, /* database service dependency */
86 NULL, /* LocalSystem account */
87 NULL); /* no password */
89 if (schService == NULL) {
90 fprintf(stderr, "install_service failed to create service %s\n", service_name);
93 sdBuf.lpDescription = "Server for Empire game";
95 if(!ChangeServiceConfig2(
96 schService, /* handle to service */
97 SERVICE_CONFIG_DESCRIPTION, /* change: description */
98 &sdBuf)) { /* value: new description */
99 fprintf(stderr, "install_service failed to set the description\n");
102 printf("Service %s installed.\n", service_name);
103 CloseServiceHandle(schService);
108 remove_service(char *service_name)
113 if (service_name == NULL)
114 service_name = DEFAULT_SERVICE_NAME;
115 else if (service_name[0] == '\0')
116 service_name = DEFAULT_SERVICE_NAME;
118 schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
120 if (schSCManager == NULL) {
121 fprintf(stderr, "remove_service failed to open Service Control Manager\n");
125 hService = OpenService(schSCManager, service_name, SERVICE_ALL_ACCESS);
127 if (hService == NULL) {
128 fprintf(stderr, "remove_service failed to open service %s\n", service_name);
132 if (DeleteService(hService) == 0) {
133 fprintf(stderr, "remove_service failed to remove service %s\n", service_name);
137 if (CloseServiceHandle(hService) == 0) {
138 fprintf(stderr, "remove_service failed to close service %s\n", service_name);
141 printf("Service %s removed.\n", service_name);
146 static SERVICE_STATUS service_status;
147 static SERVICE_STATUS_HANDLE service_status_handle;
150 service_ctrl_handler(DWORD Opcode)
154 case SERVICE_CONTROL_PAUSE:
155 service_status.dwCurrentState = SERVICE_PAUSED;
156 logerror("Pausing the service not supported");
159 case SERVICE_CONTROL_CONTINUE:
160 logerror("Continuing the service not supported");
161 service_status.dwCurrentState = SERVICE_RUNNING;
164 case SERVICE_CONTROL_STOP:
165 logerror("Service stopping");
166 empth_request_shutdown();
169 case SERVICE_CONTROL_INTERROGATE:
170 /* Fall through to send current status. */
174 logerror("Unrecognized opcode %ld in ServiceCtrlHandler",
178 /* Send current status. */
179 if (!SetServiceStatus (service_status_handle, &service_status))
180 logerror("SetServiceStatus error %ld",GetLastError());
185 service_main(DWORD argc, LPTSTR *argv)
187 service_status.dwServiceType = SERVICE_WIN32;
188 service_status.dwCurrentState = SERVICE_START_PENDING;
189 service_status.dwControlsAccepted = SERVICE_ACCEPT_STOP;
190 service_status.dwWin32ExitCode = 0;
191 service_status.dwServiceSpecificExitCode = 0;
192 service_status.dwCheckPoint = 0;
193 service_status.dwWaitHint = 0;
195 service_status_handle = RegisterServiceCtrlHandler(
196 DEFAULT_SERVICE_NAME, service_ctrl_handler);
198 if (service_status_handle == (SERVICE_STATUS_HANDLE)0) {
199 logerror("RegisterServiceCtrlHandler failed %d\n", GetLastError());
206 /* Initialization complete - report running status. */
207 service_status.dwCurrentState = SERVICE_RUNNING;
208 service_status.dwCheckPoint = 0;
209 service_status.dwWaitHint = 0;
211 if (!SetServiceStatus (service_status_handle, &service_status)) {
212 logerror("SetServiceStatus error %ld\n", GetLastError());
217 CANT_HAPPEN("main thread terminated");
224 logerror("Service stopped");
225 service_status.dwWin32ExitCode = 0;
226 service_status.dwCurrentState = SERVICE_STOPPED;
227 service_status.dwCheckPoint = 0;
228 service_status.dwWaitHint = 0;
230 if (!SetServiceStatus (service_status_handle, &service_status))
231 logerror("Error while stopping service SetServiceStatus"
232 " error %ld", GetLastError());