Changeset 16716 in vbox for trunk/src/VBox/Main/cbinding/tstdlOpen.c
- Timestamp:
- Feb 12, 2009 5:43:13 PM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Main/cbinding/tstdlOpen.c
r16714 r16716 309 309 } 310 310 311 #if 0 /* finish this. */ 311 #ifdef USE_DYNAMIC_GLUE 312 /** The dlopen handle for VBoxXPCOM. */ 313 void *g_hVBoxXPCOMC = NULL; 314 /** The last load error. */ 315 char g_szVBoxXPCOMErrMsg[256]; 316 317 #if defined(__linux__) || defined(__linux_gnu__) || defined(__sun__) 318 # define SYM_PREFIX "" 319 # define DYNLIB_NAME "VBoxXPCOM.so" 320 #elif defined(__APPLE__) 321 # define SYM_PREFIX "_" 322 # define DYNLIB_NAME "VBoxXPCOM.dylib" 323 #else 324 # error "Port me" 325 #endif 326 312 327 /** 313 328 * Try load VBoxXPCOMC.so/dylib/dll from the specified location and resolve all … … 315 330 * 316 331 * @returns 0 on success, -1 on failure. 317 * @param pszName The shared object / dylib / DLL to try load. 318 * @param fComplain Whether to complain to stderr or not when symbols 319 * are missing. 332 * @param pszHome The director where to try load VBoxXPCOMC from. Can be NULL. 333 * @param pszMsgPrefix Error message prefix. NULL means no error messages. 320 334 */ 321 int tryLoadOne(const char *pszName, int fComplain)335 static int tryLoadOne(const char *pszHome, const char *pszMsgPrefix) 322 336 { 323 337 static struct … … 325 339 const char *pszSymbol; 326 340 void **ppvSym; 327 } s_aSyms[] = 328 { 329 { "VBoxComInitialize", (void **)&VBoxComInitializePtr }, 330 .... 341 } const s_aSyms[] = 342 { 343 { SYM_PREFIX "VBoxComUninitialize", (void **)&VBoxComUninitializePtr }, 344 { SYM_PREFIX "VBoxComUnallocMem", (void **)&VBoxComUnallocMemPtr }, 345 { SYM_PREFIX "VBoxUtf16Free", (void **)&VBoxUtf16FreePtr }, 346 { SYM_PREFIX "VBoxUtf8Free", (void **)&VBoxUtf8FreePtr }, 347 { SYM_PREFIX "VBoxConvertPRUnichartoAscii", (void **)&VBoxConvertPRUnichartoAsciiPtr }, 348 { SYM_PREFIX "VBoxConvertAsciitoPRUnichar", (void **)&VBoxConvertAsciitoPRUnicharPtr }, 349 { SYM_PREFIX "VBoxUtf16ToUtf8", (void **)&VBoxUtf16ToUtf8Ptr }, 350 { SYM_PREFIX "VBoxUtf8ToUtf16", (void **)&VBoxUtf8ToUtf16Ptr }, 351 { SYM_PREFIX "VBoxGetEnv", (void **)&VBoxGetEnvPtr }, 352 { SYM_PREFIX "VBoxSetEnv", (void **)&VBoxSetEnvPtr } 331 353 }; 332 333 /* Just try dlopen it and run over the s_aSyms table calling dlsym on 334 each entry. */ 335 return 0 354 size_t cchHome = pszHome ? strlen(pszHome) : 0; 355 size_t cbBuf; 356 char * pszBuf; 357 int rc = 0; 358 359 /* 360 * Construct the full name. 361 */ 362 cbBuf = cchHome + sizeof("/" DYNLIB_NAME); 363 pszBuf = (char *)malloc(cbBuf); 364 if (!pszBuf) 365 { 366 sprintf(g_szVBoxXPCOMErrMsg, "malloc(%u) failed", (unsigned)cbBuf); 367 if (pszMsgPrefix) 368 fprintf(stderr, "%s%s\n", pszMsgPrefix, g_szVBoxXPCOMErrMsg); 369 return -1; 370 } 371 if (pszHome) 372 { 373 memcpy(pszBuf, pszHome, cchHome); 374 pszBuf[cchHome] = '/'; 375 cchHome++; 376 } 377 memcpy(&pszBuf[cchHome], DYNLIB_NAME, sizeof(DYNLIB_NAME)); 378 379 /* 380 * Try load it by that name, setting the VBOX_APP_HOME first (for now). 381 */ 382 setenv("VBOX_APP_HOME", pszBuf, 0 /* no need to overwrite */); 383 g_hVBoxXPCOMC = dlopen(pszBuf, RTLD_NOW | RTLD_LOCAL); 384 if (g_hVBoxXPCOMC) 385 { 386 unsigned i = sizeof(s_aSyms) / sizeof(s_aSyms[0]); 387 while (i-- > 0) 388 { 389 void *pv = dlsym(g_hVBoxXPCOMC, s_aSyms[i].pszSymbol); 390 if (!pv) 391 { 392 sprintf(g_szVBoxXPCOMErrMsg, "dlsym(%.80s/%.32s): %128s", 393 pszBuf, s_aSyms[i].pszSymbol, dlerror()); 394 if (pszMsgPrefix) 395 fprintf(stderr, "%s%s\n", pszMsgPrefix, g_szVBoxXPCOMErrMsg); 396 rc = -1; 397 break; 398 } 399 *s_aSyms[i].ppvSym = pv; 400 } 401 } 402 else 403 { 404 sprintf(g_szVBoxXPCOMErrMsg, "dlopen(%.80s): %128s", pszBuf, dlerror()); 405 rc = -1; 406 } 407 free(pszBuf); 408 return rc; 336 409 } 337 410 … … 341 414 * 342 415 * @returns 0 on success, -1 on failure. 343 * @param fComplain Whether to complain to stderr or not.416 * @param pszMsgPrefix Error message prefix. NULL means no error messages. 344 417 * 345 418 * @remark This should be considered moved into a separate glue library since … … 348 421 * source code all around the place. 349 422 */ 350 int tryLoad(int fComplain)423 static int tryLoad(const char *pszMsgPrefix) 351 424 { 352 /* Check getenv("VBOX_APP_HOME") first, then try the standard 353 locations for this platform, finally try load it without a path. */ 425 /* 426 * If the user specifies the location, try only that. 427 */ 428 const char *pszHome = getenv("VBOX_APP_HOME"); 429 if (pszHome) 430 return tryLoadOne(pszHome, pszMsgPrefix); 431 432 /* 433 * Try the known standard locations. 434 */ 435 #if defined(__gnu__linux__) || defined(__linux__) 436 if (tryLoadOne("/opt/VirtualBox", pszMsgPrefix) == 0) 437 return 0; 438 if (tryLoadOne("/usr/lib/virtualbox", pszMsgPrefix) == 0) 439 return 0; 440 #elif defined(__sun__) 441 if (tryLoadOne("/opt/VirtualBox/amd64", pszMsgPrefix) == 0) 442 return 0; 443 if (tryLoadOne("/opt/VirtualBox/i386", pszMsgPrefix) == 0) 444 return 0; 445 #elif defined(__APPLE__) 446 if (tryLoadOne("/Application/VirtualBox.app/Contents/MacOS", pszMsgPrefix) == 0) 447 return 0; 448 #else 449 # error "port me" 450 #endif 451 452 /* 453 * Finally try the dynamic linker search path. 454 */ 455 if (tryLoadOne(NULL, pszMsgPrefix) == 0) 456 return 0; 457 458 /* No luck, return failure. */ 459 if (pszMsgPrefix) 460 fprintf(stderr, "%sFailed to locate VBoxXPCOMC\n", pszMsgPrefix); 354 461 return -1; 355 462 } … … 366 473 PRUnichar *versionUtf16 = NULL; 367 474 PRUnichar *homefolderUtf16 = NULL; 475 nsresult rc; /* Result code of various function (method) calls. */ 476 #ifndef USE_DYNAMIC_GLUE 368 477 void *xpcomHandle = NULL; 369 478 const char *xpcomdlError; 370 479 struct stat stIgnored; 371 nsresult rc; /* Result code of various function (method) calls. */372 480 373 481 /* … … 406 514 VBoxSetEnvPtr("VBOX_APP_HOME","/usr/lib/virtualbox/"); 407 515 } 516 #else 517 /* 518 * Initialize the dynamic linking glue. 519 */ 520 521 if (tryLoad(NULL) != 0) 522 { 523 fprintf(stderr, "%s: FATAL: %s\n", argv[0], g_szVBoxXPCOMErrMsg); 524 return EXIT_FAILURE; 525 } 526 #endif 408 527 409 528 printf("Starting Main\n"); … … 416 535 */ 417 536 537 #ifndef USE_DYNAMIC_GLUE 418 538 dlerror(); /* Clear any existing error */ 419 539 *(void **) (&VBoxComInitializePtr) = dlsym(xpcomHandle, "VBoxComInitialize"); … … 422 542 return EXIT_FAILURE; 423 543 } 544 #endif 424 545 VBoxComInitializePtr(&vbox, &session); 425 546 … … 435 556 } 436 557 558 #ifndef USE_DYNAMIC_GLUE 437 559 *(void **) (&VBoxUtf16ToUtf8Ptr) = dlsym(xpcomHandle, "VBoxUtf16ToUtf8"); 438 560 if ((xpcomdlError = dlerror()) != NULL) { … … 460 582 return EXIT_FAILURE; 461 583 } 584 #endif 585 462 586 /* 463 587 * Now ask for revision, version and home folder information of … … 524 648 */ 525 649 650 #ifndef USE_DYNAMIC_GLUE 526 651 *(void **) (&VBoxComUninitializePtr) = dlsym(xpcomHandle, "VBoxComUninitialize"); 527 652 if ((xpcomdlError = dlerror()) != NULL) { … … 529 654 return EXIT_FAILURE; 530 655 } 656 #endif 531 657 VBoxComUninitializePtr(); 658 #ifndef USE_DYNAMIC_GLUE 532 659 dlclose(xpcomHandle); 660 #endif 533 661 printf("Finished Main\n"); 534 662
Note:
See TracChangeset
for help on using the changeset viewer.