VirtualBox

source: vbox/trunk/src/VBox/Frontends/VBoxAutostart/VBoxAutostart-win.cpp@ 89319

Last change on this file since 89319 was 89319, checked in by vboxsync, 4 years ago

VBoxAutostart: bugref:9341: Create "VBoxAutostart.log" release log
file in the user's vbox home directory.

Don't bother with --log* options like the posix counterpart has b/c
the user cannot change them easily anyway and is better off using
environment variables if need be.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 44.8 KB
Line 
1/* $Id: VBoxAutostart-win.cpp 89319 2021-05-27 13:38:13Z vboxsync $ */
2/** @file
3 * VirtualBox Autostart Service - Windows Specific Code.
4 */
5
6/*
7 * Copyright (C) 2012-2020 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/*********************************************************************************************************************************
20* Header Files *
21*********************************************************************************************************************************/
22#include <iprt/win/windows.h>
23#include <tchar.h>
24
25#define SECURITY_WIN32
26#include <Security.h>
27
28#include <VBox/com/array.h>
29#include <VBox/com/com.h>
30#include <VBox/com/ErrorInfo.h>
31#include <VBox/com/errorprint.h>
32#include <VBox/com/Guid.h>
33#include <VBox/com/listeners.h>
34#include <VBox/com/NativeEventQueue.h>
35#include <VBox/com/string.h>
36#include <VBox/com/VirtualBox.h>
37
38#include <VBox/log.h>
39#include <VBox/version.h>
40
41#include <iprt/dir.h>
42#include <iprt/env.h>
43#include <iprt/errcore.h>
44#include <iprt/getopt.h>
45#include <iprt/initterm.h>
46#include <iprt/mem.h>
47#include <iprt/process.h>
48#include <iprt/path.h>
49#include <iprt/semaphore.h>
50#include <iprt/stream.h>
51#include <iprt/string.h>
52#include <iprt/thread.h>
53
54#include "VBoxAutostart.h"
55#include "PasswordInput.h"
56
57
58/*********************************************************************************************************************************
59* Defined Constants And Macros *
60*********************************************************************************************************************************/
61/** The service name. */
62#define AUTOSTART_SERVICE_NAME "VBoxAutostartSvc"
63/** The service display name. */
64#define AUTOSTART_SERVICE_DISPLAY_NAME "VirtualBox Autostart Service"
65
66ComPtr<IVirtualBoxClient> g_pVirtualBoxClient = NULL;
67bool g_fVerbose = false;
68ComPtr<IVirtualBox> g_pVirtualBox = NULL;
69ComPtr<ISession> g_pSession = NULL;
70
71
72/*********************************************************************************************************************************
73* Global Variables *
74*********************************************************************************************************************************/
75/** The service control handler handle. */
76static SERVICE_STATUS_HANDLE g_hSupSvcWinCtrlHandler = NULL;
77/** The service status. */
78static uint32_t volatile g_u32SupSvcWinStatus = SERVICE_STOPPED;
79/** The semaphore the main service thread is waiting on in autostartSvcWinServiceMain. */
80static RTSEMEVENTMULTI g_hSupSvcWinEvent = NIL_RTSEMEVENTMULTI;
81/** The service name is used for send to service main. */
82static com::Bstr g_bstrServiceName;
83
84/** Logging parameters. */
85static uint32_t g_cHistory = 10; /* Enable log rotation, 10 files. */
86static uint32_t g_uHistoryFileTime = 0; /* No time limit, it's very low volume. */
87static uint64_t g_uHistoryFileSize = 100 * _1M; /* Max 100MB per file. */
88
89
90/*********************************************************************************************************************************
91* Internal Functions *
92*********************************************************************************************************************************/
93static SC_HANDLE autostartSvcWinOpenSCManager(const char *pszAction, DWORD dwAccess);
94
95static int autostartGetProcessDomainUser(com::Utf8Str &aUser)
96{
97 int rc = VERR_NOT_SUPPORTED;
98
99 RTUTF16 wszUsername[1024] = { 0 };
100 ULONG cwcUsername = RT_ELEMENTS(wszUsername);
101 char *pszUser = NULL;
102 if (!GetUserNameExW(NameSamCompatible, &wszUsername[0], &cwcUsername))
103 return RTErrConvertFromWin32(GetLastError());
104 rc = RTUtf16ToUtf8(wszUsername, &pszUser);
105 aUser = pszUser;
106 aUser.toLower();
107 RTStrFree(pszUser);
108 return rc;
109}
110
111static int autostartGetLocalDomain(com::Utf8Str &aDomain)
112{
113 RTUTF16 pwszDomain[MAX_COMPUTERNAME_LENGTH + 1] = { 0 };
114 uint32_t cwcDomainSize = MAX_COMPUTERNAME_LENGTH + 1;
115 if (!GetComputerNameW(pwszDomain, (LPDWORD)&cwcDomainSize))
116 return RTErrConvertFromWin32(GetLastError());
117 char *pszDomain = NULL;
118 int rc = RTUtf16ToUtf8(pwszDomain, &pszDomain);
119 aDomain = pszDomain;
120 aDomain.toLower();
121 RTStrFree(pszDomain);
122 return rc;
123}
124
125static int autostartGetDomainAndUser(const com::Utf8Str &aDomainAndUser, com::Utf8Str &aDomain, com::Utf8Str &aUser)
126{
127 size_t offDelim = aDomainAndUser.find("\\");
128 if (offDelim != aDomainAndUser.npos)
129 {
130 // if only domain is specified
131 if (aDomainAndUser.length() - offDelim == 1)
132 return VERR_INVALID_PARAMETER;
133
134 if (offDelim == 1 && aDomainAndUser[0] == '.')
135 {
136 int rc = autostartGetLocalDomain(aDomain);
137 aUser = aDomainAndUser.substr(offDelim + 1);
138 return rc;
139 }
140 aDomain = aDomainAndUser.substr(0, offDelim);
141 aUser = aDomainAndUser.substr(offDelim + 1);
142 aDomain.toLower();
143 aUser.toLower();
144 return VINF_SUCCESS;
145 }
146
147 offDelim = aDomainAndUser.find("@");
148 if (offDelim != aDomainAndUser.npos)
149 {
150 // if only domain is specified
151 if (offDelim == 0)
152 return VERR_INVALID_PARAMETER;
153
154 // with '@' but without domain
155 if (aDomainAndUser.length() - offDelim == 1)
156 {
157 int rc = autostartGetLocalDomain(aDomain);
158 aUser = aDomainAndUser.substr(0, offDelim);
159 return rc;
160 }
161 aDomain = aDomainAndUser.substr(offDelim + 1);
162 aUser = aDomainAndUser.substr(0, offDelim);
163 aDomain.toLower();
164 aUser.toLower();
165 return VINF_SUCCESS;
166 }
167
168 // only user is specified
169 int rc = autostartGetLocalDomain(aDomain);
170 aUser = aDomainAndUser;
171 aDomain.toLower();
172 aUser.toLower();
173 return rc;
174}
175
176/** Common helper for formatting the service name. */
177static void autostartFormatServiceName(const com::Utf8Str &aDomain, const com::Utf8Str &aUser, com::Utf8Str &aServiceName)
178{
179 aServiceName.printf("%s%s%s", AUTOSTART_SERVICE_NAME, aDomain.c_str(), aUser.c_str());
180}
181
182/** Used by the delete service operation. */
183static int autostartGetServiceName(const com::Utf8Str &aDomainAndUser, com::Utf8Str &aServiceName)
184{
185 com::Utf8Str sDomain;
186 com::Utf8Str sUser;
187 int rc = autostartGetDomainAndUser(aDomainAndUser, sDomain, sUser);
188 if (RT_FAILURE(rc))
189 return rc;
190 autostartFormatServiceName(sDomain, sUser, aServiceName);
191 return VINF_SUCCESS;
192}
193
194/**
195 * Print out progress on the console.
196 *
197 * This runs the main event queue every now and then to prevent piling up
198 * unhandled things (which doesn't cause real problems, just makes things
199 * react a little slower than in the ideal case).
200 */
201DECLHIDDEN(HRESULT) showProgress(ComPtr<IProgress> progress)
202{
203 using namespace com;
204
205 BOOL fCompleted = FALSE;
206 ULONG uCurrentPercent = 0;
207 Bstr bstrOperationDescription;
208
209 NativeEventQueue::getMainEventQueue()->processEventQueue(0);
210
211 ULONG cOperations = 1;
212 HRESULT hrc = progress->COMGETTER(OperationCount)(&cOperations);
213 if (FAILED(hrc))
214 return hrc;
215
216 /* setup signal handling if cancelable */
217 bool fCanceledAlready = false;
218 BOOL fCancelable;
219 hrc = progress->COMGETTER(Cancelable)(&fCancelable);
220 if (FAILED(hrc))
221 fCancelable = FALSE;
222
223 hrc = progress->COMGETTER(Completed(&fCompleted));
224 while (SUCCEEDED(hrc))
225 {
226 progress->COMGETTER(Percent(&uCurrentPercent));
227
228 if (fCompleted)
229 break;
230
231 /* process async cancelation */
232 if (!fCanceledAlready)
233 {
234 hrc = progress->Cancel();
235 if (SUCCEEDED(hrc))
236 fCanceledAlready = true;
237 }
238
239 /* make sure the loop is not too tight */
240 progress->WaitForCompletion(100);
241
242 NativeEventQueue::getMainEventQueue()->processEventQueue(0);
243 hrc = progress->COMGETTER(Completed(&fCompleted));
244 }
245
246 /* complete the line. */
247 LONG iRc = E_FAIL;
248 hrc = progress->COMGETTER(ResultCode)(&iRc);
249 if (SUCCEEDED(hrc))
250 {
251 hrc = iRc;
252 }
253
254 return hrc;
255}
256
257DECLHIDDEN(void) autostartSvcOsLogStr(const char *pszMsg, AUTOSTARTLOGTYPE enmLogType)
258{
259 HANDLE hEventLog = RegisterEventSourceA(NULL /* local computer */, "VBoxAutostartSvc");
260 AssertReturnVoid(hEventLog != NULL);
261 WORD wType = 0;
262 const char *apsz[2];
263 apsz[0] = "VBoxAutostartSvc";
264 apsz[1] = pszMsg;
265
266 switch (enmLogType)
267 {
268 case AUTOSTARTLOGTYPE_INFO:
269 wType = 0;
270 break;
271 case AUTOSTARTLOGTYPE_ERROR:
272 wType = EVENTLOG_ERROR_TYPE;
273 break;
274 case AUTOSTARTLOGTYPE_WARNING:
275 wType = EVENTLOG_WARNING_TYPE;
276 break;
277 case AUTOSTARTLOGTYPE_VERBOSE:
278 if (!g_fVerbose)
279 return;
280 wType = EVENTLOG_INFORMATION_TYPE;
281 break;
282 default:
283 AssertMsgFailed(("Invalid log type %d\n", enmLogType));
284 }
285
286 BOOL fRc = ReportEventA(hEventLog, /* hEventLog */
287 wType, /* wType */
288 0, /* wCategory */
289 0 /** @todo mc */, /* dwEventID */
290 NULL, /* lpUserSid */
291 RT_ELEMENTS(apsz), /* wNumStrings */
292 0, /* dwDataSize */
293 apsz, /* lpStrings */
294 NULL); /* lpRawData */
295 AssertMsg(fRc, ("%u\n", GetLastError())); NOREF(fRc);
296 DeregisterEventSource(hEventLog);
297
298 /* write it to the release log too */
299 LogRel(("%s", pszMsg));
300}
301
302/**
303 * Opens the service control manager.
304 *
305 * When this fails, an error message will be displayed.
306 *
307 * @returns Valid handle on success.
308 * NULL on failure, will display an error message.
309 *
310 * @param pszAction The action which is requesting access to SCM.
311 * @param dwAccess The desired access.
312 */
313static SC_HANDLE autostartSvcWinOpenSCManager(const char *pszAction, DWORD dwAccess)
314{
315 SC_HANDLE hSCM = OpenSCManager(NULL /* lpMachineName*/, NULL /* lpDatabaseName */, dwAccess);
316 if (hSCM == NULL)
317 {
318 DWORD err = GetLastError();
319 switch (err)
320 {
321 case ERROR_ACCESS_DENIED:
322 autostartSvcDisplayError("%s - OpenSCManager failure: access denied\n", pszAction);
323 break;
324 default:
325 autostartSvcDisplayError("%s - OpenSCManager failure: %d\n", pszAction, err);
326 break;
327 }
328 }
329 return hSCM;
330}
331
332
333/**
334 * Opens the service.
335 *
336 * Last error is preserved on failure and set to 0 on success.
337 *
338 * @returns Valid service handle on success.
339 * NULL on failure, will display an error message unless it's ignored.
340 *
341 * @param pszAction The action which is requesting access to the service.
342 * @param dwSCMAccess The service control manager access.
343 * @param dwSVCAccess The desired service access.
344 * @param cIgnoredErrors The number of ignored errors.
345 * @param ... Errors codes that should not cause a message to be displayed.
346 */
347static SC_HANDLE autostartSvcWinOpenService(const PRTUTF16 pwszServiceName, const char *pszAction, DWORD dwSCMAccess, DWORD dwSVCAccess,
348 unsigned cIgnoredErrors, ...)
349{
350 SC_HANDLE hSCM = autostartSvcWinOpenSCManager(pszAction, dwSCMAccess);
351 if (!hSCM)
352 return NULL;
353
354 SC_HANDLE hSvc = OpenServiceW(hSCM, pwszServiceName, dwSVCAccess);
355 if (hSvc)
356 {
357 CloseServiceHandle(hSCM);
358 SetLastError(0);
359 }
360 else
361 {
362 DWORD err = GetLastError();
363 bool fIgnored = false;
364 va_list va;
365 va_start(va, cIgnoredErrors);
366 while (!fIgnored && cIgnoredErrors-- > 0)
367 fIgnored = (DWORD)va_arg(va, int) == err;
368 va_end(va);
369 if (!fIgnored)
370 {
371 switch (err)
372 {
373 case ERROR_ACCESS_DENIED:
374 autostartSvcDisplayError("%s - OpenService failure: access denied\n", pszAction);
375 break;
376 case ERROR_SERVICE_DOES_NOT_EXIST:
377 autostartSvcDisplayError("%s - OpenService failure: The service %ls does not exist. Reinstall it.\n",
378 pszAction, pwszServiceName);
379 break;
380 default:
381 autostartSvcDisplayError("%s - OpenService failure: %d\n", pszAction, err);
382 break;
383 }
384 }
385
386 CloseServiceHandle(hSCM);
387 SetLastError(err);
388 }
389 return hSvc;
390}
391
392static RTEXITCODE autostartSvcWinInterrogate(int argc, char **argv)
393{
394 RT_NOREF(argc, argv);
395 RTPrintf("VBoxAutostartSvc: The \"interrogate\" action is not implemented.\n");
396 return RTEXITCODE_FAILURE;
397}
398
399
400static RTEXITCODE autostartSvcWinStop(int argc, char **argv)
401{
402 RT_NOREF(argc, argv);
403 RTPrintf("VBoxAutostartSvc: The \"stop\" action is not implemented.\n");
404 return RTEXITCODE_FAILURE;
405}
406
407
408static RTEXITCODE autostartSvcWinContinue(int argc, char **argv)
409{
410 RT_NOREF(argc, argv);
411 RTPrintf("VBoxAutostartSvc: The \"continue\" action is not implemented.\n");
412 return RTEXITCODE_FAILURE;
413}
414
415
416static RTEXITCODE autostartSvcWinPause(int argc, char **argv)
417{
418 RT_NOREF(argc, argv);
419 RTPrintf("VBoxAutostartSvc: The \"pause\" action is not implemented.\n");
420 return RTEXITCODE_FAILURE;
421}
422
423
424static RTEXITCODE autostartSvcWinStart(int argc, char **argv)
425{
426 RT_NOREF(argc, argv);
427 RTPrintf("VBoxAutostartSvc: The \"start\" action is not implemented.\n");
428 return RTEXITCODE_SUCCESS;
429}
430
431
432static RTEXITCODE autostartSvcWinQueryDescription(int argc, char **argv)
433{
434 RT_NOREF(argc, argv);
435 RTPrintf("VBoxAutostartSvc: The \"qdescription\" action is not implemented.\n");
436 return RTEXITCODE_FAILURE;
437}
438
439
440static RTEXITCODE autostartSvcWinQueryConfig(int argc, char **argv)
441{
442 RT_NOREF(argc, argv);
443 RTPrintf("VBoxAutostartSvc: The \"qconfig\" action is not implemented.\n");
444 return RTEXITCODE_FAILURE;
445}
446
447
448static RTEXITCODE autostartSvcWinDisable(int argc, char **argv)
449{
450 RT_NOREF(argc, argv);
451 RTPrintf("VBoxAutostartSvc: The \"disable\" action is not implemented.\n");
452 return RTEXITCODE_FAILURE;
453}
454
455static RTEXITCODE autostartSvcWinEnable(int argc, char **argv)
456{
457 RT_NOREF(argc, argv);
458 RTPrintf("VBoxAutostartSvc: The \"enable\" action is not implemented.\n");
459 return RTEXITCODE_FAILURE;
460}
461
462
463/**
464 * Handle the 'delete' action.
465 *
466 * @returns RTEXITCODE_SUCCESS or RTEXITCODE_FAILURE.
467 * @param argc The action argument count.
468 * @param argv The action argument vector.
469 */
470static int autostartSvcWinDelete(int argc, char **argv)
471{
472 /*
473 * Parse the arguments.
474 */
475 bool fVerbose = false;
476 const char *pszUser = NULL;
477 static const RTGETOPTDEF s_aOptions[] =
478 {
479 { "--verbose", 'v', RTGETOPT_REQ_NOTHING },
480 { "--user", 'u', RTGETOPT_REQ_STRING },
481 };
482 int ch;
483 RTGETOPTUNION Value;
484 RTGETOPTSTATE GetState;
485 RTGetOptInit(&GetState, argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions), 0, RTGETOPTINIT_FLAGS_NO_STD_OPTS);
486 while ((ch = RTGetOpt(&GetState, &Value)))
487 {
488 switch (ch)
489 {
490 case 'v':
491 fVerbose = true;
492 break;
493 case 'u':
494 pszUser = Value.psz;
495 break;
496 default:
497 return autostartSvcDisplayGetOptError("delete", ch, &Value);
498 }
499 }
500
501 if (!pszUser)
502 return autostartSvcDisplayError("delete - DeleteService failed, user name required.\n");
503
504 com::Utf8Str sServiceName;
505 int vrc = autostartGetServiceName(pszUser, sServiceName);
506 if (RT_FAILURE(vrc))
507 return autostartSvcDisplayError("delete - DeleteService failed, service name for user %s can not be constructed.\n",
508 pszUser);
509 /*
510 * Create the service.
511 */
512 RTEXITCODE rc = RTEXITCODE_FAILURE;
513 SC_HANDLE hSvc = autostartSvcWinOpenService(com::Bstr(sServiceName).raw(), "delete", SERVICE_CHANGE_CONFIG, DELETE,
514 1, ERROR_SERVICE_DOES_NOT_EXIST);
515 if (hSvc)
516 {
517 if (DeleteService(hSvc))
518 {
519 RTPrintf("Successfully deleted the %s service.\n", sServiceName.c_str());
520 rc = RTEXITCODE_SUCCESS;
521 }
522 else
523 autostartSvcDisplayError("delete - DeleteService failed, err=%d.\n", GetLastError());
524 CloseServiceHandle(hSvc);
525 }
526 else if (GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST)
527 {
528
529 if (fVerbose)
530 RTPrintf("The service %s was not installed, nothing to be done.", sServiceName.c_str());
531 else
532 RTPrintf("Successfully deleted the %s service.\n", sServiceName.c_str());
533 rc = RTEXITCODE_SUCCESS;
534 }
535 return rc;
536}
537
538
539/**
540 * Handle the 'create' action.
541 *
542 * @returns 0 or 1.
543 * @param argc The action argument count.
544 * @param argv The action argument vector.
545 */
546static RTEXITCODE autostartSvcWinCreate(int argc, char **argv)
547{
548 /*
549 * Parse the arguments.
550 */
551 bool fVerbose = false;
552 const char *pszUser = NULL;
553 com::Utf8Str strPwd;
554 const char *pszPwdFile = NULL;
555 static const RTGETOPTDEF s_aOptions[] =
556 {
557 { "--verbose", 'v', RTGETOPT_REQ_NOTHING },
558 { "--user", 'u', RTGETOPT_REQ_STRING },
559 { "--password-file", 'p', RTGETOPT_REQ_STRING }
560 };
561 int ch;
562 RTGETOPTUNION Value;
563 RTGETOPTSTATE GetState;
564 RTGetOptInit(&GetState, argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions), 0, RTGETOPTINIT_FLAGS_NO_STD_OPTS);
565 while ((ch = RTGetOpt(&GetState, &Value)))
566 {
567 switch (ch)
568 {
569 case 'v':
570 fVerbose = true;
571 break;
572 case 'u':
573 pszUser = Value.psz;
574 break;
575 case 'p':
576 pszPwdFile = Value.psz;
577 break;
578 default:
579 return autostartSvcDisplayGetOptError("create", ch, &Value);
580 }
581 }
582
583 if (!pszUser)
584 return autostartSvcDisplayError("Username is missing");
585
586 if (pszPwdFile)
587 {
588 /* Get password from file. */
589 RTEXITCODE rcExit = readPasswordFile(pszPwdFile, &strPwd);
590 if (rcExit == RTEXITCODE_FAILURE)
591 return rcExit;
592 }
593 else
594 {
595 /* Get password from console. */
596 RTEXITCODE rcExit = readPasswordFromConsole(&strPwd, "Enter password:");
597 if (rcExit == RTEXITCODE_FAILURE)
598 return rcExit;
599 }
600
601 if (strPwd.isEmpty())
602 return autostartSvcDisplayError("Password is missing");
603
604 com::Utf8Str sDomain;
605 com::Utf8Str sUserTmp;
606 int vrc = autostartGetDomainAndUser(pszUser, sDomain, sUserTmp);
607 if (RT_FAILURE(vrc))
608 return autostartSvcDisplayError("create - CreateService failed, failed to get domain and user from string %s (%d).\n",
609 pszUser, vrc);
610 com::Utf8StrFmt sUserFullName("%s\\%s", sDomain.c_str(), sUserTmp.c_str());
611 com::Utf8StrFmt sDisplayName("%s %s@%s", AUTOSTART_SERVICE_DISPLAY_NAME, sUserTmp.c_str(), sDomain.c_str());
612 com::Utf8Str sServiceName;
613 autostartFormatServiceName(sDomain, sUserTmp, sServiceName);
614
615 /*
616 * Create the service.
617 */
618 RTEXITCODE rc = RTEXITCODE_FAILURE;
619 SC_HANDLE hSCM = autostartSvcWinOpenSCManager("create", SC_MANAGER_CREATE_SERVICE); /*SC_MANAGER_ALL_ACCESS*/
620 if (hSCM)
621 {
622 char szExecPath[RTPATH_MAX];
623 if (RTProcGetExecutablePath(szExecPath, sizeof(szExecPath)))
624 {
625 if (fVerbose)
626 RTPrintf("Creating the %s service, binary \"%s\"...\n",
627 sServiceName.c_str(), szExecPath); /* yea, the binary name isn't UTF-8, but wtf. */
628
629 /*
630 * Add service name as command line parameter for the service
631 */
632 com::Utf8StrFmt sCmdLine("\"%s\" --service=%s", szExecPath, sServiceName.c_str());
633 com::Bstr bstrServiceName(sServiceName);
634 com::Bstr bstrDisplayName(sDisplayName);
635 com::Bstr bstrCmdLine(sCmdLine);
636 com::Bstr bstrUserFullName(sUserFullName);
637 com::Bstr bstrPwd(strPwd);
638
639 SC_HANDLE hSvc = CreateServiceW(hSCM, /* hSCManager */
640 bstrServiceName.raw(), /* lpServiceName */
641 bstrDisplayName.raw(), /* lpDisplayName */
642 SERVICE_CHANGE_CONFIG | SERVICE_QUERY_STATUS | SERVICE_QUERY_CONFIG, /* dwDesiredAccess */
643 SERVICE_WIN32_OWN_PROCESS, /* dwServiceType ( | SERVICE_INTERACTIVE_PROCESS? ) */
644 SERVICE_AUTO_START, /* dwStartType */
645 SERVICE_ERROR_NORMAL, /* dwErrorControl */
646 bstrCmdLine.raw(), /* lpBinaryPathName */
647 NULL, /* lpLoadOrderGroup */
648 NULL, /* lpdwTagId */
649 NULL, /* lpDependencies */
650 bstrUserFullName.raw(), /* lpServiceStartName (NULL => LocalSystem) */
651 bstrPwd.raw()); /* lpPassword */
652 if (hSvc)
653 {
654 RTPrintf("Successfully created the %s service.\n", sServiceName.c_str());
655 /** @todo Set the service description or it'll look weird in the vista service manager.
656 * Anything else that should be configured? Start access or something? */
657 rc = RTEXITCODE_SUCCESS;
658 CloseServiceHandle(hSvc);
659 }
660 else
661 {
662 DWORD err = GetLastError();
663 switch (err)
664 {
665 case ERROR_SERVICE_EXISTS:
666 autostartSvcDisplayError("create - The service already exists.\n");
667 break;
668 default:
669 autostartSvcDisplayError("create - CreateService failed, err=%d.\n", GetLastError());
670 break;
671 }
672 }
673 CloseServiceHandle(hSvc);
674 }
675 else
676 autostartSvcDisplayError("create - Failed to obtain the executable path: %d\n", GetLastError());
677 }
678 return rc;
679}
680
681
682/**
683 * Sets the service status, just a SetServiceStatus Wrapper.
684 *
685 * @returns See SetServiceStatus.
686 * @param dwStatus The current status.
687 * @param iWaitHint The wait hint, if < 0 then supply a default.
688 * @param dwExitCode The service exit code.
689 */
690static bool autostartSvcWinSetServiceStatus(DWORD dwStatus, int iWaitHint, DWORD dwExitCode)
691{
692 SERVICE_STATUS SvcStatus;
693 SvcStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
694 SvcStatus.dwWin32ExitCode = dwExitCode;
695 SvcStatus.dwServiceSpecificExitCode = 0;
696 SvcStatus.dwWaitHint = iWaitHint >= 0 ? iWaitHint : 3000;
697 SvcStatus.dwCurrentState = dwStatus;
698 LogFlow(("autostartSvcWinSetServiceStatus: %d -> %d\n", g_u32SupSvcWinStatus, dwStatus));
699 g_u32SupSvcWinStatus = dwStatus;
700 switch (dwStatus)
701 {
702 case SERVICE_START_PENDING:
703 SvcStatus.dwControlsAccepted = 0;
704 break;
705 default:
706 SvcStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;
707 break;
708 }
709
710 static DWORD dwCheckPoint = 0;
711 switch (dwStatus)
712 {
713 case SERVICE_RUNNING:
714 case SERVICE_STOPPED:
715 SvcStatus.dwCheckPoint = 0;
716 default:
717 SvcStatus.dwCheckPoint = ++dwCheckPoint;
718 break;
719 }
720 return SetServiceStatus(g_hSupSvcWinCtrlHandler, &SvcStatus) != FALSE;
721}
722
723
724/**
725 * Service control handler (extended).
726 *
727 * @returns Windows status (see HandlerEx).
728 * @retval NO_ERROR if handled.
729 * @retval ERROR_CALL_NOT_IMPLEMENTED if not handled.
730 *
731 * @param dwControl The control code.
732 * @param dwEventType Event type. (specific to the control?)
733 * @param pvEventData Event data, specific to the event.
734 * @param pvContext The context pointer registered with the handler.
735 * Currently not used.
736 */
737static DWORD WINAPI
738autostartSvcWinServiceCtrlHandlerEx(DWORD dwControl, DWORD dwEventType, LPVOID pvEventData, LPVOID pvContext) RT_NOTHROW_DEF
739{
740 LogFlow(("autostartSvcWinServiceCtrlHandlerEx: dwControl=%#x dwEventType=%#x pvEventData=%p\n",
741 dwControl, dwEventType, pvEventData));
742
743 switch (dwControl)
744 {
745 /*
746 * Interrogate the service about it's current status.
747 * MSDN says that this should just return NO_ERROR and does
748 * not need to set the status again.
749 */
750 case SERVICE_CONTROL_INTERROGATE:
751 return NO_ERROR;
752
753 /*
754 * Request to stop the service.
755 */
756 case SERVICE_CONTROL_STOP:
757 {
758 /*
759 * Check if the real services can be stopped and then tell them to stop.
760 */
761 autostartSvcWinSetServiceStatus(SERVICE_STOP_PENDING, 3000, NO_ERROR);
762 /*
763 * Notify the main thread that we're done, it will wait for the
764 * VMs to stop, and set the windows service status to SERVICE_STOPPED
765 * and return.
766 */
767 int rc = RTSemEventMultiSignal(g_hSupSvcWinEvent);
768 if (RT_FAILURE(rc))
769 autostartSvcLogError("SERVICE_CONTROL_STOP: RTSemEventMultiSignal failed, %Rrc\n", rc);
770
771 return NO_ERROR;
772 }
773
774 case SERVICE_CONTROL_PAUSE:
775 case SERVICE_CONTROL_CONTINUE:
776 case SERVICE_CONTROL_SHUTDOWN:
777 case SERVICE_CONTROL_PARAMCHANGE:
778 case SERVICE_CONTROL_NETBINDADD:
779 case SERVICE_CONTROL_NETBINDREMOVE:
780 case SERVICE_CONTROL_NETBINDENABLE:
781 case SERVICE_CONTROL_NETBINDDISABLE:
782 case SERVICE_CONTROL_DEVICEEVENT:
783 case SERVICE_CONTROL_HARDWAREPROFILECHANGE:
784 case SERVICE_CONTROL_POWEREVENT:
785 case SERVICE_CONTROL_SESSIONCHANGE:
786#ifdef SERVICE_CONTROL_PRESHUTDOWN /* vista */
787 case SERVICE_CONTROL_PRESHUTDOWN:
788#endif
789 default:
790 return ERROR_CALL_NOT_IMPLEMENTED;
791 }
792
793 NOREF(dwEventType);
794 NOREF(pvEventData);
795 NOREF(pvContext);
796 /* not reached */
797}
798
799static RTEXITCODE autostartStartVMs()
800{
801 int rc = autostartSetup();
802 if (RT_FAILURE(rc))
803 return RTEXITCODE_FAILURE;
804
805 const char *pszConfigFile = RTEnvGet("VBOXAUTOSTART_CONFIG");
806 if (!pszConfigFile)
807 return autostartSvcLogError("Starting VMs failed. VBOXAUTOSTART_CONFIG environment variable is not defined.\n");
808 bool fAllow = false;
809
810 PCFGAST pCfgAst = NULL;
811 rc = autostartParseConfig(pszConfigFile, &pCfgAst);
812 if (RT_FAILURE(rc))
813 return autostartSvcLogError("Starting VMs failed. Failed to parse the config file. Check the access permissions and file structure.\n");
814
815 PCFGAST pCfgAstPolicy = autostartConfigAstGetByName(pCfgAst, "default_policy");
816 /* Check default policy. */
817 if (pCfgAstPolicy)
818 {
819 if ( pCfgAstPolicy->enmType == CFGASTNODETYPE_KEYVALUE
820 && ( !RTStrCmp(pCfgAstPolicy->u.KeyValue.aszValue, "allow")
821 || !RTStrCmp(pCfgAstPolicy->u.KeyValue.aszValue, "deny")))
822 {
823 if (!RTStrCmp(pCfgAstPolicy->u.KeyValue.aszValue, "allow"))
824 fAllow = true;
825 }
826 else
827 {
828 autostartConfigAstDestroy(pCfgAst);
829 return autostartSvcLogError("'default_policy' must be either 'allow' or 'deny'.\n");
830 }
831 }
832
833 com::Utf8Str sUser;
834 rc = autostartGetProcessDomainUser(sUser);
835 if (RT_FAILURE(rc))
836 {
837 autostartConfigAstDestroy(pCfgAst);
838 return autostartSvcLogError("Failed to query username of the process (%Rrc).\n", rc);
839 }
840
841 PCFGAST pCfgAstUser = NULL;
842 for (unsigned i = 0; i < pCfgAst->u.Compound.cAstNodes; i++)
843 {
844 PCFGAST pNode = pCfgAst->u.Compound.apAstNodes[i];
845 com::Utf8Str sDomain;
846 com::Utf8Str sUserTmp;
847 rc = autostartGetDomainAndUser(pNode->pszKey, sDomain, sUserTmp);
848 if (RT_FAILURE(rc))
849 continue;
850 com::Utf8StrFmt sDomainUser("%s\\%s", sDomain.c_str(), sUserTmp.c_str());
851 if (sDomainUser == sUser)
852 {
853 pCfgAstUser = pNode;
854 break;
855 }
856 }
857
858 if ( pCfgAstUser
859 && pCfgAstUser->enmType == CFGASTNODETYPE_COMPOUND)
860 {
861 pCfgAstPolicy = autostartConfigAstGetByName(pCfgAstUser, "allow");
862 if (pCfgAstPolicy)
863 {
864 if ( pCfgAstPolicy->enmType == CFGASTNODETYPE_KEYVALUE
865 && ( !RTStrCmp(pCfgAstPolicy->u.KeyValue.aszValue, "true")
866 || !RTStrCmp(pCfgAstPolicy->u.KeyValue.aszValue, "false")))
867 fAllow = RTStrCmp(pCfgAstPolicy->u.KeyValue.aszValue, "true") == 0;
868 else
869 {
870 autostartConfigAstDestroy(pCfgAst);
871 return autostartSvcLogError("'allow' must be either 'true' or 'false'.\n");
872 }
873 }
874 }
875 else if (pCfgAstUser)
876 {
877 autostartConfigAstDestroy(pCfgAst);
878 return autostartSvcLogError("Invalid config, user is not a compound node.\n");
879 }
880
881 if (!fAllow)
882 {
883 autostartConfigAstDestroy(pCfgAst);
884 return autostartSvcLogError("User is not allowed to autostart VMs.\n");
885 }
886
887 RTEXITCODE rcExit = autostartStartMain(pCfgAstUser);
888 autostartConfigAstDestroy(pCfgAst);
889 if (rcExit != RTEXITCODE_SUCCESS)
890 autostartSvcLogError("Starting VMs failed\n");
891
892 return rcExit;
893}
894
895/**
896 * Windows Service Main.
897 *
898 * This is invoked when the service is started and should not return until
899 * the service has been stopped.
900 *
901 * @param cArgs Argument count.
902 * @param papwszArgs Argument vector.
903 */
904static VOID WINAPI autostartSvcWinServiceMain(DWORD cArgs, LPWSTR *papwszArgs)
905{
906 RT_NOREF(papwszArgs);
907 LogFlowFuncEnter();
908
909 /*
910 * Register the control handler function for the service and report to SCM.
911 */
912 Assert(g_u32SupSvcWinStatus == SERVICE_STOPPED);
913 g_hSupSvcWinCtrlHandler = RegisterServiceCtrlHandlerExW(g_bstrServiceName.raw(), autostartSvcWinServiceCtrlHandlerEx, NULL);
914 if (g_hSupSvcWinCtrlHandler)
915 {
916 DWORD err = ERROR_GEN_FAILURE;
917 if (autostartSvcWinSetServiceStatus(SERVICE_START_PENDING, 3000, NO_ERROR))
918 {
919 if (cArgs == 1)
920 {
921 /*
922 * Create the event semaphore we'll be waiting on and
923 * then instantiate the actual services.
924 */
925 int rc = RTSemEventMultiCreate(&g_hSupSvcWinEvent);
926 if (RT_SUCCESS(rc))
927 {
928 /*
929 * Update the status and enter the work loop.
930 */
931 if (autostartSvcWinSetServiceStatus(SERVICE_RUNNING, 0, 0))
932 {
933 LogFlow(("autostartSvcWinServiceMain: calling autostartStartVMs\n"));
934 RTEXITCODE ec = autostartStartVMs();
935 if (ec == RTEXITCODE_SUCCESS)
936 {
937 LogFlow(("autostartSvcWinServiceMain: done string VMs\n"));
938 err = NO_ERROR;
939 rc = RTSemEventMultiWait(g_hSupSvcWinEvent, RT_INDEFINITE_WAIT);
940 if (RT_SUCCESS(rc))
941 {
942 LogFlow(("autostartSvcWinServiceMain: woke up\n"));
943 /** @todo Autostop part. */
944 err = NO_ERROR;
945 }
946 else
947 autostartSvcLogError("RTSemEventWait failed, rc=%Rrc", rc);
948 }
949
950 autostartShutdown();
951 }
952 else
953 {
954 err = GetLastError();
955 autostartSvcLogError("SetServiceStatus failed, err=%u", err);
956 }
957
958 RTSemEventMultiDestroy(g_hSupSvcWinEvent);
959 g_hSupSvcWinEvent = NIL_RTSEMEVENTMULTI;
960 }
961 else
962 autostartSvcLogError("RTSemEventMultiCreate failed, rc=%Rrc", rc);
963 }
964 else
965 autostartSvcLogTooManyArgsError("main", cArgs, NULL, 0);
966 }
967 else
968 {
969 err = GetLastError();
970 autostartSvcLogError("SetServiceStatus failed, err=%u", err);
971 }
972 autostartSvcWinSetServiceStatus(SERVICE_STOPPED, 0, err);
973 }
974 else
975 autostartSvcLogError("RegisterServiceCtrlHandlerEx failed, err=%u", GetLastError());
976
977 LogFlowFuncLeave();
978}
979
980
981/**
982 * Handle the 'create' action.
983 *
984 * @returns RTEXITCODE_SUCCESS or RTEXITCODE_FAILURE.
985 * @param argc The action argument count.
986 * @param argv The action argument vector.
987 */
988static int autostartSvcWinRunIt(int argc, char **argv)
989{
990 int rc;
991
992 LogFlowFuncEnter();
993
994 /*
995 * Initialize release logging, do this early. This means command
996 * line options (like --logfile &c) can't be introduced to affect
997 * the log file parameters, but the user can't change them easily
998 * anyway and is better off using environment variables.
999 */
1000 do
1001 {
1002 char szLogFile[RTPATH_MAX];
1003 rc = com::GetVBoxUserHomeDirectory(szLogFile, sizeof(szLogFile),
1004 /* :fCreateDir */ false);
1005 if (RT_FAILURE(rc))
1006 {
1007 autostartSvcLogError("Failed to get VirtualBox user home directory: %Rrc\n", rc);
1008 break;
1009 }
1010
1011 if (!RTDirExists(szLogFile)) /* vbox user home dir */
1012 {
1013 autostartSvcLogError("%s doesn't exist\n", szLogFile);
1014 break;
1015 }
1016
1017 rc = RTPathAppend(szLogFile, sizeof(szLogFile), "VBoxAutostart.log");
1018 if (RT_FAILURE(rc))
1019 {
1020 autostartSvcLogError("Failed to construct release log file name: %Rrc\n", rc);
1021 break;
1022 }
1023
1024 rc = com::VBoxLogRelCreate("Autostart",
1025 szLogFile,
1026 RTLOGFLAGS_PREFIX_THREAD
1027 | RTLOGFLAGS_PREFIX_TIME_PROG,
1028 "all",
1029 "VBOXAUTOSTART_RELEASE_LOG",
1030 RTLOGDEST_FILE,
1031 UINT32_MAX /* cMaxEntriesPerGroup */,
1032 g_cHistory,
1033 g_uHistoryFileTime,
1034 g_uHistoryFileSize,
1035 NULL);
1036 if (RT_FAILURE(rc))
1037 autostartSvcLogError("Failed to create release log file: %Rrc\n", rc);
1038 } while (0);
1039
1040
1041
1042 /*
1043 * Parse the arguments.
1044 */
1045 static const RTGETOPTDEF s_aOptions[] =
1046 {
1047 { "--service", 's', RTGETOPT_REQ_STRING },
1048 };
1049
1050 const char *pszServiceName = NULL;
1051 int ch;
1052 RTGETOPTUNION Value;
1053 RTGETOPTSTATE GetState;
1054 RTGetOptInit(&GetState, argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions), 0, RTGETOPTINIT_FLAGS_NO_STD_OPTS);
1055 while ((ch = RTGetOpt(&GetState, &Value)))
1056 {
1057 switch (ch)
1058 {
1059 case 's':
1060 pszServiceName = Value.psz;
1061 try
1062 {
1063 g_bstrServiceName = com::Bstr(Value.psz);
1064 }
1065 catch (...)
1066 {
1067 autostartSvcLogError("runit failed, service name is not valid utf-8 string or out of memory");
1068 return RTEXITCODE_FAILURE;
1069 }
1070 break;
1071
1072 default:
1073 /**
1074 * @todo autostartSvcLogGetOptError is useless as it
1075 * is, should be change after RTGetOptPrintError.
1076 */
1077 return autostartSvcLogError("RTGetOpt: %Rrc\n", ch);
1078 }
1079 }
1080
1081 if (!pszServiceName)
1082 {
1083 autostartSvcLogError("runit failed, service name is missing");
1084 return RTEXITCODE_FAILURE;
1085 }
1086
1087 LogRel(("Starting service %ls\n", g_bstrServiceName.raw()));
1088
1089 /*
1090 * Register the service with the service control manager
1091 * and start dispatching requests from it (all done by the API).
1092 */
1093 SERVICE_TABLE_ENTRYW const s_aServiceStartTable[] =
1094 {
1095 { g_bstrServiceName.raw(), autostartSvcWinServiceMain },
1096 { NULL, NULL}
1097 };
1098 if (StartServiceCtrlDispatcherW(&s_aServiceStartTable[0]))
1099 {
1100 LogFlowFuncLeave();
1101 return RTEXITCODE_SUCCESS; /* told to quit, so quit. */
1102 }
1103
1104 DWORD err = GetLastError();
1105 switch (err)
1106 {
1107 case ERROR_FAILED_SERVICE_CONTROLLER_CONNECT:
1108 autostartSvcWinServiceMain(0, NULL);//autostartSvcDisplayError("Cannot run a service from the command line. Use the 'start' action to start it the right way.\n");
1109 break;
1110 default:
1111 autostartSvcLogError("StartServiceCtrlDispatcher failed, err=%u", err);
1112 break;
1113 }
1114 return RTEXITCODE_FAILURE;
1115}
1116
1117
1118/**
1119 * Show the version info.
1120 *
1121 * @returns RTEXITCODE_SUCCESS.
1122 */
1123static RTEXITCODE autostartSvcWinShowVersion(int argc, char **argv)
1124{
1125 /*
1126 * Parse the arguments.
1127 */
1128 bool fBrief = false;
1129 static const RTGETOPTDEF s_aOptions[] =
1130 {
1131 { "--brief", 'b', RTGETOPT_REQ_NOTHING }
1132 };
1133 int ch;
1134 RTGETOPTUNION Value;
1135 RTGETOPTSTATE GetState;
1136 RTGetOptInit(&GetState, argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions), 0, RTGETOPTINIT_FLAGS_NO_STD_OPTS);
1137 while ((ch = RTGetOpt(&GetState, &Value)))
1138 switch (ch)
1139 {
1140 case 'b': fBrief = true; break;
1141 default: return autostartSvcDisplayGetOptError("version", ch, &Value);
1142 }
1143
1144 /*
1145 * Do the printing.
1146 */
1147 if (fBrief)
1148 RTPrintf("%s\n", VBOX_VERSION_STRING);
1149 else
1150 RTPrintf("VirtualBox Autostart Service Version %s\n"
1151 "(C) 2012 Oracle Corporation\n"
1152 "All rights reserved.\n",
1153 VBOX_VERSION_STRING);
1154 return RTEXITCODE_SUCCESS;
1155}
1156
1157
1158/**
1159 * Show the usage help screen.
1160 *
1161 * @returns RTEXITCODE_SUCCESS.
1162 */
1163static RTEXITCODE autostartSvcWinShowHelp(void)
1164{
1165 RTPrintf("VirtualBox Autostart Service Version %s\n"
1166 "(C) 2012 Oracle Corporation\n"
1167 "All rights reserved.\n"
1168 "\n",
1169 VBOX_VERSION_STRING);
1170 RTPrintf("Usage:\n"
1171 "\n"
1172 "VBoxAutostartSvc\n"
1173 " Runs the service.\n"
1174 "VBoxAutostartSvc <version|-v|--version> [-brief]\n"
1175 " Displays the version.\n"
1176 "VBoxAutostartSvc <help|-?|-h|--help> [...]\n"
1177 " Displays this help screen.\n"
1178 "\n"
1179 "VBoxAutostartSvc <install|/RegServer|/i>\n"
1180 " Installs the service.\n"
1181 "VBoxAutostartSvc <uninstall|delete|/UnregServer|/u>\n"
1182 " Uninstalls the service.\n"
1183 );
1184 return RTEXITCODE_SUCCESS;
1185}
1186
1187
1188/**
1189 * VBoxAutostart main(), Windows edition.
1190 *
1191 *
1192 * @returns 0 on success.
1193 *
1194 * @param argc Number of arguments in argv.
1195 * @param argv Argument vector.
1196 */
1197int main(int argc, char **argv)
1198{
1199 /*
1200 * Initialize the IPRT first of all.
1201 */
1202 int rc = RTR3InitExe(argc, &argv, 0);
1203 if (RT_FAILURE(rc))
1204 {
1205 autostartSvcLogError("RTR3InitExe failed with rc=%Rrc", rc);
1206 return RTEXITCODE_FAILURE;
1207 }
1208
1209 RTThreadSleep(10 * 1000);
1210
1211 /*
1212 * Parse the initial arguments to determine the desired action.
1213 */
1214 enum
1215 {
1216 kAutoSvcAction_RunIt,
1217
1218 kAutoSvcAction_Create,
1219 kAutoSvcAction_Delete,
1220
1221 kAutoSvcAction_Enable,
1222 kAutoSvcAction_Disable,
1223 kAutoSvcAction_QueryConfig,
1224 kAutoSvcAction_QueryDescription,
1225
1226 kAutoSvcAction_Start,
1227 kAutoSvcAction_Pause,
1228 kAutoSvcAction_Continue,
1229 kAutoSvcAction_Stop,
1230 kAutoSvcAction_Interrogate,
1231
1232 kAutoSvcAction_End
1233 } enmAction = kAutoSvcAction_RunIt;
1234 int iArg = 1;
1235 if (argc > 1)
1236 {
1237 if ( !stricmp(argv[iArg], "/RegServer")
1238 || !stricmp(argv[iArg], "install")
1239 || !stricmp(argv[iArg], "/i"))
1240 enmAction = kAutoSvcAction_Create;
1241 else if ( !stricmp(argv[iArg], "/UnregServer")
1242 || !stricmp(argv[iArg], "/u")
1243 || !stricmp(argv[iArg], "uninstall")
1244 || !stricmp(argv[iArg], "delete"))
1245 enmAction = kAutoSvcAction_Delete;
1246
1247 else if (!stricmp(argv[iArg], "enable"))
1248 enmAction = kAutoSvcAction_Enable;
1249 else if (!stricmp(argv[iArg], "disable"))
1250 enmAction = kAutoSvcAction_Disable;
1251 else if (!stricmp(argv[iArg], "qconfig"))
1252 enmAction = kAutoSvcAction_QueryConfig;
1253 else if (!stricmp(argv[iArg], "qdescription"))
1254 enmAction = kAutoSvcAction_QueryDescription;
1255
1256 else if ( !stricmp(argv[iArg], "start")
1257 || !stricmp(argv[iArg], "/t"))
1258 enmAction = kAutoSvcAction_Start;
1259 else if (!stricmp(argv[iArg], "pause"))
1260 enmAction = kAutoSvcAction_Start;
1261 else if (!stricmp(argv[iArg], "continue"))
1262 enmAction = kAutoSvcAction_Continue;
1263 else if (!stricmp(argv[iArg], "stop"))
1264 enmAction = kAutoSvcAction_Stop;
1265 else if (!stricmp(argv[iArg], "interrogate"))
1266 enmAction = kAutoSvcAction_Interrogate;
1267 else if ( !stricmp(argv[iArg], "help")
1268 || !stricmp(argv[iArg], "?")
1269 || !stricmp(argv[iArg], "/?")
1270 || !stricmp(argv[iArg], "-?")
1271 || !stricmp(argv[iArg], "/h")
1272 || !stricmp(argv[iArg], "-h")
1273 || !stricmp(argv[iArg], "/help")
1274 || !stricmp(argv[iArg], "-help")
1275 || !stricmp(argv[iArg], "--help"))
1276 return autostartSvcWinShowHelp();
1277 else if ( !stricmp(argv[iArg], "version")
1278 || !stricmp(argv[iArg], "/v")
1279 || !stricmp(argv[iArg], "-v")
1280 || !stricmp(argv[iArg], "/version")
1281 || !stricmp(argv[iArg], "-version")
1282 || !stricmp(argv[iArg], "--version"))
1283 return autostartSvcWinShowVersion(argc - iArg - 1, argv + iArg + 1);
1284 else
1285 iArg--;
1286 iArg++;
1287 }
1288
1289 /*
1290 * Dispatch it.
1291 */
1292 switch (enmAction)
1293 {
1294 case kAutoSvcAction_RunIt:
1295 return autostartSvcWinRunIt(argc - iArg, argv + iArg);
1296
1297 case kAutoSvcAction_Create:
1298 return autostartSvcWinCreate(argc - iArg, argv + iArg);
1299 case kAutoSvcAction_Delete:
1300 return autostartSvcWinDelete(argc - iArg, argv + iArg);
1301
1302 case kAutoSvcAction_Enable:
1303 return autostartSvcWinEnable(argc - iArg, argv + iArg);
1304 case kAutoSvcAction_Disable:
1305 return autostartSvcWinDisable(argc - iArg, argv + iArg);
1306 case kAutoSvcAction_QueryConfig:
1307 return autostartSvcWinQueryConfig(argc - iArg, argv + iArg);
1308 case kAutoSvcAction_QueryDescription:
1309 return autostartSvcWinQueryDescription(argc - iArg, argv + iArg);
1310
1311 case kAutoSvcAction_Start:
1312 return autostartSvcWinStart(argc - iArg, argv + iArg);
1313 case kAutoSvcAction_Pause:
1314 return autostartSvcWinPause(argc - iArg, argv + iArg);
1315 case kAutoSvcAction_Continue:
1316 return autostartSvcWinContinue(argc - iArg, argv + iArg);
1317 case kAutoSvcAction_Stop:
1318 return autostartSvcWinStop(argc - iArg, argv + iArg);
1319 case kAutoSvcAction_Interrogate:
1320 return autostartSvcWinInterrogate(argc - iArg, argv + iArg);
1321
1322 default:
1323 AssertMsgFailed(("enmAction=%d\n", enmAction));
1324 return RTEXITCODE_FAILURE;
1325 }
1326}
1327
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette