1 --- service.c.orig Mon Jan 30 10:24:07 2006
2 +++ service.c Mon Jan 30 10:26:22 2006
4 service_main(DWORD dwArgc, LPTSTR *lpszArgv);
8 CmdDebugService(int argc, char **argv);
9 ControlHandler ( DWORD dwCtrlType );
10 GetLastErrorText( LPTSTR lpszBuf, DWORD dwSize );
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 );
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[] =
34 if ( _stricmp( "install", argv[1]+1 ) == 0 )
36 - CmdInstallService();
37 + return CmdInstallService();
39 else if ( _stricmp( "remove", argv[1]+1 ) == 0 )
42 + return CmdRemoveService();
44 + else if ( _stricmp( "start", argv[1]+1 ) == 0)
46 + return CmdStartService();
48 else if ( _stricmp( "debug", argv[1]+1 ) == 0 )
58 // if it doesn't match any of the above parameters
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."));
79 if (!(fResult = SetServiceStatus( sshStatusHandle, &ssStatus)))
81 - AddToMessageLog(TEXT("SetServiceStatus"));
82 + AddToMessageLog(MSG_FLAGS_ERROR, TEXT("SetServiceStatus"));
90 -VOID AddToMessageLog(LPTSTR lpszMsg)
91 +void AddToMessageLog(DWORD flags, LPTSTR lpszMsg)
93 TCHAR szMsg [(sizeof(SZSERVICENAME) / sizeof(TCHAR)) + 100 ];
95 - LPTSTR lpszStrings[2];
96 + LPCSTR lpszStrings[2];
100 + if (flags & MSG_FLAGS_SYS_CODE)
101 dwErr = GetLastError();
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
119 + (flags & MSG_FLAGS_ERROR)
120 + ? EVENTLOG_ERROR_TYPE : EVENTLOG_INFORMATION_TYPE,
123 NULL, // current user's SID
130 +void ResetError (void)
135 ///////////////////////////////////////////////////////////////////
137 @@ -341,21 +357,23 @@
146 -void CmdInstallService()
147 +int CmdInstallService()
149 SC_HANDLE schService;
150 SC_HANDLE schSCManager;
156 if ( GetModuleFileName( NULL, szPath, 512 ) == 0 )
158 _tprintf(TEXT("Unable to install %s - %s\n"), TEXT(SZSERVICEDISPLAYNAME), GetLastErrorText(szErr, 256));
163 schSCManager = OpenSCManager(
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 @@
176 _tprintf(TEXT("CreateService failed - %s\n"), GetLastErrorText(szErr, 256));
180 CloseServiceHandle(schSCManager);
184 _tprintf(TEXT("OpenSCManager failed - %s\n"), GetLastErrorText(szErr,256));
191 +// FUNCTION: CmdStartService()
193 +// PURPOSE: Start the service
203 +int CmdStartService()
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));
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));
233 + schService, // handle to service
234 + 0, // number of arguments
235 + NULL) ) // no arguments
237 + _tprintf(TEXT("StartService failed - %s\n"), GetLastErrorText(szErr,256));
242 + _tprintf(TEXT("Service Started\n"));
245 + CloseServiceHandle(schService);
246 + CloseServiceHandle(schSCManager);
251 // FUNCTION: CmdRemoveService()
253 @@ -407,15 +488,17 @@
262 -void CmdRemoveService()
263 +int CmdRemoveService()
265 SC_HANDLE schService;
266 SC_HANDLE schSCManager;
270 schSCManager = OpenSCManager(
271 NULL, // machine (NULL == local)
272 NULL, // database (NULL == default)
274 if ( ssStatus.dwCurrentState == SERVICE_STOPPED )
275 _tprintf(TEXT("\n%s stopped.\n"), TEXT(SZSERVICEDISPLAYNAME) );
278 _tprintf(TEXT("\n%s failed to stop.\n"), TEXT(SZSERVICEDISPLAYNAME) );
284 @@ -455,18 +541,28 @@
285 if ( DeleteService(schService) )
286 _tprintf(TEXT("%s removed.\n"), TEXT(SZSERVICEDISPLAYNAME) );
289 _tprintf(TEXT("DeleteService failed - %s\n"), GetLastErrorText(szErr,256));
294 CloseServiceHandle(schService);
298 _tprintf(TEXT("OpenService failed - %s\n"), GetLastErrorText(szErr,256));
302 CloseServiceHandle(schSCManager);
306 _tprintf(TEXT("OpenSCManager failed - %s\n"), GetLastErrorText(szErr,256));
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() );
322 --- service.h.orig Mon Jan 30 10:24:07 2006
323 +++ service.h Mon Jan 30 10:24:07 2006
325 //// todo: change to desired strings
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 //////////////////////////////////////////////////////////////////////////////
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 //////////////////////////////////////////////////////////////////////////////