src/os/linux/vm/os_linux.cpp

Print this page
rev 6140 : 8038201: Clean up misleading usage of malloc() in init_system_properties_values()
Summary: Add missing freeing of memory or use local variable. Also add a 'const' to avoid gcc write-string warnings on ppc.


 300                      "environment on Linux when /proc filesystem is not mounted.";
 301 
 302 void os::Linux::initialize_system_info() {
 303   set_processor_count(sysconf(_SC_NPROCESSORS_CONF));
 304   if (processor_count() == 1) {
 305     pid_t pid = os::Linux::gettid();
 306     char fname[32];
 307     jio_snprintf(fname, sizeof(fname), "/proc/%d", pid);
 308     FILE *fp = fopen(fname, "r");
 309     if (fp == NULL) {
 310       unsafe_chroot_detected = true;
 311     } else {
 312       fclose(fp);
 313     }
 314   }
 315   _physical_memory = (julong)sysconf(_SC_PHYS_PAGES) * (julong)sysconf(_SC_PAGESIZE);
 316   assert(processor_count() > 0, "linux error");
 317 }
 318 
 319 void os::init_system_properties_values() {
 320 //  char arch[12];
 321 //  sysinfo(SI_ARCHITECTURE, arch, sizeof(arch));
 322 
 323   // The next steps are taken in the product version:
 324   //
 325   // Obtain the JAVA_HOME value from the location of libjvm.so.
 326   // This library should be located at:
 327   // <JAVA_HOME>/jre/lib/<arch>/{client|server}/libjvm.so.
 328   //
 329   // If "/jre/lib/" appears at the right place in the path, then we
 330   // assume libjvm.so is installed in a JDK and we use this path.
 331   //
 332   // Otherwise exit with message: "Could not create the Java virtual machine."
 333   //
 334   // The following extra steps are taken in the debugging version:
 335   //
 336   // If "/jre/lib/" does NOT appear at the right place in the path
 337   // instead of exit check for $JAVA_HOME environment variable.
 338   //
 339   // If it is defined and we are able to locate $JAVA_HOME/jre/lib/<arch>,
 340   // then we append a fake suffix "hotspot/libjvm.so" to this path so
 341   // it looks like libjvm.so is installed there
 342   // <JAVA_HOME>/jre/lib/<arch>/hotspot/libjvm.so.
 343   //
 344   // Otherwise exit.
 345   //
 346   // Important note: if the location of libjvm.so changes this
 347   // code needs to be changed accordingly.
 348 
 349   // The next few definitions allow the code to be verbatim:
 350 #define malloc(n) (char*)NEW_C_HEAP_ARRAY(char, (n), mtInternal)
 351 #define getenv(n) ::getenv(n)
 352 
 353 /*
 354  * See ld(1):
 355  *      The linker uses the following search paths to locate required
 356  *      shared libraries:
 357  *        1: ...
 358  *        ...
 359  *        7: The default directories, normally /lib and /usr/lib.
 360  */
 361 #if defined(AMD64) || defined(_LP64) && (defined(SPARC) || defined(PPC) || defined(S390))
 362 #define DEFAULT_LIBPATH "/usr/lib64:/lib64:/lib:/usr/lib"
 363 #else
 364 #define DEFAULT_LIBPATH "/lib:/usr/lib"
 365 #endif
 366 


 367 #define EXTENSIONS_DIR  "/lib/ext"
 368 #define ENDORSED_DIR    "/lib/endorsed"
 369 #define REG_DIR         "/usr/java/packages"
 370 










 371   {
 372     /* sysclasspath, java_home, dll_dir */
 373     {
 374         char *home_path;
 375         char *dll_path;
 376         char *pslash;
 377         char buf[MAXPATHLEN];
 378         os::jvm_path(buf, sizeof(buf));
 379 
 380         // Found the full path to libjvm.so.
 381         // Now cut the path to <java_home>/jre if we can.
 382         *(strrchr(buf, '/')) = '\0';  /* get rid of /libjvm.so */
 383         pslash = strrchr(buf, '/');
 384         if (pslash != NULL)
 385             *pslash = '\0';           /* get rid of /{client|server|hotspot} */
 386         dll_path = malloc(strlen(buf) + 1);
 387         if (dll_path == NULL)
 388             return;
 389         strcpy(dll_path, buf);
 390         Arguments::set_dll_dir(dll_path);
 391 
 392         if (pslash != NULL) {
 393             pslash = strrchr(buf, '/');
 394             if (pslash != NULL) {
 395                 *pslash = '\0';       /* get rid of /<arch> */
 396                 pslash = strrchr(buf, '/');
 397                 if (pslash != NULL)
 398                     *pslash = '\0';   /* get rid of /lib */
 399             }
 400         }
 401 
 402         home_path = malloc(strlen(buf) + 1);
 403         if (home_path == NULL)
 404             return;
 405         strcpy(home_path, buf);
 406         Arguments::set_java_home(home_path);
 407 
 408         if (!set_boot_path('/', ':'))
 409             return;
 410     }
 411 
 412     /*
 413      * Where to look for native libraries
 414      *
 415      * Note: Due to a legacy implementation, most of the library path
 416      * is set in the launcher.  This was to accomodate linking restrictions
 417      * on legacy Linux implementations (which are no longer supported).
 418      * Eventually, all the library path setting will be done here.
 419      *
 420      * However, to prevent the proliferation of improperly built native
 421      * libraries, the new path component /usr/java/packages is added here.
 422      * Eventually, all the library path setting will be done here.
 423      */
 424     {
 425         char *ld_library_path;
 426 
 427         /*
 428          * Construct the invariant part of ld_library_path. Note that the
 429          * space for the colon and the trailing null are provided by the
 430          * nulls included by the sizeof operator (so actually we allocate
 431          * a byte more than necessary).
 432          */
 433         ld_library_path = (char *) malloc(sizeof(REG_DIR) + sizeof("/lib/") +
 434             strlen(cpu_arch) + sizeof(DEFAULT_LIBPATH));
 435         sprintf(ld_library_path, REG_DIR "/lib/%s:" DEFAULT_LIBPATH, cpu_arch);
 436 
 437         /*
 438          * Get the user setting of LD_LIBRARY_PATH, and prepended it.  It
 439          * should always exist (until the legacy problem cited above is
 440          * addressed).
 441          */
 442         char *v = getenv("LD_LIBRARY_PATH");
 443         if (v != NULL) {
 444             char *t = ld_library_path;
 445             /* That's +1 for the colon and +1 for the trailing '\0' */
 446             ld_library_path = (char *) malloc(strlen(v) + 1 + strlen(t) + 1);
 447             sprintf(ld_library_path, "%s:%s", v, t);
 448         }
 449         Arguments::set_library_path(ld_library_path);

 450     }
 451 
 452     /*
 453      * Extensions directories.
 454      *
 455      * Note that the space for the colon and the trailing null are provided
 456      * by the nulls included by the sizeof operator (so actually one byte more
 457      * than necessary is allocated).
 458      */
 459     {
 460         char *buf = malloc(strlen(Arguments::get_java_home()) +
 461             sizeof(EXTENSIONS_DIR) + sizeof(REG_DIR) + sizeof(EXTENSIONS_DIR));
 462         sprintf(buf, "%s" EXTENSIONS_DIR ":" REG_DIR EXTENSIONS_DIR,
 463             Arguments::get_java_home());
 464         Arguments::set_ext_dirs(buf);
 465     }
 466 
 467     /* Endorsed standards default directory. */
 468     {
 469         char * buf;
 470         buf = malloc(strlen(Arguments::get_java_home()) + sizeof(ENDORSED_DIR));
 471         sprintf(buf, "%s" ENDORSED_DIR, Arguments::get_java_home());
 472         Arguments::set_endorsed_dirs(buf);
 473     }
 474   }
 475 
 476 #undef malloc
 477 #undef getenv


 478 #undef EXTENSIONS_DIR
 479 #undef ENDORSED_DIR
 480 
 481   // Done
 482   return;
 483 }
 484 
 485 ////////////////////////////////////////////////////////////////////////////////
 486 // breakpoint support
 487 
 488 void os::breakpoint() {
 489   BREAKPOINT;
 490 }
 491 
 492 extern "C" void breakpoint() {
 493   // use debugger to set breakpoint here
 494 }
 495 
 496 ////////////////////////////////////////////////////////////////////////////////
 497 // signal support
 498 
 499 debug_only(static bool signal_sets_initialized = false);
 500 static sigset_t unblocked_sigs, vm_sigs, allowdebug_blocked_sigs;
 501 
 502 bool os::Linux::is_sig_ignored(int sig) {




 300                      "environment on Linux when /proc filesystem is not mounted.";
 301 
 302 void os::Linux::initialize_system_info() {
 303   set_processor_count(sysconf(_SC_NPROCESSORS_CONF));
 304   if (processor_count() == 1) {
 305     pid_t pid = os::Linux::gettid();
 306     char fname[32];
 307     jio_snprintf(fname, sizeof(fname), "/proc/%d", pid);
 308     FILE *fp = fopen(fname, "r");
 309     if (fp == NULL) {
 310       unsafe_chroot_detected = true;
 311     } else {
 312       fclose(fp);
 313     }
 314   }
 315   _physical_memory = (julong)sysconf(_SC_PHYS_PAGES) * (julong)sysconf(_SC_PAGESIZE);
 316   assert(processor_count() > 0, "linux error");
 317 }
 318 
 319 void os::init_system_properties_values() {



 320   // The next steps are taken in the product version:
 321   //
 322   // Obtain the JAVA_HOME value from the location of libjvm.so.
 323   // This library should be located at:
 324   // <JAVA_HOME>/jre/lib/<arch>/{client|server}/libjvm.so.
 325   //
 326   // If "/jre/lib/" appears at the right place in the path, then we
 327   // assume libjvm.so is installed in a JDK and we use this path.
 328   //
 329   // Otherwise exit with message: "Could not create the Java virtual machine."
 330   //
 331   // The following extra steps are taken in the debugging version:
 332   //
 333   // If "/jre/lib/" does NOT appear at the right place in the path
 334   // instead of exit check for $JAVA_HOME environment variable.
 335   //
 336   // If it is defined and we are able to locate $JAVA_HOME/jre/lib/<arch>,
 337   // then we append a fake suffix "hotspot/libjvm.so" to this path so
 338   // it looks like libjvm.so is installed there
 339   // <JAVA_HOME>/jre/lib/<arch>/hotspot/libjvm.so.
 340   //
 341   // Otherwise exit.
 342   //
 343   // Important note: if the location of libjvm.so changes this
 344   // code needs to be changed accordingly.
 345 
 346 // See ld(1):
 347 //      The linker uses the following search paths to locate required
 348 //      shared libraries:
 349 //        1: ...
 350 //        ...
 351 //        7: The default directories, normally /lib and /usr/lib.






 352 #if defined(AMD64) || defined(_LP64) && (defined(SPARC) || defined(PPC) || defined(S390))
 353 #define DEFAULT_LIBPATH "/usr/lib64:/lib64:/lib:/usr/lib"
 354 #else
 355 #define DEFAULT_LIBPATH "/lib:/usr/lib"
 356 #endif
 357 
 358 // Base path of extensions installed on the system.
 359 #define SYS_EXT_DIR     "/usr/java/packages"
 360 #define EXTENSIONS_DIR  "/lib/ext"
 361 #define ENDORSED_DIR    "/lib/endorsed"

 362 
 363   // Buffer that fits several sprintfs.
 364   // Note that the space for the colon and the trailing null are provided
 365   // by the nulls included by the sizeof operator.
 366   const size_t bufsize =
 367     MAX3((size_t)MAXPATHLEN,  // For dll_dir & friends.
 368          (size_t)MAXPATHLEN + sizeof(EXTENSIONS_DIR) + sizeof(SYS_EXT_DIR) + sizeof(EXTENSIONS_DIR), // extensions dir
 369          (size_t)MAXPATHLEN + sizeof(ENDORSED_DIR)); // endorsed dir
 370   char *buf = (char *)NEW_C_HEAP_ARRAY(char, bufsize, mtInternal);
 371 
 372   // sysclasspath, java_home, dll_dir
 373   {




 374     char *pslash;
 375     os::jvm_path(buf, bufsize);

 376 
 377     // Found the full path to libjvm.so.
 378     // Now cut the path to <java_home>/jre if we can.
 379     *(strrchr(buf, '/')) = '\0'; // Get rid of /libjvm.so.
 380     pslash = strrchr(buf, '/');
 381     if (pslash != NULL) {
 382       *pslash = '\0';            // Get rid of /{client|server|hotspot}.
 383     }
 384     Arguments::set_dll_dir(buf);



 385 
 386     if (pslash != NULL) {
 387       pslash = strrchr(buf, '/');
 388       if (pslash != NULL) {
 389         *pslash = '\0';          // Get rid of /<arch>.
 390         pslash = strrchr(buf, '/');
 391         if (pslash != NULL) {
 392           *pslash = '\0';        // Get rid of /lib.
 393         }
 394       }
 395     }
 396     Arguments::set_java_home(buf);
 397     set_boot_path('/', ':');






 398   }
 399 
 400   // Where to look for native libraries.
 401   //
 402   // Note: Due to a legacy implementation, most of the library path
 403   // is set in the launcher. This was to accomodate linking restrictions
 404   // on legacy Linux implementations (which are no longer supported).
 405   // Eventually, all the library path setting will be done here.
 406   //
 407   // However, to prevent the proliferation of improperly built native
 408   // libraries, the new path component /usr/java/packages is added here.
 409   // Eventually, all the library path setting will be done here.


 410   {
 411     // Get the user setting of LD_LIBRARY_PATH, and prepended it. It
 412     // should always exist (until the legacy problem cited above is
 413     // addressed).
 414     char *v = ::getenv("LD_LIBRARY_PATH");
 415     const char *v_colon = ":";
 416     if (v == NULL) { v = ""; v_colon = ""; }
 417     // That's +1 for the colon and +1 for the trailing '\0'.
 418     char *ld_library_path = (char *)NEW_C_HEAP_ARRAY(char,
 419                                                      strlen(v) + 1 +
 420                                                      sizeof(SYS_EXT_DIR) + sizeof("/lib/") + strlen(cpu_arch) + sizeof(DEFAULT_LIBPATH) + 1,
 421                                                      mtInternal);
 422     sprintf(ld_library_path, "%s%s" SYS_EXT_DIR "/lib/%s:" DEFAULT_LIBPATH, v, v_colon, cpu_arch);












 423     Arguments::set_library_path(ld_library_path);
 424     FREE_C_HEAP_ARRAY(char, ld_library_path, mtInternal);
 425   }
 426 
 427   // Extensions directories.
 428   sprintf(buf, "%s" EXTENSIONS_DIR ":" SYS_EXT_DIR EXTENSIONS_DIR, Arguments::get_java_home());










 429   Arguments::set_ext_dirs(buf);

 430 
 431   // Endorsed standards default directory.



 432   sprintf(buf, "%s" ENDORSED_DIR, Arguments::get_java_home());
 433   Arguments::set_endorsed_dirs(buf);


 434 
 435   FREE_C_HEAP_ARRAY(char, buf, mtInternal);
 436 
 437 #undef DEFAULT_LIBPATH
 438 #undef SYS_EXT_DIR
 439 #undef EXTENSIONS_DIR
 440 #undef ENDORSED_DIR



 441 }
 442 
 443 ////////////////////////////////////////////////////////////////////////////////
 444 // breakpoint support
 445 
 446 void os::breakpoint() {
 447   BREAKPOINT;
 448 }
 449 
 450 extern "C" void breakpoint() {
 451   // use debugger to set breakpoint here
 452 }
 453 
 454 ////////////////////////////////////////////////////////////////////////////////
 455 // signal support
 456 
 457 debug_only(static bool signal_sets_initialized = false);
 458 static sigset_t unblocked_sigs, vm_sigs, allowdebug_blocked_sigs;
 459 
 460 bool os::Linux::is_sig_ignored(int sig) {