Changeset 18670 in vbox for trunk/src/VBox/Additions/common/VBoxService
- Timestamp:
- Apr 3, 2009 7:44:47 AM (16 years ago)
- svn:sync-xref-src-repo-rev:
- 45599
- Location:
- trunk/src/VBox/Additions/common/VBoxService
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Additions/common/VBoxService/VBoxService.cpp
r18648 r18670 36 36 #include <iprt/asm.h> 37 37 #include <iprt/path.h> 38 #include <VBox/Log.h> 38 39 #include <VBox/VBoxGuest.h> 39 40 #include "VBoxServiceInternal.h" … … 158 159 int VBoxServiceError(const char *pszFormat, ...) 159 160 { 161 char szBuffer[1024] = { 0 }; 160 162 RTStrmPrintf(g_pStdErr, "%s: error: ", g_pszProgName); 161 163 … … 163 165 va_start(va, pszFormat); 164 166 RTStrmPrintfV(g_pStdErr, pszFormat, va); 167 168 RTStrPrintfV(szBuffer, sizeof(szBuffer), pszFormat, va); 169 LogRel((szBuffer)); 170 165 171 va_end(va); 166 172 … … 184 190 va_start(va, pszFormat); 185 191 RTStrmPrintfV(g_pStdOut, pszFormat, va); 192 193 #ifdef _DEBUG 194 char szBuffer[1024] = { 0 }; 195 RTStrPrintfV(szBuffer, sizeof(szBuffer), pszFormat, va); 196 LogRel((szBuffer)); 197 #endif 198 186 199 va_end(va); 187 200 } … … 239 252 } 240 253 254 int VBoxServiceStartServices(int iMain) 255 { 256 int rc = 0; 257 258 /* 259 * Initialize the services. 260 */ 261 for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++) 262 { 263 rc = g_aServices[j].pDesc->pfnInit(); 264 if (RT_FAILURE(rc)) 265 return VBoxServiceError("Service '%s' failed pre-init: %Rrc\n", g_aServices[j].pDesc->pszName); 266 } 267 268 if (iMain == 0) /* If 0 is specified, start all services. Useful for the Windows SCM stuff. */ 269 iMain = RT_ELEMENTS(g_aServices); 270 271 /* 272 * Start the service(s). 273 */ 274 for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++) 275 { 276 if ( !g_aServices[j].fEnabled 277 || j == iMain) 278 continue; 279 280 rc = RTThreadCreate(&g_aServices[j].Thread, VBoxServiceThread, (void *)(uintptr_t)j, 0, 281 RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, g_aServices[j].pDesc->pszName); 282 if (RT_FAILURE(rc)) 283 { 284 VBoxServiceError("RTThreadCreate failed, rc=%Rrc\n", rc); 285 break; 286 } 287 g_aServices[j].fStarted = true; 288 289 /* wait for the thread to initialize */ 290 RTThreadUserWait(g_aServices[j].Thread, 60 * 1000); 291 if (g_aServices[j].fShutdown) 292 rc = VERR_GENERAL_FAILURE; 293 } 294 if (RT_SUCCESS(rc)) 295 { 296 /* The final service runs in the main thread. */ 297 VBoxServiceVerbose(1, "starting '%s' in the main thread\n", g_aServices[iMain].pDesc->pszName); 298 rc = g_aServices[iMain].pDesc->pfnWorker(&g_fShutdown); 299 VBoxServiceError("service '%s' stopped unexpected; rc=%Rrc\n", g_aServices[iMain].pDesc->pszName, rc); 300 } 301 302 return rc; 303 } 304 305 /* 306 * Stops and terminates the services. 307 */ 308 int VBoxServiceStopServices() 309 { 310 int rc = 0; 311 for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++) 312 ASMAtomicXchgBool(&g_aServices[j].fShutdown, true); 313 for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++) 314 if (g_aServices[j].fStarted) 315 g_aServices[j].pDesc->pfnStop(); 316 for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++) 317 { 318 if (g_aServices[j].Thread != NIL_RTTHREAD) 319 { 320 rc = RTThreadWait(g_aServices[j].Thread, 30*1000, NULL); 321 if (RT_FAILURE(rc)) 322 VBoxServiceError("service '%s' failed to stop. (%Rrc)\n", g_aServices[j].pDesc->pszName, rc); 323 } 324 g_aServices[j].pDesc->pfnTerm(); 325 } 326 327 return rc; 328 } 329 241 330 int main(int argc, char **argv) 242 331 { … … 279 368 if( (*psz != '-') 280 369 #if defined(RT_OS_WINDOWS) 281 && (*psz != '/') 282 #endif 283 ) 370 && (*psz != '/')) 371 #endif 284 372 return VBoxServiceSyntax("Unknown argument '%s'\n", psz); 285 286 373 psz++; 287 374 … … 441 528 /** @todo Make the main thread responsive to signal so it can shutdown/restart the threads on non-SIGKILL signals. */ 442 529 443 /* 444 * Initialize the services. 445 */ 446 for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++) 447 { 448 rc = g_aServices[j].pDesc->pfnInit(); 449 if (RT_FAILURE(rc)) 450 return VBoxServiceError("Service '%s' failed pre-init: %Rrc\n", g_aServices[j].pDesc->pszName); 451 } 452 453 /* 454 * Start the service(s). 455 */ 456 for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++) 457 { 458 if ( !g_aServices[j].fEnabled 459 || j == iMain) 460 continue; 461 462 rc = RTThreadCreate(&g_aServices[j].Thread, VBoxServiceThread, (void *)(uintptr_t)j, 0, 463 RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, g_aServices[j].pDesc->pszName); 464 if (RT_FAILURE(rc)) 465 { 466 VBoxServiceError("RTThreadCreate failed, rc=%Rrc\n", rc); 467 break; 530 rc = VBoxServiceStartServices(iMain); 531 532 #if defined(RT_OS_WINDOWS) 533 if (fDaemonize && fDaemonzied) 534 { 535 while (1) { 536 Sleep (100); /** @todo */ 468 537 } 469 g_aServices[j].fStarted = true; 470 471 /* wait for the thread to initialize */ 472 RTThreadUserWait(g_aServices[j].Thread, 60 * 1000); 473 if (g_aServices[j].fShutdown) 474 rc = VERR_GENERAL_FAILURE; 475 } 476 if (RT_SUCCESS(rc)) 477 { 478 /* The final service runs in the main thread. */ 479 VBoxServiceVerbose(1, "starting '%s' in the main thread\n", g_aServices[iMain].pDesc->pszName); 480 rc = g_aServices[iMain].pDesc->pfnWorker(&g_fShutdown); 481 VBoxServiceError("service '%s' stopped unexpected; rc=%Rrc\n", g_aServices[iMain].pDesc->pszName, rc); 482 } 483 484 /* 485 * Stop and terminate the services. 486 */ 487 for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++) 488 ASMAtomicXchgBool(&g_aServices[j].fShutdown, true); 489 for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++) 490 if (g_aServices[j].fStarted) 491 g_aServices[j].pDesc->pfnStop(); 492 for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++) 493 { 494 if (g_aServices[j].Thread != NIL_RTTHREAD) 495 { 496 rc = RTThreadWait(g_aServices[j].Thread, 30*1000, NULL); 497 if (RT_FAILURE(rc)) 498 VBoxServiceError("service '%s' failed to stop. (%Rrc)\n", g_aServices[j].pDesc->pszName, rc); 499 } 500 g_aServices[j].pDesc->pfnTerm(); 501 } 502 503 return 0; 504 } 505 538 } 539 #endif 540 541 rc = VBoxServiceStopServices(); 542 543 #if defined(RT_OS_WINDOWS) 544 /* Release instance mutex. */ 545 if (hMutexAppRunning != NULL) { 546 CloseHandle (hMutexAppRunning); 547 hMutexAppRunning = NULL; 548 } 549 #endif 550 return rc; 551 } 552 -
trunk/src/VBox/Additions/common/VBoxService/VBoxServiceInternal.h
r18642 r18670 111 111 extern void VBoxServiceVerbose(int iLevel, const char *pszFormat, ...); 112 112 extern int VBoxServiceArgUInt32(int argc, char **argv, const char *psz, int *pi, uint32_t *pu32, uint32_t u32Min, uint32_t u32Max); 113 extern int VBoxServiceStartServices(int iMain); 114 extern int VBoxServiceStopServices(); 113 115 114 116 #if defined(RT_OS_WINDOWS)
Note:
See TracChangeset
for help on using the changeset viewer.