< prev index next >

src/os/bsd/vm/perfMemory_bsd.cpp

Print this page




 516   // directory search
 517   char* oldest_user = NULL;
 518   time_t oldest_ctime = 0;
 519 
 520   const char* tmpdirname = os::get_temp_directory();
 521 
 522   // open the temp directory
 523   DIR* tmpdirp = os::opendir(tmpdirname);
 524 
 525   if (tmpdirp == NULL) {
 526     // Cannot open the directory to get the user name, return.
 527     return NULL;
 528   }
 529 
 530   // for each entry in the directory that matches the pattern hsperfdata_*,
 531   // open the directory and check if the file for the given vmid exists.
 532   // The file with the expected name and the latest creation date is used
 533   // to determine the user name for the process id.
 534   //
 535   struct dirent* dentry;
 536   char* tdbuf = NEW_C_HEAP_ARRAY(char, os::readdir_buf_size(tmpdirname), mtInternal);
 537   errno = 0;
 538   while ((dentry = os::readdir(tmpdirp, (struct dirent *)tdbuf)) != NULL) {
 539 
 540     // check if the directory entry is a hsperfdata file
 541     if (strncmp(dentry->d_name, PERFDATA_NAME, strlen(PERFDATA_NAME)) != 0) {
 542       continue;
 543     }
 544 
 545     char* usrdir_name = NEW_C_HEAP_ARRAY(char,
 546                  strlen(tmpdirname) + strlen(dentry->d_name) + 2, mtInternal);
 547     strcpy(usrdir_name, tmpdirname);
 548     strcat(usrdir_name, "/");
 549     strcat(usrdir_name, dentry->d_name);
 550 
 551     // open the user directory
 552     DIR* subdirp = open_directory_secure(usrdir_name);
 553 
 554     if (subdirp == NULL) {
 555       FREE_C_HEAP_ARRAY(char, usrdir_name, mtInternal);
 556       continue;
 557     }
 558 
 559     struct dirent* udentry;
 560     char* udbuf = NEW_C_HEAP_ARRAY(char, os::readdir_buf_size(usrdir_name), mtInternal);
 561     errno = 0;
 562     while ((udentry = os::readdir(subdirp, (struct dirent *)udbuf)) != NULL) {
 563 
 564       if (filename_to_pid(udentry->d_name) == vmid) {
 565         struct stat statbuf;
 566         int result;
 567 
 568         char* filename = NEW_C_HEAP_ARRAY(char,
 569                  strlen(usrdir_name) + strlen(udentry->d_name) + 2, mtInternal);
 570 
 571         strcpy(filename, usrdir_name);
 572         strcat(filename, "/");
 573         strcat(filename, udentry->d_name);
 574 
 575         // don't follow symbolic links for the file
 576         RESTARTABLE(::lstat(filename, &statbuf), result);
 577         if (result == OS_ERR) {
 578            FREE_C_HEAP_ARRAY(char, filename, mtInternal);
 579            continue;
 580         }
 581 
 582         // skip over files that are not regular files.


 586         }
 587 
 588         // compare and save filename with latest creation time
 589         if (statbuf.st_size > 0 && statbuf.st_ctime > oldest_ctime) {
 590 
 591           if (statbuf.st_ctime > oldest_ctime) {
 592             char* user = strchr(dentry->d_name, '_') + 1;
 593 
 594             if (oldest_user != NULL) FREE_C_HEAP_ARRAY(char, oldest_user, mtInternal);
 595             oldest_user = NEW_C_HEAP_ARRAY(char, strlen(user)+1, mtInternal);
 596 
 597             strcpy(oldest_user, user);
 598             oldest_ctime = statbuf.st_ctime;
 599           }
 600         }
 601 
 602         FREE_C_HEAP_ARRAY(char, filename, mtInternal);
 603       }
 604     }
 605     os::closedir(subdirp);
 606     FREE_C_HEAP_ARRAY(char, udbuf, mtInternal);
 607     FREE_C_HEAP_ARRAY(char, usrdir_name, mtInternal);
 608   }
 609   os::closedir(tmpdirp);
 610   FREE_C_HEAP_ARRAY(char, tdbuf, mtInternal);
 611 
 612   return(oldest_user);
 613 }
 614 
 615 // return the name of the user that owns the JVM indicated by the given vmid.
 616 //
 617 static char* get_user_name(int vmid, TRAPS) {
 618   return get_user_name_slow(vmid, CHECK_NULL);
 619 }
 620 
 621 // return the file name of the backing store file for the named
 622 // shared memory region for the given user name and vmid.
 623 //
 624 // the caller is expected to free the allocated memory.
 625 //
 626 static char* get_sharedmem_filename(const char* dirname, int vmid) {
 627 
 628   // add 2 for the file separator and a null terminator.
 629   size_t nbytes = strlen(dirname) + UINT_CHARS + 2;
 630 


 669 //
 670 static void cleanup_sharedmem_resources(const char* dirname) {
 671 
 672   int saved_cwd_fd;
 673   // open the directory and set the current working directory to it
 674   DIR* dirp = open_directory_secure_cwd(dirname, &saved_cwd_fd);
 675   if (dirp == NULL) {
 676     // directory doesn't exist or is insecure, so there is nothing to cleanup
 677     return;
 678   }
 679 
 680   // for each entry in the directory that matches the expected file
 681   // name pattern, determine if the file resources are stale and if
 682   // so, remove the file resources. Note, instrumented HotSpot processes
 683   // for this user may start and/or terminate during this search and
 684   // remove or create new files in this directory. The behavior of this
 685   // loop under these conditions is dependent upon the implementation of
 686   // opendir/readdir.
 687   //
 688   struct dirent* entry;
 689   char* dbuf = NEW_C_HEAP_ARRAY(char, os::readdir_buf_size(dirname), mtInternal);
 690 
 691   errno = 0;
 692   while ((entry = os::readdir(dirp, (struct dirent *)dbuf)) != NULL) {
 693 
 694     pid_t pid = filename_to_pid(entry->d_name);
 695 
 696     if (pid == 0) {
 697 
 698       if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) {
 699 
 700         // attempt to remove all unexpected files, except "." and ".."
 701         unlink(entry->d_name);
 702       }
 703 
 704       errno = 0;
 705       continue;
 706     }
 707 
 708     // we now have a file name that converts to a valid integer
 709     // that could represent a process id . if this process id
 710     // matches the current process id or the process is not running,
 711     // then remove the stale file resources.
 712     //
 713     // process liveness is detected by sending signal number 0 to
 714     // the process id (see kill(2)). if kill determines that the
 715     // process does not exist, then the file resources are removed.
 716     // if kill determines that that we don't have permission to
 717     // signal the process, then the file resources are assumed to
 718     // be stale and are removed because the resources for such a
 719     // process should be in a different user specific directory.
 720     //
 721     if ((pid == os::current_process_id()) ||
 722         (kill(pid, 0) == OS_ERR && (errno == ESRCH || errno == EPERM))) {
 723 
 724         unlink(entry->d_name);
 725     }
 726     errno = 0;
 727   }
 728 
 729   // close the directory and reset the current working directory
 730   close_directory_secure_cwd(dirp, saved_cwd_fd);
 731 
 732   FREE_C_HEAP_ARRAY(char, dbuf, mtInternal);
 733 }
 734 
 735 // make the user specific temporary directory. Returns true if
 736 // the directory exists and is secure upon return. Returns false
 737 // if the directory exists but is either a symlink, is otherwise
 738 // insecure, or if an error occurred.
 739 //
 740 static bool make_user_tmp_dir(const char* dirname) {
 741 
 742   // create the directory with 0755 permissions. note that the directory
 743   // will be owned by euid::egid, which may not be the same as uid::gid.
 744   //
 745   if (mkdir(dirname, S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH) == OS_ERR) {
 746     if (errno == EEXIST) {
 747       // The directory already exists and was probably created by another
 748       // JVM instance. However, this could also be the result of a
 749       // deliberate symlink. Verify that the existing directory is safe.
 750       //
 751       if (!is_directory_secure(dirname)) {
 752         // directory is not secure




 516   // directory search
 517   char* oldest_user = NULL;
 518   time_t oldest_ctime = 0;
 519 
 520   const char* tmpdirname = os::get_temp_directory();
 521 
 522   // open the temp directory
 523   DIR* tmpdirp = os::opendir(tmpdirname);
 524 
 525   if (tmpdirp == NULL) {
 526     // Cannot open the directory to get the user name, return.
 527     return NULL;
 528   }
 529 
 530   // for each entry in the directory that matches the pattern hsperfdata_*,
 531   // open the directory and check if the file for the given vmid exists.
 532   // The file with the expected name and the latest creation date is used
 533   // to determine the user name for the process id.
 534   //
 535   struct dirent* dentry;

 536   errno = 0;
 537   while ((dentry = os::readdir(tmpdirp)) != NULL) {
 538 
 539     // check if the directory entry is a hsperfdata file
 540     if (strncmp(dentry->d_name, PERFDATA_NAME, strlen(PERFDATA_NAME)) != 0) {
 541       continue;
 542     }
 543 
 544     char* usrdir_name = NEW_C_HEAP_ARRAY(char,
 545                  strlen(tmpdirname) + strlen(dentry->d_name) + 2, mtInternal);
 546     strcpy(usrdir_name, tmpdirname);
 547     strcat(usrdir_name, "/");
 548     strcat(usrdir_name, dentry->d_name);
 549 
 550     // open the user directory
 551     DIR* subdirp = open_directory_secure(usrdir_name);
 552 
 553     if (subdirp == NULL) {
 554       FREE_C_HEAP_ARRAY(char, usrdir_name, mtInternal);
 555       continue;
 556     }
 557 
 558     struct dirent* udentry;

 559     errno = 0;
 560     while ((udentry = os::readdir(subdirp)) != NULL) {
 561 
 562       if (filename_to_pid(udentry->d_name) == vmid) {
 563         struct stat statbuf;
 564         int result;
 565 
 566         char* filename = NEW_C_HEAP_ARRAY(char,
 567                  strlen(usrdir_name) + strlen(udentry->d_name) + 2, mtInternal);
 568 
 569         strcpy(filename, usrdir_name);
 570         strcat(filename, "/");
 571         strcat(filename, udentry->d_name);
 572 
 573         // don't follow symbolic links for the file
 574         RESTARTABLE(::lstat(filename, &statbuf), result);
 575         if (result == OS_ERR) {
 576            FREE_C_HEAP_ARRAY(char, filename, mtInternal);
 577            continue;
 578         }
 579 
 580         // skip over files that are not regular files.


 584         }
 585 
 586         // compare and save filename with latest creation time
 587         if (statbuf.st_size > 0 && statbuf.st_ctime > oldest_ctime) {
 588 
 589           if (statbuf.st_ctime > oldest_ctime) {
 590             char* user = strchr(dentry->d_name, '_') + 1;
 591 
 592             if (oldest_user != NULL) FREE_C_HEAP_ARRAY(char, oldest_user, mtInternal);
 593             oldest_user = NEW_C_HEAP_ARRAY(char, strlen(user)+1, mtInternal);
 594 
 595             strcpy(oldest_user, user);
 596             oldest_ctime = statbuf.st_ctime;
 597           }
 598         }
 599 
 600         FREE_C_HEAP_ARRAY(char, filename, mtInternal);
 601       }
 602     }
 603     os::closedir(subdirp);

 604     FREE_C_HEAP_ARRAY(char, usrdir_name, mtInternal);
 605   }
 606   os::closedir(tmpdirp);

 607 
 608   return(oldest_user);
 609 }
 610 
 611 // return the name of the user that owns the JVM indicated by the given vmid.
 612 //
 613 static char* get_user_name(int vmid, TRAPS) {
 614   return get_user_name_slow(vmid, CHECK_NULL);
 615 }
 616 
 617 // return the file name of the backing store file for the named
 618 // shared memory region for the given user name and vmid.
 619 //
 620 // the caller is expected to free the allocated memory.
 621 //
 622 static char* get_sharedmem_filename(const char* dirname, int vmid) {
 623 
 624   // add 2 for the file separator and a null terminator.
 625   size_t nbytes = strlen(dirname) + UINT_CHARS + 2;
 626 


 665 //
 666 static void cleanup_sharedmem_resources(const char* dirname) {
 667 
 668   int saved_cwd_fd;
 669   // open the directory and set the current working directory to it
 670   DIR* dirp = open_directory_secure_cwd(dirname, &saved_cwd_fd);
 671   if (dirp == NULL) {
 672     // directory doesn't exist or is insecure, so there is nothing to cleanup
 673     return;
 674   }
 675 
 676   // for each entry in the directory that matches the expected file
 677   // name pattern, determine if the file resources are stale and if
 678   // so, remove the file resources. Note, instrumented HotSpot processes
 679   // for this user may start and/or terminate during this search and
 680   // remove or create new files in this directory. The behavior of this
 681   // loop under these conditions is dependent upon the implementation of
 682   // opendir/readdir.
 683   //
 684   struct dirent* entry;


 685   errno = 0;
 686   while ((entry = os::readdir(dirp)) != NULL) {
 687 
 688     pid_t pid = filename_to_pid(entry->d_name);
 689 
 690     if (pid == 0) {
 691 
 692       if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) {
 693 
 694         // attempt to remove all unexpected files, except "." and ".."
 695         unlink(entry->d_name);
 696       }
 697 
 698       errno = 0;
 699       continue;
 700     }
 701 
 702     // we now have a file name that converts to a valid integer
 703     // that could represent a process id . if this process id
 704     // matches the current process id or the process is not running,
 705     // then remove the stale file resources.
 706     //
 707     // process liveness is detected by sending signal number 0 to
 708     // the process id (see kill(2)). if kill determines that the
 709     // process does not exist, then the file resources are removed.
 710     // if kill determines that that we don't have permission to
 711     // signal the process, then the file resources are assumed to
 712     // be stale and are removed because the resources for such a
 713     // process should be in a different user specific directory.
 714     //
 715     if ((pid == os::current_process_id()) ||
 716         (kill(pid, 0) == OS_ERR && (errno == ESRCH || errno == EPERM))) {
 717 
 718         unlink(entry->d_name);
 719     }
 720     errno = 0;
 721   }
 722 
 723   // close the directory and reset the current working directory
 724   close_directory_secure_cwd(dirp, saved_cwd_fd);


 725 }
 726 
 727 // make the user specific temporary directory. Returns true if
 728 // the directory exists and is secure upon return. Returns false
 729 // if the directory exists but is either a symlink, is otherwise
 730 // insecure, or if an error occurred.
 731 //
 732 static bool make_user_tmp_dir(const char* dirname) {
 733 
 734   // create the directory with 0755 permissions. note that the directory
 735   // will be owned by euid::egid, which may not be the same as uid::gid.
 736   //
 737   if (mkdir(dirname, S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH) == OS_ERR) {
 738     if (errno == EEXIST) {
 739       // The directory already exists and was probably created by another
 740       // JVM instance. However, this could also be the result of a
 741       // deliberate symlink. Verify that the existing directory is safe.
 742       //
 743       if (!is_directory_secure(dirname)) {
 744         // directory is not secure


< prev index next >