comparison src/os/linux/vm/os_linux.cpp @ 17848:6048424d3865

8038201: Clean up misleading usage of malloc() in init_system_properties_values() Summary: Remove the misleading malloc macro and cleanup the code Reviewed-by: dsamersoff, kvn Contributed-by: goetz.lindenmaier@sap.com
author goetz
date Thu, 10 Apr 2014 04:07:45 -0700
parents 21dd1c827123
children 78bbf4d43a14 364b73402247
comparison
equal deleted inserted replaced
17847:a57ba009d4dc 17848:6048424d3865
317 _physical_memory = (julong)sysconf(_SC_PHYS_PAGES) * (julong)sysconf(_SC_PAGESIZE); 317 _physical_memory = (julong)sysconf(_SC_PHYS_PAGES) * (julong)sysconf(_SC_PAGESIZE);
318 assert(processor_count() > 0, "linux error"); 318 assert(processor_count() > 0, "linux error");
319 } 319 }
320 320
321 void os::init_system_properties_values() { 321 void os::init_system_properties_values() {
322 // char arch[12];
323 // sysinfo(SI_ARCHITECTURE, arch, sizeof(arch));
324
325 // The next steps are taken in the product version: 322 // The next steps are taken in the product version:
326 // 323 //
327 // Obtain the JAVA_HOME value from the location of libjvm.so. 324 // Obtain the JAVA_HOME value from the location of libjvm.so.
328 // This library should be located at: 325 // This library should be located at:
329 // <JAVA_HOME>/jre/lib/<arch>/{client|server}/libjvm.so. 326 // <JAVA_HOME>/jre/lib/<arch>/{client|server}/libjvm.so.
346 // Otherwise exit. 343 // Otherwise exit.
347 // 344 //
348 // Important note: if the location of libjvm.so changes this 345 // Important note: if the location of libjvm.so changes this
349 // code needs to be changed accordingly. 346 // code needs to be changed accordingly.
350 347
351 // The next few definitions allow the code to be verbatim: 348 // See ld(1):
352 #define malloc(n) (char*)NEW_C_HEAP_ARRAY(char, (n), mtInternal) 349 // The linker uses the following search paths to locate required
353 #define getenv(n) ::getenv(n) 350 // shared libraries:
354 351 // 1: ...
355 /* 352 // ...
356 * See ld(1): 353 // 7: The default directories, normally /lib and /usr/lib.
357 * The linker uses the following search paths to locate required
358 * shared libraries:
359 * 1: ...
360 * ...
361 * 7: The default directories, normally /lib and /usr/lib.
362 */
363 #if defined(AMD64) || defined(_LP64) && (defined(SPARC) || defined(PPC) || defined(S390)) 354 #if defined(AMD64) || defined(_LP64) && (defined(SPARC) || defined(PPC) || defined(S390))
364 #define DEFAULT_LIBPATH "/usr/lib64:/lib64:/lib:/usr/lib" 355 #define DEFAULT_LIBPATH "/usr/lib64:/lib64:/lib:/usr/lib"
365 #else 356 #else
366 #define DEFAULT_LIBPATH "/lib:/usr/lib" 357 #define DEFAULT_LIBPATH "/lib:/usr/lib"
367 #endif 358 #endif
368 359
360 // Base path of extensions installed on the system.
361 #define SYS_EXT_DIR "/usr/java/packages"
369 #define EXTENSIONS_DIR "/lib/ext" 362 #define EXTENSIONS_DIR "/lib/ext"
370 #define ENDORSED_DIR "/lib/endorsed" 363 #define ENDORSED_DIR "/lib/endorsed"
371 #define REG_DIR "/usr/java/packages" 364
372 365 // Buffer that fits several sprintfs.
366 // Note that the space for the colon and the trailing null are provided
367 // by the nulls included by the sizeof operator.
368 const size_t bufsize =
369 MAX3((size_t)MAXPATHLEN, // For dll_dir & friends.
370 (size_t)MAXPATHLEN + sizeof(EXTENSIONS_DIR) + sizeof(SYS_EXT_DIR) + sizeof(EXTENSIONS_DIR), // extensions dir
371 (size_t)MAXPATHLEN + sizeof(ENDORSED_DIR)); // endorsed dir
372 char *buf = (char *)NEW_C_HEAP_ARRAY(char, bufsize, mtInternal);
373
374 // sysclasspath, java_home, dll_dir
373 { 375 {
374 /* sysclasspath, java_home, dll_dir */ 376 char *pslash;
375 { 377 os::jvm_path(buf, bufsize);
376 char *home_path; 378
377 char *dll_path; 379 // Found the full path to libjvm.so.
378 char *pslash; 380 // Now cut the path to <java_home>/jre if we can.
379 char buf[MAXPATHLEN]; 381 *(strrchr(buf, '/')) = '\0'; // Get rid of /libjvm.so.
380 os::jvm_path(buf, sizeof(buf)); 382 pslash = strrchr(buf, '/');
381 383 if (pslash != NULL) {
382 // Found the full path to libjvm.so. 384 *pslash = '\0'; // Get rid of /{client|server|hotspot}.
383 // Now cut the path to <java_home>/jre if we can. 385 }
384 *(strrchr(buf, '/')) = '\0'; /* get rid of /libjvm.so */ 386 Arguments::set_dll_dir(buf);
387
388 if (pslash != NULL) {
389 pslash = strrchr(buf, '/');
390 if (pslash != NULL) {
391 *pslash = '\0'; // Get rid of /<arch>.
385 pslash = strrchr(buf, '/'); 392 pslash = strrchr(buf, '/');
386 if (pslash != NULL)
387 *pslash = '\0'; /* get rid of /{client|server|hotspot} */
388 dll_path = malloc(strlen(buf) + 1);
389 if (dll_path == NULL)
390 return;
391 strcpy(dll_path, buf);
392 Arguments::set_dll_dir(dll_path);
393
394 if (pslash != NULL) { 393 if (pslash != NULL) {
395 pslash = strrchr(buf, '/'); 394 *pslash = '\0'; // Get rid of /lib.
396 if (pslash != NULL) {
397 *pslash = '\0'; /* get rid of /<arch> */
398 pslash = strrchr(buf, '/');
399 if (pslash != NULL)
400 *pslash = '\0'; /* get rid of /lib */
401 }
402 } 395 }
403 396 }
404 home_path = malloc(strlen(buf) + 1); 397 }
405 if (home_path == NULL) 398 Arguments::set_java_home(buf);
406 return; 399 set_boot_path('/', ':');
407 strcpy(home_path, buf); 400 }
408 Arguments::set_java_home(home_path); 401
409 402 // Where to look for native libraries.
410 if (!set_boot_path('/', ':')) 403 //
411 return; 404 // Note: Due to a legacy implementation, most of the library path
412 } 405 // is set in the launcher. This was to accomodate linking restrictions
413 406 // on legacy Linux implementations (which are no longer supported).
414 /* 407 // Eventually, all the library path setting will be done here.
415 * Where to look for native libraries 408 //
416 * 409 // However, to prevent the proliferation of improperly built native
417 * Note: Due to a legacy implementation, most of the library path 410 // libraries, the new path component /usr/java/packages is added here.
418 * is set in the launcher. This was to accomodate linking restrictions 411 // Eventually, all the library path setting will be done here.
419 * on legacy Linux implementations (which are no longer supported). 412 {
420 * Eventually, all the library path setting will be done here. 413 // Get the user setting of LD_LIBRARY_PATH, and prepended it. It
421 * 414 // should always exist (until the legacy problem cited above is
422 * However, to prevent the proliferation of improperly built native 415 // addressed).
423 * libraries, the new path component /usr/java/packages is added here. 416 const char *v = ::getenv("LD_LIBRARY_PATH");
424 * Eventually, all the library path setting will be done here. 417 const char *v_colon = ":";
425 */ 418 if (v == NULL) { v = ""; v_colon = ""; }
426 { 419 // That's +1 for the colon and +1 for the trailing '\0'.
427 char *ld_library_path; 420 char *ld_library_path = (char *)NEW_C_HEAP_ARRAY(char,
428 421 strlen(v) + 1 +
429 /* 422 sizeof(SYS_EXT_DIR) + sizeof("/lib/") + strlen(cpu_arch) + sizeof(DEFAULT_LIBPATH) + 1,
430 * Construct the invariant part of ld_library_path. Note that the 423 mtInternal);
431 * space for the colon and the trailing null are provided by the 424 sprintf(ld_library_path, "%s%s" SYS_EXT_DIR "/lib/%s:" DEFAULT_LIBPATH, v, v_colon, cpu_arch);
432 * nulls included by the sizeof operator (so actually we allocate 425 Arguments::set_library_path(ld_library_path);
433 * a byte more than necessary). 426 FREE_C_HEAP_ARRAY(char, ld_library_path, mtInternal);
434 */ 427 }
435 ld_library_path = (char *) malloc(sizeof(REG_DIR) + sizeof("/lib/") + 428
436 strlen(cpu_arch) + sizeof(DEFAULT_LIBPATH)); 429 // Extensions directories.
437 sprintf(ld_library_path, REG_DIR "/lib/%s:" DEFAULT_LIBPATH, cpu_arch); 430 sprintf(buf, "%s" EXTENSIONS_DIR ":" SYS_EXT_DIR EXTENSIONS_DIR, Arguments::get_java_home());
438 431 Arguments::set_ext_dirs(buf);
439 /* 432
440 * Get the user setting of LD_LIBRARY_PATH, and prepended it. It 433 // Endorsed standards default directory.
441 * should always exist (until the legacy problem cited above is 434 sprintf(buf, "%s" ENDORSED_DIR, Arguments::get_java_home());
442 * addressed). 435 Arguments::set_endorsed_dirs(buf);
443 */ 436
444 char *v = getenv("LD_LIBRARY_PATH"); 437 FREE_C_HEAP_ARRAY(char, buf, mtInternal);
445 if (v != NULL) { 438
446 char *t = ld_library_path; 439 #undef DEFAULT_LIBPATH
447 /* That's +1 for the colon and +1 for the trailing '\0' */ 440 #undef SYS_EXT_DIR
448 ld_library_path = (char *) malloc(strlen(v) + 1 + strlen(t) + 1);
449 sprintf(ld_library_path, "%s:%s", v, t);
450 }
451 Arguments::set_library_path(ld_library_path);
452 }
453
454 /*
455 * Extensions directories.
456 *
457 * Note that the space for the colon and the trailing null are provided
458 * by the nulls included by the sizeof operator (so actually one byte more
459 * than necessary is allocated).
460 */
461 {
462 char *buf = malloc(strlen(Arguments::get_java_home()) +
463 sizeof(EXTENSIONS_DIR) + sizeof(REG_DIR) + sizeof(EXTENSIONS_DIR));
464 sprintf(buf, "%s" EXTENSIONS_DIR ":" REG_DIR EXTENSIONS_DIR,
465 Arguments::get_java_home());
466 Arguments::set_ext_dirs(buf);
467 }
468
469 /* Endorsed standards default directory. */
470 {
471 char * buf;
472 buf = malloc(strlen(Arguments::get_java_home()) + sizeof(ENDORSED_DIR));
473 sprintf(buf, "%s" ENDORSED_DIR, Arguments::get_java_home());
474 Arguments::set_endorsed_dirs(buf);
475 }
476 }
477
478 #undef malloc
479 #undef getenv
480 #undef EXTENSIONS_DIR 441 #undef EXTENSIONS_DIR
481 #undef ENDORSED_DIR 442 #undef ENDORSED_DIR
482
483 // Done
484 return;
485 } 443 }
486 444
487 //////////////////////////////////////////////////////////////////////////////// 445 ////////////////////////////////////////////////////////////////////////////////
488 // breakpoint support 446 // breakpoint support
489 447