]> git.pond.sub.org Git - empserver/blob - src/lib/w32/service.c
Update copyright notice
[empserver] / src / lib / w32 / service.c
1 /*
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
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 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.
25  *
26  *  ---
27  *
28  *  service.c: Windows services support
29  * 
30  *  Known contributors to this file:
31  *     Ron Koenderink, 2004
32  */
33
34 #include <config.h>
35
36 #include <windows.h>
37
38 #include "service.h"
39 #include "empthread.h"
40 #include "prototypes.h"
41
42 int
43 install_service(char *program_name, char *service_name, char *config_file)
44 {
45     HANDLE schSCManager,schService;
46     SERVICE_DESCRIPTION sdBuf;
47
48     if (config_file != NULL)
49         snprintf(&program_name[strlen(program_name)], _MAX_PATH-strlen(program_name), " -e %s",
50             config_file);
51
52     if (service_name == NULL)
53         service_name = DEFAULT_SERVICE_NAME;
54     else if (service_name[0] == '\0')
55         service_name = DEFAULT_SERVICE_NAME;
56
57     schSCManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
58
59     if (schSCManager == NULL) {
60         fprintf(stderr, "install_service failed to open Service Control Manager\n"); 
61         return EXIT_FAILURE;
62     }
63
64     schService = CreateService(schSCManager,
65         service_name,
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 */
77  
78     if (schService == NULL) {
79         fprintf(stderr, "install_service failed to create service %s\n", service_name);
80         return EXIT_FAILURE;
81     }
82     sdBuf.lpDescription = "Server for Empire game";
83
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");
89     }
90
91     printf("Service %s installed.\n", service_name);
92     CloseServiceHandle(schService);
93     return EXIT_SUCCESS;
94 }
95
96 int
97 remove_service(char *service_name)
98 {
99     HANDLE schSCManager;
100     SC_HANDLE hService;
101
102     if (service_name == NULL)
103         service_name = DEFAULT_SERVICE_NAME;
104     else if (service_name[0] == '\0')
105         service_name = DEFAULT_SERVICE_NAME;
106
107     schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
108
109     if (schSCManager == NULL) {
110         fprintf(stderr, "remove_service failed to open Service Control Manager\n"); 
111         return EXIT_FAILURE;
112     }
113
114     hService = OpenService(schSCManager, service_name, SERVICE_ALL_ACCESS);
115
116     if (hService == NULL) {
117         fprintf(stderr, "remove_service failed to open service %s\n", service_name);
118         return EXIT_FAILURE;
119     }
120
121     if (DeleteService(hService) == 0) {
122         fprintf(stderr, "remove_service failed to remove service %s\n", service_name);
123         return EXIT_FAILURE;
124     }
125
126     if (CloseServiceHandle(hService) == 0) {
127         fprintf(stderr, "remove_service failed to close service %s\n", service_name); 
128         return EXIT_FAILURE;
129     } else {
130         printf("Service %s removed.\n", service_name); 
131         return EXIT_SUCCESS;
132     }
133 }
134
135 static SERVICE_STATUS           service_status; 
136 static SERVICE_STATUS_HANDLE    service_status_handle;
137
138 static void WINAPI
139 service_ctrl_handler(DWORD Opcode) 
140
141     switch(Opcode) { 
142         case SERVICE_CONTROL_PAUSE: 
143             service_status.dwCurrentState = SERVICE_PAUSED;
144             logerror("Pausing the service not supported");
145             break; 
146  
147         case SERVICE_CONTROL_CONTINUE: 
148             logerror("Continuing the service not supported");
149             service_status.dwCurrentState = SERVICE_RUNNING; 
150             break; 
151  
152         case SERVICE_CONTROL_STOP:
153             logerror("Service stopping");
154             empth_request_shutdown();
155             return; 
156  
157         case SERVICE_CONTROL_INTERROGATE: 
158         /* Fall through to send current status.  */
159             break; 
160  
161         default: 
162             logerror("Unrecognized opcode %ld in ServiceCtrlHandler", 
163                 Opcode); 
164     } 
165  
166     /* Send current status. */
167     if (!SetServiceStatus (service_status_handle,  &service_status))
168         logerror("SetServiceStatus error %ld",GetLastError()); 
169     return; 
170
171
172 void WINAPI
173 service_main(DWORD argc, LPTSTR *argv)
174 {
175     int sig;
176     
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; 
184  
185     service_status_handle = RegisterServiceCtrlHandler(
186         DEFAULT_SERVICE_NAME, service_ctrl_handler);
187  
188     if (service_status_handle == (SERVICE_STATUS_HANDLE)0) { 
189         logerror("RegisterServiceCtrlHandler failed %lu\n", GetLastError());
190         finish_server();
191         return;
192     }
193  
194     start_server(0);
195  
196     /* Initialization complete - report running status. */
197     service_status.dwCurrentState       = SERVICE_RUNNING; 
198     service_status.dwCheckPoint         = 0; 
199     service_status.dwWaitHint           = 0; 
200  
201     if (!SetServiceStatus (service_status_handle, &service_status)) { 
202         logerror("SetServiceStatus error %ld\n", GetLastError());
203     }
204
205     sig = empth_wait_for_signal();
206
207     shutdwn(sig);
208
209     CANT_REACH();
210     finish_server();
211 }
212
213 void
214 stop_service(void)
215 {
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; 
221
222     if (!SetServiceStatus (service_status_handle, &service_status)) 
223         logerror("Error while stopping service SetServiceStatus"
224             " error %ld", GetLastError()); 
225 }