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
37 #include "prototypes.h"
42 install_service(char *program_name, char *service_name, int datadir_set, 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);
57 strcat(strDir, " -D ");
58 strcat(strDir, datadir);
60 if (config_file != NULL) {
61 strcat(strDir, " -e ");
62 strcat(strDir, config_file);
65 if (service_name == NULL)
66 service_name = DEFAULT_SERVICE_NAME;
67 else if (service_name[0] == '\0')
68 service_name = DEFAULT_SERVICE_NAME;
70 schSCManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
72 if (schSCManager == NULL) {
73 fprintf(stderr, "install_service failed to open Service Control Manager\n");
77 lpszBinaryPathName = strDir;
79 schService = CreateService(schSCManager,
81 service_name, /* service name to display */
82 SERVICE_ALL_ACCESS, /* desired access */
83 SERVICE_WIN32_OWN_PROCESS, /* service type */
84 SERVICE_AUTO_START, /* start type */
85 SERVICE_ERROR_NORMAL, /* error control type */
86 lpszBinaryPathName, /* service's binary */
87 NULL, /* no load ordering group */
88 NULL, /* no tag identifier */
89 NULL, /* database service dependency */
90 NULL, /* LocalSystem account */
91 NULL); /* no password */
93 if (schService == NULL) {
94 fprintf(stderr, "install_service failed to create service %s\n", service_name);
97 sdBuf.lpDescription = "Server for Empire game";
99 if(!ChangeServiceConfig2(
100 schService, /* handle to service */
101 SERVICE_CONFIG_DESCRIPTION, /* change: description */
102 &sdBuf)) { /* value: new description */
103 fprintf(stderr, "install_service failed to set the description\n");
106 printf("Service %s installed.\n", service_name);
107 CloseServiceHandle(schService);
112 remove_service(char *service_name)
117 if (service_name == NULL)
118 service_name = DEFAULT_SERVICE_NAME;
119 else if (service_name[0] == '\0')
120 service_name = DEFAULT_SERVICE_NAME;
122 schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
124 if (schSCManager == NULL) {
125 fprintf(stderr, "remove_service failed to open Service Control Manager\n");
129 hService = OpenService(schSCManager, service_name, SERVICE_ALL_ACCESS);
131 if (hService == NULL) {
132 fprintf(stderr, "remove_service failed to open service %s\n", service_name);
136 if (DeleteService(hService) == 0) {
137 fprintf(stderr, "remove_service failed to remove service %s\n", service_name);
141 if (CloseServiceHandle(hService) == 0) {
142 fprintf(stderr, "remove_service failed to close service %s\n", service_name);
145 printf("Service %s removed.\n", service_name);
150 static SERVICE_STATUS service_status;
151 static SERVICE_STATUS_HANDLE service_status_handle;
154 service_ctrl_handler(DWORD Opcode)
158 case SERVICE_CONTROL_PAUSE:
159 service_status.dwCurrentState = SERVICE_PAUSED;
160 logerror("Pausing the service not supported");
163 case SERVICE_CONTROL_CONTINUE:
164 logerror("Continuing the service not supported");
165 service_status.dwCurrentState = SERVICE_RUNNING;
168 case SERVICE_CONTROL_STOP:
169 logerror("Service stopping");
170 empth_request_shutdown();
173 case SERVICE_CONTROL_INTERROGATE:
174 /* Fall through to send current status. */
178 logerror("Unrecognized opcode %ld in ServiceCtrlHandler",
182 /* Send current status. */
183 if (!SetServiceStatus (service_status_handle, &service_status))
184 logerror("SetServiceStatus error %ld",GetLastError());
189 service_main(DWORD argc, LPTSTR *argv)
191 service_status.dwServiceType = SERVICE_WIN32;
192 service_status.dwCurrentState = SERVICE_START_PENDING;
193 service_status.dwControlsAccepted = SERVICE_ACCEPT_STOP;
194 service_status.dwWin32ExitCode = 0;
195 service_status.dwServiceSpecificExitCode = 0;
196 service_status.dwCheckPoint = 0;
197 service_status.dwWaitHint = 0;
199 service_status_handle = RegisterServiceCtrlHandler(
200 DEFAULT_SERVICE_NAME, service_ctrl_handler);
202 if (service_status_handle == (SERVICE_STATUS_HANDLE)0) {
203 logerror("RegisterServiceCtrlHandler failed %d\n", GetLastError());
210 /* Initialization complete - report running status. */
211 service_status.dwCurrentState = SERVICE_RUNNING;
212 service_status.dwCheckPoint = 0;
213 service_status.dwWaitHint = 0;
215 if (!SetServiceStatus (service_status_handle, &service_status)) {
216 logerror("SetServiceStatus error %ld\n", GetLastError());
221 CANT_HAPPEN("main thread terminated");
228 logerror("Service stopped");
229 service_status.dwWin32ExitCode = 0;
230 service_status.dwCurrentState = SERVICE_STOPPED;
231 service_status.dwCheckPoint = 0;
232 service_status.dwWaitHint = 0;
234 if (!SetServiceStatus (service_status_handle, &service_status))
235 logerror("Error while stopping service SetServiceStatus"
236 " error %ld", GetLastError());