perfMemory_linux.cpp revision 7421:55e38e5032af
1169689Skan/*
2169689Skan * Copyright (c) 2001, 2014, Oracle and/or its affiliates. All rights reserved.
3169689Skan * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4169689Skan *
5169689Skan * This code is free software; you can redistribute it and/or modify it
6169689Skan * under the terms of the GNU General Public License version 2 only, as
7169689Skan * published by the Free Software Foundation.
8169689Skan *
9169689Skan * This code is distributed in the hope that it will be useful, but WITHOUT
10169689Skan * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11169689Skan * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12169689Skan * version 2 for more details (a copy is included in the LICENSE file that
13169689Skan * accompanied this code).
14169689Skan *
15169689Skan * You should have received a copy of the GNU General Public License version
16169689Skan * 2 along with this work; if not, write to the Free Software Foundation,
17169689Skan * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18169689Skan *
19169689Skan * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20169689Skan * or visit www.oracle.com if you need additional information or have any
21169689Skan * questions.
22169689Skan *
23169689Skan */
24169689Skan
25169689Skan#include "precompiled.hpp"
26169689Skan#include "classfile/vmSymbols.hpp"
27169689Skan#include "memory/allocation.inline.hpp"
28169689Skan#include "memory/resourceArea.hpp"
29169689Skan#include "oops/oop.inline.hpp"
30169689Skan#include "os_linux.inline.hpp"
31169689Skan#include "runtime/handles.inline.hpp"
32169689Skan#include "runtime/perfMemory.hpp"
33169689Skan#include "services/memTracker.hpp"
34169689Skan#include "utilities/exceptions.hpp"
35169689Skan
36169689Skan// put OS-includes here
37169689Skan# include <sys/types.h>
38169689Skan# include <sys/mman.h>
39169689Skan# include <errno.h>
40169689Skan# include <stdio.h>
41169689Skan# include <unistd.h>
42169689Skan# include <sys/stat.h>
43169689Skan# include <signal.h>
44169689Skan# include <pwd.h>
45169689Skan
46169689Skanstatic char* backing_store_file_name = NULL;  // name of the backing store
47169689Skan                                              // file, if successfully created.
48169689Skan
49169689Skan// Standard Memory Implementation Details
50169689Skan
51169689Skan// create the PerfData memory region in standard memory.
52169689Skan//
53169689Skanstatic char* create_standard_memory(size_t size) {
54169689Skan
55169689Skan  // allocate an aligned chuck of memory
56169689Skan  char* mapAddress = os::reserve_memory(size);
57169689Skan
58169689Skan  if (mapAddress == NULL) {
59169689Skan    return NULL;
60169689Skan  }
61169689Skan
62169689Skan  // commit memory
63169689Skan  if (!os::commit_memory(mapAddress, size, !ExecMem)) {
64169689Skan    if (PrintMiscellaneous && Verbose) {
65169689Skan      warning("Could not commit PerfData memory\n");
66169689Skan    }
67169689Skan    os::release_memory(mapAddress, size);
68169689Skan    return NULL;
69169689Skan  }
70169689Skan
71169689Skan  return mapAddress;
72169689Skan}
73169689Skan
74169689Skan// delete the PerfData memory region
75169689Skan//
76169689Skanstatic void delete_standard_memory(char* addr, size_t size) {
77169689Skan
78169689Skan  // there are no persistent external resources to cleanup for standard
79169689Skan  // memory. since DestroyJavaVM does not support unloading of the JVM,
80169689Skan  // cleanup of the memory resource is not performed. The memory will be
81169689Skan  // reclaimed by the OS upon termination of the process.
82169689Skan  //
83169689Skan  return;
84169689Skan}
85169689Skan
86169689Skan// save the specified memory region to the given file
87169689Skan//
88169689Skan// Note: this function might be called from signal handler (by os::abort()),
89169689Skan// don't allocate heap memory.
90169689Skan//
91169689Skanstatic void save_memory_to_file(char* addr, size_t size) {
92169689Skan
93169689Skan const char* destfile = PerfMemory::get_perfdata_file_path();
94169689Skan assert(destfile[0] != '\0', "invalid PerfData file path");
95169689Skan
96169689Skan  int result;
97169689Skan
98169689Skan  RESTARTABLE(::open(destfile, O_CREAT|O_WRONLY|O_TRUNC, S_IREAD|S_IWRITE),
99169689Skan              result);;
100169689Skan  if (result == OS_ERR) {
101169689Skan    if (PrintMiscellaneous && Verbose) {
102169689Skan      warning("Could not create Perfdata save file: %s: %s\n",
103169689Skan              destfile, strerror(errno));
104169689Skan    }
105169689Skan  } else {
106169689Skan    int fd = result;
107169689Skan
108169689Skan    for (size_t remaining = size; remaining > 0;) {
109169689Skan
110169689Skan      RESTARTABLE(::write(fd, addr, remaining), result);
111169689Skan      if (result == OS_ERR) {
112169689Skan        if (PrintMiscellaneous && Verbose) {
113169689Skan          warning("Could not write Perfdata save file: %s: %s\n",
114169689Skan                  destfile, strerror(errno));
115169689Skan        }
116169689Skan        break;
117169689Skan      }
118169689Skan
119169689Skan      remaining -= (size_t)result;
120169689Skan      addr += result;
121169689Skan    }
122169689Skan
123169689Skan    result = ::close(fd);
124169689Skan    if (PrintMiscellaneous && Verbose) {
125169689Skan      if (result == OS_ERR) {
126169689Skan        warning("Could not close %s: %s\n", destfile, strerror(errno));
127169689Skan      }
128169689Skan    }
129169689Skan  }
130169689Skan  FREE_C_HEAP_ARRAY(char, destfile, mtInternal);
131169689Skan}
132169689Skan
133169689Skan
134169689Skan// Shared Memory Implementation Details
135169689Skan
136169689Skan// Note: the solaris and linux shared memory implementation uses the mmap
137169689Skan// interface with a backing store file to implement named shared memory.
138169689Skan// Using the file system as the name space for shared memory allows a
139169689Skan// common name space to be supported across a variety of platforms. It
140169689Skan// also provides a name space that Java applications can deal with through
141169689Skan// simple file apis.
142169689Skan//
143169689Skan// The solaris and linux implementations store the backing store file in
144169689Skan// a user specific temporary directory located in the /tmp file system,
145169689Skan// which is always a local file system and is sometimes a RAM based file
146169689Skan// system.
147169689Skan
148169689Skan// return the user specific temporary directory name.
149169689Skan//
150169689Skan// the caller is expected to free the allocated memory.
151169689Skan//
152169689Skanstatic char* get_user_tmp_dir(const char* user) {
153169689Skan
154169689Skan  const char* tmpdir = os::get_temp_directory();
155169689Skan  const char* perfdir = PERFDATA_NAME;
156169689Skan  size_t nbytes = strlen(tmpdir) + strlen(perfdir) + strlen(user) + 3;
157169689Skan  char* dirname = NEW_C_HEAP_ARRAY(char, nbytes, mtInternal);
158169689Skan
159169689Skan  // construct the path name to user specific tmp directory
160169689Skan  snprintf(dirname, nbytes, "%s/%s_%s", tmpdir, perfdir, user);
161169689Skan
162169689Skan  return dirname;
163169689Skan}
164169689Skan
165169689Skan// convert the given file name into a process id. if the file
166169689Skan// does not meet the file naming constraints, return 0.
167169689Skan//
168169689Skanstatic pid_t filename_to_pid(const char* filename) {
169169689Skan
170169689Skan  // a filename that doesn't begin with a digit is not a
171169689Skan  // candidate for conversion.
172169689Skan  //
173169689Skan  if (!isdigit(*filename)) {
174169689Skan    return 0;
175169689Skan  }
176169689Skan
177169689Skan  // check if file name can be converted to an integer without
178169689Skan  // any leftover characters.
179169689Skan  //
180169689Skan  char* remainder = NULL;
181169689Skan  errno = 0;
182169689Skan  pid_t pid = (pid_t)strtol(filename, &remainder, 10);
183169689Skan
184169689Skan  if (errno != 0) {
185169689Skan    return 0;
186169689Skan  }
187169689Skan
188169689Skan  // check for left over characters. If any, then the filename is
189169689Skan  // not a candidate for conversion.
190169689Skan  //
191169689Skan  if (remainder != NULL && *remainder != '\0') {
192169689Skan    return 0;
193169689Skan  }
194169689Skan
195169689Skan  // successful conversion, return the pid
196169689Skan  return pid;
197169689Skan}
198169689Skan
199169689Skan
200169689Skan// check if the given path is considered a secure directory for
201169689Skan// the backing store files. Returns true if the directory exists
202169689Skan// and is considered a secure location. Returns false if the path
203169689Skan// is a symbolic link or if an error occurred.
204169689Skan//
205169689Skanstatic bool is_directory_secure(const char* path) {
206169689Skan  struct stat statbuf;
207169689Skan  int result = 0;
208169689Skan
209169689Skan  RESTARTABLE(::lstat(path, &statbuf), result);
210169689Skan  if (result == OS_ERR) {
211169689Skan    return false;
212169689Skan  }
213169689Skan
214169689Skan  // the path exists, now check it's mode
215169689Skan  if (S_ISLNK(statbuf.st_mode) || !S_ISDIR(statbuf.st_mode)) {
216169689Skan    // the path represents a link or some non-directory file type,
217169689Skan    // which is not what we expected. declare it insecure.
218169689Skan    //
219169689Skan    return false;
220169689Skan  }
221169689Skan  else {
222169689Skan    // we have an existing directory, check if the permissions are safe.
223169689Skan    //
224169689Skan    if ((statbuf.st_mode & (S_IWGRP|S_IWOTH)) != 0) {
225169689Skan      // the directory is open for writing and could be subjected
226169689Skan      // to a symlnk attack. declare it insecure.
227169689Skan      //
228169689Skan      return false;
229169689Skan    }
230169689Skan  }
231169689Skan  return true;
232169689Skan}
233169689Skan
234169689Skan
235169689Skan// return the user name for the given user id
236169689Skan//
237169689Skan// the caller is expected to free the allocated memory.
238169689Skan//
239169689Skanstatic char* get_user_name(uid_t uid) {
240169689Skan
241169689Skan  struct passwd pwent;
242169689Skan
243169689Skan  // determine the max pwbuf size from sysconf, and hardcode
244169689Skan  // a default if this not available through sysconf.
245169689Skan  //
246169689Skan  long bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
247169689Skan  if (bufsize == -1)
248169689Skan    bufsize = 1024;
249169689Skan
250169689Skan  char* pwbuf = NEW_C_HEAP_ARRAY(char, bufsize, mtInternal);
251169689Skan
252169689Skan  // POSIX interface to getpwuid_r is used on LINUX
253169689Skan  struct passwd* p;
254169689Skan  int result = getpwuid_r(uid, &pwent, pwbuf, (size_t)bufsize, &p);
255169689Skan
256169689Skan  if (result != 0 || p == NULL || p->pw_name == NULL || *(p->pw_name) == '\0') {
257169689Skan    if (PrintMiscellaneous && Verbose) {
258169689Skan      if (result != 0) {
259169689Skan        warning("Could not retrieve passwd entry: %s\n",
260169689Skan                strerror(result));
261169689Skan      }
262169689Skan      else if (p == NULL) {
263169689Skan        // this check is added to protect against an observed problem
264169689Skan        // with getpwuid_r() on RedHat 9 where getpwuid_r returns 0,
265169689Skan        // indicating success, but has p == NULL. This was observed when
266169689Skan        // inserting a file descriptor exhaustion fault prior to the call
267169689Skan        // getpwuid_r() call. In this case, error is set to the appropriate
268169689Skan        // error condition, but this is undocumented behavior. This check
269169689Skan        // is safe under any condition, but the use of errno in the output
270169689Skan        // message may result in an erroneous message.
271169689Skan        // Bug Id 89052 was opened with RedHat.
272169689Skan        //
273169689Skan        warning("Could not retrieve passwd entry: %s\n",
274169689Skan                strerror(errno));
275169689Skan      }
276169689Skan      else {
277169689Skan        warning("Could not determine user name: %s\n",
278169689Skan                p->pw_name == NULL ? "pw_name = NULL" :
279169689Skan                                     "pw_name zero length");
280169689Skan      }
281169689Skan    }
282169689Skan    FREE_C_HEAP_ARRAY(char, pwbuf, mtInternal);
283169689Skan    return NULL;
284169689Skan  }
285169689Skan
286169689Skan  char* user_name = NEW_C_HEAP_ARRAY(char, strlen(p->pw_name) + 1, mtInternal);
287169689Skan  strcpy(user_name, p->pw_name);
288169689Skan
289169689Skan  FREE_C_HEAP_ARRAY(char, pwbuf, mtInternal);
290169689Skan  return user_name;
291169689Skan}
292169689Skan
293169689Skan// return the name of the user that owns the process identified by vmid.
294169689Skan//
295169689Skan// This method uses a slow directory search algorithm to find the backing
296169689Skan// store file for the specified vmid and returns the user name, as determined
297169689Skan// by the user name suffix of the hsperfdata_<username> directory name.
298169689Skan//
299169689Skan// the caller is expected to free the allocated memory.
300169689Skan//
301169689Skanstatic char* get_user_name_slow(int vmid, TRAPS) {
302169689Skan
303169689Skan  // short circuit the directory search if the process doesn't even exist.
304169689Skan  if (kill(vmid, 0) == OS_ERR) {
305169689Skan    if (errno == ESRCH) {
306169689Skan      THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(),
307169689Skan                  "Process not found");
308169689Skan    }
309169689Skan    else /* EPERM */ {
310169689Skan      THROW_MSG_0(vmSymbols::java_io_IOException(), strerror(errno));
311169689Skan    }
312169689Skan  }
313169689Skan
314169689Skan  // directory search
315169689Skan  char* oldest_user = NULL;
316169689Skan  time_t oldest_ctime = 0;
317169689Skan
318169689Skan  const char* tmpdirname = os::get_temp_directory();
319169689Skan
320169689Skan  DIR* tmpdirp = os::opendir(tmpdirname);
321169689Skan
322169689Skan  if (tmpdirp == NULL) {
323169689Skan    return NULL;
324169689Skan  }
325169689Skan
326169689Skan  // for each entry in the directory that matches the pattern hsperfdata_*,
327169689Skan  // open the directory and check if the file for the given vmid exists.
328169689Skan  // The file with the expected name and the latest creation date is used
329169689Skan  // to determine the user name for the process id.
330169689Skan  //
331169689Skan  struct dirent* dentry;
332169689Skan  char* tdbuf = NEW_C_HEAP_ARRAY(char, os::readdir_buf_size(tmpdirname), mtInternal);
333169689Skan  errno = 0;
334169689Skan  while ((dentry = os::readdir(tmpdirp, (struct dirent *)tdbuf)) != NULL) {
335169689Skan
336169689Skan    // check if the directory entry is a hsperfdata file
337169689Skan    if (strncmp(dentry->d_name, PERFDATA_NAME, strlen(PERFDATA_NAME)) != 0) {
338169689Skan      continue;
339169689Skan    }
340169689Skan
341169689Skan    char* usrdir_name = NEW_C_HEAP_ARRAY(char,
342169689Skan                     strlen(tmpdirname) + strlen(dentry->d_name) + 2, mtInternal);
343169689Skan    strcpy(usrdir_name, tmpdirname);
344169689Skan    strcat(usrdir_name, "/");
345169689Skan    strcat(usrdir_name, dentry->d_name);
346169689Skan
347169689Skan    DIR* subdirp = os::opendir(usrdir_name);
348169689Skan
349169689Skan    if (subdirp == NULL) {
350169689Skan      FREE_C_HEAP_ARRAY(char, usrdir_name, mtInternal);
351169689Skan      continue;
352169689Skan    }
353169689Skan
354169689Skan    // Since we don't create the backing store files in directories
355169689Skan    // pointed to by symbolic links, we also don't follow them when
356169689Skan    // looking for the files. We check for a symbolic link after the
357169689Skan    // call to opendir in order to eliminate a small window where the
358169689Skan    // symlink can be exploited.
359169689Skan    //
360169689Skan    if (!is_directory_secure(usrdir_name)) {
361169689Skan      FREE_C_HEAP_ARRAY(char, usrdir_name, mtInternal);
362169689Skan      os::closedir(subdirp);
363169689Skan      continue;
364169689Skan    }
365169689Skan
366169689Skan    struct dirent* udentry;
367169689Skan    char* udbuf = NEW_C_HEAP_ARRAY(char, os::readdir_buf_size(usrdir_name), mtInternal);
368169689Skan    errno = 0;
369169689Skan    while ((udentry = os::readdir(subdirp, (struct dirent *)udbuf)) != NULL) {
370169689Skan
371169689Skan      if (filename_to_pid(udentry->d_name) == vmid) {
372169689Skan        struct stat statbuf;
373169689Skan        int result;
374169689Skan
375169689Skan        char* filename = NEW_C_HEAP_ARRAY(char,
376169689Skan                   strlen(usrdir_name) + strlen(udentry->d_name) + 2, mtInternal);
377169689Skan
378169689Skan        strcpy(filename, usrdir_name);
379169689Skan        strcat(filename, "/");
380169689Skan        strcat(filename, udentry->d_name);
381169689Skan
382169689Skan        // don't follow symbolic links for the file
383169689Skan        RESTARTABLE(::lstat(filename, &statbuf), result);
384169689Skan        if (result == OS_ERR) {
385169689Skan           FREE_C_HEAP_ARRAY(char, filename, mtInternal);
386169689Skan           continue;
387169689Skan        }
388169689Skan
389169689Skan        // skip over files that are not regular files.
390169689Skan        if (!S_ISREG(statbuf.st_mode)) {
391169689Skan          FREE_C_HEAP_ARRAY(char, filename, mtInternal);
392169689Skan          continue;
393169689Skan        }
394169689Skan
395169689Skan        // compare and save filename with latest creation time
396169689Skan        if (statbuf.st_size > 0 && statbuf.st_ctime > oldest_ctime) {
397169689Skan
398169689Skan          if (statbuf.st_ctime > oldest_ctime) {
399169689Skan            char* user = strchr(dentry->d_name, '_') + 1;
400169689Skan
401169689Skan            if (oldest_user != NULL) FREE_C_HEAP_ARRAY(char, oldest_user, mtInternal);
402169689Skan            oldest_user = NEW_C_HEAP_ARRAY(char, strlen(user)+1, mtInternal);
403169689Skan
404169689Skan            strcpy(oldest_user, user);
405169689Skan            oldest_ctime = statbuf.st_ctime;
406169689Skan          }
407169689Skan        }
408169689Skan
409169689Skan        FREE_C_HEAP_ARRAY(char, filename, mtInternal);
410169689Skan      }
411169689Skan    }
412169689Skan    os::closedir(subdirp);
413169689Skan    FREE_C_HEAP_ARRAY(char, udbuf, mtInternal);
414169689Skan    FREE_C_HEAP_ARRAY(char, usrdir_name, mtInternal);
415169689Skan  }
416169689Skan  os::closedir(tmpdirp);
417169689Skan  FREE_C_HEAP_ARRAY(char, tdbuf, mtInternal);
418169689Skan
419169689Skan  return(oldest_user);
420169689Skan}
421169689Skan
422169689Skan// return the name of the user that owns the JVM indicated by the given vmid.
423169689Skan//
424169689Skanstatic char* get_user_name(int vmid, TRAPS) {
425169689Skan  return get_user_name_slow(vmid, THREAD);
426169689Skan}
427169689Skan
428169689Skan// return the file name of the backing store file for the named
429169689Skan// shared memory region for the given user name and vmid.
430169689Skan//
431169689Skan// the caller is expected to free the allocated memory.
432169689Skan//
433169689Skanstatic char* get_sharedmem_filename(const char* dirname, int vmid) {
434169689Skan
435169689Skan  // add 2 for the file separator and a null terminator.
436169689Skan  size_t nbytes = strlen(dirname) + UINT_CHARS + 2;
437169689Skan
438169689Skan  char* name = NEW_C_HEAP_ARRAY(char, nbytes, mtInternal);
439169689Skan  snprintf(name, nbytes, "%s/%d", dirname, vmid);
440169689Skan
441169689Skan  return name;
442169689Skan}
443169689Skan
444169689Skan
445169689Skan// remove file
446169689Skan//
447169689Skan// this method removes the file specified by the given path
448169689Skan//
449169689Skanstatic void remove_file(const char* path) {
450169689Skan
451169689Skan  int result;
452169689Skan
453169689Skan  // if the file is a directory, the following unlink will fail. since
454169689Skan  // we don't expect to find directories in the user temp directory, we
455169689Skan  // won't try to handle this situation. even if accidentially or
456169689Skan  // maliciously planted, the directory's presence won't hurt anything.
457169689Skan  //
458169689Skan  RESTARTABLE(::unlink(path), result);
459169689Skan  if (PrintMiscellaneous && Verbose && result == OS_ERR) {
460169689Skan    if (errno != ENOENT) {
461169689Skan      warning("Could not unlink shared memory backing"
462169689Skan              " store file %s : %s\n", path, strerror(errno));
463169689Skan    }
464169689Skan  }
465169689Skan}
466169689Skan
467169689Skan
468169689Skan// remove file
469169689Skan//
470169689Skan// this method removes the file with the given file name in the
471169689Skan// named directory.
472169689Skan//
473169689Skanstatic void remove_file(const char* dirname, const char* filename) {
474169689Skan
475169689Skan  size_t nbytes = strlen(dirname) + strlen(filename) + 2;
476169689Skan  char* path = NEW_C_HEAP_ARRAY(char, nbytes, mtInternal);
477169689Skan
478169689Skan  strcpy(path, dirname);
479169689Skan  strcat(path, "/");
480169689Skan  strcat(path, filename);
481169689Skan
482169689Skan  remove_file(path);
483169689Skan
484169689Skan  FREE_C_HEAP_ARRAY(char, path, mtInternal);
485169689Skan}
486169689Skan
487169689Skan
488169689Skan// cleanup stale shared memory resources
489169689Skan//
490169689Skan// This method attempts to remove all stale shared memory files in
491169689Skan// the named user temporary directory. It scans the named directory
492169689Skan// for files matching the pattern ^$[0-9]*$. For each file found, the
493169689Skan// process id is extracted from the file name and a test is run to
494169689Skan// determine if the process is alive. If the process is not alive,
495169689Skan// any stale file resources are removed.
496169689Skan//
497169689Skanstatic void cleanup_sharedmem_resources(const char* dirname) {
498169689Skan
499169689Skan  // open the user temp directory
500169689Skan  DIR* dirp = os::opendir(dirname);
501169689Skan
502169689Skan  if (dirp == NULL) {
503169689Skan    // directory doesn't exist, so there is nothing to cleanup
504169689Skan    return;
505169689Skan  }
506169689Skan
507169689Skan  if (!is_directory_secure(dirname)) {
508169689Skan    // the directory is not a secure directory
509169689Skan    return;
510169689Skan  }
511169689Skan
512169689Skan  // for each entry in the directory that matches the expected file
513169689Skan  // name pattern, determine if the file resources are stale and if
514169689Skan  // so, remove the file resources. Note, instrumented HotSpot processes
515169689Skan  // for this user may start and/or terminate during this search and
516169689Skan  // remove or create new files in this directory. The behavior of this
517169689Skan  // loop under these conditions is dependent upon the implementation of
518169689Skan  // opendir/readdir.
519169689Skan  //
520169689Skan  struct dirent* entry;
521169689Skan  char* dbuf = NEW_C_HEAP_ARRAY(char, os::readdir_buf_size(dirname), mtInternal);
522169689Skan  errno = 0;
523169689Skan  while ((entry = os::readdir(dirp, (struct dirent *)dbuf)) != NULL) {
524169689Skan
525169689Skan    pid_t pid = filename_to_pid(entry->d_name);
526169689Skan
527169689Skan    if (pid == 0) {
528169689Skan
529169689Skan      if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) {
530169689Skan
531169689Skan        // attempt to remove all unexpected files, except "." and ".."
532169689Skan        remove_file(dirname, entry->d_name);
533169689Skan      }
534169689Skan
535169689Skan      errno = 0;
536169689Skan      continue;
537169689Skan    }
538169689Skan
539169689Skan    // we now have a file name that converts to a valid integer
540169689Skan    // that could represent a process id . if this process id
541169689Skan    // matches the current process id or the process is not running,
542169689Skan    // then remove the stale file resources.
543169689Skan    //
544169689Skan    // process liveness is detected by sending signal number 0 to
545169689Skan    // the process id (see kill(2)). if kill determines that the
546169689Skan    // process does not exist, then the file resources are removed.
547169689Skan    // if kill determines that that we don't have permission to
548169689Skan    // signal the process, then the file resources are assumed to
549169689Skan    // be stale and are removed because the resources for such a
550169689Skan    // process should be in a different user specific directory.
551169689Skan    //
552169689Skan    if ((pid == os::current_process_id()) ||
553169689Skan        (kill(pid, 0) == OS_ERR && (errno == ESRCH || errno == EPERM))) {
554169689Skan
555169689Skan        remove_file(dirname, entry->d_name);
556169689Skan    }
557169689Skan    errno = 0;
558169689Skan  }
559169689Skan  os::closedir(dirp);
560169689Skan  FREE_C_HEAP_ARRAY(char, dbuf, mtInternal);
561169689Skan}
562169689Skan
563169689Skan// make the user specific temporary directory. Returns true if
564169689Skan// the directory exists and is secure upon return. Returns false
565169689Skan// if the directory exists but is either a symlink, is otherwise
566169689Skan// insecure, or if an error occurred.
567169689Skan//
568169689Skanstatic bool make_user_tmp_dir(const char* dirname) {
569169689Skan
570169689Skan  // create the directory with 0755 permissions. note that the directory
571169689Skan  // will be owned by euid::egid, which may not be the same as uid::gid.
572169689Skan  //
573  if (mkdir(dirname, S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH) == OS_ERR) {
574    if (errno == EEXIST) {
575      // The directory already exists and was probably created by another
576      // JVM instance. However, this could also be the result of a
577      // deliberate symlink. Verify that the existing directory is safe.
578      //
579      if (!is_directory_secure(dirname)) {
580        // directory is not secure
581        if (PrintMiscellaneous && Verbose) {
582          warning("%s directory is insecure\n", dirname);
583        }
584        return false;
585      }
586    }
587    else {
588      // we encountered some other failure while attempting
589      // to create the directory
590      //
591      if (PrintMiscellaneous && Verbose) {
592        warning("could not create directory %s: %s\n",
593                dirname, strerror(errno));
594      }
595      return false;
596    }
597  }
598  return true;
599}
600
601// create the shared memory file resources
602//
603// This method creates the shared memory file with the given size
604// This method also creates the user specific temporary directory, if
605// it does not yet exist.
606//
607static int create_sharedmem_resources(const char* dirname, const char* filename, size_t size) {
608
609  // make the user temporary directory
610  if (!make_user_tmp_dir(dirname)) {
611    // could not make/find the directory or the found directory
612    // was not secure
613    return -1;
614  }
615
616  int result;
617
618  RESTARTABLE(::open(filename, O_RDWR|O_CREAT|O_TRUNC, S_IREAD|S_IWRITE), result);
619  if (result == OS_ERR) {
620    if (PrintMiscellaneous && Verbose) {
621      warning("could not create file %s: %s\n", filename, strerror(errno));
622    }
623    return -1;
624  }
625
626  // save the file descriptor
627  int fd = result;
628
629  // set the file size
630  RESTARTABLE(::ftruncate(fd, (off_t)size), result);
631  if (result == OS_ERR) {
632    if (PrintMiscellaneous && Verbose) {
633      warning("could not set shared memory file size: %s\n", strerror(errno));
634    }
635    ::close(fd);
636    return -1;
637  }
638
639  // Verify that we have enough disk space for this file.
640  // We'll get random SIGBUS crashes on memory accesses if
641  // we don't.
642
643  for (size_t seekpos = 0; seekpos < size; seekpos += os::vm_page_size()) {
644    int zero_int = 0;
645    result = (int)os::seek_to_file_offset(fd, (jlong)(seekpos));
646    if (result == -1 ) break;
647    RESTARTABLE(::write(fd, &zero_int, 1), result);
648    if (result != 1) {
649      if (errno == ENOSPC) {
650        warning("Insufficient space for shared memory file:\n   %s\nTry using the -Djava.io.tmpdir= option to select an alternate temp location.\n", filename);
651      }
652      break;
653    }
654  }
655
656  if (result != -1) {
657    return fd;
658  } else {
659    ::close(fd);
660    return -1;
661  }
662}
663
664// open the shared memory file for the given user and vmid. returns
665// the file descriptor for the open file or -1 if the file could not
666// be opened.
667//
668static int open_sharedmem_file(const char* filename, int oflags, TRAPS) {
669
670  // open the file
671  int result;
672  RESTARTABLE(::open(filename, oflags), result);
673  if (result == OS_ERR) {
674    if (errno == ENOENT) {
675      THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(),
676                  "Process not found", OS_ERR);
677    }
678    else if (errno == EACCES) {
679      THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(),
680                  "Permission denied", OS_ERR);
681    }
682    else {
683      THROW_MSG_(vmSymbols::java_io_IOException(), strerror(errno), OS_ERR);
684    }
685  }
686
687  return result;
688}
689
690// create a named shared memory region. returns the address of the
691// memory region on success or NULL on failure. A return value of
692// NULL will ultimately disable the shared memory feature.
693//
694// On Solaris and Linux, the name space for shared memory objects
695// is the file system name space.
696//
697// A monitoring application attaching to a JVM does not need to know
698// the file system name of the shared memory object. However, it may
699// be convenient for applications to discover the existence of newly
700// created and terminating JVMs by watching the file system name space
701// for files being created or removed.
702//
703static char* mmap_create_shared(size_t size) {
704
705  int result;
706  int fd;
707  char* mapAddress;
708
709  int vmid = os::current_process_id();
710
711  char* user_name = get_user_name(geteuid());
712
713  if (user_name == NULL)
714    return NULL;
715
716  char* dirname = get_user_tmp_dir(user_name);
717  char* filename = get_sharedmem_filename(dirname, vmid);
718
719  // cleanup any stale shared memory files
720  cleanup_sharedmem_resources(dirname);
721
722  assert(((size > 0) && (size % os::vm_page_size() == 0)),
723         "unexpected PerfMemory region size");
724
725  fd = create_sharedmem_resources(dirname, filename, size);
726
727  FREE_C_HEAP_ARRAY(char, user_name, mtInternal);
728  FREE_C_HEAP_ARRAY(char, dirname, mtInternal);
729
730  if (fd == -1) {
731    FREE_C_HEAP_ARRAY(char, filename, mtInternal);
732    return NULL;
733  }
734
735  mapAddress = (char*)::mmap((char*)0, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
736
737  result = ::close(fd);
738  assert(result != OS_ERR, "could not close file");
739
740  if (mapAddress == MAP_FAILED) {
741    if (PrintMiscellaneous && Verbose) {
742      warning("mmap failed -  %s\n", strerror(errno));
743    }
744    remove_file(filename);
745    FREE_C_HEAP_ARRAY(char, filename, mtInternal);
746    return NULL;
747  }
748
749  // save the file name for use in delete_shared_memory()
750  backing_store_file_name = filename;
751
752  // clear the shared memory region
753  (void)::memset((void*) mapAddress, 0, size);
754
755  // it does not go through os api, the operation has to record from here
756  MemTracker::record_virtual_memory_reserve_and_commit((address)mapAddress, size, CURRENT_PC, mtInternal);
757
758  return mapAddress;
759}
760
761// release a named shared memory region
762//
763static void unmap_shared(char* addr, size_t bytes) {
764  os::release_memory(addr, bytes);
765}
766
767// create the PerfData memory region in shared memory.
768//
769static char* create_shared_memory(size_t size) {
770
771  // create the shared memory region.
772  return mmap_create_shared(size);
773}
774
775// delete the shared PerfData memory region
776//
777static void delete_shared_memory(char* addr, size_t size) {
778
779  // cleanup the persistent shared memory resources. since DestroyJavaVM does
780  // not support unloading of the JVM, unmapping of the memory resource is
781  // not performed. The memory will be reclaimed by the OS upon termination of
782  // the process. The backing store file is deleted from the file system.
783
784  assert(!PerfDisableSharedMem, "shouldn't be here");
785
786  if (backing_store_file_name != NULL) {
787    remove_file(backing_store_file_name);
788    // Don't.. Free heap memory could deadlock os::abort() if it is called
789    // from signal handler. OS will reclaim the heap memory.
790    // FREE_C_HEAP_ARRAY(char, backing_store_file_name);
791    backing_store_file_name = NULL;
792  }
793}
794
795// return the size of the file for the given file descriptor
796// or 0 if it is not a valid size for a shared memory file
797//
798static size_t sharedmem_filesize(int fd, TRAPS) {
799
800  struct stat statbuf;
801  int result;
802
803  RESTARTABLE(::fstat(fd, &statbuf), result);
804  if (result == OS_ERR) {
805    if (PrintMiscellaneous && Verbose) {
806      warning("fstat failed: %s\n", strerror(errno));
807    }
808    THROW_MSG_0(vmSymbols::java_io_IOException(),
809                "Could not determine PerfMemory size");
810  }
811
812  if ((statbuf.st_size == 0) ||
813     ((size_t)statbuf.st_size % os::vm_page_size() != 0)) {
814    THROW_MSG_0(vmSymbols::java_lang_Exception(),
815                "Invalid PerfMemory size");
816  }
817
818  return (size_t)statbuf.st_size;
819}
820
821// attach to a named shared memory region.
822//
823static void mmap_attach_shared(const char* user, int vmid, PerfMemory::PerfMemoryMode mode, char** addr, size_t* sizep, TRAPS) {
824
825  char* mapAddress;
826  int result;
827  int fd;
828  size_t size = 0;
829  const char* luser = NULL;
830
831  int mmap_prot;
832  int file_flags;
833
834  ResourceMark rm;
835
836  // map the high level access mode to the appropriate permission
837  // constructs for the file and the shared memory mapping.
838  if (mode == PerfMemory::PERF_MODE_RO) {
839    mmap_prot = PROT_READ;
840    file_flags = O_RDONLY;
841  }
842  else if (mode == PerfMemory::PERF_MODE_RW) {
843#ifdef LATER
844    mmap_prot = PROT_READ | PROT_WRITE;
845    file_flags = O_RDWR;
846#else
847    THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
848              "Unsupported access mode");
849#endif
850  }
851  else {
852    THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
853              "Illegal access mode");
854  }
855
856  if (user == NULL || strlen(user) == 0) {
857    luser = get_user_name(vmid, CHECK);
858  }
859  else {
860    luser = user;
861  }
862
863  if (luser == NULL) {
864    THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
865              "Could not map vmid to user Name");
866  }
867
868  char* dirname = get_user_tmp_dir(luser);
869
870  // since we don't follow symbolic links when creating the backing
871  // store file, we don't follow them when attaching either.
872  //
873  if (!is_directory_secure(dirname)) {
874    FREE_C_HEAP_ARRAY(char, dirname, mtInternal);
875    THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
876              "Process not found");
877  }
878
879  char* filename = get_sharedmem_filename(dirname, vmid);
880
881  // copy heap memory to resource memory. the open_sharedmem_file
882  // method below need to use the filename, but could throw an
883  // exception. using a resource array prevents the leak that
884  // would otherwise occur.
885  char* rfilename = NEW_RESOURCE_ARRAY(char, strlen(filename) + 1);
886  strcpy(rfilename, filename);
887
888  // free the c heap resources that are no longer needed
889  if (luser != user) FREE_C_HEAP_ARRAY(char, luser, mtInternal);
890  FREE_C_HEAP_ARRAY(char, dirname, mtInternal);
891  FREE_C_HEAP_ARRAY(char, filename, mtInternal);
892
893  // open the shared memory file for the give vmid
894  fd = open_sharedmem_file(rfilename, file_flags, THREAD);
895
896  if (fd == OS_ERR) {
897    return;
898  }
899
900  if (HAS_PENDING_EXCEPTION) {
901    ::close(fd);
902    return;
903  }
904
905  if (*sizep == 0) {
906    size = sharedmem_filesize(fd, CHECK);
907  } else {
908    size = *sizep;
909  }
910
911  assert(size > 0, "unexpected size <= 0");
912
913  mapAddress = (char*)::mmap((char*)0, size, mmap_prot, MAP_SHARED, fd, 0);
914
915  result = ::close(fd);
916  assert(result != OS_ERR, "could not close file");
917
918  if (mapAddress == MAP_FAILED) {
919    if (PrintMiscellaneous && Verbose) {
920      warning("mmap failed: %s\n", strerror(errno));
921    }
922    THROW_MSG(vmSymbols::java_lang_OutOfMemoryError(),
923              "Could not map PerfMemory");
924  }
925
926  // it does not go through os api, the operation has to record from here
927  MemTracker::record_virtual_memory_reserve_and_commit((address)mapAddress, size, CURRENT_PC, mtInternal);
928
929  *addr = mapAddress;
930  *sizep = size;
931
932  if (PerfTraceMemOps) {
933    tty->print("mapped " SIZE_FORMAT " bytes for vmid %d at "
934               INTPTR_FORMAT "\n", size, vmid, p2i((void*)mapAddress));
935  }
936}
937
938
939
940
941// create the PerfData memory region
942//
943// This method creates the memory region used to store performance
944// data for the JVM. The memory may be created in standard or
945// shared memory.
946//
947void PerfMemory::create_memory_region(size_t size) {
948
949  if (PerfDisableSharedMem) {
950    // do not share the memory for the performance data.
951    _start = create_standard_memory(size);
952  }
953  else {
954    _start = create_shared_memory(size);
955    if (_start == NULL) {
956
957      // creation of the shared memory region failed, attempt
958      // to create a contiguous, non-shared memory region instead.
959      //
960      if (PrintMiscellaneous && Verbose) {
961        warning("Reverting to non-shared PerfMemory region.\n");
962      }
963      PerfDisableSharedMem = true;
964      _start = create_standard_memory(size);
965    }
966  }
967
968  if (_start != NULL) _capacity = size;
969
970}
971
972// delete the PerfData memory region
973//
974// This method deletes the memory region used to store performance
975// data for the JVM. The memory region indicated by the <address, size>
976// tuple will be inaccessible after a call to this method.
977//
978void PerfMemory::delete_memory_region() {
979
980  assert((start() != NULL && capacity() > 0), "verify proper state");
981
982  // If user specifies PerfDataSaveFile, it will save the performance data
983  // to the specified file name no matter whether PerfDataSaveToFile is specified
984  // or not. In other word, -XX:PerfDataSaveFile=.. overrides flag
985  // -XX:+PerfDataSaveToFile.
986  if (PerfDataSaveToFile || PerfDataSaveFile != NULL) {
987    save_memory_to_file(start(), capacity());
988  }
989
990  if (PerfDisableSharedMem) {
991    delete_standard_memory(start(), capacity());
992  }
993  else {
994    delete_shared_memory(start(), capacity());
995  }
996}
997
998// attach to the PerfData memory region for another JVM
999//
1000// This method returns an <address, size> tuple that points to
1001// a memory buffer that is kept reasonably synchronized with
1002// the PerfData memory region for the indicated JVM. This
1003// buffer may be kept in synchronization via shared memory
1004// or some other mechanism that keeps the buffer updated.
1005//
1006// If the JVM chooses not to support the attachability feature,
1007// this method should throw an UnsupportedOperation exception.
1008//
1009// This implementation utilizes named shared memory to map
1010// the indicated process's PerfData memory region into this JVMs
1011// address space.
1012//
1013void PerfMemory::attach(const char* user, int vmid, PerfMemoryMode mode, char** addrp, size_t* sizep, TRAPS) {
1014
1015  if (vmid == 0 || vmid == os::current_process_id()) {
1016     *addrp = start();
1017     *sizep = capacity();
1018     return;
1019  }
1020
1021  mmap_attach_shared(user, vmid, mode, addrp, sizep, CHECK);
1022}
1023
1024// detach from the PerfData memory region of another JVM
1025//
1026// This method detaches the PerfData memory region of another
1027// JVM, specified as an <address, size> tuple of a buffer
1028// in this process's address space. This method may perform
1029// arbitrary actions to accomplish the detachment. The memory
1030// region specified by <address, size> will be inaccessible after
1031// a call to this method.
1032//
1033// If the JVM chooses not to support the attachability feature,
1034// this method should throw an UnsupportedOperation exception.
1035//
1036// This implementation utilizes named shared memory to detach
1037// the indicated process's PerfData memory region from this
1038// process's address space.
1039//
1040void PerfMemory::detach(char* addr, size_t bytes, TRAPS) {
1041
1042  assert(addr != 0, "address sanity check");
1043  assert(bytes > 0, "capacity sanity check");
1044
1045  if (PerfMemory::contains(addr) || PerfMemory::contains(addr + bytes - 1)) {
1046    // prevent accidental detachment of this process's PerfMemory region
1047    return;
1048  }
1049
1050  unmap_shared(addr, bytes);
1051}
1052