Changeset 19031 in vbox for trunk/src/VBox/Additions/common/VBoxService/VBoxService.cpp
- Timestamp:
- Apr 20, 2009 2:48:09 PM (16 years ago)
- svn:sync-xref-src-repo-rev:
- 46142
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Additions/common/VBoxService/VBoxService.cpp
r18712 r19031 162 162 va_end(va); 163 163 164 #if 0 /* enable after 2.2 */165 164 va_start(va, pszFormat); 166 LogRel(("%s: error: %N", g_pszProgName, pszFormat, &va));165 LogRel(("%s: Error: %N", g_pszProgName, pszFormat, &va)); 167 166 va_end(va); 168 #endif169 167 170 168 return 1; … … 247 245 248 246 247 unsigned VBoxServiceGetStartedServices(void) 248 { 249 unsigned iMain = ~0U; 250 for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++) 251 if (g_aServices[j].fEnabled) 252 { 253 iMain = j; 254 break; 255 } 256 257 return iMain; /* Return the index of the main service (must always come last!). */ 258 } 259 249 260 /** 250 261 * Starts the service. … … 262 273 * Initialize the services. 263 274 */ 275 VBoxServiceVerbose(2, "Initializing services ...\n"); 264 276 for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++) 265 277 { … … 275 287 * Start the service(s). 276 288 */ 289 VBoxServiceVerbose(2, "Starting services ...\n"); 277 290 rc = VINF_SUCCESS; 278 291 for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++) … … 282 295 continue; 283 296 297 VBoxServiceVerbose(2, "Starting service '%s' ...\n", g_aServices[j].pDesc->pszName); 284 298 rc = RTThreadCreate(&g_aServices[j].Thread, VBoxServiceThread, (void *)(uintptr_t)j, 0, 285 299 RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, g_aServices[j].pDesc->pszName); … … 299 313 { 300 314 /* The final service runs in the main thread. */ 301 VBoxServiceVerbose(1, " starting '%s' in the main thread\n", g_aServices[iMain].pDesc->pszName);315 VBoxServiceVerbose(1, "Starting '%s' in the main thread\n", g_aServices[iMain].pDesc->pszName); 302 316 rc = g_aServices[iMain].pDesc->pfnWorker(&g_fShutdown); 303 VBoxServiceError("service '%s' stopped unexpected; rc=%Rrc\n", g_aServices[iMain].pDesc->pszName, rc); 304 } 305 317 VBoxServiceError("Service '%s' stopped unexpected; rc=%Rrc\n", g_aServices[iMain].pDesc->pszName, rc); 318 } 319 320 /* Should never get here. */ 306 321 return rc; 307 322 } … … 314 329 * clean up anything that we succeeded in starting. 315 330 */ 316 void VBoxServiceStopServices(void) 317 { 331 int VBoxServiceStopServices(void) 332 { 333 int rc = VINF_SUCCESS; 334 318 335 for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++) 319 336 ASMAtomicXchgBool(&g_aServices[j].fShutdown, true); … … 327 344 int rc = RTThreadWait(g_aServices[j].Thread, 30*1000, NULL); 328 345 if (RT_FAILURE(rc)) 329 VBoxServiceError(" service '%s' failed to stop. (%Rrc)\n", g_aServices[j].pDesc->pszName, rc);346 VBoxServiceError("Service '%s' failed to stop. (%Rrc)\n", g_aServices[j].pDesc->pszName, rc); 330 347 } 331 348 g_aServices[j].pDesc->pfnTerm(); 332 349 } 350 351 VBoxServiceVerbose(2, "Stopping services returned: rc=%Rrc\n", rc); 352 return rc; 333 353 } 334 354 … … 336 356 int main(int argc, char **argv) 337 357 { 338 int rc ;358 int rc = VINF_SUCCESS; 339 359 340 360 /* … … 351 371 352 372 #ifdef RT_OS_WINDOWS 353 /* 354 * Explain the purpose of this exercise and why ignoring the result is a good idea and everything. 355 */ 356 /* Do not use a global namespace ("Global\\") for mutex name here, will blow up NT4 compatibility! */ 373 /* Make sure only one instance of VBoxService runs at a time. Create a global mutex for that. 374 Do not use a global namespace ("Global\\") for mutex name here, will blow up NT4 compatibility! */ 357 375 HANDLE hMutexAppRunning = CreateMutex (NULL, FALSE, VBOXSERVICE_NAME); 358 376 if ( hMutexAppRunning != NULL … … 492 510 * Check that at least one service is enabled. 493 511 */ 494 unsigned iMain = ~0U; 495 for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++) 496 if (g_aServices[j].fEnabled) 497 { 498 iMain = j; 499 break; 500 } 512 unsigned iMain = VBoxServiceGetStartedServices(); 501 513 if (iMain == ~0U) 502 514 return VBoxServiceSyntax("At least one service must be enabled.\n"); … … 529 541 * continue. 530 542 */ 543 VBoxServiceVerbose(2, "Starting service dispatcher ...\n"); 531 544 if (!StartServiceCtrlDispatcher(&g_aServiceTable[0])) 532 545 return VBoxServiceError("StartServiceCtrlDispatcher: %u\n", GetLastError()); 533 546 /* Service now lives in the control dispatcher registered above. */ 534 547 #else 535 548 VBoxServiceVerbose(1, "Daemonizing...\n"); … … 540 553 #endif 541 554 } 542 543 /** @todo Make the main thread responsive to signal so it can shutdown/restart the threads on non-SIGKILL signals. */ 544 545 #ifdef RT_OS_WINDOWS 546 if (not using StartServiceCtrlDispatcher or auto register & starting the service) 547 #endif 548 { 555 else 556 { 557 /** @todo Make the main thread responsive to signal so it can shutdown/restart the threads on non-SIGKILL signals. */ 558 549 559 /* 550 560 * Start the service, enter the main threads run loop and stop them again when it returns. … … 554 564 } 555 565 556 557 566 #ifdef RT_OS_WINDOWS 558 567 /* … … 560 569 */ 561 570 if (hMutexAppRunning != NULL) 562 CloseHandle(hMutexAppRunning); 571 { 572 ::CloseHandle(hMutexAppRunning); 573 hMutexAppRunning = NULL; 574 } 563 575 #endif 564 576
Note:
See TracChangeset
for help on using the changeset viewer.