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     MAX4((size_t)MAXPATHLEN,  // For dll_dir & friends.
 368          sizeof(SYS_EXT_DIR) + sizeof("/lib/") + strlen(cpu_arch) + sizeof(DEFAULT_LIBPATH), // invariant ld_library_path
 369          (size_t)MAXPATHLEN + sizeof(EXTENSIONS_DIR) + sizeof(SYS_EXT_DIR) + sizeof(EXTENSIONS_DIR), // extensions dir
 370          (size_t)MAXPATHLEN + sizeof(ENDORSED_DIR)); // endorsed dir
 371   char *buf = (char *)NEW_C_HEAP_ARRAY(char, bufsize, mtInternal);
 372 
 373   // sysclasspath, java_home, dll_dir
 374   {




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

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



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






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


 414   {
 415     char *ld_library_path_inv = buf;
 416 
 417     // Construct the invariant part of ld_library_path.
 418     sprintf(ld_library_path_inv, SYS_EXT_DIR "/lib/%s:" DEFAULT_LIBPATH, cpu_arch);







 419 
 420     // Get the user setting of LD_LIBRARY_PATH, and prepended it. It
 421     // should always exist (until the legacy problem cited above is
 422     // addressed).
 423     char *v = ::getenv("LD_LIBRARY_PATH");


 424     if (v != NULL) {
 425       // That's +1 for the colon and +1 for the trailing '\0'.
 426       char *ld_library_path = (char *)NEW_C_HEAP_ARRAY(char, strlen(v) + 1 + strlen(ld_library_path_inv) + 1, mtInternal);
 427       sprintf(ld_library_path, "%s:%s", v, ld_library_path_inv);


 428       Arguments::set_library_path(ld_library_path);
 429       FREE_C_HEAP_ARRAY(char, ld_library_path, mtInternal);
 430     } else {
 431       Arguments::set_library_path(ld_library_path_inv);
 432     }
 433   }
 434 
 435   // Extensions directories.
 436   sprintf(buf, "%s" EXTENSIONS_DIR ":" SYS_EXT_DIR EXTENSIONS_DIR, Arguments::get_java_home());










 437   Arguments::set_ext_dirs(buf);

 438 
 439   // Endorsed standards default directory.



 440   sprintf(buf, "%s" ENDORSED_DIR, Arguments::get_java_home());
 441   Arguments::set_endorsed_dirs(buf);


 442 
 443   FREE_C_HEAP_ARRAY(char, buf, mtInternal);
 444 
 445 #undef DEFAULT_LIBPATH
 446 #undef SYS_EXT_DIR
 447 #undef EXTENSIONS_DIR
 448 #undef ENDORSED_DIR



 449 }
 450 
 451 ////////////////////////////////////////////////////////////////////////////////
 452 // breakpoint support
 453 
 454 void os::breakpoint() {
 455   BREAKPOINT;
 456 }
 457 
 458 extern "C" void breakpoint() {
 459   // use debugger to set breakpoint here
 460 }
 461 
 462 ////////////////////////////////////////////////////////////////////////////////
 463 // signal support
 464 
 465 debug_only(static bool signal_sets_initialized = false);
 466 static sigset_t unblocked_sigs, vm_sigs, allowdebug_blocked_sigs;
 467 
 468 bool os::Linux::is_sig_ignored(int sig) {