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