1 /*
   2  * Copyright (c) 2001, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "classfile/vmSymbols.hpp"
  27 #include "memory/allocation.inline.hpp"
  28 #include "memory/resourceArea.hpp"
  29 #include "oops/oop.inline.hpp"
  30 #include "os_linux.inline.hpp"
  31 #include "runtime/handles.inline.hpp"
  32 #include "runtime/perfMemory.hpp"
  33 #include "services/memTracker.hpp"
  34 #include "utilities/exceptions.hpp"
  35 
  36 // put OS-includes here
  37 # include <sys/types.h>
  38 # include <sys/mman.h>
  39 # include <errno.h>
  40 # include <stdio.h>
  41 # include <unistd.h>
  42 # include <sys/stat.h>
  43 # include <signal.h>
  44 # include <pwd.h>
  45 
  46 static char* backing_store_file_name = NULL;  // name of the backing store
  47                                               // file, if successfully created.
  48 
  49 // Standard Memory Implementation Details
  50 
  51 // create the PerfData memory region in standard memory.
  52 //
  53 static char* create_standard_memory(size_t size) {
  54 
  55   // allocate an aligned chuck of memory
  56   char* mapAddress = os::reserve_memory(size);
  57 
  58   if (mapAddress == NULL) {
  59     return NULL;
  60   }
  61 
  62   // commit memory
  63   if (!os::commit_memory(mapAddress, size, !ExecMem)) {
  64     if (PrintMiscellaneous && Verbose) {
  65       warning("Could not commit PerfData memory\n");
  66     }
  67     os::release_memory(mapAddress, size);
  68     return NULL;
  69   }
  70 
  71   return mapAddress;
  72 }
  73 
  74 // delete the PerfData memory region
  75 //
  76 static void delete_standard_memory(char* addr, size_t size) {
  77 
  78   // there are no persistent external resources to cleanup for standard
  79   // memory. since DestroyJavaVM does not support unloading of the JVM,
  80   // cleanup of the memory resource is not performed. The memory will be
  81   // reclaimed by the OS upon termination of the process.
  82   //
  83   return;
  84 }
  85 
  86 // save the specified memory region to the given file
  87 //
  88 // Note: this function might be called from signal handler (by os::abort()),
  89 // don't allocate heap memory.
  90 //
  91 static void save_memory_to_file(char* addr, size_t size) {
  92 
  93  const char* destfile = PerfMemory::get_perfdata_file_path();
  94  assert(destfile[0] != '\0', "invalid PerfData file path");
  95 
  96   int result;
  97 
  98   RESTARTABLE(::open(destfile, O_CREAT|O_WRONLY|O_TRUNC, S_IREAD|S_IWRITE),
  99               result);;
 100   if (result == OS_ERR) {
 101     if (PrintMiscellaneous && Verbose) {
 102       warning("Could not create Perfdata save file: %s: %s\n",
 103               destfile, strerror(errno));
 104     }
 105   } else {
 106     int fd = result;
 107 
 108     for (size_t remaining = size; remaining > 0;) {
 109 
 110       RESTARTABLE(::write(fd, addr, remaining), result);
 111       if (result == OS_ERR) {
 112         if (PrintMiscellaneous && Verbose) {
 113           warning("Could not write Perfdata save file: %s: %s\n",
 114                   destfile, strerror(errno));
 115         }
 116         break;
 117       }
 118 
 119       remaining -= (size_t)result;
 120       addr += result;
 121     }
 122 
 123     result = ::close(fd);
 124     if (PrintMiscellaneous && Verbose) {
 125       if (result == OS_ERR) {
 126         warning("Could not close %s: %s\n", destfile, strerror(errno));
 127       }
 128     }
 129   }
 130   FREE_C_HEAP_ARRAY(char, destfile);
 131 }
 132 
 133 
 134 // Shared Memory Implementation Details
 135 
 136 // Note: the solaris and linux shared memory implementation uses the mmap
 137 // interface with a backing store file to implement named shared memory.
 138 // Using the file system as the name space for shared memory allows a
 139 // common name space to be supported across a variety of platforms. It
 140 // also provides a name space that Java applications can deal with through
 141 // simple file apis.
 142 //
 143 // The solaris and linux implementations store the backing store file in
 144 // a user specific temporary directory located in the /tmp file system,
 145 // which is always a local file system and is sometimes a RAM based file
 146 // system.
 147 
 148 // return the user specific temporary directory name.
 149 //
 150 // the caller is expected to free the allocated memory.
 151 //
 152 static char* get_user_tmp_dir(const char* user) {
 153 
 154   const char* tmpdir = os::get_temp_directory();
 155   const char* perfdir = PERFDATA_NAME;
 156   size_t nbytes = strlen(tmpdir) + strlen(perfdir) + strlen(user) + 3;
 157   char* dirname = NEW_C_HEAP_ARRAY(char, nbytes, mtInternal);
 158 
 159   // construct the path name to user specific tmp directory
 160   snprintf(dirname, nbytes, "%s/%s_%s", tmpdir, perfdir, user);
 161 
 162   return dirname;
 163 }
 164 
 165 // convert the given file name into a process id. if the file
 166 // does not meet the file naming constraints, return 0.
 167 //
 168 static pid_t filename_to_pid(const char* filename) {
 169 
 170   // a filename that doesn't begin with a digit is not a
 171   // candidate for conversion.
 172   //
 173   if (!isdigit(*filename)) {
 174     return 0;
 175   }
 176 
 177   // check if file name can be converted to an integer without
 178   // any leftover characters.
 179   //
 180   char* remainder = NULL;
 181   errno = 0;
 182   pid_t pid = (pid_t)strtol(filename, &remainder, 10);
 183 
 184   if (errno != 0) {
 185     return 0;
 186   }
 187 
 188   // check for left over characters. If any, then the filename is
 189   // not a candidate for conversion.
 190   //
 191   if (remainder != NULL && *remainder != '\0') {
 192     return 0;
 193   }
 194 
 195   // successful conversion, return the pid
 196   return pid;
 197 }
 198 
 199 
 200 // Check if the given statbuf is considered a secure directory for
 201 // the backing store files. Returns true if the directory is considered
 202 // a secure location. Returns false if the statbuf is a symbolic link or
 203 // if an error occurred.
 204 //
 205 static bool is_statbuf_secure(struct stat *statp) {
 206   if (S_ISLNK(statp->st_mode) || !S_ISDIR(statp->st_mode)) {
 207     // The path represents a link or some non-directory file type,
 208     // which is not what we expected. Declare it insecure.
 209     //
 210     return false;
 211   }
 212   // We have an existing directory, check if the permissions are safe.
 213   //
 214   if ((statp->st_mode & (S_IWGRP|S_IWOTH)) != 0) {
 215     // The directory is open for writing and could be subjected
 216     // to a symlink or a hard link attack. Declare it insecure.
 217     //
 218     return false;
 219   }
 220   // See if the uid of the directory matches the effective uid of the process.
 221   //
 222   if (statp->st_uid != geteuid()) {
 223     // The directory was not created by this user, declare it insecure.
 224     //
 225     return false;
 226   }
 227   return true;
 228 }
 229 
 230 
 231 // Check if the given path is considered a secure directory for
 232 // the backing store files. Returns true if the directory exists
 233 // and is considered a secure location. Returns false if the path
 234 // is a symbolic link or if an error occurred.
 235 //
 236 static bool is_directory_secure(const char* path) {
 237   struct stat statbuf;
 238   int result = 0;
 239 
 240   RESTARTABLE(::lstat(path, &statbuf), result);
 241   if (result == OS_ERR) {
 242     return false;
 243   }
 244 
 245   // The path exists, see if it is secure.
 246   return is_statbuf_secure(&statbuf);
 247 }
 248 
 249 
 250 // Check if the given directory file descriptor is considered a secure
 251 // directory for the backing store files. Returns true if the directory
 252 // exists and is considered a secure location. Returns false if the path
 253 // is a symbolic link or if an error occurred.
 254 //
 255 static bool is_dirfd_secure(int dir_fd) {
 256   struct stat statbuf;
 257   int result = 0;
 258 
 259   RESTARTABLE(::fstat(dir_fd, &statbuf), result);
 260   if (result == OS_ERR) {
 261     return false;
 262   }
 263 
 264   // The path exists, now check its mode.
 265   return is_statbuf_secure(&statbuf);
 266 }
 267 
 268 
 269 // Check to make sure fd1 and fd2 are referencing the same file system object.
 270 //
 271 static bool is_same_fsobject(int fd1, int fd2) {
 272   struct stat statbuf1;
 273   struct stat statbuf2;
 274   int result = 0;
 275 
 276   RESTARTABLE(::fstat(fd1, &statbuf1), result);
 277   if (result == OS_ERR) {
 278     return false;
 279   }
 280   RESTARTABLE(::fstat(fd2, &statbuf2), result);
 281   if (result == OS_ERR) {
 282     return false;
 283   }
 284 
 285   if ((statbuf1.st_ino == statbuf2.st_ino) &&
 286       (statbuf1.st_dev == statbuf2.st_dev)) {
 287     return true;
 288   } else {
 289     return false;
 290   }
 291 }
 292 
 293 
 294 // Open the directory of the given path and validate it.
 295 // Return a DIR * of the open directory.
 296 //
 297 static DIR *open_directory_secure(const char* dirname) {
 298   // Open the directory using open() so that it can be verified
 299   // to be secure by calling is_dirfd_secure(), opendir() and then check
 300   // to see if they are the same file system object.  This method does not
 301   // introduce a window of opportunity for the directory to be attacked that
 302   // calling opendir() and is_directory_secure() does.
 303   int result;
 304   DIR *dirp = NULL;
 305   RESTARTABLE(::open(dirname, O_RDONLY|O_NOFOLLOW), result);
 306   if (result == OS_ERR) {
 307     if (PrintMiscellaneous && Verbose) {
 308       if (errno == ELOOP) {
 309         warning("directory %s is a symlink and is not secure\n", dirname);
 310       } else {
 311         warning("could not open directory %s: %s\n", dirname, strerror(errno));
 312       }
 313     }
 314     return dirp;
 315   }
 316   int fd = result;
 317 
 318   // Determine if the open directory is secure.
 319   if (!is_dirfd_secure(fd)) {
 320     // The directory is not a secure directory.
 321     os::close(fd);
 322     return dirp;
 323   }
 324 
 325   // Open the directory.
 326   dirp = ::opendir(dirname);
 327   if (dirp == NULL) {
 328     // The directory doesn't exist, close fd and return.
 329     os::close(fd);
 330     return dirp;
 331   }
 332 
 333   // Check to make sure fd and dirp are referencing the same file system object.
 334   if (!is_same_fsobject(fd, dirfd(dirp))) {
 335     // The directory is not secure.
 336     os::close(fd);
 337     os::closedir(dirp);
 338     dirp = NULL;
 339     return dirp;
 340   }
 341 
 342   // Close initial open now that we know directory is secure
 343   os::close(fd);
 344 
 345   return dirp;
 346 }
 347 
 348 // NOTE: The code below uses fchdir(), open() and unlink() because
 349 // fdopendir(), openat() and unlinkat() are not supported on all
 350 // versions.  Once the support for fdopendir(), openat() and unlinkat()
 351 // is available on all supported versions the code can be changed
 352 // to use these functions.
 353 
 354 // Open the directory of the given path, validate it and set the
 355 // current working directory to it.
 356 // Return a DIR * of the open directory and the saved cwd fd.
 357 //
 358 static DIR *open_directory_secure_cwd(const char* dirname, int *saved_cwd_fd) {
 359 
 360   // Open the directory.
 361   DIR* dirp = open_directory_secure(dirname);
 362   if (dirp == NULL) {
 363     // Directory doesn't exist or is insecure, so there is nothing to cleanup.
 364     return dirp;
 365   }
 366   int fd = dirfd(dirp);
 367 
 368   // Open a fd to the cwd and save it off.
 369   int result;
 370   RESTARTABLE(::open(".", O_RDONLY), result);
 371   if (result == OS_ERR) {
 372     *saved_cwd_fd = -1;
 373   } else {
 374     *saved_cwd_fd = result;
 375   }
 376 
 377   // Set the current directory to dirname by using the fd of the directory.
 378   result = fchdir(fd);
 379 
 380   return dirp;
 381 }
 382 
 383 // Close the directory and restore the current working directory.
 384 //
 385 static void close_directory_secure_cwd(DIR* dirp, int saved_cwd_fd) {
 386 
 387   int result;
 388   // If we have a saved cwd change back to it and close the fd.
 389   if (saved_cwd_fd != -1) {
 390     result = fchdir(saved_cwd_fd);
 391     ::close(saved_cwd_fd);
 392   }
 393 
 394   // Close the directory.
 395   os::closedir(dirp);
 396 }
 397 
 398 // Check if the given file descriptor is considered a secure.
 399 //
 400 static bool is_file_secure(int fd, const char *filename) {
 401 
 402   int result;
 403   struct stat statbuf;
 404 
 405   // Determine if the file is secure.
 406   RESTARTABLE(::fstat(fd, &statbuf), result);
 407   if (result == OS_ERR) {
 408     if (PrintMiscellaneous && Verbose) {
 409       warning("fstat failed on %s: %s\n", filename, strerror(errno));
 410     }
 411     return false;
 412   }
 413   if (statbuf.st_nlink > 1) {
 414     // A file with multiple links is not expected.
 415     if (PrintMiscellaneous && Verbose) {
 416       warning("file %s has multiple links\n", filename);
 417     }
 418     return false;
 419   }
 420   return true;
 421 }
 422 
 423 
 424 // return the user name for the given user id
 425 //
 426 // the caller is expected to free the allocated memory.
 427 //
 428 static char* get_user_name(uid_t uid) {
 429 
 430   struct passwd pwent;
 431 
 432   // determine the max pwbuf size from sysconf, and hardcode
 433   // a default if this not available through sysconf.
 434   //
 435   long bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
 436   if (bufsize == -1)
 437     bufsize = 1024;
 438 
 439   char* pwbuf = NEW_C_HEAP_ARRAY(char, bufsize, mtInternal);
 440 
 441   // POSIX interface to getpwuid_r is used on LINUX
 442   struct passwd* p;
 443   int result = getpwuid_r(uid, &pwent, pwbuf, (size_t)bufsize, &p);
 444 
 445   if (result != 0 || p == NULL || p->pw_name == NULL || *(p->pw_name) == '\0') {
 446     if (PrintMiscellaneous && Verbose) {
 447       if (result != 0) {
 448         warning("Could not retrieve passwd entry: %s\n",
 449                 strerror(result));
 450       }
 451       else if (p == NULL) {
 452         // this check is added to protect against an observed problem
 453         // with getpwuid_r() on RedHat 9 where getpwuid_r returns 0,
 454         // indicating success, but has p == NULL. This was observed when
 455         // inserting a file descriptor exhaustion fault prior to the call
 456         // getpwuid_r() call. In this case, error is set to the appropriate
 457         // error condition, but this is undocumented behavior. This check
 458         // is safe under any condition, but the use of errno in the output
 459         // message may result in an erroneous message.
 460         // Bug Id 89052 was opened with RedHat.
 461         //
 462         warning("Could not retrieve passwd entry: %s\n",
 463                 strerror(errno));
 464       }
 465       else {
 466         warning("Could not determine user name: %s\n",
 467                 p->pw_name == NULL ? "pw_name = NULL" :
 468                                      "pw_name zero length");
 469       }
 470     }
 471     FREE_C_HEAP_ARRAY(char, pwbuf);
 472     return NULL;
 473   }
 474 
 475   char* user_name = NEW_C_HEAP_ARRAY(char, strlen(p->pw_name) + 1, mtInternal);
 476   strcpy(user_name, p->pw_name);
 477 
 478   FREE_C_HEAP_ARRAY(char, pwbuf);
 479   return user_name;
 480 }
 481 
 482 // return the name of the user that owns the process identified by vmid.
 483 //
 484 // This method uses a slow directory search algorithm to find the backing
 485 // store file for the specified vmid and returns the user name, as determined
 486 // by the user name suffix of the hsperfdata_<username> directory name.
 487 //
 488 // the caller is expected to free the allocated memory.
 489 //
 490 static char* get_user_name_slow(int vmid, TRAPS) {
 491 
 492   // short circuit the directory search if the process doesn't even exist.
 493   if (kill(vmid, 0) == OS_ERR) {
 494     if (errno == ESRCH) {
 495       THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(),
 496                   "Process not found");
 497     }
 498     else /* EPERM */ {
 499       THROW_MSG_0(vmSymbols::java_io_IOException(), strerror(errno));
 500     }
 501   }
 502 
 503   // directory search
 504   char* oldest_user = NULL;
 505   time_t oldest_ctime = 0;
 506 
 507   const char* tmpdirname = os::get_temp_directory();
 508 
 509   // open the temp directory
 510   DIR* tmpdirp = os::opendir(tmpdirname);
 511 
 512   if (tmpdirp == NULL) {
 513     // Cannot open the directory to get the user name, return.
 514     return NULL;
 515   }
 516 
 517   // for each entry in the directory that matches the pattern hsperfdata_*,
 518   // open the directory and check if the file for the given vmid exists.
 519   // The file with the expected name and the latest creation date is used
 520   // to determine the user name for the process id.
 521   //
 522   struct dirent* dentry;
 523   char* tdbuf = NEW_C_HEAP_ARRAY(char, os::readdir_buf_size(tmpdirname), mtInternal);
 524   errno = 0;
 525   while ((dentry = os::readdir(tmpdirp, (struct dirent *)tdbuf)) != NULL) {
 526 
 527     // check if the directory entry is a hsperfdata file
 528     if (strncmp(dentry->d_name, PERFDATA_NAME, strlen(PERFDATA_NAME)) != 0) {
 529       continue;
 530     }
 531 
 532     char* usrdir_name = NEW_C_HEAP_ARRAY(char,
 533                      strlen(tmpdirname) + strlen(dentry->d_name) + 2, mtInternal);
 534     strcpy(usrdir_name, tmpdirname);
 535     strcat(usrdir_name, "/");
 536     strcat(usrdir_name, dentry->d_name);
 537 
 538     // open the user directory
 539     DIR* subdirp = open_directory_secure(usrdir_name);
 540 
 541     if (subdirp == NULL) {
 542       FREE_C_HEAP_ARRAY(char, usrdir_name);
 543       continue;
 544     }
 545 
 546     // Since we don't create the backing store files in directories
 547     // pointed to by symbolic links, we also don't follow them when
 548     // looking for the files. We check for a symbolic link after the
 549     // call to opendir in order to eliminate a small window where the
 550     // symlink can be exploited.
 551     //
 552     if (!is_directory_secure(usrdir_name)) {
 553       FREE_C_HEAP_ARRAY(char, usrdir_name);
 554       os::closedir(subdirp);
 555       continue;
 556     }
 557 
 558     struct dirent* udentry;
 559     char* udbuf = NEW_C_HEAP_ARRAY(char, os::readdir_buf_size(usrdir_name), mtInternal);
 560     errno = 0;
 561     while ((udentry = os::readdir(subdirp, (struct dirent *)udbuf)) != NULL) {
 562 
 563       if (filename_to_pid(udentry->d_name) == vmid) {
 564         struct stat statbuf;
 565         int result;
 566 
 567         char* filename = NEW_C_HEAP_ARRAY(char,
 568                    strlen(usrdir_name) + strlen(udentry->d_name) + 2, mtInternal);
 569 
 570         strcpy(filename, usrdir_name);
 571         strcat(filename, "/");
 572         strcat(filename, udentry->d_name);
 573 
 574         // don't follow symbolic links for the file
 575         RESTARTABLE(::lstat(filename, &statbuf), result);
 576         if (result == OS_ERR) {
 577            FREE_C_HEAP_ARRAY(char, filename);
 578            continue;
 579         }
 580 
 581         // skip over files that are not regular files.
 582         if (!S_ISREG(statbuf.st_mode)) {
 583           FREE_C_HEAP_ARRAY(char, filename);
 584           continue;
 585         }
 586 
 587         // compare and save filename with latest creation time
 588         if (statbuf.st_size > 0 && statbuf.st_ctime > oldest_ctime) {
 589 
 590           if (statbuf.st_ctime > oldest_ctime) {
 591             char* user = strchr(dentry->d_name, '_') + 1;
 592 
 593             if (oldest_user != NULL) FREE_C_HEAP_ARRAY(char, oldest_user);
 594             oldest_user = NEW_C_HEAP_ARRAY(char, strlen(user)+1, mtInternal);
 595 
 596             strcpy(oldest_user, user);
 597             oldest_ctime = statbuf.st_ctime;
 598           }
 599         }
 600 
 601         FREE_C_HEAP_ARRAY(char, filename);
 602       }
 603     }
 604     os::closedir(subdirp);
 605     FREE_C_HEAP_ARRAY(char, udbuf);
 606     FREE_C_HEAP_ARRAY(char, usrdir_name);
 607   }
 608   os::closedir(tmpdirp);
 609   FREE_C_HEAP_ARRAY(char, tdbuf);
 610 
 611   return(oldest_user);
 612 }
 613 
 614 // return the name of the user that owns the JVM indicated by the given vmid.
 615 //
 616 static char* get_user_name(int vmid, TRAPS) {
 617   return get_user_name_slow(vmid, THREAD);
 618 }
 619 
 620 // return the file name of the backing store file for the named
 621 // shared memory region for the given user name and vmid.
 622 //
 623 // the caller is expected to free the allocated memory.
 624 //
 625 static char* get_sharedmem_filename(const char* dirname, int vmid) {
 626 
 627   // add 2 for the file separator and a null terminator.
 628   size_t nbytes = strlen(dirname) + UINT_CHARS + 2;
 629 
 630   char* name = NEW_C_HEAP_ARRAY(char, nbytes, mtInternal);
 631   snprintf(name, nbytes, "%s/%d", dirname, vmid);
 632 
 633   return name;
 634 }
 635 
 636 
 637 // remove file
 638 //
 639 // this method removes the file specified by the given path
 640 //
 641 static void remove_file(const char* path) {
 642 
 643   int result;
 644 
 645   // if the file is a directory, the following unlink will fail. since
 646   // we don't expect to find directories in the user temp directory, we
 647   // won't try to handle this situation. even if accidentially or
 648   // maliciously planted, the directory's presence won't hurt anything.
 649   //
 650   RESTARTABLE(::unlink(path), result);
 651   if (PrintMiscellaneous && Verbose && result == OS_ERR) {
 652     if (errno != ENOENT) {
 653       warning("Could not unlink shared memory backing"
 654               " store file %s : %s\n", path, strerror(errno));
 655     }
 656   }
 657 }
 658 
 659 
 660 // cleanup stale shared memory resources
 661 //
 662 // This method attempts to remove all stale shared memory files in
 663 // the named user temporary directory. It scans the named directory
 664 // for files matching the pattern ^$[0-9]*$. For each file found, the
 665 // process id is extracted from the file name and a test is run to
 666 // determine if the process is alive. If the process is not alive,
 667 // any stale file resources are removed.
 668 //
 669 static void cleanup_sharedmem_resources(const char* dirname) {
 670 
 671   int saved_cwd_fd;
 672   // open the directory
 673   DIR* dirp = open_directory_secure_cwd(dirname, &saved_cwd_fd);
 674   if (dirp == NULL) {
 675     // directory doesn't exist or is insecure, so there is nothing to cleanup
 676     return;
 677   }
 678 
 679   // for each entry in the directory that matches the expected file
 680   // name pattern, determine if the file resources are stale and if
 681   // so, remove the file resources. Note, instrumented HotSpot processes
 682   // for this user may start and/or terminate during this search and
 683   // remove or create new files in this directory. The behavior of this
 684   // loop under these conditions is dependent upon the implementation of
 685   // opendir/readdir.
 686   //
 687   struct dirent* entry;
 688   char* dbuf = NEW_C_HEAP_ARRAY(char, os::readdir_buf_size(dirname), mtInternal);
 689 
 690   errno = 0;
 691   while ((entry = os::readdir(dirp, (struct dirent *)dbuf)) != NULL) {
 692 
 693     pid_t pid = filename_to_pid(entry->d_name);
 694 
 695     if (pid == 0) {
 696 
 697       if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) {
 698         // attempt to remove all unexpected files, except "." and ".."
 699         unlink(entry->d_name);
 700       }
 701 
 702       errno = 0;
 703       continue;
 704     }
 705 
 706     // we now have a file name that converts to a valid integer
 707     // that could represent a process id . if this process id
 708     // matches the current process id or the process is not running,
 709     // then remove the stale file resources.
 710     //
 711     // process liveness is detected by sending signal number 0 to
 712     // the process id (see kill(2)). if kill determines that the
 713     // process does not exist, then the file resources are removed.
 714     // if kill determines that that we don't have permission to
 715     // signal the process, then the file resources are assumed to
 716     // be stale and are removed because the resources for such a
 717     // process should be in a different user specific directory.
 718     //
 719     if ((pid == os::current_process_id()) ||
 720         (kill(pid, 0) == OS_ERR && (errno == ESRCH || errno == EPERM))) {
 721         unlink(entry->d_name);
 722     }
 723     errno = 0;
 724   }
 725 
 726   // close the directory and reset the current working directory
 727   close_directory_secure_cwd(dirp, saved_cwd_fd);
 728 
 729   FREE_C_HEAP_ARRAY(char, dbuf);
 730 }
 731 
 732 // make the user specific temporary directory. Returns true if
 733 // the directory exists and is secure upon return. Returns false
 734 // if the directory exists but is either a symlink, is otherwise
 735 // insecure, or if an error occurred.
 736 //
 737 static bool make_user_tmp_dir(const char* dirname) {
 738 
 739   // create the directory with 0755 permissions. note that the directory
 740   // will be owned by euid::egid, which may not be the same as uid::gid.
 741   //
 742   if (mkdir(dirname, S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH) == OS_ERR) {
 743     if (errno == EEXIST) {
 744       // The directory already exists and was probably created by another
 745       // JVM instance. However, this could also be the result of a
 746       // deliberate symlink. Verify that the existing directory is safe.
 747       //
 748       if (!is_directory_secure(dirname)) {
 749         // directory is not secure
 750         if (PrintMiscellaneous && Verbose) {
 751           warning("%s directory is insecure\n", dirname);
 752         }
 753         return false;
 754       }
 755     }
 756     else {
 757       // we encountered some other failure while attempting
 758       // to create the directory
 759       //
 760       if (PrintMiscellaneous && Verbose) {
 761         warning("could not create directory %s: %s\n",
 762                 dirname, strerror(errno));
 763       }
 764       return false;
 765     }
 766   }
 767   return true;
 768 }
 769 
 770 // create the shared memory file resources
 771 //
 772 // This method creates the shared memory file with the given size
 773 // This method also creates the user specific temporary directory, if
 774 // it does not yet exist.
 775 //
 776 static int create_sharedmem_resources(const char* dirname, const char* filename, size_t size) {
 777 
 778   // make the user temporary directory
 779   if (!make_user_tmp_dir(dirname)) {
 780     // could not make/find the directory or the found directory
 781     // was not secure
 782     return -1;
 783   }
 784 
 785   int saved_cwd_fd;
 786   // open the directory and set the current working directory to it
 787   DIR* dirp = open_directory_secure_cwd(dirname, &saved_cwd_fd);
 788   if (dirp == NULL) {
 789     // Directory doesn't exist or is insecure, so cannot create shared
 790     // memory file.
 791     return -1;
 792   }
 793 
 794   // Open the filename in the current directory.
 795   // Cannot use O_TRUNC here; truncation of an existing file has to happen
 796   // after the is_file_secure() check below.
 797   int result;
 798   RESTARTABLE(::open(filename, O_RDWR|O_CREAT|O_NOFOLLOW, S_IREAD|S_IWRITE), result);
 799   if (result == OS_ERR) {
 800     if (PrintMiscellaneous && Verbose) {
 801       if (errno == ELOOP) {
 802         warning("file %s is a symlink and is not secure\n", filename);
 803       } else {
 804         warning("could not create file %s: %s\n", filename, strerror(errno));
 805       }
 806     }
 807     // close the directory and reset the current working directory
 808     close_directory_secure_cwd(dirp, saved_cwd_fd);
 809 
 810     return -1;
 811   }
 812   // close the directory and reset the current working directory
 813   close_directory_secure_cwd(dirp, saved_cwd_fd);
 814 
 815   // save the file descriptor
 816   int fd = result;
 817 
 818   // check to see if the file is secure
 819   if (!is_file_secure(fd, filename)) {
 820     ::close(fd);
 821     return -1;
 822   }
 823 
 824   // truncate the file to get rid of any existing data
 825   RESTARTABLE(::ftruncate(fd, (off_t)0), result);
 826   if (result == OS_ERR) {
 827     if (PrintMiscellaneous && Verbose) {
 828       warning("could not truncate shared memory file: %s\n", strerror(errno));
 829     }
 830     ::close(fd);
 831     return -1;
 832   }
 833   // set the file size
 834   RESTARTABLE(::ftruncate(fd, (off_t)size), result);
 835   if (result == OS_ERR) {
 836     if (PrintMiscellaneous && Verbose) {
 837       warning("could not set shared memory file size: %s\n", strerror(errno));
 838     }
 839     ::close(fd);
 840     return -1;
 841   }
 842 
 843   // Verify that we have enough disk space for this file.
 844   // We'll get random SIGBUS crashes on memory accesses if
 845   // we don't.
 846 
 847   for (size_t seekpos = 0; seekpos < size; seekpos += os::vm_page_size()) {
 848     int zero_int = 0;
 849     result = (int)os::seek_to_file_offset(fd, (jlong)(seekpos));
 850     if (result == -1 ) break;
 851     RESTARTABLE(::write(fd, &zero_int, 1), result);
 852     if (result != 1) {
 853       if (errno == ENOSPC) {
 854         warning("Insufficient space for shared memory file:\n   %s\nTry using the -Djava.io.tmpdir= option to select an alternate temp location.\n", filename);
 855       }
 856       break;
 857     }
 858   }
 859 
 860   if (result != -1) {
 861     return fd;
 862   } else {
 863     ::close(fd);
 864     return -1;
 865   }
 866 }
 867 
 868 // open the shared memory file for the given user and vmid. returns
 869 // the file descriptor for the open file or -1 if the file could not
 870 // be opened.
 871 //
 872 static int open_sharedmem_file(const char* filename, int oflags, TRAPS) {
 873 
 874   // open the file
 875   int result;
 876   RESTARTABLE(::open(filename, oflags), result);
 877   if (result == OS_ERR) {
 878     if (errno == ENOENT) {
 879       THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(),
 880                   "Process not found", OS_ERR);
 881     }
 882     else if (errno == EACCES) {
 883       THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(),
 884                   "Permission denied", OS_ERR);
 885     }
 886     else {
 887       THROW_MSG_(vmSymbols::java_io_IOException(), strerror(errno), OS_ERR);
 888     }
 889   }
 890   int fd = result;
 891 
 892   // check to see if the file is secure
 893   if (!is_file_secure(fd, filename)) {
 894     ::close(fd);
 895     return -1;
 896   }
 897 
 898   return fd;
 899 }
 900 
 901 // create a named shared memory region. returns the address of the
 902 // memory region on success or NULL on failure. A return value of
 903 // NULL will ultimately disable the shared memory feature.
 904 //
 905 // On Solaris and Linux, the name space for shared memory objects
 906 // is the file system name space.
 907 //
 908 // A monitoring application attaching to a JVM does not need to know
 909 // the file system name of the shared memory object. However, it may
 910 // be convenient for applications to discover the existence of newly
 911 // created and terminating JVMs by watching the file system name space
 912 // for files being created or removed.
 913 //
 914 static char* mmap_create_shared(size_t size) {
 915 
 916   int result;
 917   int fd;
 918   char* mapAddress;
 919 
 920   int vmid = os::current_process_id();
 921 
 922   char* user_name = get_user_name(geteuid());
 923 
 924   if (user_name == NULL)
 925     return NULL;
 926 
 927   char* dirname = get_user_tmp_dir(user_name);
 928   char* filename = get_sharedmem_filename(dirname, vmid);
 929   // get the short filename
 930   char* short_filename = strrchr(filename, '/');
 931   if (short_filename == NULL) {
 932     short_filename = filename;
 933   } else {
 934     short_filename++;
 935   }
 936 
 937   // cleanup any stale shared memory files
 938   cleanup_sharedmem_resources(dirname);
 939 
 940   assert(((size > 0) && (size % os::vm_page_size() == 0)),
 941          "unexpected PerfMemory region size");
 942 
 943   fd = create_sharedmem_resources(dirname, short_filename, size);
 944 
 945   FREE_C_HEAP_ARRAY(char, user_name);
 946   FREE_C_HEAP_ARRAY(char, dirname);
 947 
 948   if (fd == -1) {
 949     FREE_C_HEAP_ARRAY(char, filename);
 950     return NULL;
 951   }
 952 
 953   mapAddress = (char*)::mmap((char*)0, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
 954 
 955   result = ::close(fd);
 956   assert(result != OS_ERR, "could not close file");
 957 
 958   if (mapAddress == MAP_FAILED) {
 959     if (PrintMiscellaneous && Verbose) {
 960       warning("mmap failed -  %s\n", strerror(errno));
 961     }
 962     remove_file(filename);
 963     FREE_C_HEAP_ARRAY(char, filename);
 964     return NULL;
 965   }
 966 
 967   // save the file name for use in delete_shared_memory()
 968   backing_store_file_name = filename;
 969 
 970   // clear the shared memory region
 971   (void)::memset((void*) mapAddress, 0, size);
 972 
 973   // it does not go through os api, the operation has to record from here
 974   MemTracker::record_virtual_memory_reserve_and_commit((address)mapAddress, size, CURRENT_PC, mtInternal);
 975 
 976   return mapAddress;
 977 }
 978 
 979 // release a named shared memory region
 980 //
 981 static void unmap_shared(char* addr, size_t bytes) {
 982   os::release_memory(addr, bytes);
 983 }
 984 
 985 // create the PerfData memory region in shared memory.
 986 //
 987 static char* create_shared_memory(size_t size) {
 988 
 989   // create the shared memory region.
 990   return mmap_create_shared(size);
 991 }
 992 
 993 // delete the shared PerfData memory region
 994 //
 995 static void delete_shared_memory(char* addr, size_t size) {
 996 
 997   // cleanup the persistent shared memory resources. since DestroyJavaVM does
 998   // not support unloading of the JVM, unmapping of the memory resource is
 999   // not performed. The memory will be reclaimed by the OS upon termination of
1000   // the process. The backing store file is deleted from the file system.
1001 
1002   assert(!PerfDisableSharedMem, "shouldn't be here");
1003 
1004   if (backing_store_file_name != NULL) {
1005     remove_file(backing_store_file_name);
1006     // Don't.. Free heap memory could deadlock os::abort() if it is called
1007     // from signal handler. OS will reclaim the heap memory.
1008     // FREE_C_HEAP_ARRAY(char, backing_store_file_name);
1009     backing_store_file_name = NULL;
1010   }
1011 }
1012 
1013 // return the size of the file for the given file descriptor
1014 // or 0 if it is not a valid size for a shared memory file
1015 //
1016 static size_t sharedmem_filesize(int fd, TRAPS) {
1017 
1018   struct stat statbuf;
1019   int result;
1020 
1021   RESTARTABLE(::fstat(fd, &statbuf), result);
1022   if (result == OS_ERR) {
1023     if (PrintMiscellaneous && Verbose) {
1024       warning("fstat failed: %s\n", strerror(errno));
1025     }
1026     THROW_MSG_0(vmSymbols::java_io_IOException(),
1027                 "Could not determine PerfMemory size");
1028   }
1029 
1030   if ((statbuf.st_size == 0) ||
1031      ((size_t)statbuf.st_size % os::vm_page_size() != 0)) {
1032     THROW_MSG_0(vmSymbols::java_lang_Exception(),
1033                 "Invalid PerfMemory size");
1034   }
1035 
1036   return (size_t)statbuf.st_size;
1037 }
1038 
1039 // attach to a named shared memory region.
1040 //
1041 static void mmap_attach_shared(const char* user, int vmid, PerfMemory::PerfMemoryMode mode, char** addr, size_t* sizep, TRAPS) {
1042 
1043   char* mapAddress;
1044   int result;
1045   int fd;
1046   size_t size = 0;
1047   const char* luser = NULL;
1048 
1049   int mmap_prot;
1050   int file_flags;
1051 
1052   ResourceMark rm;
1053 
1054   // map the high level access mode to the appropriate permission
1055   // constructs for the file and the shared memory mapping.
1056   if (mode == PerfMemory::PERF_MODE_RO) {
1057     mmap_prot = PROT_READ;
1058     file_flags = O_RDONLY | O_NOFOLLOW;
1059   }
1060   else if (mode == PerfMemory::PERF_MODE_RW) {
1061 #ifdef LATER
1062     mmap_prot = PROT_READ | PROT_WRITE;
1063     file_flags = O_RDWR | O_NOFOLLOW;
1064 #else
1065     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
1066               "Unsupported access mode");
1067 #endif
1068   }
1069   else {
1070     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
1071               "Illegal access mode");
1072   }
1073 
1074   if (user == NULL || strlen(user) == 0) {
1075     luser = get_user_name(vmid, CHECK);
1076   }
1077   else {
1078     luser = user;
1079   }
1080 
1081   if (luser == NULL) {
1082     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
1083               "Could not map vmid to user Name");
1084   }
1085 
1086   char* dirname = get_user_tmp_dir(luser);
1087 
1088   // since we don't follow symbolic links when creating the backing
1089   // store file, we don't follow them when attaching either.
1090   //
1091   if (!is_directory_secure(dirname)) {
1092     FREE_C_HEAP_ARRAY(char, dirname);
1093     if (luser != user) {
1094       FREE_C_HEAP_ARRAY(char, luser);
1095     }
1096     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
1097               "Process not found");
1098   }
1099 
1100   char* filename = get_sharedmem_filename(dirname, vmid);
1101 
1102   // copy heap memory to resource memory. the open_sharedmem_file
1103   // method below need to use the filename, but could throw an
1104   // exception. using a resource array prevents the leak that
1105   // would otherwise occur.
1106   char* rfilename = NEW_RESOURCE_ARRAY(char, strlen(filename) + 1);
1107   strcpy(rfilename, filename);
1108 
1109   // free the c heap resources that are no longer needed
1110   if (luser != user) FREE_C_HEAP_ARRAY(char, luser);
1111   FREE_C_HEAP_ARRAY(char, dirname);
1112   FREE_C_HEAP_ARRAY(char, filename);
1113 
1114   // open the shared memory file for the give vmid
1115   fd = open_sharedmem_file(rfilename, file_flags, THREAD);
1116 
1117   if (fd == OS_ERR) {
1118     return;
1119   }
1120 
1121   if (HAS_PENDING_EXCEPTION) {
1122     ::close(fd);
1123     return;
1124   }
1125 
1126   if (*sizep == 0) {
1127     size = sharedmem_filesize(fd, CHECK);
1128   } else {
1129     size = *sizep;
1130   }
1131 
1132   assert(size > 0, "unexpected size <= 0");
1133 
1134   mapAddress = (char*)::mmap((char*)0, size, mmap_prot, MAP_SHARED, fd, 0);
1135 
1136   result = ::close(fd);
1137   assert(result != OS_ERR, "could not close file");
1138 
1139   if (mapAddress == MAP_FAILED) {
1140     if (PrintMiscellaneous && Verbose) {
1141       warning("mmap failed: %s\n", strerror(errno));
1142     }
1143     THROW_MSG(vmSymbols::java_lang_OutOfMemoryError(),
1144               "Could not map PerfMemory");
1145   }
1146 
1147   // it does not go through os api, the operation has to record from here
1148   MemTracker::record_virtual_memory_reserve_and_commit((address)mapAddress, size, CURRENT_PC, mtInternal);
1149 
1150   *addr = mapAddress;
1151   *sizep = size;
1152 
1153   if (PerfTraceMemOps) {
1154     tty->print("mapped " SIZE_FORMAT " bytes for vmid %d at "
1155                INTPTR_FORMAT "\n", size, vmid, p2i((void*)mapAddress));
1156   }
1157 }
1158 
1159 
1160 
1161 
1162 // create the PerfData memory region
1163 //
1164 // This method creates the memory region used to store performance
1165 // data for the JVM. The memory may be created in standard or
1166 // shared memory.
1167 //
1168 void PerfMemory::create_memory_region(size_t size) {
1169 
1170   if (PerfDisableSharedMem) {
1171     // do not share the memory for the performance data.
1172     _start = create_standard_memory(size);
1173   }
1174   else {
1175     _start = create_shared_memory(size);
1176     if (_start == NULL) {
1177 
1178       // creation of the shared memory region failed, attempt
1179       // to create a contiguous, non-shared memory region instead.
1180       //
1181       if (PrintMiscellaneous && Verbose) {
1182         warning("Reverting to non-shared PerfMemory region.\n");
1183       }
1184       PerfDisableSharedMem = true;
1185       _start = create_standard_memory(size);
1186     }
1187   }
1188 
1189   if (_start != NULL) _capacity = size;
1190 
1191 }
1192 
1193 // delete the PerfData memory region
1194 //
1195 // This method deletes the memory region used to store performance
1196 // data for the JVM. The memory region indicated by the <address, size>
1197 // tuple will be inaccessible after a call to this method.
1198 //
1199 void PerfMemory::delete_memory_region() {
1200 
1201   assert((start() != NULL && capacity() > 0), "verify proper state");
1202 
1203   // If user specifies PerfDataSaveFile, it will save the performance data
1204   // to the specified file name no matter whether PerfDataSaveToFile is specified
1205   // or not. In other word, -XX:PerfDataSaveFile=.. overrides flag
1206   // -XX:+PerfDataSaveToFile.
1207   if (PerfDataSaveToFile || PerfDataSaveFile != NULL) {
1208     save_memory_to_file(start(), capacity());
1209   }
1210 
1211   if (PerfDisableSharedMem) {
1212     delete_standard_memory(start(), capacity());
1213   }
1214   else {
1215     delete_shared_memory(start(), capacity());
1216   }
1217 }
1218 
1219 // attach to the PerfData memory region for another JVM
1220 //
1221 // This method returns an <address, size> tuple that points to
1222 // a memory buffer that is kept reasonably synchronized with
1223 // the PerfData memory region for the indicated JVM. This
1224 // buffer may be kept in synchronization via shared memory
1225 // or some other mechanism that keeps the buffer updated.
1226 //
1227 // If the JVM chooses not to support the attachability feature,
1228 // this method should throw an UnsupportedOperation exception.
1229 //
1230 // This implementation utilizes named shared memory to map
1231 // the indicated process's PerfData memory region into this JVMs
1232 // address space.
1233 //
1234 void PerfMemory::attach(const char* user, int vmid, PerfMemoryMode mode, char** addrp, size_t* sizep, TRAPS) {
1235 
1236   if (vmid == 0 || vmid == os::current_process_id()) {
1237      *addrp = start();
1238      *sizep = capacity();
1239      return;
1240   }
1241 
1242   mmap_attach_shared(user, vmid, mode, addrp, sizep, CHECK);
1243 }
1244 
1245 // detach from the PerfData memory region of another JVM
1246 //
1247 // This method detaches the PerfData memory region of another
1248 // JVM, specified as an <address, size> tuple of a buffer
1249 // in this process's address space. This method may perform
1250 // arbitrary actions to accomplish the detachment. The memory
1251 // region specified by <address, size> will be inaccessible after
1252 // a call to this method.
1253 //
1254 // If the JVM chooses not to support the attachability feature,
1255 // this method should throw an UnsupportedOperation exception.
1256 //
1257 // This implementation utilizes named shared memory to detach
1258 // the indicated process's PerfData memory region from this
1259 // process's address space.
1260 //
1261 void PerfMemory::detach(char* addr, size_t bytes, TRAPS) {
1262 
1263   assert(addr != 0, "address sanity check");
1264   assert(bytes > 0, "capacity sanity check");
1265 
1266   if (PerfMemory::contains(addr) || PerfMemory::contains(addr + bytes - 1)) {
1267     // prevent accidental detachment of this process's PerfMemory region
1268     return;
1269   }
1270 
1271   unmap_shared(addr, bytes);
1272 }