2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2006, 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 files README, COPYING and CREDITS in the root of the source
23 * tree for related information and legal notices. It is expected
24 * that future projects/authors will amend these files as needed.
28 * service.c: Windows services support
30 * Known contributors to this file:
31 * Ron Koenderink, 2004
40 #include "empthread.h"
41 #include "prototypes.h"
44 install_service(char *program_name, char *service_name, char *config_file)
46 HANDLE schSCManager,schService;
47 SERVICE_DESCRIPTION sdBuf;
49 if (config_file != NULL)
50 snprintf(&program_name[strlen(program_name)], _MAX_PATH-strlen(program_name), " -e %s",
53 if (service_name == NULL)
54 service_name = DEFAULT_SERVICE_NAME;
55 else if (service_name[0] == '\0')
56 service_name = DEFAULT_SERVICE_NAME;
58 schSCManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
60 if (schSCManager == NULL) {
61 fprintf(stderr, "install_service failed to open Service Control Manager\n");
65 schService = CreateService(schSCManager,
67 service_name, /* service name to display */
68 SERVICE_ALL_ACCESS, /* desired access */
69 SERVICE_WIN32_OWN_PROCESS, /* service type */
70 SERVICE_AUTO_START, /* start type */
71 SERVICE_ERROR_NORMAL, /* error control type */
72 program_name, /* service's binary */
73 NULL, /* no load ordering group */
74 NULL, /* no tag identifier */
75 NULL, /* database service dependency */
76 NULL, /* LocalSystem account */
77 NULL); /* no password */
79 if (schService == NULL) {
80 fprintf(stderr, "install_service failed to create service %s\n", service_name);
83 sdBuf.lpDescription = "Server for Empire game";
85 if(!ChangeServiceConfig2(
86 schService, /* handle to service */
87 SERVICE_CONFIG_DESCRIPTION, /* change: description */
88 &sdBuf)) { /* value: new description */
89 fprintf(stderr, "install_service failed to set the description\n");
92 printf("Service %s installed.\n", service_name);
93 CloseServiceHandle(schService);
98 remove_service(char *service_name)
103 if (service_name == NULL)
104 service_name = DEFAULT_SERVICE_NAME;
105 else if (service_name[0] == '\0')
106 service_name = DEFAULT_SERVICE_NAME;
108 schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
110 if (schSCManager == NULL) {
111 fprintf(stderr, "remove_service failed to open Service Control Manager\n");
115 hService = OpenService(schSCManager, service_name, SERVICE_ALL_ACCESS);
117 if (hService == NULL) {
118 fprintf(stderr, "remove_service failed to open service %s\n", service_name);
122 if (DeleteService(hService) == 0) {
123 fprintf(stderr, "remove_service failed to remove service %s\n", service_name);
127 if (CloseServiceHandle(hService) == 0) {
128 fprintf(stderr, "remove_service failed to close service %s\n", service_name);
131 printf("Service %s removed.\n", service_name);
136 static SERVICE_STATUS service_status;
137 static SERVICE_STATUS_HANDLE service_status_handle;
140 service_ctrl_handler(DWORD Opcode)
143 case SERVICE_CONTROL_PAUSE:
144 service_status.dwCurrentState = SERVICE_PAUSED;
145 logerror("Pausing the service not supported");
148 case SERVICE_CONTROL_CONTINUE:
149 logerror("Continuing the service not supported");
150 service_status.dwCurrentState = SERVICE_RUNNING;
153 case SERVICE_CONTROL_STOP:
154 logerror("Service stopping");
155 empth_request_shutdown();
158 case SERVICE_CONTROL_INTERROGATE:
159 /* Fall through to send current status. */
163 logerror("Unrecognized opcode %ld in ServiceCtrlHandler",
167 /* Send current status. */
168 if (!SetServiceStatus (service_status_handle, &service_status))
169 logerror("SetServiceStatus error %ld",GetLastError());
174 service_main(DWORD argc, LPTSTR *argv)
178 service_status.dwServiceType = SERVICE_WIN32;
179 service_status.dwCurrentState = SERVICE_START_PENDING;
180 service_status.dwControlsAccepted = SERVICE_ACCEPT_STOP;
181 service_status.dwWin32ExitCode = 0;
182 service_status.dwServiceSpecificExitCode = 0;
183 service_status.dwCheckPoint = 0;
184 service_status.dwWaitHint = 0;
186 service_status_handle = RegisterServiceCtrlHandler(
187 DEFAULT_SERVICE_NAME, service_ctrl_handler);
189 if (service_status_handle == (SERVICE_STATUS_HANDLE)0) {
190 logerror("RegisterServiceCtrlHandler failed %lu\n", GetLastError());
197 /* Initialization complete - report running status. */
198 service_status.dwCurrentState = SERVICE_RUNNING;
199 service_status.dwCheckPoint = 0;
200 service_status.dwWaitHint = 0;
202 if (!SetServiceStatus (service_status_handle, &service_status)) {
203 logerror("SetServiceStatus error %ld\n", GetLastError());
206 sig = empth_wait_for_signal();
217 logerror("Service stopped");
218 service_status.dwWin32ExitCode = 0;
219 service_status.dwCurrentState = SERVICE_STOPPED;
220 service_status.dwCheckPoint = 0;
221 service_status.dwWaitHint = 0;
223 if (!SetServiceStatus (service_status_handle, &service_status))
224 logerror("Error while stopping service SetServiceStatus"
225 " error %ld", GetLastError());