/[LeafOK_CVS]/pvpgn-1.7.4/src/win32/service.c
ViewVC logotype

Contents of /pvpgn-1.7.4/src/win32/service.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1.1.1 - (show annotations) (vendor branch)
Tue Jun 6 03:41:38 2006 UTC (19 years, 9 months ago) by sysadm
Branch: GNU, MAIN
CVS Tags: arelease, HEAD
Changes since 1.1: +0 -0 lines
Content type: text/x-csrc
no message

1 /*
2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public License
4 * as published by the Free Software Foundation; either version 2
5 * of the License, or (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15 */
16
17 #include <windows.h>
18 #include <winsvc.h>
19 #include <stdlib.h>
20
21 #if !defined(WINADVAPI)
22 #if !defined(_ADVAPI32_)
23 #define WINADVAPI DECLSPEC_IMPORT
24 #else
25 #define WINADVAPI
26 #endif
27 #endif
28
29 extern char serviceLongName[];
30 extern char serviceName[];
31 extern char serviceDescription[];
32
33 extern int g_ServiceStatus;
34
35 #ifdef WIN32_GUI
36 extern int server_main(int argc, char *argv[]);
37 #else
38 extern int main(int argc, char *argv[]);
39 #endif
40
41 SERVICE_STATUS serviceStatus;
42 SERVICE_STATUS_HANDLE serviceStatusHandle = 0;
43
44 typedef WINADVAPI BOOL (WINAPI *CSD_T)(SC_HANDLE, DWORD, LPCVOID);
45
46 void Win32_ServiceInstall()
47 {
48 SERVICE_DESCRIPTION sdBuf;
49 CSD_T ChangeServiceDescription;
50 HANDLE advapi32;
51 SC_HANDLE serviceControlManager = OpenSCManager(0, 0, SC_MANAGER_CREATE_SERVICE);
52
53 if (serviceControlManager)
54 {
55 char path[_MAX_PATH + 10];
56 if (GetModuleFileName( 0, path, sizeof(path)/sizeof(path[0]) ) > 0)
57 {
58 SC_HANDLE service;
59 strcat(path, " --service");
60 service = CreateService(serviceControlManager,
61 serviceName, serviceLongName,
62 SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS,
63 SERVICE_AUTO_START, SERVICE_ERROR_IGNORE, path,
64 0, 0, 0, 0, 0);
65 if (service)
66 {
67 sdBuf.lpDescription = serviceDescription;
68
69 if (!(advapi32 = GetModuleHandle("ADVAPI32.DLL")))
70 {
71 CloseServiceHandle(service);
72 CloseServiceHandle(serviceControlManager);
73 return;
74 }
75
76 ;
77 if (!(ChangeServiceDescription = (CSD_T) GetProcAddress(advapi32, "ChangeServiceConfig2A")))
78 {
79 CloseServiceHandle(service);
80 CloseServiceHandle(serviceControlManager);
81 return;
82 }
83
84 ChangeServiceDescription(
85 service, // handle to service
86 SERVICE_CONFIG_DESCRIPTION, // change: description
87 &sdBuf);
88 CloseServiceHandle(service);
89 }
90 }
91 CloseServiceHandle(serviceControlManager);
92 }
93 }
94
95 void Win32_ServiceUninstall()
96 {
97 SC_HANDLE serviceControlManager = OpenSCManager(0, 0, SC_MANAGER_CONNECT);
98
99 if (serviceControlManager)
100 {
101 SC_HANDLE service = OpenService(serviceControlManager,
102 serviceName, SERVICE_QUERY_STATUS | DELETE);
103 if (service)
104 {
105 SERVICE_STATUS serviceStatus;
106 if (QueryServiceStatus(service, &serviceStatus))
107 {
108 if (serviceStatus.dwCurrentState == SERVICE_STOPPED)
109 DeleteService(service);
110 }
111 //DeleteService(service);
112
113 CloseServiceHandle(service);
114 }
115
116 CloseServiceHandle(serviceControlManager);
117 }
118
119 }
120
121 void WINAPI ServiceControlHandler(DWORD controlCode)
122 {
123 switch (controlCode)
124 {
125 case SERVICE_CONTROL_INTERROGATE:
126 break;
127
128 case SERVICE_CONTROL_SHUTDOWN:
129 case SERVICE_CONTROL_STOP:
130 serviceStatus.dwCurrentState = SERVICE_STOP_PENDING;
131 SetServiceStatus(serviceStatusHandle, &serviceStatus);
132
133 g_ServiceStatus = 0;
134 return;
135
136 case SERVICE_CONTROL_PAUSE:
137 g_ServiceStatus = 2;
138 serviceStatus.dwCurrentState = SERVICE_PAUSED;
139 SetServiceStatus(serviceStatusHandle, &serviceStatus);
140 break;
141
142 case SERVICE_CONTROL_CONTINUE:
143 serviceStatus.dwCurrentState = SERVICE_RUNNING;
144 SetServiceStatus(serviceStatusHandle, &serviceStatus);
145 g_ServiceStatus = 1;
146 break;
147
148 default:
149 if ( controlCode >= 128 && controlCode <= 255 )
150 // user defined control code
151 break;
152 else
153 // unrecognized control code
154 break;
155 }
156
157 SetServiceStatus(serviceStatusHandle, &serviceStatus);
158 }
159
160 void WINAPI ServiceMain(DWORD argc, char *argv[])
161 {
162 // initialise service status
163 serviceStatus.dwServiceType = SERVICE_WIN32;
164 serviceStatus.dwCurrentState = SERVICE_START_PENDING;
165 serviceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP |
166 SERVICE_ACCEPT_PAUSE_CONTINUE;
167 serviceStatus.dwWin32ExitCode = NO_ERROR;
168 serviceStatus.dwServiceSpecificExitCode = NO_ERROR;
169 serviceStatus.dwCheckPoint = 0;
170 serviceStatus.dwWaitHint = 0;
171
172 serviceStatusHandle = RegisterServiceCtrlHandler(serviceName, ServiceControlHandler);
173
174 if ( serviceStatusHandle )
175 {
176 char path[_MAX_PATH + 1];
177 int i, last_slash = 0;
178
179 GetModuleFileName(0, path, sizeof(path)/sizeof(path[0]));
180
181 for (i = 0; i < strlen(path); i++) {
182 if (path[i] == '\\') last_slash = i;
183 }
184
185 path[last_slash] = 0;
186
187 // service is starting
188 serviceStatus.dwCurrentState = SERVICE_START_PENDING;
189 SetServiceStatus(serviceStatusHandle, &serviceStatus);
190
191 // do initialisation here
192 SetCurrentDirectory(path);
193
194 // running
195 serviceStatus.dwControlsAccepted |= (SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN);
196 serviceStatus.dwCurrentState = SERVICE_RUNNING;
197 SetServiceStatus( serviceStatusHandle, &serviceStatus );
198
199 ////////////////////////
200 // service main cycle //
201 ////////////////////////
202
203 g_ServiceStatus = 1;
204 argc = 1;
205
206 #ifdef WIN32_GUI
207 server_main(argc, argv);
208 #else
209 main(argc, argv);
210 #endif
211
212 // service was stopped
213 serviceStatus.dwCurrentState = SERVICE_STOP_PENDING;
214 SetServiceStatus(serviceStatusHandle, &serviceStatus);
215
216 // do cleanup here
217
218 // service is now stopped
219 serviceStatus.dwControlsAccepted &= ~(SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN);
220 serviceStatus.dwCurrentState = SERVICE_STOPPED;
221 SetServiceStatus(serviceStatusHandle, &serviceStatus);
222 }
223 }
224
225 void Win32_ServiceRun()
226 {
227 SERVICE_TABLE_ENTRY serviceTable[] =
228 {
229 { serviceName, ServiceMain },
230 { 0, 0 }
231 };
232
233 if (!StartServiceCtrlDispatcher(serviceTable))
234 {
235 }
236 }

webmaster@leafok.com
ViewVC Help
Powered by ViewVC 1.3.0-beta1