2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2004, 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
39 #include "prototypes.h"
41 #include "../gen/getopt.h"
45 char *config_file = NULL;
48 install_service(char *program_name, char *service_name, int datadir_set)
51 HANDLE schSCManager,schService;
52 LPCTSTR lpszBinaryPathName;
53 SERVICE_DESCRIPTION sdBuf;
55 if (strrchr(program_name,'\\') == NULL) {
56 GetCurrentDirectory(sizeof(strDir), strDir);
58 strcat(strDir, program_name);
60 strcpy(strDir, program_name);
63 strcat(strDir, " -D ");
64 strcat(strDir, datadir);
66 if (config_file != NULL) {
67 strcat(strDir, " -e ");
68 strcat(strDir, config_file);
71 if (service_name == NULL)
72 service_name = DEFAULT_SERVICE_NAME;
73 else if (service_name[0] == '\0')
74 service_name = DEFAULT_SERVICE_NAME;
76 schSCManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
78 if (schSCManager == NULL) {
79 logerror("install_service failed to open Service Control Manager");
80 printf("Install service: failed to open Service Control Manager.\n");
84 lpszBinaryPathName = strDir;
86 schService = CreateService(schSCManager,
88 service_name, /* service name to display */
89 SERVICE_ALL_ACCESS, /* desired access */
90 SERVICE_WIN32_OWN_PROCESS, /* service type */
91 SERVICE_AUTO_START, /* start type */
92 SERVICE_ERROR_NORMAL, /* error control type */
93 lpszBinaryPathName, /* service's binary */
94 NULL, /* no load ordering group */
95 NULL, /* no tag identifier */
96 NULL, /* database service dependency */
97 NULL, /* LocalSystem account */
98 NULL); /* no password */
100 if (schService == NULL) {
101 logerror("install_service failed to create service %s", service_name);
102 printf("Install service: failed to create service %s.\n", service_name);
105 sdBuf.lpDescription = "Server for Empire game";
107 if(!ChangeServiceConfig2(
108 schService, /* handle to service */
109 SERVICE_CONFIG_DESCRIPTION, /* change: description */
110 &sdBuf)) { /* value: new description */
111 logerror("install_service failed to set the description");
112 printf("Install service: failed to set the description.\n");
115 logerror("install_service successfully created the service %s", service_name);
116 printf("Service %s installed.\n", service_name);
117 CloseServiceHandle(schService);
122 remove_service(char *service_name)
127 if (service_name == NULL)
128 service_name = DEFAULT_SERVICE_NAME;
129 else if (service_name[0] == '\0')
130 service_name = DEFAULT_SERVICE_NAME;
132 schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
134 if (schSCManager == NULL) {
135 logerror("remove_service failed to open Service Control Manager");
136 printf("remove service: failed to open Service Control Manager.\n");
140 hService = OpenService(schSCManager, service_name, SERVICE_ALL_ACCESS);
142 if (hService == NULL) {
143 logerror("remove_service failed to open service %s", service_name);
144 printf("Remove service: failed to open service %s.\n", service_name);
148 if (DeleteService(hService) == 0) {
149 logerror("remove_service failed to remove service %s", service_name);
150 printf("Remove service: failed to remove service %s.\n", service_name);
154 if (CloseServiceHandle(hService) == 0) {
155 logerror("remove_service failed to close service %s", service_name);
156 printf("Remove service: failed to close service %s.\n", service_name);
159 logerror("remove_service successfully removed service %s", service_name);
160 printf("Service %s removed.\n", service_name);
165 static SERVICE_STATUS service_status;
166 static SERVICE_STATUS_HANDLE service_status_handle;
169 service_ctrl_handler(DWORD Opcode)
175 case SERVICE_CONTROL_PAUSE:
176 service_status.dwCurrentState = SERVICE_PAUSED;
177 logerror("Pausing the service not supported");
180 case SERVICE_CONTROL_CONTINUE:
181 logerror("Continuing the service not supported");
182 service_status.dwCurrentState = SERVICE_RUNNING;
185 case SERVICE_CONTROL_STOP:
186 service_status.dwWin32ExitCode = 0;
187 service_status.dwCurrentState = SERVICE_STOPPED;
188 service_status.dwCheckPoint = 0;
189 service_status.dwWaitHint = 0;
191 if (!SetServiceStatus (service_status_handle,
193 status = GetLastError();
194 logerror("Error while stopping service SetServiceStatus"
195 " error %ld", status);
198 logerror("Service stopped");
201 case SERVICE_CONTROL_INTERROGATE:
202 /* Fall through to send current status. */
206 logerror("Unrecognized opcode %ld in ServiceCtrlHandler",
210 /* Send current status. */
211 if (!SetServiceStatus (service_status_handle, &service_status)) {
212 status = GetLastError();
213 logerror("SetServiceStatus error %ld",status);
219 service_main(DWORD argc, LPTSTR *argv)
228 while ((op = getopt(argc, argv, "D:e:")) != EOF) {
234 config_file = optarg;
239 if (config_file == NULL) {
240 sprintf(tbuf, "%s/econfig", datadir);
244 service_status.dwServiceType = SERVICE_WIN32;
245 service_status.dwCurrentState = SERVICE_START_PENDING;
246 service_status.dwControlsAccepted = SERVICE_ACCEPT_STOP;
247 service_status.dwWin32ExitCode = 0;
248 service_status.dwServiceSpecificExitCode = 0;
249 service_status.dwCheckPoint = 0;
250 service_status.dwWaitHint = 0;
252 service_status_handle = RegisterServiceCtrlHandler(
253 DEFAULT_SERVICE_NAME, service_ctrl_handler);
255 if (service_status_handle == (SERVICE_STATUS_HANDLE)0) {
256 logerror("RegisterServiceCtrlHandler failed %d\n", GetLastError());
260 /* Initialization code goes here. */
261 start_server(0, config_file);
263 /* Initialization complete - report running status. */
264 service_status.dwCurrentState = SERVICE_RUNNING;
265 service_status.dwCheckPoint = 0;
266 service_status.dwWaitHint = 0;
268 if (!SetServiceStatus (service_status_handle, &service_status)) {
269 status = GetLastError();
270 logerror("SetServiceStatus error %ld\n",status);
275 /* We should never get here. But, just in case... */
280 // This is where the service does its work.
281 logerror("Returning the Main Thread \n",0);
286 service_stopped(void)
288 if (service_status.dwCurrentState == SERVICE_STOPPED)