src/os/linux/vm/os_linux.cpp

Print this page
rev 6139 : 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 /*
 347  * See ld(1):
 348  *      The linker uses the following search paths to locate required
 349  *      shared libraries:
 350  *        1: ...
 351  *        ...
 352  *        7: The default directories, normally /lib and /usr/lib.
 353  */
 354 #if defined(AMD64) || defined(_LP64) && (defined(SPARC) || defined(PPC) || defined(S390))
 355 #define DEFAULT_LIBPATH "/usr/lib64:/lib64:/lib:/usr/lib"
 356 #else
 357 #define DEFAULT_LIBPATH "/lib:/usr/lib"
 358 #endif
 359 
 360 #define EXTENSIONS_DIR  "/lib/ext"
 361 #define ENDORSED_DIR    "/lib/endorsed"
 362 #define REG_DIR         "/usr/java/packages"
 363 

 364   /* sysclasspath, java_home, dll_dir */
 365   {


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



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






 392       return;
 393     }
 394   }
 395 
 396   /*
 397    * Where to look for native libraries
 398    *
 399    * Note: Due to a legacy implementation, most of the library path
 400    * is set in the launcher.  This was to accomodate linking restrictions
 401    * on legacy Linux implementations (which are no longer supported).
 402    * Eventually, all the library path setting will be done here.
 403    *
 404    * However, to prevent the proliferation of improperly built native
 405    * libraries, the new path component /usr/java/packages is added here.
 406    * Eventually, all the library path setting will be done here.
 407    */
 408   {
 409     char *ld_library_path;
 410 
 411     /*
 412      * Construct the invariant part of ld_library_path. Note that the
 413      * space for the colon and the trailing null are provided by the
 414      * nulls included by the sizeof operator (so actually we allocate
 415      * a byte more than necessary).
 416      */
 417     ld_library_path = (char *)NEW_C_HEAP_ARRAY(char, sizeof(REG_DIR) + sizeof("/lib/") +
 418                                                strlen(cpu_arch) + sizeof(DEFAULT_LIBPATH), mtInternal);
 419     sprintf(ld_library_path, REG_DIR "/lib/%s:" DEFAULT_LIBPATH, cpu_arch);
 420 
 421     /*
 422      * Get the user setting of LD_LIBRARY_PATH, and prepended it.  It
 423      * should always exist (until the legacy problem cited above is
 424      * addressed).
 425      */
 426     char *v = ::getenv("LD_LIBRARY_PATH");
 427     if (v != NULL) {
 428       char *t = ld_library_path;
 429       /* That's +1 for the colon and +1 for the trailing '\0' */
 430       ld_library_path = (char *)NEW_C_HEAP_ARRAY(char, strlen(v) + 1 + strlen(t) + 1, mtInternal);
 431       sprintf(ld_library_path, "%s:%s", v, t);
 432       FREE_C_HEAP_ARRAY(char, t, mtInternal);
 433     }
 434     Arguments::set_library_path(ld_library_path);
 435     FREE_C_HEAP_ARRAY(char, ld_library_path, mtInternal);
 436   }
 437 
 438   /*
 439    * Buffer that fits both sprintfs.

 440    * Note that the space for the colon and the trailing null are provided
 441    * by the nulls included by the sizeof operator (so actually one byte more
 442    * than necessary is allocated).
 443    */
 444   char buf[MAXPATHLEN + sizeof(EXTENSIONS_DIR) + sizeof(REG_DIR) + sizeof(EXTENSIONS_DIR) + sizeof(ENDORSED_DIR)];
 445 
 446   /* Extensions directories. */
 447   sprintf(buf, "%s" EXTENSIONS_DIR ":" REG_DIR EXTENSIONS_DIR, Arguments::get_java_home());

 448   Arguments::set_ext_dirs(buf);

 449 
 450   /* Endorsed standards default directory. */



 451   sprintf(buf, "%s" ENDORSED_DIR, Arguments::get_java_home());
 452   Arguments::set_endorsed_dirs(buf);


 453 


 454 #undef EXTENSIONS_DIR
 455 #undef ENDORSED_DIR



 456 }
 457 
 458 ////////////////////////////////////////////////////////////////////////////////
 459 // breakpoint support
 460 
 461 void os::breakpoint() {
 462   BREAKPOINT;
 463 }
 464 
 465 extern "C" void breakpoint() {
 466   // use debugger to set breakpoint here
 467 }
 468 
 469 ////////////////////////////////////////////////////////////////////////////////
 470 // signal support
 471 
 472 debug_only(static bool signal_sets_initialized = false);
 473 static sigset_t unblocked_sigs, vm_sigs, allowdebug_blocked_sigs;
 474 
 475 bool os::Linux::is_sig_ignored(int sig) {