hotspot/src/os/bsd/vm/perfMemory_bsd.cpp
changeset 27880 afb974a04396
parent 27471 6e56277909f1
child 27883 3179632c8f66
equal deleted inserted replaced
27879:419385282044 27880:afb974a04396
   125       if (result == OS_ERR) {
   125       if (result == OS_ERR) {
   126         warning("Could not close %s: %s\n", destfile, strerror(errno));
   126         warning("Could not close %s: %s\n", destfile, strerror(errno));
   127       }
   127       }
   128     }
   128     }
   129   }
   129   }
   130   FREE_C_HEAP_ARRAY(char, destfile, mtInternal);
   130   FREE_C_HEAP_ARRAY(char, destfile);
   131 }
   131 }
   132 
   132 
   133 
   133 
   134 // Shared Memory Implementation Details
   134 // Shared Memory Implementation Details
   135 
   135 
   277         warning("Could not determine user name: %s\n",
   277         warning("Could not determine user name: %s\n",
   278                 p->pw_name == NULL ? "pw_name = NULL" :
   278                 p->pw_name == NULL ? "pw_name = NULL" :
   279                                      "pw_name zero length");
   279                                      "pw_name zero length");
   280       }
   280       }
   281     }
   281     }
   282     FREE_C_HEAP_ARRAY(char, pwbuf, mtInternal);
   282     FREE_C_HEAP_ARRAY(char, pwbuf);
   283     return NULL;
   283     return NULL;
   284   }
   284   }
   285 
   285 
   286   char* user_name = NEW_C_HEAP_ARRAY(char, strlen(p->pw_name) + 1, mtInternal);
   286   char* user_name = NEW_C_HEAP_ARRAY(char, strlen(p->pw_name) + 1, mtInternal);
   287   strcpy(user_name, p->pw_name);
   287   strcpy(user_name, p->pw_name);
   288 
   288 
   289   FREE_C_HEAP_ARRAY(char, pwbuf, mtInternal);
   289   FREE_C_HEAP_ARRAY(char, pwbuf);
   290   return user_name;
   290   return user_name;
   291 }
   291 }
   292 
   292 
   293 // return the name of the user that owns the process identified by vmid.
   293 // return the name of the user that owns the process identified by vmid.
   294 //
   294 //
   345     strcat(usrdir_name, dentry->d_name);
   345     strcat(usrdir_name, dentry->d_name);
   346 
   346 
   347     DIR* subdirp = os::opendir(usrdir_name);
   347     DIR* subdirp = os::opendir(usrdir_name);
   348 
   348 
   349     if (subdirp == NULL) {
   349     if (subdirp == NULL) {
   350       FREE_C_HEAP_ARRAY(char, usrdir_name, mtInternal);
   350       FREE_C_HEAP_ARRAY(char, usrdir_name);
   351       continue;
   351       continue;
   352     }
   352     }
   353 
   353 
   354     // Since we don't create the backing store files in directories
   354     // Since we don't create the backing store files in directories
   355     // pointed to by symbolic links, we also don't follow them when
   355     // pointed to by symbolic links, we also don't follow them when
   356     // looking for the files. We check for a symbolic link after the
   356     // looking for the files. We check for a symbolic link after the
   357     // call to opendir in order to eliminate a small window where the
   357     // call to opendir in order to eliminate a small window where the
   358     // symlink can be exploited.
   358     // symlink can be exploited.
   359     //
   359     //
   360     if (!is_directory_secure(usrdir_name)) {
   360     if (!is_directory_secure(usrdir_name)) {
   361       FREE_C_HEAP_ARRAY(char, usrdir_name, mtInternal);
   361       FREE_C_HEAP_ARRAY(char, usrdir_name);
   362       os::closedir(subdirp);
   362       os::closedir(subdirp);
   363       continue;
   363       continue;
   364     }
   364     }
   365 
   365 
   366     struct dirent* udentry;
   366     struct dirent* udentry;
   380         strcat(filename, udentry->d_name);
   380         strcat(filename, udentry->d_name);
   381 
   381 
   382         // don't follow symbolic links for the file
   382         // don't follow symbolic links for the file
   383         RESTARTABLE(::lstat(filename, &statbuf), result);
   383         RESTARTABLE(::lstat(filename, &statbuf), result);
   384         if (result == OS_ERR) {
   384         if (result == OS_ERR) {
   385            FREE_C_HEAP_ARRAY(char, filename, mtInternal);
   385            FREE_C_HEAP_ARRAY(char, filename);
   386            continue;
   386            continue;
   387         }
   387         }
   388 
   388 
   389         // skip over files that are not regular files.
   389         // skip over files that are not regular files.
   390         if (!S_ISREG(statbuf.st_mode)) {
   390         if (!S_ISREG(statbuf.st_mode)) {
   391           FREE_C_HEAP_ARRAY(char, filename, mtInternal);
   391           FREE_C_HEAP_ARRAY(char, filename);
   392           continue;
   392           continue;
   393         }
   393         }
   394 
   394 
   395         // compare and save filename with latest creation time
   395         // compare and save filename with latest creation time
   396         if (statbuf.st_size > 0 && statbuf.st_ctime > oldest_ctime) {
   396         if (statbuf.st_size > 0 && statbuf.st_ctime > oldest_ctime) {
   397 
   397 
   398           if (statbuf.st_ctime > oldest_ctime) {
   398           if (statbuf.st_ctime > oldest_ctime) {
   399             char* user = strchr(dentry->d_name, '_') + 1;
   399             char* user = strchr(dentry->d_name, '_') + 1;
   400 
   400 
   401             if (oldest_user != NULL) FREE_C_HEAP_ARRAY(char, oldest_user, mtInternal);
   401             if (oldest_user != NULL) FREE_C_HEAP_ARRAY(char, oldest_user);
   402             oldest_user = NEW_C_HEAP_ARRAY(char, strlen(user)+1, mtInternal);
   402             oldest_user = NEW_C_HEAP_ARRAY(char, strlen(user)+1, mtInternal);
   403 
   403 
   404             strcpy(oldest_user, user);
   404             strcpy(oldest_user, user);
   405             oldest_ctime = statbuf.st_ctime;
   405             oldest_ctime = statbuf.st_ctime;
   406           }
   406           }
   407         }
   407         }
   408 
   408 
   409         FREE_C_HEAP_ARRAY(char, filename, mtInternal);
   409         FREE_C_HEAP_ARRAY(char, filename);
   410       }
   410       }
   411     }
   411     }
   412     os::closedir(subdirp);
   412     os::closedir(subdirp);
   413     FREE_C_HEAP_ARRAY(char, udbuf, mtInternal);
   413     FREE_C_HEAP_ARRAY(char, udbuf);
   414     FREE_C_HEAP_ARRAY(char, usrdir_name, mtInternal);
   414     FREE_C_HEAP_ARRAY(char, usrdir_name);
   415   }
   415   }
   416   os::closedir(tmpdirp);
   416   os::closedir(tmpdirp);
   417   FREE_C_HEAP_ARRAY(char, tdbuf, mtInternal);
   417   FREE_C_HEAP_ARRAY(char, tdbuf);
   418 
   418 
   419   return(oldest_user);
   419   return(oldest_user);
   420 }
   420 }
   421 
   421 
   422 // return the name of the user that owns the JVM indicated by the given vmid.
   422 // return the name of the user that owns the JVM indicated by the given vmid.
   479   strcat(path, "/");
   479   strcat(path, "/");
   480   strcat(path, filename);
   480   strcat(path, filename);
   481 
   481 
   482   remove_file(path);
   482   remove_file(path);
   483 
   483 
   484   FREE_C_HEAP_ARRAY(char, path, mtInternal);
   484   FREE_C_HEAP_ARRAY(char, path);
   485 }
   485 }
   486 
   486 
   487 
   487 
   488 // cleanup stale shared memory resources
   488 // cleanup stale shared memory resources
   489 //
   489 //
   556         remove_file(dirname, entry->d_name);
   556         remove_file(dirname, entry->d_name);
   557     }
   557     }
   558     errno = 0;
   558     errno = 0;
   559   }
   559   }
   560   os::closedir(dirp);
   560   os::closedir(dirp);
   561   FREE_C_HEAP_ARRAY(char, dbuf, mtInternal);
   561   FREE_C_HEAP_ARRAY(char, dbuf);
   562 }
   562 }
   563 
   563 
   564 // make the user specific temporary directory. Returns true if
   564 // make the user specific temporary directory. Returns true if
   565 // the directory exists and is secure upon return. Returns false
   565 // the directory exists and is secure upon return. Returns false
   566 // if the directory exists but is either a symlink, is otherwise
   566 // if the directory exists but is either a symlink, is otherwise
   723   assert(((size > 0) && (size % os::vm_page_size() == 0)),
   723   assert(((size > 0) && (size % os::vm_page_size() == 0)),
   724          "unexpected PerfMemory region size");
   724          "unexpected PerfMemory region size");
   725 
   725 
   726   fd = create_sharedmem_resources(dirname, filename, size);
   726   fd = create_sharedmem_resources(dirname, filename, size);
   727 
   727 
   728   FREE_C_HEAP_ARRAY(char, user_name, mtInternal);
   728   FREE_C_HEAP_ARRAY(char, user_name);
   729   FREE_C_HEAP_ARRAY(char, dirname, mtInternal);
   729   FREE_C_HEAP_ARRAY(char, dirname);
   730 
   730 
   731   if (fd == -1) {
   731   if (fd == -1) {
   732     FREE_C_HEAP_ARRAY(char, filename, mtInternal);
   732     FREE_C_HEAP_ARRAY(char, filename);
   733     return NULL;
   733     return NULL;
   734   }
   734   }
   735 
   735 
   736   mapAddress = (char*)::mmap((char*)0, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
   736   mapAddress = (char*)::mmap((char*)0, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
   737 
   737 
   741   if (mapAddress == MAP_FAILED) {
   741   if (mapAddress == MAP_FAILED) {
   742     if (PrintMiscellaneous && Verbose) {
   742     if (PrintMiscellaneous && Verbose) {
   743       warning("mmap failed -  %s\n", strerror(errno));
   743       warning("mmap failed -  %s\n", strerror(errno));
   744     }
   744     }
   745     remove_file(filename);
   745     remove_file(filename);
   746     FREE_C_HEAP_ARRAY(char, filename, mtInternal);
   746     FREE_C_HEAP_ARRAY(char, filename);
   747     return NULL;
   747     return NULL;
   748   }
   748   }
   749 
   749 
   750   // save the file name for use in delete_shared_memory()
   750   // save the file name for use in delete_shared_memory()
   751   backing_store_file_name = filename;
   751   backing_store_file_name = filename;
   870 
   870 
   871   // since we don't follow symbolic links when creating the backing
   871   // since we don't follow symbolic links when creating the backing
   872   // store file, we don't follow them when attaching either.
   872   // store file, we don't follow them when attaching either.
   873   //
   873   //
   874   if (!is_directory_secure(dirname)) {
   874   if (!is_directory_secure(dirname)) {
   875     FREE_C_HEAP_ARRAY(char, dirname, mtInternal);
   875     FREE_C_HEAP_ARRAY(char, dirname);
   876     if (luser != user) {
   876     if (luser != user) {
   877       FREE_C_HEAP_ARRAY(char, luser, mtInternal);
   877       FREE_C_HEAP_ARRAY(char, luser);
   878     }
   878     }
   879     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
   879     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
   880               "Process not found");
   880               "Process not found");
   881   }
   881   }
   882 
   882 
   888   // would otherwise occur.
   888   // would otherwise occur.
   889   char* rfilename = NEW_RESOURCE_ARRAY(char, strlen(filename) + 1);
   889   char* rfilename = NEW_RESOURCE_ARRAY(char, strlen(filename) + 1);
   890   strcpy(rfilename, filename);
   890   strcpy(rfilename, filename);
   891 
   891 
   892   // free the c heap resources that are no longer needed
   892   // free the c heap resources that are no longer needed
   893   if (luser != user) FREE_C_HEAP_ARRAY(char, luser, mtInternal);
   893   if (luser != user) FREE_C_HEAP_ARRAY(char, luser);
   894   FREE_C_HEAP_ARRAY(char, dirname, mtInternal);
   894   FREE_C_HEAP_ARRAY(char, dirname);
   895   FREE_C_HEAP_ARRAY(char, filename, mtInternal);
   895   FREE_C_HEAP_ARRAY(char, filename);
   896 
   896 
   897   // open the shared memory file for the give vmid
   897   // open the shared memory file for the give vmid
   898   fd = open_sharedmem_file(rfilename, file_flags, CHECK);
   898   fd = open_sharedmem_file(rfilename, file_flags, CHECK);
   899   assert(fd != OS_ERR, "unexpected value");
   899   assert(fd != OS_ERR, "unexpected value");
   900 
   900