2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2010, 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-2009
37 #include "empthread.h"
38 #include "prototypes.h"
41 install_service(char *program_name, char *service_name, char *config_file)
43 HANDLE schSCManager,schService;
44 SERVICE_DESCRIPTION sdBuf;
46 if (config_file != NULL)
47 snprintf(&program_name[strlen(program_name)],
48 _MAX_PATH - strlen(program_name),
49 " -e %s", config_file);
51 if (service_name == NULL)
52 service_name = DEFAULT_SERVICE_NAME;
53 else if (service_name[0] == '\0')
54 service_name = DEFAULT_SERVICE_NAME;
56 schSCManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
58 if (schSCManager == NULL) {
59 fprintf(stderr, "install_service failed to open Service Control Manager\n");
63 schService = CreateService(schSCManager,
65 service_name, /* service name to display */
66 SERVICE_ALL_ACCESS, /* desired access */
67 SERVICE_WIN32_OWN_PROCESS, /* service type */
68 SERVICE_AUTO_START, /* start type */
69 SERVICE_ERROR_NORMAL, /* error control type */
70 program_name, /* service's binary */
71 NULL, /* no load ordering group */
72 NULL, /* no tag identifier */
73 NULL, /* database service dependency */
74 NULL, /* LocalSystem account */
75 NULL); /* no password */
77 if (schService == NULL) {
78 fprintf(stderr, "install_service failed to create service %s\n", service_name);
81 sdBuf.lpDescription = "Server for Empire game";
83 if(!ChangeServiceConfig2(
84 schService, /* handle to service */
85 SERVICE_CONFIG_DESCRIPTION, /* change: description */
86 &sdBuf)) { /* value: new description */
87 fprintf(stderr, "install_service failed to set the description\n");
90 printf("Service %s installed.\n", service_name);
91 CloseServiceHandle(schService);
96 remove_service(char *service_name)
101 if (service_name == NULL)
102 service_name = DEFAULT_SERVICE_NAME;
103 else if (service_name[0] == '\0')
104 service_name = DEFAULT_SERVICE_NAME;
106 schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
108 if (schSCManager == NULL) {
109 fprintf(stderr, "remove_service failed to open Service Control Manager\n");
113 hService = OpenService(schSCManager, service_name, SERVICE_ALL_ACCESS);
115 if (hService == NULL) {
116 fprintf(stderr, "remove_service failed to open service %s\n", service_name);
120 if (DeleteService(hService) == 0) {
121 fprintf(stderr, "remove_service failed to remove service %s\n", service_name);
125 if (CloseServiceHandle(hService) == 0) {
126 fprintf(stderr, "remove_service failed to close service %s\n", service_name);
129 printf("Service %s removed.\n", service_name);
134 static SERVICE_STATUS service_status;
135 static SERVICE_STATUS_HANDLE service_status_handle;
138 service_ctrl_handler(DWORD Opcode)
141 case SERVICE_CONTROL_PAUSE:
142 service_status.dwCurrentState = SERVICE_PAUSED;
143 logerror("Pausing the service not supported");
146 case SERVICE_CONTROL_CONTINUE:
147 logerror("Continuing the service not supported");
148 service_status.dwCurrentState = SERVICE_RUNNING;
151 case SERVICE_CONTROL_STOP:
152 logerror("Service stopping");
153 empth_request_shutdown();
156 case SERVICE_CONTROL_INTERROGATE:
157 /* Fall through to send current status. */
161 logerror("Unrecognized opcode %ld in ServiceCtrlHandler",
165 /* Send current status. */
166 if (!SetServiceStatus (service_status_handle, &service_status))
167 logerror("SetServiceStatus error %ld",GetLastError());
172 service_main(DWORD argc, LPTSTR *argv)
176 service_status.dwServiceType = SERVICE_WIN32;
177 service_status.dwCurrentState = SERVICE_START_PENDING;
178 service_status.dwControlsAccepted = SERVICE_ACCEPT_STOP;
179 service_status.dwWin32ExitCode = 0;
180 service_status.dwServiceSpecificExitCode = 0;
181 service_status.dwCheckPoint = 0;
182 service_status.dwWaitHint = 0;
184 service_status_handle = RegisterServiceCtrlHandler(
185 DEFAULT_SERVICE_NAME, service_ctrl_handler);
187 if (service_status_handle == (SERVICE_STATUS_HANDLE)0) {
188 logerror("RegisterServiceCtrlHandler failed %lu\n", GetLastError());
195 /* Initialization complete - report running status. */
196 service_status.dwCurrentState = SERVICE_RUNNING;
197 service_status.dwCheckPoint = 0;
198 service_status.dwWaitHint = 0;
200 if (!SetServiceStatus (service_status_handle, &service_status)) {
201 logerror("SetServiceStatus error %ld\n", GetLastError());
204 sig = empth_wait_for_signal();
215 logerror("Service stopped");
216 service_status.dwWin32ExitCode = 0;
217 service_status.dwCurrentState = SERVICE_STOPPED;
218 service_status.dwCheckPoint = 0;
219 service_status.dwWaitHint = 0;
221 if (!SetServiceStatus (service_status_handle, &service_status))
222 logerror("Error while stopping service SetServiceStatus"
223 " error %ld", GetLastError());