2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2008, 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
39 #include "empthread.h"
40 #include "prototypes.h"
43 install_service(char *program_name, char *service_name, char *config_file)
45 HANDLE schSCManager,schService;
46 SERVICE_DESCRIPTION sdBuf;
48 if (config_file != NULL)
49 snprintf(&program_name[strlen(program_name)], _MAX_PATH-strlen(program_name), " -e %s",
52 if (service_name == NULL)
53 service_name = DEFAULT_SERVICE_NAME;
54 else if (service_name[0] == '\0')
55 service_name = DEFAULT_SERVICE_NAME;
57 schSCManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
59 if (schSCManager == NULL) {
60 fprintf(stderr, "install_service failed to open Service Control Manager\n");
64 schService = CreateService(schSCManager,
66 service_name, /* service name to display */
67 SERVICE_ALL_ACCESS, /* desired access */
68 SERVICE_WIN32_OWN_PROCESS, /* service type */
69 SERVICE_AUTO_START, /* start type */
70 SERVICE_ERROR_NORMAL, /* error control type */
71 program_name, /* service's binary */
72 NULL, /* no load ordering group */
73 NULL, /* no tag identifier */
74 NULL, /* database service dependency */
75 NULL, /* LocalSystem account */
76 NULL); /* no password */
78 if (schService == NULL) {
79 fprintf(stderr, "install_service failed to create service %s\n", service_name);
82 sdBuf.lpDescription = "Server for Empire game";
84 if(!ChangeServiceConfig2(
85 schService, /* handle to service */
86 SERVICE_CONFIG_DESCRIPTION, /* change: description */
87 &sdBuf)) { /* value: new description */
88 fprintf(stderr, "install_service failed to set the description\n");
91 printf("Service %s installed.\n", service_name);
92 CloseServiceHandle(schService);
97 remove_service(char *service_name)
102 if (service_name == NULL)
103 service_name = DEFAULT_SERVICE_NAME;
104 else if (service_name[0] == '\0')
105 service_name = DEFAULT_SERVICE_NAME;
107 schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
109 if (schSCManager == NULL) {
110 fprintf(stderr, "remove_service failed to open Service Control Manager\n");
114 hService = OpenService(schSCManager, service_name, SERVICE_ALL_ACCESS);
116 if (hService == NULL) {
117 fprintf(stderr, "remove_service failed to open service %s\n", service_name);
121 if (DeleteService(hService) == 0) {
122 fprintf(stderr, "remove_service failed to remove service %s\n", service_name);
126 if (CloseServiceHandle(hService) == 0) {
127 fprintf(stderr, "remove_service failed to close service %s\n", service_name);
130 printf("Service %s removed.\n", service_name);
135 static SERVICE_STATUS service_status;
136 static SERVICE_STATUS_HANDLE service_status_handle;
139 service_ctrl_handler(DWORD Opcode)
142 case SERVICE_CONTROL_PAUSE:
143 service_status.dwCurrentState = SERVICE_PAUSED;
144 logerror("Pausing the service not supported");
147 case SERVICE_CONTROL_CONTINUE:
148 logerror("Continuing the service not supported");
149 service_status.dwCurrentState = SERVICE_RUNNING;
152 case SERVICE_CONTROL_STOP:
153 logerror("Service stopping");
154 empth_request_shutdown();
157 case SERVICE_CONTROL_INTERROGATE:
158 /* Fall through to send current status. */
162 logerror("Unrecognized opcode %ld in ServiceCtrlHandler",
166 /* Send current status. */
167 if (!SetServiceStatus (service_status_handle, &service_status))
168 logerror("SetServiceStatus error %ld",GetLastError());
173 service_main(DWORD argc, LPTSTR *argv)
177 service_status.dwServiceType = SERVICE_WIN32;
178 service_status.dwCurrentState = SERVICE_START_PENDING;
179 service_status.dwControlsAccepted = SERVICE_ACCEPT_STOP;
180 service_status.dwWin32ExitCode = 0;
181 service_status.dwServiceSpecificExitCode = 0;
182 service_status.dwCheckPoint = 0;
183 service_status.dwWaitHint = 0;
185 service_status_handle = RegisterServiceCtrlHandler(
186 DEFAULT_SERVICE_NAME, service_ctrl_handler);
188 if (service_status_handle == (SERVICE_STATUS_HANDLE)0) {
189 logerror("RegisterServiceCtrlHandler failed %lu\n", GetLastError());
196 /* Initialization complete - report running status. */
197 service_status.dwCurrentState = SERVICE_RUNNING;
198 service_status.dwCheckPoint = 0;
199 service_status.dwWaitHint = 0;
201 if (!SetServiceStatus (service_status_handle, &service_status)) {
202 logerror("SetServiceStatus error %ld\n", GetLastError());
205 sig = empth_wait_for_signal();
216 logerror("Service stopped");
217 service_status.dwWin32ExitCode = 0;
218 service_status.dwCurrentState = SERVICE_STOPPED;
219 service_status.dwCheckPoint = 0;
220 service_status.dwWaitHint = 0;
222 if (!SetServiceStatus (service_status_handle, &service_status))
223 logerror("Error while stopping service SetServiceStatus"
224 " error %ld", GetLastError());