hotspot/src/os/aix/vm/perfMemory_aix.cpp
changeset 27880 afb974a04396
parent 27471 6e56277909f1
child 27883 3179632c8f66
equal deleted inserted replaced
27879:419385282044 27880:afb974a04396
     1 /*
     1 /*
     2  * Copyright (c) 2001, 2013, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2001, 2014, Oracle and/or its affiliates. All rights reserved.
     3  * Copyright 2012, 2013 SAP AG. All rights reserved.
     3  * Copyright 2012, 2013 SAP AG. All rights reserved.
     4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     5  *
     5  *
     6  * This code is free software; you can redistribute it and/or modify it
     6  * This code is free software; you can redistribute it and/or modify it
     7  * under the terms of the GNU General Public License version 2 only, as
     7  * under the terms of the GNU General Public License version 2 only, as
   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
   701   assert(((size > 0) && (size % os::vm_page_size() == 0)),
   701   assert(((size > 0) && (size % os::vm_page_size() == 0)),
   702          "unexpected PerfMemory region size");
   702          "unexpected PerfMemory region size");
   703 
   703 
   704   fd = create_sharedmem_resources(dirname, filename, size);
   704   fd = create_sharedmem_resources(dirname, filename, size);
   705 
   705 
   706   FREE_C_HEAP_ARRAY(char, user_name, mtInternal);
   706   FREE_C_HEAP_ARRAY(char, user_name);
   707   FREE_C_HEAP_ARRAY(char, dirname, mtInternal);
   707   FREE_C_HEAP_ARRAY(char, dirname);
   708 
   708 
   709   if (fd == -1) {
   709   if (fd == -1) {
   710     FREE_C_HEAP_ARRAY(char, filename, mtInternal);
   710     FREE_C_HEAP_ARRAY(char, filename);
   711     return NULL;
   711     return NULL;
   712   }
   712   }
   713 
   713 
   714   mapAddress = (char*)::mmap((char*)0, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
   714   mapAddress = (char*)::mmap((char*)0, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
   715 
   715 
   721   if (mapAddress == MAP_FAILED) {
   721   if (mapAddress == MAP_FAILED) {
   722     if (PrintMiscellaneous && Verbose) {
   722     if (PrintMiscellaneous && Verbose) {
   723       warning("mmap failed -  %s\n", strerror(errno));
   723       warning("mmap failed -  %s\n", strerror(errno));
   724     }
   724     }
   725     remove_file(filename);
   725     remove_file(filename);
   726     FREE_C_HEAP_ARRAY(char, filename, mtInternal);
   726     FREE_C_HEAP_ARRAY(char, filename);
   727     return NULL;
   727     return NULL;
   728   }
   728   }
   729 
   729 
   730   // save the file name for use in delete_shared_memory()
   730   // save the file name for use in delete_shared_memory()
   731   backing_store_file_name = filename;
   731   backing_store_file_name = filename;
   767 
   767 
   768   if (backing_store_file_name != NULL) {
   768   if (backing_store_file_name != NULL) {
   769     remove_file(backing_store_file_name);
   769     remove_file(backing_store_file_name);
   770     // Don't.. Free heap memory could deadlock os::abort() if it is called
   770     // Don't.. Free heap memory could deadlock os::abort() if it is called
   771     // from signal handler. OS will reclaim the heap memory.
   771     // from signal handler. OS will reclaim the heap memory.
   772     // FREE_C_HEAP_ARRAY(char, backing_store_file_name, mtInternal);
   772     // FREE_C_HEAP_ARRAY(char, backing_store_file_name);
   773     backing_store_file_name = NULL;
   773     backing_store_file_name = NULL;
   774   }
   774   }
   775 }
   775 }
   776 
   776 
   777 // return the size of the file for the given file descriptor
   777 // return the size of the file for the given file descriptor
   851 
   851 
   852   // since we don't follow symbolic links when creating the backing
   852   // since we don't follow symbolic links when creating the backing
   853   // store file, we don't follow them when attaching either.
   853   // store file, we don't follow them when attaching either.
   854   //
   854   //
   855   if (!is_directory_secure(dirname)) {
   855   if (!is_directory_secure(dirname)) {
   856     FREE_C_HEAP_ARRAY(char, dirname, mtInternal);
   856     FREE_C_HEAP_ARRAY(char, dirname);
   857     if (luser != user) {
   857     if (luser != user) {
   858       FREE_C_HEAP_ARRAY(char, luser, mtInternal);
   858       FREE_C_HEAP_ARRAY(char, luser);
   859     }
   859     }
   860     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
   860     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
   861               "Process not found");
   861               "Process not found");
   862   }
   862   }
   863 
   863 
   869   // would otherwise occur.
   869   // would otherwise occur.
   870   char* rfilename = NEW_RESOURCE_ARRAY(char, strlen(filename) + 1);
   870   char* rfilename = NEW_RESOURCE_ARRAY(char, strlen(filename) + 1);
   871   strcpy(rfilename, filename);
   871   strcpy(rfilename, filename);
   872 
   872 
   873   // free the c heap resources that are no longer needed
   873   // free the c heap resources that are no longer needed
   874   if (luser != user) FREE_C_HEAP_ARRAY(char, luser, mtInternal);
   874   if (luser != user) FREE_C_HEAP_ARRAY(char, luser);
   875   FREE_C_HEAP_ARRAY(char, dirname, mtInternal);
   875   FREE_C_HEAP_ARRAY(char, dirname);
   876   FREE_C_HEAP_ARRAY(char, filename, mtInternal);
   876   FREE_C_HEAP_ARRAY(char, filename);
   877 
   877 
   878   // open the shared memory file for the give vmid
   878   // open the shared memory file for the give vmid
   879   fd = open_sharedmem_file(rfilename, file_flags, CHECK);
   879   fd = open_sharedmem_file(rfilename, file_flags, CHECK);
   880   assert(fd != OS_ERR, "unexpected value");
   880   assert(fd != OS_ERR, "unexpected value");
   881 
   881