0
|
1 /*
|
|
2 * Copyright 2001-2007 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
20 * CA 95054 USA or visit www.sun.com if you need additional information or
|
|
21 * have any questions.
|
|
22 *
|
|
23 */
|
|
24
|
|
25 # include "incls/_precompiled.incl"
|
|
26 # include "incls/_perfMemory_solaris.cpp.incl"
|
|
27
|
|
28 // put OS-includes here
|
|
29 # include <sys/types.h>
|
|
30 # include <sys/mman.h>
|
|
31 # include <errno.h>
|
|
32 # include <stdio.h>
|
|
33 # include <unistd.h>
|
|
34 # include <sys/stat.h>
|
|
35 # include <signal.h>
|
|
36 # include <pwd.h>
|
|
37 # include <procfs.h>
|
|
38
|
|
39
|
|
40 static char* backing_store_file_name = NULL; // name of the backing store
|
|
41 // file, if successfully created.
|
|
42
|
|
43 // Standard Memory Implementation Details
|
|
44
|
|
45 // create the PerfData memory region in standard memory.
|
|
46 //
|
|
47 static char* create_standard_memory(size_t size) {
|
|
48
|
|
49 // allocate an aligned chuck of memory
|
|
50 char* mapAddress = os::reserve_memory(size);
|
|
51
|
|
52 if (mapAddress == NULL) {
|
|
53 return NULL;
|
|
54 }
|
|
55
|
|
56 // commit memory
|
|
57 if (!os::commit_memory(mapAddress, size)) {
|
|
58 if (PrintMiscellaneous && Verbose) {
|
|
59 warning("Could not commit PerfData memory\n");
|
|
60 }
|
|
61 os::release_memory(mapAddress, size);
|
|
62 return NULL;
|
|
63 }
|
|
64
|
|
65 return mapAddress;
|
|
66 }
|
|
67
|
|
68 // delete the PerfData memory region
|
|
69 //
|
|
70 static void delete_standard_memory(char* addr, size_t size) {
|
|
71
|
|
72 // there are no persistent external resources to cleanup for standard
|
|
73 // memory. since DestroyJavaVM does not support unloading of the JVM,
|
|
74 // cleanup of the memory resource is not performed. The memory will be
|
|
75 // reclaimed by the OS upon termination of the process.
|
|
76 //
|
|
77 return;
|
|
78 }
|
|
79
|
|
80 // save the specified memory region to the given file
|
|
81 //
|
|
82 // Note: this function might be called from signal handler (by os::abort()),
|
|
83 // don't allocate heap memory.
|
|
84 //
|
|
85 static void save_memory_to_file(char* addr, size_t size) {
|
|
86
|
|
87 const char* destfile = PerfMemory::get_perfdata_file_path();
|
|
88 assert(destfile[0] != '\0', "invalid PerfData file path");
|
|
89
|
|
90 int result;
|
|
91
|
|
92 RESTARTABLE(::open(destfile, O_CREAT|O_WRONLY|O_TRUNC, S_IREAD|S_IWRITE),
|
|
93 result);;
|
|
94 if (result == OS_ERR) {
|
|
95 if (PrintMiscellaneous && Verbose) {
|
|
96 warning("Could not create Perfdata save file: %s: %s\n",
|
|
97 destfile, strerror(errno));
|
|
98 }
|
|
99 } else {
|
|
100
|
|
101 int fd = result;
|
|
102
|
|
103 for (size_t remaining = size; remaining > 0;) {
|
|
104
|
|
105 RESTARTABLE(::write(fd, addr, remaining), result);
|
|
106 if (result == OS_ERR) {
|
|
107 if (PrintMiscellaneous && Verbose) {
|
|
108 warning("Could not write Perfdata save file: %s: %s\n",
|
|
109 destfile, strerror(errno));
|
|
110 }
|
|
111 break;
|
|
112 }
|
|
113 remaining -= (size_t)result;
|
|
114 addr += result;
|
|
115 }
|
|
116
|
|
117 RESTARTABLE(::close(fd), result);
|
|
118 if (PrintMiscellaneous && Verbose) {
|
|
119 if (result == OS_ERR) {
|
|
120 warning("Could not close %s: %s\n", destfile, strerror(errno));
|
|
121 }
|
|
122 }
|
|
123 }
|
|
124 FREE_C_HEAP_ARRAY(char, destfile);
|
|
125 }
|
|
126
|
|
127
|
|
128 // Shared Memory Implementation Details
|
|
129
|
|
130 // Note: the solaris and linux shared memory implementation uses the mmap
|
|
131 // interface with a backing store file to implement named shared memory.
|
|
132 // Using the file system as the name space for shared memory allows a
|
|
133 // common name space to be supported across a variety of platforms. It
|
|
134 // also provides a name space that Java applications can deal with through
|
|
135 // simple file apis.
|
|
136 //
|
|
137 // The solaris and linux implementations store the backing store file in
|
|
138 // a user specific temporary directory located in the /tmp file system,
|
|
139 // which is always a local file system and is sometimes a RAM based file
|
|
140 // system.
|
|
141
|
|
142 // return the user specific temporary directory name.
|
|
143 //
|
|
144 // the caller is expected to free the allocated memory.
|
|
145 //
|
|
146 static char* get_user_tmp_dir(const char* user) {
|
|
147
|
|
148 const char* tmpdir = os::get_temp_directory();
|
|
149 const char* perfdir = PERFDATA_NAME;
|
|
150 size_t nbytes = strlen(tmpdir) + strlen(perfdir) + strlen(user) + 2;
|
|
151 char* dirname = NEW_C_HEAP_ARRAY(char, nbytes);
|
|
152
|
|
153 // construct the path name to user specific tmp directory
|
|
154 snprintf(dirname, nbytes, "%s%s_%s", tmpdir, perfdir, user);
|
|
155
|
|
156 return dirname;
|
|
157 }
|
|
158
|
|
159 // convert the given file name into a process id. if the file
|
|
160 // does not meet the file naming constraints, return 0.
|
|
161 //
|
|
162 static pid_t filename_to_pid(const char* filename) {
|
|
163
|
|
164 // a filename that doesn't begin with a digit is not a
|
|
165 // candidate for conversion.
|
|
166 //
|
|
167 if (!isdigit(*filename)) {
|
|
168 return 0;
|
|
169 }
|
|
170
|
|
171 // check if file name can be converted to an integer without
|
|
172 // any leftover characters.
|
|
173 //
|
|
174 char* remainder = NULL;
|
|
175 errno = 0;
|
|
176 pid_t pid = (pid_t)strtol(filename, &remainder, 10);
|
|
177
|
|
178 if (errno != 0) {
|
|
179 return 0;
|
|
180 }
|
|
181
|
|
182 // check for left over characters. If any, then the filename is
|
|
183 // not a candidate for conversion.
|
|
184 //
|
|
185 if (remainder != NULL && *remainder != '\0') {
|
|
186 return 0;
|
|
187 }
|
|
188
|
|
189 // successful conversion, return the pid
|
|
190 return pid;
|
|
191 }
|
|
192
|
|
193
|
|
194 // check if the given path is considered a secure directory for
|
|
195 // the backing store files. Returns true if the directory exists
|
|
196 // and is considered a secure location. Returns false if the path
|
605
|
197 // is a symbolic link or if an error occurred.
|
0
|
198 //
|
|
199 static bool is_directory_secure(const char* path) {
|
|
200 struct stat statbuf;
|
|
201 int result = 0;
|
|
202
|
|
203 RESTARTABLE(::lstat(path, &statbuf), result);
|
|
204 if (result == OS_ERR) {
|
|
205 return false;
|
|
206 }
|
|
207
|
|
208 // the path exists, now check it's mode
|
|
209 if (S_ISLNK(statbuf.st_mode) || !S_ISDIR(statbuf.st_mode)) {
|
|
210 // the path represents a link or some non-directory file type,
|
|
211 // which is not what we expected. declare it insecure.
|
|
212 //
|
|
213 return false;
|
|
214 }
|
|
215 else {
|
|
216 // we have an existing directory, check if the permissions are safe.
|
|
217 //
|
|
218 if ((statbuf.st_mode & (S_IWGRP|S_IWOTH)) != 0) {
|
|
219 // the directory is open for writing and could be subjected
|
|
220 // to a symlnk attack. declare it insecure.
|
|
221 //
|
|
222 return false;
|
|
223 }
|
|
224 }
|
|
225 return true;
|
|
226 }
|
|
227
|
|
228
|
|
229 // return the user name for the given user id
|
|
230 //
|
|
231 // the caller is expected to free the allocated memory.
|
|
232 //
|
|
233 static char* get_user_name(uid_t uid) {
|
|
234
|
|
235 struct passwd pwent;
|
|
236
|
|
237 // determine the max pwbuf size from sysconf, and hardcode
|
|
238 // a default if this not available through sysconf.
|
|
239 //
|
|
240 long bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
|
|
241 if (bufsize == -1)
|
|
242 bufsize = 1024;
|
|
243
|
|
244 char* pwbuf = NEW_C_HEAP_ARRAY(char, bufsize);
|
|
245
|
|
246 #ifdef _GNU_SOURCE
|
|
247 struct passwd* p = NULL;
|
|
248 int result = getpwuid_r(uid, &pwent, pwbuf, (size_t)bufsize, &p);
|
|
249 #else // _GNU_SOURCE
|
|
250 struct passwd* p = getpwuid_r(uid, &pwent, pwbuf, (int)bufsize);
|
|
251 #endif // _GNU_SOURCE
|
|
252
|
|
253 if (p == NULL || p->pw_name == NULL || *(p->pw_name) == '\0') {
|
|
254 if (PrintMiscellaneous && Verbose) {
|
|
255 if (p == NULL) {
|
|
256 warning("Could not retrieve passwd entry: %s\n",
|
|
257 strerror(errno));
|
|
258 }
|
|
259 else {
|
|
260 warning("Could not determine user name: %s\n",
|
|
261 p->pw_name == NULL ? "pw_name = NULL" :
|
|
262 "pw_name zero length");
|
|
263 }
|
|
264 }
|
|
265 FREE_C_HEAP_ARRAY(char, pwbuf);
|
|
266 return NULL;
|
|
267 }
|
|
268
|
|
269 char* user_name = NEW_C_HEAP_ARRAY(char, strlen(p->pw_name) + 1);
|
|
270 strcpy(user_name, p->pw_name);
|
|
271
|
|
272 FREE_C_HEAP_ARRAY(char, pwbuf);
|
|
273 return user_name;
|
|
274 }
|
|
275
|
|
276 // return the name of the user that owns the process identified by vmid.
|
|
277 //
|
|
278 // This method uses a slow directory search algorithm to find the backing
|
|
279 // store file for the specified vmid and returns the user name, as determined
|
|
280 // by the user name suffix of the hsperfdata_<username> directory name.
|
|
281 //
|
|
282 // the caller is expected to free the allocated memory.
|
|
283 //
|
|
284 static char* get_user_name_slow(int vmid, TRAPS) {
|
|
285
|
|
286 // short circuit the directory search if the process doesn't even exist.
|
|
287 if (kill(vmid, 0) == OS_ERR) {
|
|
288 if (errno == ESRCH) {
|
|
289 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(),
|
|
290 "Process not found");
|
|
291 }
|
|
292 else /* EPERM */ {
|
|
293 THROW_MSG_0(vmSymbols::java_io_IOException(), strerror(errno));
|
|
294 }
|
|
295 }
|
|
296
|
|
297 // directory search
|
|
298 char* oldest_user = NULL;
|
|
299 time_t oldest_ctime = 0;
|
|
300
|
|
301 const char* tmpdirname = os::get_temp_directory();
|
|
302
|
|
303 DIR* tmpdirp = os::opendir(tmpdirname);
|
|
304
|
|
305 if (tmpdirp == NULL) {
|
|
306 return NULL;
|
|
307 }
|
|
308
|
|
309 // for each entry in the directory that matches the pattern hsperfdata_*,
|
|
310 // open the directory and check if the file for the given vmid exists.
|
|
311 // The file with the expected name and the latest creation date is used
|
|
312 // to determine the user name for the process id.
|
|
313 //
|
|
314 struct dirent* dentry;
|
|
315 char* tdbuf = NEW_C_HEAP_ARRAY(char, os::readdir_buf_size(tmpdirname));
|
|
316 errno = 0;
|
|
317 while ((dentry = os::readdir(tmpdirp, (struct dirent *)tdbuf)) != NULL) {
|
|
318
|
|
319 // check if the directory entry is a hsperfdata file
|
|
320 if (strncmp(dentry->d_name, PERFDATA_NAME, strlen(PERFDATA_NAME)) != 0) {
|
|
321 continue;
|
|
322 }
|
|
323
|
|
324 char* usrdir_name = NEW_C_HEAP_ARRAY(char,
|
|
325 strlen(tmpdirname) + strlen(dentry->d_name) + 1);
|
|
326 strcpy(usrdir_name, tmpdirname);
|
|
327 strcat(usrdir_name, dentry->d_name);
|
|
328
|
|
329 DIR* subdirp = os::opendir(usrdir_name);
|
|
330
|
|
331 if (subdirp == NULL) {
|
|
332 FREE_C_HEAP_ARRAY(char, usrdir_name);
|
|
333 continue;
|
|
334 }
|
|
335
|
|
336 // Since we don't create the backing store files in directories
|
|
337 // pointed to by symbolic links, we also don't follow them when
|
|
338 // looking for the files. We check for a symbolic link after the
|
|
339 // call to opendir in order to eliminate a small window where the
|
|
340 // symlink can be exploited.
|
|
341 //
|
|
342 if (!is_directory_secure(usrdir_name)) {
|
|
343 FREE_C_HEAP_ARRAY(char, usrdir_name);
|
|
344 os::closedir(subdirp);
|
|
345 continue;
|
|
346 }
|
|
347
|
|
348 struct dirent* udentry;
|
|
349 char* udbuf = NEW_C_HEAP_ARRAY(char, os::readdir_buf_size(usrdir_name));
|
|
350 errno = 0;
|
|
351 while ((udentry = os::readdir(subdirp, (struct dirent *)udbuf)) != NULL) {
|
|
352
|
|
353 if (filename_to_pid(udentry->d_name) == vmid) {
|
|
354 struct stat statbuf;
|
|
355 int result;
|
|
356
|
|
357 char* filename = NEW_C_HEAP_ARRAY(char,
|
|
358 strlen(usrdir_name) + strlen(udentry->d_name) + 2);
|
|
359
|
|
360 strcpy(filename, usrdir_name);
|
|
361 strcat(filename, "/");
|
|
362 strcat(filename, udentry->d_name);
|
|
363
|
|
364 // don't follow symbolic links for the file
|
|
365 RESTARTABLE(::lstat(filename, &statbuf), result);
|
|
366 if (result == OS_ERR) {
|
|
367 FREE_C_HEAP_ARRAY(char, filename);
|
|
368 continue;
|
|
369 }
|
|
370
|
|
371 // skip over files that are not regular files.
|
|
372 if (!S_ISREG(statbuf.st_mode)) {
|
|
373 FREE_C_HEAP_ARRAY(char, filename);
|
|
374 continue;
|
|
375 }
|
|
376
|
|
377 // compare and save filename with latest creation time
|
|
378 if (statbuf.st_size > 0 && statbuf.st_ctime > oldest_ctime) {
|
|
379
|
|
380 if (statbuf.st_ctime > oldest_ctime) {
|
|
381 char* user = strchr(dentry->d_name, '_') + 1;
|
|
382
|
|
383 if (oldest_user != NULL) FREE_C_HEAP_ARRAY(char, oldest_user);
|
|
384 oldest_user = NEW_C_HEAP_ARRAY(char, strlen(user)+1);
|
|
385
|
|
386 strcpy(oldest_user, user);
|
|
387 oldest_ctime = statbuf.st_ctime;
|
|
388 }
|
|
389 }
|
|
390
|
|
391 FREE_C_HEAP_ARRAY(char, filename);
|
|
392 }
|
|
393 }
|
|
394 os::closedir(subdirp);
|
|
395 FREE_C_HEAP_ARRAY(char, udbuf);
|
|
396 FREE_C_HEAP_ARRAY(char, usrdir_name);
|
|
397 }
|
|
398 os::closedir(tmpdirp);
|
|
399 FREE_C_HEAP_ARRAY(char, tdbuf);
|
|
400
|
|
401 return(oldest_user);
|
|
402 }
|
|
403
|
|
404 // return the name of the user that owns the JVM indicated by the given vmid.
|
|
405 //
|
|
406 static char* get_user_name(int vmid, TRAPS) {
|
|
407
|
|
408 char psinfo_name[PATH_MAX];
|
|
409 int result;
|
|
410
|
|
411 snprintf(psinfo_name, PATH_MAX, "/proc/%d/psinfo", vmid);
|
|
412
|
|
413 RESTARTABLE(::open(psinfo_name, O_RDONLY), result);
|
|
414
|
|
415 if (result != OS_ERR) {
|
|
416 int fd = result;
|
|
417
|
|
418 psinfo_t psinfo;
|
|
419 char* addr = (char*)&psinfo;
|
|
420
|
|
421 for (size_t remaining = sizeof(psinfo_t); remaining > 0;) {
|
|
422
|
|
423 RESTARTABLE(::read(fd, addr, remaining), result);
|
|
424 if (result == OS_ERR) {
|
|
425 THROW_MSG_0(vmSymbols::java_io_IOException(), "Read error");
|
|
426 }
|
|
427 remaining-=result;
|
|
428 addr+=result;
|
|
429 }
|
|
430
|
|
431 RESTARTABLE(::close(fd), result);
|
|
432
|
|
433 // get the user name for the effective user id of the process
|
|
434 char* user_name = get_user_name(psinfo.pr_euid);
|
|
435
|
|
436 return user_name;
|
|
437 }
|
|
438
|
|
439 if (result == OS_ERR && errno == EACCES) {
|
|
440
|
|
441 // In this case, the psinfo file for the process id existed,
|
|
442 // but we didn't have permission to access it.
|
|
443 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(),
|
|
444 strerror(errno));
|
|
445 }
|
|
446
|
|
447 // at this point, we don't know if the process id itself doesn't
|
|
448 // exist or if the psinfo file doesn't exit. If the psinfo file
|
|
449 // doesn't exist, then we are running on Solaris 2.5.1 or earlier.
|
|
450 // since the structured procfs and old procfs interfaces can't be
|
|
451 // mixed, we attempt to find the file through a directory search.
|
|
452
|
|
453 return get_user_name_slow(vmid, CHECK_NULL);
|
|
454 }
|
|
455
|
|
456 // return the file name of the backing store file for the named
|
|
457 // shared memory region for the given user name and vmid.
|
|
458 //
|
|
459 // the caller is expected to free the allocated memory.
|
|
460 //
|
|
461 static char* get_sharedmem_filename(const char* dirname, int vmid) {
|
|
462
|
|
463 // add 2 for the file separator and a NULL terminator.
|
|
464 size_t nbytes = strlen(dirname) + UINT_CHARS + 2;
|
|
465
|
|
466 char* name = NEW_C_HEAP_ARRAY(char, nbytes);
|
|
467 snprintf(name, nbytes, "%s/%d", dirname, vmid);
|
|
468
|
|
469 return name;
|
|
470 }
|
|
471
|
|
472
|
|
473 // remove file
|
|
474 //
|
|
475 // this method removes the file specified by the given path
|
|
476 //
|
|
477 static void remove_file(const char* path) {
|
|
478
|
|
479 int result;
|
|
480
|
|
481 // if the file is a directory, the following unlink will fail. since
|
|
482 // we don't expect to find directories in the user temp directory, we
|
|
483 // won't try to handle this situation. even if accidentially or
|
|
484 // maliciously planted, the directory's presence won't hurt anything.
|
|
485 //
|
|
486 RESTARTABLE(::unlink(path), result);
|
|
487 if (PrintMiscellaneous && Verbose && result == OS_ERR) {
|
|
488 if (errno != ENOENT) {
|
|
489 warning("Could not unlink shared memory backing"
|
|
490 " store file %s : %s\n", path, strerror(errno));
|
|
491 }
|
|
492 }
|
|
493 }
|
|
494
|
|
495
|
|
496 // remove file
|
|
497 //
|
|
498 // this method removes the file with the given file name in the
|
|
499 // named directory.
|
|
500 //
|
|
501 static void remove_file(const char* dirname, const char* filename) {
|
|
502
|
|
503 size_t nbytes = strlen(dirname) + strlen(filename) + 2;
|
|
504 char* path = NEW_C_HEAP_ARRAY(char, nbytes);
|
|
505
|
|
506 strcpy(path, dirname);
|
|
507 strcat(path, "/");
|
|
508 strcat(path, filename);
|
|
509
|
|
510 remove_file(path);
|
|
511
|
|
512 FREE_C_HEAP_ARRAY(char, path);
|
|
513 }
|
|
514
|
|
515
|
|
516 // cleanup stale shared memory resources
|
|
517 //
|
|
518 // This method attempts to remove all stale shared memory files in
|
|
519 // the named user temporary directory. It scans the named directory
|
|
520 // for files matching the pattern ^$[0-9]*$. For each file found, the
|
|
521 // process id is extracted from the file name and a test is run to
|
|
522 // determine if the process is alive. If the process is not alive,
|
|
523 // any stale file resources are removed.
|
|
524 //
|
|
525 static void cleanup_sharedmem_resources(const char* dirname) {
|
|
526
|
|
527 // open the user temp directory
|
|
528 DIR* dirp = os::opendir(dirname);
|
|
529
|
|
530 if (dirp == NULL) {
|
|
531 // directory doesn't exist, so there is nothing to cleanup
|
|
532 return;
|
|
533 }
|
|
534
|
|
535 if (!is_directory_secure(dirname)) {
|
|
536 // the directory is not a secure directory
|
|
537 return;
|
|
538 }
|
|
539
|
|
540 // for each entry in the directory that matches the expected file
|
|
541 // name pattern, determine if the file resources are stale and if
|
|
542 // so, remove the file resources. Note, instrumented HotSpot processes
|
|
543 // for this user may start and/or terminate during this search and
|
|
544 // remove or create new files in this directory. The behavior of this
|
|
545 // loop under these conditions is dependent upon the implementation of
|
|
546 // opendir/readdir.
|
|
547 //
|
|
548 struct dirent* entry;
|
|
549 char* dbuf = NEW_C_HEAP_ARRAY(char, os::readdir_buf_size(dirname));
|
|
550 errno = 0;
|
|
551 while ((entry = os::readdir(dirp, (struct dirent *)dbuf)) != NULL) {
|
|
552
|
|
553 pid_t pid = filename_to_pid(entry->d_name);
|
|
554
|
|
555 if (pid == 0) {
|
|
556
|
|
557 if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) {
|
|
558
|
|
559 // attempt to remove all unexpected files, except "." and ".."
|
|
560 remove_file(dirname, entry->d_name);
|
|
561 }
|
|
562
|
|
563 errno = 0;
|
|
564 continue;
|
|
565 }
|
|
566
|
|
567 // we now have a file name that converts to a valid integer
|
|
568 // that could represent a process id . if this process id
|
|
569 // matches the current process id or the process is not running,
|
|
570 // then remove the stale file resources.
|
|
571 //
|
|
572 // process liveness is detected by sending signal number 0 to
|
|
573 // the process id (see kill(2)). if kill determines that the
|
|
574 // process does not exist, then the file resources are removed.
|
|
575 // if kill determines that that we don't have permission to
|
|
576 // signal the process, then the file resources are assumed to
|
|
577 // be stale and are removed because the resources for such a
|
|
578 // process should be in a different user specific directory.
|
|
579 //
|
|
580 if ((pid == os::current_process_id()) ||
|
|
581 (kill(pid, 0) == OS_ERR && (errno == ESRCH || errno == EPERM))) {
|
|
582
|
|
583 remove_file(dirname, entry->d_name);
|
|
584 }
|
|
585 errno = 0;
|
|
586 }
|
|
587 os::closedir(dirp);
|
|
588 FREE_C_HEAP_ARRAY(char, dbuf);
|
|
589 }
|
|
590
|
|
591 // make the user specific temporary directory. Returns true if
|
|
592 // the directory exists and is secure upon return. Returns false
|
|
593 // if the directory exists but is either a symlink, is otherwise
|
|
594 // insecure, or if an error occurred.
|
|
595 //
|
|
596 static bool make_user_tmp_dir(const char* dirname) {
|
|
597
|
|
598 // create the directory with 0755 permissions. note that the directory
|
|
599 // will be owned by euid::egid, which may not be the same as uid::gid.
|
|
600 //
|
|
601 if (mkdir(dirname, S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH) == OS_ERR) {
|
|
602 if (errno == EEXIST) {
|
|
603 // The directory already exists and was probably created by another
|
|
604 // JVM instance. However, this could also be the result of a
|
|
605 // deliberate symlink. Verify that the existing directory is safe.
|
|
606 //
|
|
607 if (!is_directory_secure(dirname)) {
|
|
608 // directory is not secure
|
|
609 if (PrintMiscellaneous && Verbose) {
|
|
610 warning("%s directory is insecure\n", dirname);
|
|
611 }
|
|
612 return false;
|
|
613 }
|
|
614 }
|
|
615 else {
|
|
616 // we encountered some other failure while attempting
|
|
617 // to create the directory
|
|
618 //
|
|
619 if (PrintMiscellaneous && Verbose) {
|
|
620 warning("could not create directory %s: %s\n",
|
|
621 dirname, strerror(errno));
|
|
622 }
|
|
623 return false;
|
|
624 }
|
|
625 }
|
|
626 return true;
|
|
627 }
|
|
628
|
|
629 // create the shared memory file resources
|
|
630 //
|
|
631 // This method creates the shared memory file with the given size
|
|
632 // This method also creates the user specific temporary directory, if
|
|
633 // it does not yet exist.
|
|
634 //
|
|
635 static int create_sharedmem_resources(const char* dirname, const char* filename, size_t size) {
|
|
636
|
|
637 // make the user temporary directory
|
|
638 if (!make_user_tmp_dir(dirname)) {
|
|
639 // could not make/find the directory or the found directory
|
|
640 // was not secure
|
|
641 return -1;
|
|
642 }
|
|
643
|
|
644 int result;
|
|
645
|
|
646 RESTARTABLE(::open(filename, O_RDWR|O_CREAT|O_TRUNC, S_IREAD|S_IWRITE), result);
|
|
647 if (result == OS_ERR) {
|
|
648 if (PrintMiscellaneous && Verbose) {
|
|
649 warning("could not create file %s: %s\n", filename, strerror(errno));
|
|
650 }
|
|
651 return -1;
|
|
652 }
|
|
653
|
|
654 // save the file descriptor
|
|
655 int fd = result;
|
|
656
|
|
657 // set the file size
|
|
658 RESTARTABLE(::ftruncate(fd, (off_t)size), result);
|
|
659 if (result == OS_ERR) {
|
|
660 if (PrintMiscellaneous && Verbose) {
|
|
661 warning("could not set shared memory file size: %s\n", strerror(errno));
|
|
662 }
|
|
663 RESTARTABLE(::close(fd), result);
|
|
664 return -1;
|
|
665 }
|
|
666
|
|
667 return fd;
|
|
668 }
|
|
669
|
|
670 // open the shared memory file for the given user and vmid. returns
|
|
671 // the file descriptor for the open file or -1 if the file could not
|
|
672 // be opened.
|
|
673 //
|
|
674 static int open_sharedmem_file(const char* filename, int oflags, TRAPS) {
|
|
675
|
|
676 // open the file
|
|
677 int result;
|
|
678 RESTARTABLE(::open(filename, oflags), result);
|
|
679 if (result == OS_ERR) {
|
|
680 if (errno == ENOENT) {
|
|
681 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(),
|
|
682 "Process not found");
|
|
683 }
|
|
684 else if (errno == EACCES) {
|
|
685 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(),
|
|
686 "Permission denied");
|
|
687 }
|
|
688 else {
|
|
689 THROW_MSG_0(vmSymbols::java_io_IOException(), strerror(errno));
|
|
690 }
|
|
691 }
|
|
692
|
|
693 return result;
|
|
694 }
|
|
695
|
|
696 // create a named shared memory region. returns the address of the
|
|
697 // memory region on success or NULL on failure. A return value of
|
|
698 // NULL will ultimately disable the shared memory feature.
|
|
699 //
|
|
700 // On Solaris and Linux, the name space for shared memory objects
|
|
701 // is the file system name space.
|
|
702 //
|
|
703 // A monitoring application attaching to a JVM does not need to know
|
|
704 // the file system name of the shared memory object. However, it may
|
|
705 // be convenient for applications to discover the existence of newly
|
|
706 // created and terminating JVMs by watching the file system name space
|
|
707 // for files being created or removed.
|
|
708 //
|
|
709 static char* mmap_create_shared(size_t size) {
|
|
710
|
|
711 int result;
|
|
712 int fd;
|
|
713 char* mapAddress;
|
|
714
|
|
715 int vmid = os::current_process_id();
|
|
716
|
|
717 char* user_name = get_user_name(geteuid());
|
|
718
|
|
719 if (user_name == NULL)
|
|
720 return NULL;
|
|
721
|
|
722 char* dirname = get_user_tmp_dir(user_name);
|
|
723 char* filename = get_sharedmem_filename(dirname, vmid);
|
|
724
|
|
725 // cleanup any stale shared memory files
|
|
726 cleanup_sharedmem_resources(dirname);
|
|
727
|
|
728 assert(((size > 0) && (size % os::vm_page_size() == 0)),
|
|
729 "unexpected PerfMemory region size");
|
|
730
|
|
731 fd = create_sharedmem_resources(dirname, filename, size);
|
|
732
|
|
733 FREE_C_HEAP_ARRAY(char, user_name);
|
|
734 FREE_C_HEAP_ARRAY(char, dirname);
|
|
735
|
|
736 if (fd == -1) {
|
|
737 FREE_C_HEAP_ARRAY(char, filename);
|
|
738 return NULL;
|
|
739 }
|
|
740
|
|
741 mapAddress = (char*)::mmap((char*)0, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
|
|
742
|
|
743 // attempt to close the file - restart it if it was interrupted,
|
|
744 // but ignore other failures
|
|
745 RESTARTABLE(::close(fd), result);
|
|
746 assert(result != OS_ERR, "could not close file");
|
|
747
|
|
748 if (mapAddress == MAP_FAILED) {
|
|
749 if (PrintMiscellaneous && Verbose) {
|
|
750 warning("mmap failed - %s\n", strerror(errno));
|
|
751 }
|
|
752 remove_file(filename);
|
|
753 FREE_C_HEAP_ARRAY(char, filename);
|
|
754 return NULL;
|
|
755 }
|
|
756
|
|
757 // save the file name for use in delete_shared_memory()
|
|
758 backing_store_file_name = filename;
|
|
759
|
|
760 // clear the shared memory region
|
|
761 (void)::memset((void*) mapAddress, 0, size);
|
|
762
|
|
763 return mapAddress;
|
|
764 }
|
|
765
|
|
766 // release a named shared memory region
|
|
767 //
|
|
768 static void unmap_shared(char* addr, size_t bytes) {
|
|
769 os::release_memory(addr, bytes);
|
|
770 }
|
|
771
|
|
772 // create the PerfData memory region in shared memory.
|
|
773 //
|
|
774 static char* create_shared_memory(size_t size) {
|
|
775
|
|
776 // create the shared memory region.
|
|
777 return mmap_create_shared(size);
|
|
778 }
|
|
779
|
|
780 // delete the shared PerfData memory region
|
|
781 //
|
|
782 static void delete_shared_memory(char* addr, size_t size) {
|
|
783
|
|
784 // cleanup the persistent shared memory resources. since DestroyJavaVM does
|
|
785 // not support unloading of the JVM, unmapping of the memory resource is
|
|
786 // not performed. The memory will be reclaimed by the OS upon termination of
|
|
787 // the process. The backing store file is deleted from the file system.
|
|
788
|
|
789 assert(!PerfDisableSharedMem, "shouldn't be here");
|
|
790
|
|
791 if (backing_store_file_name != NULL) {
|
|
792 remove_file(backing_store_file_name);
|
|
793 // Don't.. Free heap memory could deadlock os::abort() if it is called
|
|
794 // from signal handler. OS will reclaim the heap memory.
|
|
795 // FREE_C_HEAP_ARRAY(char, backing_store_file_name);
|
|
796 backing_store_file_name = NULL;
|
|
797 }
|
|
798 }
|
|
799
|
|
800 // return the size of the file for the given file descriptor
|
|
801 // or 0 if it is not a valid size for a shared memory file
|
|
802 //
|
|
803 static size_t sharedmem_filesize(int fd, TRAPS) {
|
|
804
|
|
805 struct stat statbuf;
|
|
806 int result;
|
|
807
|
|
808 RESTARTABLE(::fstat(fd, &statbuf), result);
|
|
809 if (result == OS_ERR) {
|
|
810 if (PrintMiscellaneous && Verbose) {
|
|
811 warning("fstat failed: %s\n", strerror(errno));
|
|
812 }
|
|
813 THROW_MSG_0(vmSymbols::java_io_IOException(),
|
|
814 "Could not determine PerfMemory size");
|
|
815 }
|
|
816
|
|
817 if ((statbuf.st_size == 0) ||
|
|
818 ((size_t)statbuf.st_size % os::vm_page_size() != 0)) {
|
|
819 THROW_MSG_0(vmSymbols::java_lang_Exception(),
|
|
820 "Invalid PerfMemory size");
|
|
821 }
|
|
822
|
|
823 return (size_t)statbuf.st_size;
|
|
824 }
|
|
825
|
|
826 // attach to a named shared memory region.
|
|
827 //
|
|
828 static void mmap_attach_shared(const char* user, int vmid, PerfMemory::PerfMemoryMode mode, char** addr, size_t* sizep, TRAPS) {
|
|
829
|
|
830 char* mapAddress;
|
|
831 int result;
|
|
832 int fd;
|
|
833 size_t size;
|
|
834 const char* luser = NULL;
|
|
835
|
|
836 int mmap_prot;
|
|
837 int file_flags;
|
|
838
|
|
839 ResourceMark rm;
|
|
840
|
|
841 // map the high level access mode to the appropriate permission
|
|
842 // constructs for the file and the shared memory mapping.
|
|
843 if (mode == PerfMemory::PERF_MODE_RO) {
|
|
844 mmap_prot = PROT_READ;
|
|
845 file_flags = O_RDONLY;
|
|
846 }
|
|
847 else if (mode == PerfMemory::PERF_MODE_RW) {
|
|
848 #ifdef LATER
|
|
849 mmap_prot = PROT_READ | PROT_WRITE;
|
|
850 file_flags = O_RDWR;
|
|
851 #else
|
|
852 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
|
|
853 "Unsupported access mode");
|
|
854 #endif
|
|
855 }
|
|
856 else {
|
|
857 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
|
|
858 "Illegal access mode");
|
|
859 }
|
|
860
|
|
861 if (user == NULL || strlen(user) == 0) {
|
|
862 luser = get_user_name(vmid, CHECK);
|
|
863 }
|
|
864 else {
|
|
865 luser = user;
|
|
866 }
|
|
867
|
|
868 if (luser == NULL) {
|
|
869 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
|
|
870 "Could not map vmid to user Name");
|
|
871 }
|
|
872
|
|
873 char* dirname = get_user_tmp_dir(luser);
|
|
874
|
|
875 // since we don't follow symbolic links when creating the backing
|
|
876 // store file, we don't follow them when attaching either.
|
|
877 //
|
|
878 if (!is_directory_secure(dirname)) {
|
|
879 FREE_C_HEAP_ARRAY(char, dirname);
|
|
880 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
|
|
881 "Process not found");
|
|
882 }
|
|
883
|
|
884 char* filename = get_sharedmem_filename(dirname, vmid);
|
|
885
|
|
886 // copy heap memory to resource memory. the open_sharedmem_file
|
|
887 // method below need to use the filename, but could throw an
|
|
888 // exception. using a resource array prevents the leak that
|
|
889 // would otherwise occur.
|
|
890 char* rfilename = NEW_RESOURCE_ARRAY(char, strlen(filename) + 1);
|
|
891 strcpy(rfilename, filename);
|
|
892
|
|
893 // free the c heap resources that are no longer needed
|
|
894 if (luser != user) FREE_C_HEAP_ARRAY(char, luser);
|
|
895 FREE_C_HEAP_ARRAY(char, dirname);
|
|
896 FREE_C_HEAP_ARRAY(char, filename);
|
|
897
|
|
898 // open the shared memory file for the give vmid
|
|
899 fd = open_sharedmem_file(rfilename, file_flags, CHECK);
|
|
900 assert(fd != OS_ERR, "unexpected value");
|
|
901
|
|
902 if (*sizep == 0) {
|
|
903 size = sharedmem_filesize(fd, CHECK);
|
|
904 assert(size != 0, "unexpected size");
|
|
905 }
|
|
906
|
|
907 mapAddress = (char*)::mmap((char*)0, size, mmap_prot, MAP_SHARED, fd, 0);
|
|
908
|
|
909 // attempt to close the file - restart if it gets interrupted,
|
|
910 // but ignore other failures
|
|
911 RESTARTABLE(::close(fd), result);
|
|
912 assert(result != OS_ERR, "could not close file");
|
|
913
|
|
914 if (mapAddress == MAP_FAILED) {
|
|
915 if (PrintMiscellaneous && Verbose) {
|
|
916 warning("mmap failed: %s\n", strerror(errno));
|
|
917 }
|
|
918 THROW_MSG(vmSymbols::java_lang_OutOfMemoryError(),
|
|
919 "Could not map PerfMemory");
|
|
920 }
|
|
921
|
|
922 *addr = mapAddress;
|
|
923 *sizep = size;
|
|
924
|
|
925 if (PerfTraceMemOps) {
|
|
926 tty->print("mapped " SIZE_FORMAT " bytes for vmid %d at "
|
|
927 INTPTR_FORMAT "\n", size, vmid, (void*)mapAddress);
|
|
928 }
|
|
929 }
|
|
930
|
|
931
|
|
932
|
|
933
|
|
934 // create the PerfData memory region
|
|
935 //
|
|
936 // This method creates the memory region used to store performance
|
|
937 // data for the JVM. The memory may be created in standard or
|
|
938 // shared memory.
|
|
939 //
|
|
940 void PerfMemory::create_memory_region(size_t size) {
|
|
941
|
|
942 if (PerfDisableSharedMem) {
|
|
943 // do not share the memory for the performance data.
|
|
944 _start = create_standard_memory(size);
|
|
945 }
|
|
946 else {
|
|
947 _start = create_shared_memory(size);
|
|
948 if (_start == NULL) {
|
|
949
|
|
950 // creation of the shared memory region failed, attempt
|
|
951 // to create a contiguous, non-shared memory region instead.
|
|
952 //
|
|
953 if (PrintMiscellaneous && Verbose) {
|
|
954 warning("Reverting to non-shared PerfMemory region.\n");
|
|
955 }
|
|
956 PerfDisableSharedMem = true;
|
|
957 _start = create_standard_memory(size);
|
|
958 }
|
|
959 }
|
|
960
|
|
961 if (_start != NULL) _capacity = size;
|
|
962
|
|
963 }
|
|
964
|
|
965 // delete the PerfData memory region
|
|
966 //
|
|
967 // This method deletes the memory region used to store performance
|
|
968 // data for the JVM. The memory region indicated by the <address, size>
|
|
969 // tuple will be inaccessible after a call to this method.
|
|
970 //
|
|
971 void PerfMemory::delete_memory_region() {
|
|
972
|
|
973 assert((start() != NULL && capacity() > 0), "verify proper state");
|
|
974
|
|
975 // If user specifies PerfDataSaveFile, it will save the performance data
|
|
976 // to the specified file name no matter whether PerfDataSaveToFile is specified
|
|
977 // or not. In other word, -XX:PerfDataSaveFile=.. overrides flag
|
|
978 // -XX:+PerfDataSaveToFile.
|
|
979 if (PerfDataSaveToFile || PerfDataSaveFile != NULL) {
|
|
980 save_memory_to_file(start(), capacity());
|
|
981 }
|
|
982
|
|
983 if (PerfDisableSharedMem) {
|
|
984 delete_standard_memory(start(), capacity());
|
|
985 }
|
|
986 else {
|
|
987 delete_shared_memory(start(), capacity());
|
|
988 }
|
|
989 }
|
|
990
|
|
991 // attach to the PerfData memory region for another JVM
|
|
992 //
|
|
993 // This method returns an <address, size> tuple that points to
|
|
994 // a memory buffer that is kept reasonably synchronized with
|
|
995 // the PerfData memory region for the indicated JVM. This
|
|
996 // buffer may be kept in synchronization via shared memory
|
|
997 // or some other mechanism that keeps the buffer updated.
|
|
998 //
|
|
999 // If the JVM chooses not to support the attachability feature,
|
|
1000 // this method should throw an UnsupportedOperation exception.
|
|
1001 //
|
|
1002 // This implementation utilizes named shared memory to map
|
|
1003 // the indicated process's PerfData memory region into this JVMs
|
|
1004 // address space.
|
|
1005 //
|
|
1006 void PerfMemory::attach(const char* user, int vmid, PerfMemoryMode mode, char** addrp, size_t* sizep, TRAPS) {
|
|
1007
|
|
1008 if (vmid == 0 || vmid == os::current_process_id()) {
|
|
1009 *addrp = start();
|
|
1010 *sizep = capacity();
|
|
1011 return;
|
|
1012 }
|
|
1013
|
|
1014 mmap_attach_shared(user, vmid, mode, addrp, sizep, CHECK);
|
|
1015 }
|
|
1016
|
|
1017 // detach from the PerfData memory region of another JVM
|
|
1018 //
|
|
1019 // This method detaches the PerfData memory region of another
|
|
1020 // JVM, specified as an <address, size> tuple of a buffer
|
|
1021 // in this process's address space. This method may perform
|
|
1022 // arbitrary actions to accomplish the detachment. The memory
|
|
1023 // region specified by <address, size> will be inaccessible after
|
|
1024 // a call to this method.
|
|
1025 //
|
|
1026 // If the JVM chooses not to support the attachability feature,
|
|
1027 // this method should throw an UnsupportedOperation exception.
|
|
1028 //
|
|
1029 // This implementation utilizes named shared memory to detach
|
|
1030 // the indicated process's PerfData memory region from this
|
|
1031 // process's address space.
|
|
1032 //
|
|
1033 void PerfMemory::detach(char* addr, size_t bytes, TRAPS) {
|
|
1034
|
|
1035 assert(addr != 0, "address sanity check");
|
|
1036 assert(bytes > 0, "capacity sanity check");
|
|
1037
|
|
1038 if (PerfMemory::contains(addr) || PerfMemory::contains(addr + bytes - 1)) {
|
|
1039 // prevent accidental detachment of this process's PerfMemory region
|
|
1040 return;
|
|
1041 }
|
|
1042
|
|
1043 unmap_shared(addr, bytes);
|
|
1044 }
|
|
1045
|
|
1046 char* PerfMemory::backing_store_filename() {
|
|
1047 return backing_store_file_name;
|
|
1048 }
|