src/os/linux/vm/os_linux.cpp

Print this page
rev 6136 : 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 #define EXTENSIONS_DIR  "/lib/ext"
 359 #define ENDORSED_DIR    "/lib/endorsed"
 360 #define REG_DIR         "/usr/java/packages"
 361 
 362   // sysclasspath, java_home, dll_dir
 363   {




 364     char *pslash;
 365     char buf[MAXPATHLEN];
 366     os::jvm_path(buf, sizeof(buf));
 367 
 368     // Found the full path to libjvm.so.
 369     // Now cut the path to <java_home>/jre if we can.
 370     *(strrchr(buf, '/')) = '\0'; // Get rid of /libjvm.so.
 371     pslash = strrchr(buf, '/');
 372     if (pslash != NULL) {
 373       *pslash = '\0';            // Get rid of /{client|server|hotspot}.
 374     }
 375     Arguments::set_dll_dir(buf);



 376 
 377     if (pslash != NULL) {
 378       pslash = strrchr(buf, '/');
 379       if (pslash != NULL) {
 380         *pslash = '\0';          // Get rid of /<arch>.
 381         pslash = strrchr(buf, '/');
 382         if (pslash != NULL) {
 383           *pslash = '\0';        // Get rid of /lib.
 384         }
 385       }
 386     }
 387     Arguments::set_java_home(buf);
 388 
 389     if (!set_boot_path('/', ':')) {






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


 404   {
 405     char *ld_library_path;
 406 
 407     // Construct the invariant part of ld_library_path. Note that the
 408     // space for the colon and the trailing null are provided by the
 409     // nulls included by the sizeof operator (so actually we allocate
 410     // a byte more than necessary).
 411     ld_library_path = (char *)NEW_C_HEAP_ARRAY(char, sizeof(REG_DIR) + sizeof("/lib/") +
 412                                                strlen(cpu_arch) + sizeof(DEFAULT_LIBPATH), mtInternal);


 413     sprintf(ld_library_path, REG_DIR "/lib/%s:" DEFAULT_LIBPATH, cpu_arch);
 414 
 415     // Get the user setting of LD_LIBRARY_PATH, and prepended it. It
 416     // should always exist (until the legacy problem cited above is
 417     // addressed).
 418     char *v = ::getenv("LD_LIBRARY_PATH");


 419     if (v != NULL) {
 420       char *t = ld_library_path;
 421       // That's +1 for the colon and +1 for the trailing '\0'.
 422       ld_library_path = (char *)NEW_C_HEAP_ARRAY(char, strlen(v) + 1 + strlen(t) + 1, mtInternal);
 423       sprintf(ld_library_path, "%s:%s", v, t);
 424       FREE_C_HEAP_ARRAY(char, t, mtInternal);
 425     }
 426     Arguments::set_library_path(ld_library_path);
 427     FREE_C_HEAP_ARRAY(char, ld_library_path, mtInternal);
 428   }
 429 
 430   // Buffer that fits both sprintfs.
 431   // Note that the space for the colon and the trailing null are provided
 432   // by the nulls included by the sizeof operator (so actually one byte more
 433   // than necessary is allocated).
 434   char buf[MAXPATHLEN + sizeof(EXTENSIONS_DIR) + sizeof(REG_DIR) + sizeof(EXTENSIONS_DIR) + sizeof(ENDORSED_DIR)];
 435 
 436   // Extensions directories.
 437   sprintf(buf, "%s" EXTENSIONS_DIR ":" REG_DIR EXTENSIONS_DIR, Arguments::get_java_home());




 438   Arguments::set_ext_dirs(buf);

 439 
 440   // Endorsed standards default directory.



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


 443 
 444 #undef DEFAULT_LIBPATH

 445 #undef EXTENSIONS_DIR
 446 #undef ENDORSED_DIR
 447 #undef REG_DIR


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