]> git.pond.sub.org Git - empserver/blob - src/lib/gen/service.c
(main,print_usage) [_WIN32]: Add the ability to enable
[empserver] / src / lib / gen / service.c
1 /*
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
5  *
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.
10  *
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.
15  *
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
19  *
20  *  ---
21  *
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.
25  *
26  *  ---
27  *
28  *  service.c: Windows services support
29  * 
30  *  Known contributors to this file:
31  *     Ron Koenderink, 2004
32  */
33
34 #ifdef _WIN32
35 #include <windows.h>
36
37 #include "prototypes.h"
38 #include "service.h"
39 #include "optlist.h"
40
41 int
42 install_service(char *program_name, char *service_name, int datadir_set, char *config_file)
43 {
44     char strDir[1024];
45     HANDLE schSCManager,schService;
46     LPCTSTR lpszBinaryPathName;
47     SERVICE_DESCRIPTION sdBuf;
48
49     if (strrchr(program_name,'\\') == NULL) {
50         GetCurrentDirectory(sizeof(strDir), strDir);
51         strcat(strDir, "\\");
52         strcat(strDir, program_name);
53     } else
54         strcpy(strDir, program_name);
55
56     if (datadir_set) {
57         strcat(strDir, " -D ");
58         strcat(strDir, datadir);
59     }
60     if (config_file != NULL) {
61         strcat(strDir, " -e ");
62         strcat(strDir, config_file);
63     }
64
65     if (service_name == NULL)
66         service_name = DEFAULT_SERVICE_NAME;
67     else if (service_name[0] == '\0')
68         service_name = DEFAULT_SERVICE_NAME;
69
70     schSCManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
71
72     if (schSCManager == NULL) {
73         fprintf(stderr, "install_service failed to open Service Control Manager\n"); 
74         return EXIT_FAILURE;
75     }
76
77     lpszBinaryPathName = strDir;
78
79     schService = CreateService(schSCManager,
80         service_name,
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 */
92  
93     if (schService == NULL) {
94         fprintf(stderr, "install_service failed to create service %s\n", service_name);
95         return EXIT_FAILURE;
96     }
97     sdBuf.lpDescription = "Server for Empire game";
98
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");
104     }
105
106     printf("Service %s installed.\n", service_name);
107     CloseServiceHandle(schService);
108     return EXIT_SUCCESS;
109 }
110
111 int
112 remove_service(char *service_name)
113 {
114     HANDLE schSCManager;
115     SC_HANDLE hService;
116
117     if (service_name == NULL)
118         service_name = DEFAULT_SERVICE_NAME;
119     else if (service_name[0] == '\0')
120         service_name = DEFAULT_SERVICE_NAME;
121
122     schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
123
124     if (schSCManager == NULL) {
125         fprintf(stderr, "remove_service failed to open Service Control Manager\n"); 
126         return EXIT_FAILURE;
127     }
128
129     hService = OpenService(schSCManager, service_name, SERVICE_ALL_ACCESS);
130
131     if (hService == NULL) {
132         fprintf(stderr, "remove_service failed to open service %s\n", service_name);
133         return EXIT_FAILURE;
134     }
135
136     if (DeleteService(hService) == 0) {
137         fprintf(stderr, "remove_service failed to remove service %s\n", service_name);
138         return EXIT_FAILURE;
139     }
140
141     if (CloseServiceHandle(hService) == 0) {
142         fprintf(stderr, "remove_service failed to close service %s\n", service_name); 
143         return EXIT_FAILURE;
144     } else {
145         printf("Service %s removed.\n", service_name); 
146         return EXIT_SUCCESS;
147     }
148 }
149
150 static SERVICE_STATUS           service_status; 
151 static SERVICE_STATUS_HANDLE    service_status_handle;
152 static HANDLE                   hShutdownEvent = NULL;
153
154 void WINAPI
155 service_ctrl_handler(DWORD Opcode) 
156
157     switch(Opcode) 
158     { 
159         case SERVICE_CONTROL_PAUSE: 
160             service_status.dwCurrentState = SERVICE_PAUSED;
161             logerror("Pausing the service not supported");
162             break; 
163  
164         case SERVICE_CONTROL_CONTINUE: 
165             logerror("Continuing the service not supported");
166             service_status.dwCurrentState = SERVICE_RUNNING; 
167             break; 
168  
169         case SERVICE_CONTROL_STOP:
170             logerror("Service stopping");
171             SetEvent(hShutdownEvent);
172             return; 
173  
174         case SERVICE_CONTROL_INTERROGATE: 
175         /* Fall through to send current status.  */
176             break; 
177  
178         default: 
179             logerror("Unrecognized opcode %ld in ServiceCtrlHandler", 
180                 Opcode); 
181     } 
182  
183     /* Send current status. */
184     if (!SetServiceStatus (service_status_handle,  &service_status))
185         logerror("SetServiceStatus error %ld",GetLastError()); 
186     return; 
187
188
189 void WINAPI
190 service_main(DWORD argc, LPTSTR *argv)
191 {
192     service_status.dwServiceType        = SERVICE_WIN32; 
193     service_status.dwCurrentState       = SERVICE_START_PENDING; 
194     service_status.dwControlsAccepted   = SERVICE_ACCEPT_STOP; 
195     service_status.dwWin32ExitCode      = 0; 
196     service_status.dwServiceSpecificExitCode = 0; 
197     service_status.dwCheckPoint         = 0; 
198     service_status.dwWaitHint           = 0; 
199  
200     service_status_handle = RegisterServiceCtrlHandler(
201         DEFAULT_SERVICE_NAME, service_ctrl_handler);
202  
203     if (service_status_handle == (SERVICE_STATUS_HANDLE)0) { 
204         logerror("RegisterServiceCtrlHandler failed %d\n", GetLastError());
205         finish_server();
206         return;
207     }
208  
209     if ((hShutdownEvent = CreateEvent(NULL, TRUE, FALSE, NULL)) == NULL) {
210         logerror("CreateEvent for Shutdown failed %d\n", GetLastError());
211         finish_server();
212         return;
213     }
214
215     start_server(0);
216  
217     /* Initialization complete - report running status. */
218     service_status.dwCurrentState       = SERVICE_RUNNING; 
219     service_status.dwCheckPoint         = 0; 
220     service_status.dwWaitHint           = 0; 
221  
222     if (!SetServiceStatus (service_status_handle, &service_status)) { 
223         logerror("SetServiceStatus error %ld\n", GetLastError());
224     }
225
226     empth_exit();
227
228     CANT_HAPPEN("main thread terminated");
229     finish_server();
230 }
231
232 void
233 service_stopped(void)
234 {
235     if (hShutdownEvent != NULL) {
236         WaitForSingleObject(hShutdownEvent,INFINITE);
237     }
238 }
239
240 void
241 stop_service(void)
242 {
243     logerror("Service stopped");
244     service_status.dwWin32ExitCode = 0; 
245     service_status.dwCurrentState  = SERVICE_STOPPED; 
246     service_status.dwCheckPoint    = 0; 
247     service_status.dwWaitHint      = 0; 
248
249     if (!SetServiceStatus (service_status_handle, &service_status)) 
250         logerror("Error while stopping service SetServiceStatus"
251             " error %ld", GetLastError()); 
252 }
253
254 #endif /* _WIN32 */