svn cleanup
[anytun.git] / openvpn / service-win32 / service.patch
blob9edefd7d4673653b20c3cfd15f9b057435cbb1fe
1 --- service.c.orig Mon Jan 30 10:24:07 2006
2 +++ service.c Mon Jan 30 10:26:22 2006
3 @@ -16,6 +16,7 @@
4 service_main(DWORD dwArgc, LPTSTR *lpszArgv);
5 CmdInstallService();
6 CmdRemoveService();
7 + CmdStartService();
8 CmdDebugService(int argc, char **argv);
9 ControlHandler ( DWORD dwCtrlType );
10 GetLastErrorText( LPTSTR lpszBuf, DWORD dwSize );
11 @@ -40,8 +41,9 @@
12 // internal function prototypes
13 VOID WINAPI service_ctrl(DWORD dwCtrlCode);
14 VOID WINAPI service_main(DWORD dwArgc, LPTSTR *lpszArgv);
15 -VOID CmdInstallService();
16 -VOID CmdRemoveService();
17 +int CmdInstallService();
18 +int CmdRemoveService();
19 +int CmdStartService();
20 VOID CmdDebugService(int argc, char **argv);
21 BOOL WINAPI ControlHandler ( DWORD dwCtrlType );
22 LPTSTR GetLastErrorText( LPTSTR lpszBuf, DWORD dwSize );
23 @@ -64,7 +66,7 @@
24 // main service thread. When the this call returns,
25 // the service has stopped, so exit.
27 -void __cdecl main(int argc, char **argv)
28 +int __cdecl main(int argc, char **argv)
30 SERVICE_TABLE_ENTRY dispatchTable[] =
32 @@ -77,11 +79,15 @@
34 if ( _stricmp( "install", argv[1]+1 ) == 0 )
36 - CmdInstallService();
37 + return CmdInstallService();
39 else if ( _stricmp( "remove", argv[1]+1 ) == 0 )
41 - CmdRemoveService();
42 + return CmdRemoveService();
43 + }
44 + else if ( _stricmp( "start", argv[1]+1 ) == 0)
45 + {
46 + return CmdStartService();
48 else if ( _stricmp( "debug", argv[1]+1 ) == 0 )
50 @@ -92,7 +98,7 @@
52 goto dispatch;
54 - exit(0);
55 + return 0;
58 // if it doesn't match any of the above parameters
59 @@ -101,13 +107,16 @@
60 dispatch:
61 // this is just to be friendly
62 printf( "%s -install to install the service\n", SZAPPNAME );
63 + printf( "%s -start to start the service\n", SZAPPNAME );
64 printf( "%s -remove to remove the service\n", SZAPPNAME );
65 printf( "%s -debug <params> to run as a console app for debugging\n", SZAPPNAME );
66 printf( "\nStartServiceCtrlDispatcher being called.\n" );
67 printf( "This may take several seconds. Please wait.\n" );
69 if (!StartServiceCtrlDispatcher(dispatchTable))
70 - AddToMessageLog(TEXT("StartServiceCtrlDispatcher failed."));
71 + AddToMessageLog(MSG_FLAGS_ERROR, TEXT("StartServiceCtrlDispatcher failed."));
73 + return 0;
77 @@ -267,7 +276,7 @@
79 if (!(fResult = SetServiceStatus( sshStatusHandle, &ssStatus)))
81 - AddToMessageLog(TEXT("SetServiceStatus"));
82 + AddToMessageLog(MSG_FLAGS_ERROR, TEXT("SetServiceStatus"));
85 return fResult;
86 @@ -288,28 +297,33 @@
88 // COMMENTS:
90 -VOID AddToMessageLog(LPTSTR lpszMsg)
91 +void AddToMessageLog(DWORD flags, LPTSTR lpszMsg)
93 TCHAR szMsg [(sizeof(SZSERVICENAME) / sizeof(TCHAR)) + 100 ];
94 HANDLE hEventSource;
95 - LPTSTR lpszStrings[2];
96 + LPCSTR lpszStrings[2];
98 if ( !bDebug )
100 + if (flags & MSG_FLAGS_SYS_CODE)
101 dwErr = GetLastError();
102 + else
103 + dwErr = 0;
105 // Use event logging to log the error.
107 hEventSource = RegisterEventSource(NULL, TEXT(SZSERVICENAME));
109 - _stprintf(szMsg, TEXT("%s error: %d"), TEXT(SZSERVICENAME), dwErr);
110 + _stprintf(szMsg, TEXT("%s error: %d"), TEXT(SZSERVICENAME), (int)dwErr);
111 lpszStrings[0] = szMsg;
112 lpszStrings[1] = lpszMsg;
114 if (hEventSource != NULL)
116 ReportEvent(hEventSource, // handle of event source
117 - EVENTLOG_ERROR_TYPE, // event type
118 + // event type
119 + (flags & MSG_FLAGS_ERROR)
120 + ? EVENTLOG_ERROR_TYPE : EVENTLOG_INFORMATION_TYPE,
121 0, // event category
122 0, // event ID
123 NULL, // current user's SID
124 @@ -323,8 +337,10 @@
130 +void ResetError (void)
132 + dwErr = 0;
135 ///////////////////////////////////////////////////////////////////
137 @@ -341,21 +357,23 @@
138 // none
140 // RETURN VALUE:
141 -// none
142 +// 0 if success
144 // COMMENTS:
146 -void CmdInstallService()
147 +int CmdInstallService()
149 SC_HANDLE schService;
150 SC_HANDLE schSCManager;
152 TCHAR szPath[512];
154 + int ret = 0;
156 if ( GetModuleFileName( NULL, szPath, 512 ) == 0 )
158 _tprintf(TEXT("Unable to install %s - %s\n"), TEXT(SZSERVICEDISPLAYNAME), GetLastErrorText(szErr, 256));
159 - return;
160 + return 1;
163 schSCManager = OpenSCManager(
164 @@ -371,7 +389,7 @@
165 TEXT(SZSERVICEDISPLAYNAME), // name to display
166 SERVICE_QUERY_STATUS, // desired access
167 SERVICE_WIN32_OWN_PROCESS, // service type
168 - SERVICE_DEMAND_START, // start type
169 + SERVICE_DEMAND_START, // start type -- alternative: SERVICE_AUTO_START
170 SERVICE_ERROR_NORMAL, // error control type
171 szPath, // service's binary
172 NULL, // no load ordering group
173 @@ -388,16 +406,79 @@
174 else
176 _tprintf(TEXT("CreateService failed - %s\n"), GetLastErrorText(szErr, 256));
177 + ret = 1;
180 CloseServiceHandle(schSCManager);
182 else
184 _tprintf(TEXT("OpenSCManager failed - %s\n"), GetLastErrorText(szErr,256));
185 + ret = 1;
187 + return ret;
191 +// FUNCTION: CmdStartService()
193 +// PURPOSE: Start the service
195 +// PARAMETERS:
196 +// none
198 +// RETURN VALUE:
199 +// 0 if success
201 +// COMMENTS:
203 +int CmdStartService()
205 + int ret = 0;
207 + SC_HANDLE schSCManager;
208 + SC_HANDLE schService;
211 + // Open a handle to the SC Manager database.
212 + schSCManager = OpenSCManager(
213 + NULL, // local machine
214 + NULL, // ServicesActive database
215 + SC_MANAGER_ALL_ACCESS); // full access rights
217 + if (NULL == schSCManager) {
218 + _tprintf(TEXT("OpenSCManager failed - %s\n"), GetLastErrorText(szErr,256));
219 + ret = 1;
222 + schService = OpenService(
223 + schSCManager, // SCM database
224 + SZSERVICENAME, // service name
225 + SERVICE_ALL_ACCESS);
227 + if (schService == NULL) {
228 + _tprintf(TEXT("OpenService failed - %s\n"), GetLastErrorText(szErr,256));
229 + ret = 1;
232 + if (!StartService(
233 + schService, // handle to service
234 + 0, // number of arguments
235 + NULL) ) // no arguments
237 + _tprintf(TEXT("StartService failed - %s\n"), GetLastErrorText(szErr,256));
238 + ret = 1;
240 + else
242 + _tprintf(TEXT("Service Started\n"));
243 + ret = 0;
245 + CloseServiceHandle(schService);
246 + CloseServiceHandle(schSCManager);
247 + return ret;
251 // FUNCTION: CmdRemoveService()
253 @@ -407,15 +488,17 @@
254 // none
256 // RETURN VALUE:
257 -// none
258 +// 0 if success
260 // COMMENTS:
262 -void CmdRemoveService()
263 +int CmdRemoveService()
265 SC_HANDLE schService;
266 SC_HANDLE schSCManager;
268 + int ret = 0;
270 schSCManager = OpenSCManager(
271 NULL, // machine (NULL == local)
272 NULL, // database (NULL == default)
273 @@ -447,7 +530,10 @@
274 if ( ssStatus.dwCurrentState == SERVICE_STOPPED )
275 _tprintf(TEXT("\n%s stopped.\n"), TEXT(SZSERVICEDISPLAYNAME) );
276 else
278 _tprintf(TEXT("\n%s failed to stop.\n"), TEXT(SZSERVICEDISPLAYNAME) );
279 + ret = 1;
284 @@ -455,18 +541,28 @@
285 if ( DeleteService(schService) )
286 _tprintf(TEXT("%s removed.\n"), TEXT(SZSERVICEDISPLAYNAME) );
287 else
289 _tprintf(TEXT("DeleteService failed - %s\n"), GetLastErrorText(szErr,256));
290 + ret = 1;
294 CloseServiceHandle(schService);
296 else
298 _tprintf(TEXT("OpenService failed - %s\n"), GetLastErrorText(szErr,256));
299 + ret = 1;
302 CloseServiceHandle(schSCManager);
304 else
306 _tprintf(TEXT("OpenSCManager failed - %s\n"), GetLastErrorText(szErr,256));
307 + ret = 1;
309 + return ret;
313 @@ -587,7 +683,7 @@
314 else
316 lpszTemp[lstrlen(lpszTemp)-2] = TEXT('\0'); //remove cr and newline character
317 - _stprintf( lpszBuf, TEXT("%s (0x%x)"), lpszTemp, GetLastError() );
318 + _stprintf( lpszBuf, TEXT("%s (0x%x)"), lpszTemp, (int)GetLastError() );
321 if ( lpszTemp )
322 --- service.h.orig Mon Jan 30 10:24:07 2006
323 +++ service.h Mon Jan 30 10:24:07 2006
324 @@ -62,13 +62,13 @@
325 //// todo: change to desired strings
326 ////
327 // name of the executable
328 -#define SZAPPNAME "Simple"
329 +#define SZAPPNAME "openvpnserv"
330 // internal name of the service
331 -#define SZSERVICENAME "SimpleService"
332 +#define SZSERVICENAME "OpenVPNService"
333 // displayed name of the service
334 -#define SZSERVICEDISPLAYNAME "Simple Service"
335 +#define SZSERVICEDISPLAYNAME "OpenVPN Service"
336 // list of service dependencies - "dep1\0dep2\0\0"
337 -#define SZDEPENDENCIES ""
338 +#define SZDEPENDENCIES "TAP0801\0\0"
339 //////////////////////////////////////////////////////////////////////////////
342 @@ -126,7 +126,10 @@
343 // RETURN VALUE:
344 // none
346 - void AddToMessageLog(LPTSTR lpszMsg);
347 +# define MSG_FLAGS_ERROR (1<<0)
348 +# define MSG_FLAGS_SYS_CODE (1<<1)
349 + void AddToMessageLog(DWORD flags, LPTSTR lpszMsg);
350 + void ResetError (void);
351 //////////////////////////////////////////////////////////////////////////////