281
281
VBoxServiceVerbose(2, "Initializing services ...\n");
282
282
for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
284
rc = g_aServices[j].pDesc->pfnInit();
283
if (g_aServices[j].fEnabled)
287
VBoxServiceError("Service '%s' failed pre-init: %Rrc\n", g_aServices[j].pDesc->pszName);
285
rc = g_aServices[j].pDesc->pfnInit();
288
VBoxServiceError("Service '%s' failed to initialize: %Rrc\n",
289
g_aServices[j].pDesc->pszName, rc);
293
295
* Start the service(s).
313
315
/* wait for the thread to initialize */
314
316
RTThreadUserWait(g_aServices[j].Thread, 60 * 1000);
315
317
if (g_aServices[j].fShutdown)
319
VBoxServiceError("Service '%s' failed to start!\n", g_aServices[j].pDesc->pszName);
316
320
rc = VERR_GENERAL_FAILURE;
318
323
if (RT_SUCCESS(rc))
347
350
if (g_aServices[j].fStarted)
348
351
g_aServices[j].pDesc->pfnStop();
349
352
for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
351
if (g_aServices[j].Thread != NIL_RTTHREAD)
353
if (g_aServices[j].fEnabled)
353
int rc = RTThreadWait(g_aServices[j].Thread, 30*1000, NULL);
355
VBoxServiceError("Service '%s' failed to stop. (%Rrc)\n", g_aServices[j].pDesc->pszName, rc);
355
if (g_aServices[j].Thread != NIL_RTTHREAD)
358
VBoxServiceVerbose(2, "Waiting for service '%s' to stop ...\n", g_aServices[j].pDesc->pszName);
359
for (int i=0; i<30; i++) /* Wait 30 seconds in total */
361
rc = RTThreadWait(g_aServices[j].Thread, 1000 /* Wait 1 second */, NULL);
365
/* Notify SCM that it takes a bit longer ... */
366
VBoxServiceWinSetStatus(SERVICE_STOP_PENDING, i);
370
VBoxServiceError("Service '%s' failed to stop. (%Rrc)\n", g_aServices[j].pDesc->pszName, rc);
372
VBoxServiceVerbose(3, "Terminating service '%s' (%d) ...\n", g_aServices[j].pDesc->pszName, j);
373
g_aServices[j].pDesc->pfnTerm();
357
g_aServices[j].pDesc->pfnTerm();
360
376
VBoxServiceVerbose(2, "Stopping services returned: rc=%Rrc\n", rc);
365
381
int main(int argc, char **argv)
367
383
int rc = VINF_SUCCESS;
370
385
* Init globals and such.
390
* Connect to the kernel part before daemonizing so we can fail
391
* and complain if there is some kind of problem. We need to initialize
392
* the guest lib *before* we do the pre-init just in case one of services
393
* needs do to some initial stuff with it.
395
VBoxServiceVerbose(2, "Calling VbgR3Init()\n");
398
return VBoxServiceError("VbglR3Init failed with rc=%Rrc.\n", rc);
400
/* Do pre-init of services. */
373
401
g_pszProgName = RTPathFilename(argv[0]);
374
402
for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
521
549
if (iMain == ~0U)
522
550
return VBoxServiceSyntax("At least one service must be enabled.\n");
525
* Connect to the kernel part before daemonizing so we can fail
526
* and complain if there is some kind of problem.
528
VBoxServiceVerbose(2, "Calling VbgR3Init()\n");
531
return VBoxServiceError("VbglR3Init failed with rc=%Rrc.\n", rc);
533
552
VBoxServiceVerbose(0, "Started. Verbose level = %d\n", g_cVerbosity);