1 /*
2 * Copyright (c) 2001, 2015, Oracle and/or its affiliates. All rights reserved.
3 * Copyright (c) 2012, 2013 SAP SE. All rights reserved.
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 *
6 * This code is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 only, as
8 * published by the Free Software Foundation.
9 *
10 * This code is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * version 2 for more details (a copy is included in the LICENSE file that
14 * accompanied this code).
15 *
16 * You should have received a copy of the GNU General Public License version
17 * 2 along with this work; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21 * or visit www.oracle.com if you need additional information or have any
22 * questions.
23 *
104 destfile, strerror(errno));
105 }
106 } else {
107 int fd = result;
108
109 for (size_t remaining = size; remaining > 0;) {
110
111 RESTARTABLE(::write(fd, addr, remaining), result);
112 if (result == OS_ERR) {
113 if (PrintMiscellaneous && Verbose) {
114 warning("Could not write Perfdata save file: %s: %s\n",
115 destfile, strerror(errno));
116 }
117 break;
118 }
119
120 remaining -= (size_t)result;
121 addr += result;
122 }
123
124 RESTARTABLE(::close(fd), result);
125 if (PrintMiscellaneous && Verbose) {
126 if (result == OS_ERR) {
127 warning("Could not close %s: %s\n", destfile, strerror(errno));
128 }
129 }
130 }
131 FREE_C_HEAP_ARRAY(char, destfile);
132 }
133
134
135 // Shared Memory Implementation Details
136
137 // Note: the solaris and linux shared memory implementation uses the mmap
138 // interface with a backing store file to implement named shared memory.
139 // Using the file system as the name space for shared memory allows a
140 // common name space to be supported across a variety of platforms. It
141 // also provides a name space that Java applications can deal with through
142 // simple file apis.
143 //
144 // The solaris and linux implementations store the backing store file in
282
283 if ((statbuf1.st_ino == statbuf2.st_ino) &&
284 (statbuf1.st_dev == statbuf2.st_dev)) {
285 return true;
286 } else {
287 return false;
288 }
289 }
290
291 // Helper functions for open without O_NOFOLLOW which is not present on AIX 5.3/6.1.
292 // We use the jdk6 implementation here.
293 #ifndef O_NOFOLLOW
294 // The O_NOFOLLOW oflag doesn't exist before solaris 5.10, this is to simulate that behaviour
295 // was done in jdk 5/6 hotspot by Oracle this way
296 static int open_o_nofollow_impl(const char* path, int oflag, mode_t mode, bool use_mode) {
297 struct stat orig_st;
298 struct stat new_st;
299 bool create;
300 int error;
301 int fd;
302
303 create = false;
304
305 if (lstat(path, &orig_st) != 0) {
306 if (errno == ENOENT && (oflag & O_CREAT) != 0) {
307 // File doesn't exist, but_we want to create it, add O_EXCL flag
308 // to make sure no-one creates it (or a symlink) before us
309 // This works as we expect with symlinks, from posix man page:
310 // 'If O_EXCL and O_CREAT are set, and path names a symbolic
311 // link, open() shall fail and set errno to [EEXIST]'.
312 oflag |= O_EXCL;
313 create = true;
314 } else {
315 // File doesn't exist, and we are not creating it.
316 return OS_ERR;
317 }
318 } else {
319 // Lstat success, check if existing file is a link.
320 if ((orig_st.st_mode & S_IFMT) == S_IFLNK) {
321 // File is a symlink.
322 errno = ELOOP;
323 return OS_ERR;
324 }
325 }
326
327 if (use_mode == true) {
328 fd = open(path, oflag, mode);
329 } else {
330 fd = open(path, oflag);
331 }
332
333 if (fd == OS_ERR) {
334 return fd;
335 }
336
337 // Can't do inode checks on before/after if we created the file.
338 if (create == false) {
339 if (fstat(fd, &new_st) != 0) {
340 // Keep errno from fstat, in case close also fails.
341 error = errno;
342 ::close(fd);
343 errno = error;
344 return OS_ERR;
345 }
346
347 if (orig_st.st_dev != new_st.st_dev || orig_st.st_ino != new_st.st_ino) {
348 // File was tampered with during race window.
349 ::close(fd);
350 errno = EEXIST;
351 if (PrintMiscellaneous && Verbose) {
352 warning("possible file tampering attempt detected when opening %s", path);
353 }
354 return OS_ERR;
355 }
356 }
357
358 return fd;
359 }
367 }
368 #endif
369
370 // Open the directory of the given path and validate it.
371 // Return a DIR * of the open directory.
372 static DIR *open_directory_secure(const char* dirname) {
373 // Open the directory using open() so that it can be verified
374 // to be secure by calling is_dirfd_secure(), opendir() and then check
375 // to see if they are the same file system object. This method does not
376 // introduce a window of opportunity for the directory to be attacked that
377 // calling opendir() and is_directory_secure() does.
378 int result;
379 DIR *dirp = NULL;
380
381 // No O_NOFOLLOW defined at buildtime, and it is not documented for open;
382 // so provide a workaround in this case.
383 #ifdef O_NOFOLLOW
384 RESTARTABLE(::open(dirname, O_RDONLY|O_NOFOLLOW), result);
385 #else
386 // workaround (jdk6 coding)
387 RESTARTABLE(::open_o_nofollow(dirname, O_RDONLY), result);
388 #endif
389
390 if (result == OS_ERR) {
391 // Directory doesn't exist or is a symlink, so there is nothing to cleanup.
392 if (PrintMiscellaneous && Verbose) {
393 if (errno == ELOOP) {
394 warning("directory %s is a symlink and is not secure\n", dirname);
395 } else {
396 warning("could not open directory %s: %s\n", dirname, strerror(errno));
397 }
398 }
399 return dirp;
400 }
401 int fd = result;
402
403 // Determine if the open directory is secure.
404 if (!is_dirfd_secure(fd)) {
405 // The directory is not a secure directory.
406 os::close(fd);
407 return dirp;
871 int saved_cwd_fd;
872 // Open the directory and set the current working directory to it.
873 DIR* dirp = open_directory_secure_cwd(dirname, &saved_cwd_fd);
874 if (dirp == NULL) {
875 // Directory doesn't exist or is insecure, so cannot create shared
876 // memory file.
877 return -1;
878 }
879
880 // Open the filename in the current directory.
881 // Cannot use O_TRUNC here; truncation of an existing file has to happen
882 // after the is_file_secure() check below.
883 int result;
884
885 // No O_NOFOLLOW defined at buildtime, and it is not documented for open;
886 // so provide a workaround in this case.
887 #ifdef O_NOFOLLOW
888 RESTARTABLE(::open(filename, O_RDWR|O_CREAT|O_NOFOLLOW, S_IREAD|S_IWRITE), result);
889 #else
890 // workaround function (jdk6 code)
891 RESTARTABLE(::open_o_nofollow(filename, O_RDWR|O_CREAT, S_IREAD|S_IWRITE), result);
892 #endif
893
894 if (result == OS_ERR) {
895 if (PrintMiscellaneous && Verbose) {
896 if (errno == ELOOP) {
897 warning("file %s is a symlink and is not secure\n", filename);
898 } else {
899 warning("could not create file %s: %s\n", filename, strerror(errno));
900 }
901 }
902 // Close the directory and reset the current working directory.
903 close_directory_secure_cwd(dirp, saved_cwd_fd);
904
905 return -1;
906 }
907 // Close the directory and reset the current working directory.
908 close_directory_secure_cwd(dirp, saved_cwd_fd);
909
910 // save the file descriptor
911 int fd = result;
914 if (!is_file_secure(fd, filename)) {
915 ::close(fd);
916 return -1;
917 }
918
919 // Truncate the file to get rid of any existing data.
920 RESTARTABLE(::ftruncate(fd, (off_t)0), result);
921 if (result == OS_ERR) {
922 if (PrintMiscellaneous && Verbose) {
923 warning("could not truncate shared memory file: %s\n", strerror(errno));
924 }
925 ::close(fd);
926 return -1;
927 }
928 // set the file size
929 RESTARTABLE(::ftruncate(fd, (off_t)size), result);
930 if (result == OS_ERR) {
931 if (PrintMiscellaneous && Verbose) {
932 warning("could not set shared memory file size: %s\n", strerror(errno));
933 }
934 RESTARTABLE(::close(fd), result);
935 return -1;
936 }
937
938 return fd;
939 }
940
941 // open the shared memory file for the given user and vmid. returns
942 // the file descriptor for the open file or -1 if the file could not
943 // be opened.
944 //
945 static int open_sharedmem_file(const char* filename, int oflags, TRAPS) {
946
947 // open the file
948 int result;
949 // No O_NOFOLLOW defined at buildtime, and it is not documented for open;
950 // so provide a workaround in this case
951 #ifdef O_NOFOLLOW
952 RESTARTABLE(::open(filename, oflags), result);
953 #else
954 RESTARTABLE(::open_o_nofollow(filename, oflags), result);
955 #endif
956
957 if (result == OS_ERR) {
958 if (errno == ENOENT) {
959 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(),
960 "Process not found");
961 }
962 else if (errno == EACCES) {
963 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(),
964 "Permission denied");
965 }
966 else {
967 THROW_MSG_0(vmSymbols::java_io_IOException(), strerror(errno));
968 }
969 }
970 int fd = result;
971
972 // Check to see if the file is secure.
973 if (!is_file_secure(fd, filename)) {
974 ::close(fd);
989 // the file system name of the shared memory object. However, it may
990 // be convenient for applications to discover the existence of newly
991 // created and terminating JVMs by watching the file system name space
992 // for files being created or removed.
993 //
994 static char* mmap_create_shared(size_t size) {
995
996 int result;
997 int fd;
998 char* mapAddress;
999
1000 int vmid = os::current_process_id();
1001
1002 char* user_name = get_user_name(geteuid());
1003
1004 if (user_name == NULL)
1005 return NULL;
1006
1007 char* dirname = get_user_tmp_dir(user_name);
1008 char* filename = get_sharedmem_filename(dirname, vmid);
1009
1010 // Get the short filename.
1011 char* short_filename = strrchr(filename, '/');
1012 if (short_filename == NULL) {
1013 short_filename = filename;
1014 } else {
1015 short_filename++;
1016 }
1017
1018 // cleanup any stale shared memory files
1019 cleanup_sharedmem_resources(dirname);
1020
1021 assert(((size > 0) && (size % os::vm_page_size() == 0)),
1022 "unexpected PerfMemory region size");
1023
1024 fd = create_sharedmem_resources(dirname, short_filename, size);
1025
1026 FREE_C_HEAP_ARRAY(char, user_name);
1027 FREE_C_HEAP_ARRAY(char, dirname);
1028
1029 if (fd == -1) {
1030 FREE_C_HEAP_ARRAY(char, filename);
1031 return NULL;
1032 }
1033
1034 mapAddress = (char*)::mmap((char*)0, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
1035
1036 // attempt to close the file - restart it if it was interrupted,
1037 // but ignore other failures
1038 RESTARTABLE(::close(fd), result);
1039 assert(result != OS_ERR, "could not close file");
1040
1041 if (mapAddress == MAP_FAILED) {
1042 if (PrintMiscellaneous && Verbose) {
1043 warning("mmap failed - %s\n", strerror(errno));
1044 }
1045 remove_file(filename);
1046 FREE_C_HEAP_ARRAY(char, filename);
1047 return NULL;
1048 }
1049
1050 // save the file name for use in delete_shared_memory()
1051 backing_store_file_name = filename;
1052
1053 // clear the shared memory region
1054 (void)::memset((void*) mapAddress, 0, size);
1055
1056 // It does not go through os api, the operation has to record from here.
1057 MemTracker::record_virtual_memory_reserve((address)mapAddress, size, CURRENT_PC, mtInternal);
1058
1125
1126 // attach to a named shared memory region.
1127 //
1128 static void mmap_attach_shared(const char* user, int vmid, PerfMemory::PerfMemoryMode mode, char** addr, size_t* sizep, TRAPS) {
1129
1130 char* mapAddress;
1131 int result;
1132 int fd;
1133 size_t size = 0;
1134 const char* luser = NULL;
1135
1136 int mmap_prot;
1137 int file_flags;
1138
1139 ResourceMark rm;
1140
1141 // map the high level access mode to the appropriate permission
1142 // constructs for the file and the shared memory mapping.
1143 if (mode == PerfMemory::PERF_MODE_RO) {
1144 mmap_prot = PROT_READ;
1145
1146 // No O_NOFOLLOW defined at buildtime, and it is not documented for open.
1147 #ifdef O_NOFOLLOW
1148 file_flags = O_RDONLY | O_NOFOLLOW;
1149 #else
1150 file_flags = O_RDONLY;
1151 #endif
1152 }
1153 else if (mode == PerfMemory::PERF_MODE_RW) {
1154 #ifdef LATER
1155 mmap_prot = PROT_READ | PROT_WRITE;
1156 file_flags = O_RDWR | O_NOFOLLOW;
1157 #else
1158 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
1159 "Unsupported access mode");
1160 #endif
1161 }
1162 else {
1163 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
1164 "Illegal access mode");
1165 }
1188 }
1189 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
1190 "Process not found");
1191 }
1192
1193 char* filename = get_sharedmem_filename(dirname, vmid);
1194
1195 // copy heap memory to resource memory. the open_sharedmem_file
1196 // method below need to use the filename, but could throw an
1197 // exception. using a resource array prevents the leak that
1198 // would otherwise occur.
1199 char* rfilename = NEW_RESOURCE_ARRAY(char, strlen(filename) + 1);
1200 strcpy(rfilename, filename);
1201
1202 // free the c heap resources that are no longer needed
1203 if (luser != user) FREE_C_HEAP_ARRAY(char, luser);
1204 FREE_C_HEAP_ARRAY(char, dirname);
1205 FREE_C_HEAP_ARRAY(char, filename);
1206
1207 // open the shared memory file for the give vmid
1208 fd = open_sharedmem_file(rfilename, file_flags, CHECK);
1209 assert(fd != OS_ERR, "unexpected value");
1210
1211 if (*sizep == 0) {
1212 size = sharedmem_filesize(fd, CHECK);
1213 assert(size != 0, "unexpected size");
1214 } else {
1215 size = *sizep;
1216 }
1217
1218 mapAddress = (char*)::mmap((char*)0, size, mmap_prot, MAP_SHARED, fd, 0);
1219
1220 // attempt to close the file - restart if it gets interrupted,
1221 // but ignore other failures
1222 RESTARTABLE(::close(fd), result);
1223 assert(result != OS_ERR, "could not close file");
1224
1225 if (mapAddress == MAP_FAILED) {
1226 if (PrintMiscellaneous && Verbose) {
1227 warning("mmap failed: %s\n", strerror(errno));
1228 }
1229 THROW_MSG(vmSymbols::java_lang_OutOfMemoryError(),
1230 "Could not map PerfMemory");
1231 }
1232
1233 // It does not go through os api, the operation has to record from here.
1234 MemTracker::record_virtual_memory_reserve((address)mapAddress, size, CURRENT_PC, mtInternal);
1235
1236 *addr = mapAddress;
1237 *sizep = size;
1238
1239 if (PerfTraceMemOps) {
1240 tty->print("mapped " SIZE_FORMAT " bytes for vmid %d at "
1241 INTPTR_FORMAT "\n", size, vmid, (void*)mapAddress);
1242 }
1243 }
1244
1245
1246
1247
1248 // create the PerfData memory region
1249 //
1250 // This method creates the memory region used to store performance
1251 // data for the JVM. The memory may be created in standard or
1252 // shared memory.
1253 //
1254 void PerfMemory::create_memory_region(size_t size) {
1255
1256 if (PerfDisableSharedMem) {
1257 // do not share the memory for the performance data.
1258 _start = create_standard_memory(size);
1259 }
1260 else {
1261 _start = create_shared_memory(size);
|
1 /*
2 * Copyright (c) 2001, 2016, Oracle and/or its affiliates. All rights reserved.
3 * Copyright (c) 2012, 2016 SAP SE. All rights reserved.
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 *
6 * This code is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 only, as
8 * published by the Free Software Foundation.
9 *
10 * This code is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * version 2 for more details (a copy is included in the LICENSE file that
14 * accompanied this code).
15 *
16 * You should have received a copy of the GNU General Public License version
17 * 2 along with this work; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21 * or visit www.oracle.com if you need additional information or have any
22 * questions.
23 *
104 destfile, strerror(errno));
105 }
106 } else {
107 int fd = result;
108
109 for (size_t remaining = size; remaining > 0;) {
110
111 RESTARTABLE(::write(fd, addr, remaining), result);
112 if (result == OS_ERR) {
113 if (PrintMiscellaneous && Verbose) {
114 warning("Could not write Perfdata save file: %s: %s\n",
115 destfile, strerror(errno));
116 }
117 break;
118 }
119
120 remaining -= (size_t)result;
121 addr += result;
122 }
123
124 result = ::close(fd);
125 if (PrintMiscellaneous && Verbose) {
126 if (result == OS_ERR) {
127 warning("Could not close %s: %s\n", destfile, strerror(errno));
128 }
129 }
130 }
131 FREE_C_HEAP_ARRAY(char, destfile);
132 }
133
134
135 // Shared Memory Implementation Details
136
137 // Note: the solaris and linux shared memory implementation uses the mmap
138 // interface with a backing store file to implement named shared memory.
139 // Using the file system as the name space for shared memory allows a
140 // common name space to be supported across a variety of platforms. It
141 // also provides a name space that Java applications can deal with through
142 // simple file apis.
143 //
144 // The solaris and linux implementations store the backing store file in
282
283 if ((statbuf1.st_ino == statbuf2.st_ino) &&
284 (statbuf1.st_dev == statbuf2.st_dev)) {
285 return true;
286 } else {
287 return false;
288 }
289 }
290
291 // Helper functions for open without O_NOFOLLOW which is not present on AIX 5.3/6.1.
292 // We use the jdk6 implementation here.
293 #ifndef O_NOFOLLOW
294 // The O_NOFOLLOW oflag doesn't exist before solaris 5.10, this is to simulate that behaviour
295 // was done in jdk 5/6 hotspot by Oracle this way
296 static int open_o_nofollow_impl(const char* path, int oflag, mode_t mode, bool use_mode) {
297 struct stat orig_st;
298 struct stat new_st;
299 bool create;
300 int error;
301 int fd;
302 int result;
303
304 create = false;
305
306 RESTARTABLE(::lstat(path, &orig_st), result);
307
308 if (result == OS_ERR) {
309 if (errno == ENOENT && (oflag & O_CREAT) != 0) {
310 // File doesn't exist, but_we want to create it, add O_EXCL flag
311 // to make sure no-one creates it (or a symlink) before us
312 // This works as we expect with symlinks, from posix man page:
313 // 'If O_EXCL and O_CREAT are set, and path names a symbolic
314 // link, open() shall fail and set errno to [EEXIST]'.
315 oflag |= O_EXCL;
316 create = true;
317 } else {
318 // File doesn't exist, and we are not creating it.
319 return OS_ERR;
320 }
321 } else {
322 // lstat success, check if existing file is a link.
323 if ((orig_st.st_mode & S_IFMT) == S_IFLNK) {
324 // File is a symlink.
325 errno = ELOOP;
326 return OS_ERR;
327 }
328 }
329
330 if (use_mode == true) {
331 RESTARTABLE(::open(path, oflag, mode), fd);
332 } else {
333 RESTARTABLE(::open(path, oflag), fd);
334 }
335
336 if (fd == OS_ERR) {
337 return fd;
338 }
339
340 // Can't do inode checks on before/after if we created the file.
341 if (create == false) {
342 RESTARTABLE(::fstat(fd, &new_st), result);
343 if (result == OS_ERR) {
344 // Keep errno from fstat, in case close also fails.
345 error = errno;
346 ::close(fd);
347 errno = error;
348 return OS_ERR;
349 }
350
351 if (orig_st.st_dev != new_st.st_dev || orig_st.st_ino != new_st.st_ino) {
352 // File was tampered with during race window.
353 ::close(fd);
354 errno = EEXIST;
355 if (PrintMiscellaneous && Verbose) {
356 warning("possible file tampering attempt detected when opening %s", path);
357 }
358 return OS_ERR;
359 }
360 }
361
362 return fd;
363 }
371 }
372 #endif
373
374 // Open the directory of the given path and validate it.
375 // Return a DIR * of the open directory.
376 static DIR *open_directory_secure(const char* dirname) {
377 // Open the directory using open() so that it can be verified
378 // to be secure by calling is_dirfd_secure(), opendir() and then check
379 // to see if they are the same file system object. This method does not
380 // introduce a window of opportunity for the directory to be attacked that
381 // calling opendir() and is_directory_secure() does.
382 int result;
383 DIR *dirp = NULL;
384
385 // No O_NOFOLLOW defined at buildtime, and it is not documented for open;
386 // so provide a workaround in this case.
387 #ifdef O_NOFOLLOW
388 RESTARTABLE(::open(dirname, O_RDONLY|O_NOFOLLOW), result);
389 #else
390 // workaround (jdk6 coding)
391 result = open_o_nofollow(dirname, O_RDONLY);
392 #endif
393
394 if (result == OS_ERR) {
395 // Directory doesn't exist or is a symlink, so there is nothing to cleanup.
396 if (PrintMiscellaneous && Verbose) {
397 if (errno == ELOOP) {
398 warning("directory %s is a symlink and is not secure\n", dirname);
399 } else {
400 warning("could not open directory %s: %s\n", dirname, strerror(errno));
401 }
402 }
403 return dirp;
404 }
405 int fd = result;
406
407 // Determine if the open directory is secure.
408 if (!is_dirfd_secure(fd)) {
409 // The directory is not a secure directory.
410 os::close(fd);
411 return dirp;
875 int saved_cwd_fd;
876 // Open the directory and set the current working directory to it.
877 DIR* dirp = open_directory_secure_cwd(dirname, &saved_cwd_fd);
878 if (dirp == NULL) {
879 // Directory doesn't exist or is insecure, so cannot create shared
880 // memory file.
881 return -1;
882 }
883
884 // Open the filename in the current directory.
885 // Cannot use O_TRUNC here; truncation of an existing file has to happen
886 // after the is_file_secure() check below.
887 int result;
888
889 // No O_NOFOLLOW defined at buildtime, and it is not documented for open;
890 // so provide a workaround in this case.
891 #ifdef O_NOFOLLOW
892 RESTARTABLE(::open(filename, O_RDWR|O_CREAT|O_NOFOLLOW, S_IREAD|S_IWRITE), result);
893 #else
894 // workaround function (jdk6 code)
895 result = open_o_nofollow(filename, O_RDWR|O_CREAT, S_IREAD|S_IWRITE);
896 #endif
897
898 if (result == OS_ERR) {
899 if (PrintMiscellaneous && Verbose) {
900 if (errno == ELOOP) {
901 warning("file %s is a symlink and is not secure\n", filename);
902 } else {
903 warning("could not create file %s: %s\n", filename, strerror(errno));
904 }
905 }
906 // Close the directory and reset the current working directory.
907 close_directory_secure_cwd(dirp, saved_cwd_fd);
908
909 return -1;
910 }
911 // Close the directory and reset the current working directory.
912 close_directory_secure_cwd(dirp, saved_cwd_fd);
913
914 // save the file descriptor
915 int fd = result;
918 if (!is_file_secure(fd, filename)) {
919 ::close(fd);
920 return -1;
921 }
922
923 // Truncate the file to get rid of any existing data.
924 RESTARTABLE(::ftruncate(fd, (off_t)0), result);
925 if (result == OS_ERR) {
926 if (PrintMiscellaneous && Verbose) {
927 warning("could not truncate shared memory file: %s\n", strerror(errno));
928 }
929 ::close(fd);
930 return -1;
931 }
932 // set the file size
933 RESTARTABLE(::ftruncate(fd, (off_t)size), result);
934 if (result == OS_ERR) {
935 if (PrintMiscellaneous && Verbose) {
936 warning("could not set shared memory file size: %s\n", strerror(errno));
937 }
938 ::close(fd);
939 return -1;
940 }
941
942 return fd;
943 }
944
945 // open the shared memory file for the given user and vmid. returns
946 // the file descriptor for the open file or -1 if the file could not
947 // be opened.
948 //
949 static int open_sharedmem_file(const char* filename, int oflags, TRAPS) {
950
951 // open the file
952 int result;
953 // No O_NOFOLLOW defined at buildtime, and it is not documented for open;
954 // so provide a workaround in this case
955 #ifdef O_NOFOLLOW
956 RESTARTABLE(::open(filename, oflags), result);
957 #else
958 open_o_nofollow(filename, oflags);
959 #endif
960
961 if (result == OS_ERR) {
962 if (errno == ENOENT) {
963 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(),
964 "Process not found");
965 }
966 else if (errno == EACCES) {
967 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(),
968 "Permission denied");
969 }
970 else {
971 THROW_MSG_0(vmSymbols::java_io_IOException(), strerror(errno));
972 }
973 }
974 int fd = result;
975
976 // Check to see if the file is secure.
977 if (!is_file_secure(fd, filename)) {
978 ::close(fd);
993 // the file system name of the shared memory object. However, it may
994 // be convenient for applications to discover the existence of newly
995 // created and terminating JVMs by watching the file system name space
996 // for files being created or removed.
997 //
998 static char* mmap_create_shared(size_t size) {
999
1000 int result;
1001 int fd;
1002 char* mapAddress;
1003
1004 int vmid = os::current_process_id();
1005
1006 char* user_name = get_user_name(geteuid());
1007
1008 if (user_name == NULL)
1009 return NULL;
1010
1011 char* dirname = get_user_tmp_dir(user_name);
1012 char* filename = get_sharedmem_filename(dirname, vmid);
1013 // get the short filename.
1014 char* short_filename = strrchr(filename, '/');
1015 if (short_filename == NULL) {
1016 short_filename = filename;
1017 } else {
1018 short_filename++;
1019 }
1020
1021 // cleanup any stale shared memory files
1022 cleanup_sharedmem_resources(dirname);
1023
1024 assert(((size > 0) && (size % os::vm_page_size() == 0)),
1025 "unexpected PerfMemory region size");
1026
1027 fd = create_sharedmem_resources(dirname, short_filename, size);
1028
1029 FREE_C_HEAP_ARRAY(char, user_name);
1030 FREE_C_HEAP_ARRAY(char, dirname);
1031
1032 if (fd == -1) {
1033 FREE_C_HEAP_ARRAY(char, filename);
1034 return NULL;
1035 }
1036
1037 mapAddress = (char*)::mmap((char*)0, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
1038
1039 result = ::close(fd);
1040 assert(result != OS_ERR, "could not close file");
1041
1042 if (mapAddress == MAP_FAILED) {
1043 if (PrintMiscellaneous && Verbose) {
1044 warning("mmap failed - %s\n", strerror(errno));
1045 }
1046 remove_file(filename);
1047 FREE_C_HEAP_ARRAY(char, filename);
1048 return NULL;
1049 }
1050
1051 // save the file name for use in delete_shared_memory()
1052 backing_store_file_name = filename;
1053
1054 // clear the shared memory region
1055 (void)::memset((void*) mapAddress, 0, size);
1056
1057 // It does not go through os api, the operation has to record from here.
1058 MemTracker::record_virtual_memory_reserve((address)mapAddress, size, CURRENT_PC, mtInternal);
1059
1126
1127 // attach to a named shared memory region.
1128 //
1129 static void mmap_attach_shared(const char* user, int vmid, PerfMemory::PerfMemoryMode mode, char** addr, size_t* sizep, TRAPS) {
1130
1131 char* mapAddress;
1132 int result;
1133 int fd;
1134 size_t size = 0;
1135 const char* luser = NULL;
1136
1137 int mmap_prot;
1138 int file_flags;
1139
1140 ResourceMark rm;
1141
1142 // map the high level access mode to the appropriate permission
1143 // constructs for the file and the shared memory mapping.
1144 if (mode == PerfMemory::PERF_MODE_RO) {
1145 mmap_prot = PROT_READ;
1146 // No O_NOFOLLOW defined at buildtime, and it is not documented for open.
1147 #ifdef O_NOFOLLOW
1148 file_flags = O_RDONLY | O_NOFOLLOW;
1149 #else
1150 file_flags = O_RDONLY;
1151 #endif
1152 }
1153 else if (mode == PerfMemory::PERF_MODE_RW) {
1154 #ifdef LATER
1155 mmap_prot = PROT_READ | PROT_WRITE;
1156 file_flags = O_RDWR | O_NOFOLLOW;
1157 #else
1158 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
1159 "Unsupported access mode");
1160 #endif
1161 }
1162 else {
1163 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
1164 "Illegal access mode");
1165 }
1188 }
1189 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
1190 "Process not found");
1191 }
1192
1193 char* filename = get_sharedmem_filename(dirname, vmid);
1194
1195 // copy heap memory to resource memory. the open_sharedmem_file
1196 // method below need to use the filename, but could throw an
1197 // exception. using a resource array prevents the leak that
1198 // would otherwise occur.
1199 char* rfilename = NEW_RESOURCE_ARRAY(char, strlen(filename) + 1);
1200 strcpy(rfilename, filename);
1201
1202 // free the c heap resources that are no longer needed
1203 if (luser != user) FREE_C_HEAP_ARRAY(char, luser);
1204 FREE_C_HEAP_ARRAY(char, dirname);
1205 FREE_C_HEAP_ARRAY(char, filename);
1206
1207 // open the shared memory file for the give vmid
1208 fd = open_sharedmem_file(rfilename, file_flags, THREAD);
1209
1210 if (fd == OS_ERR) {
1211 return;
1212 }
1213
1214 if (HAS_PENDING_EXCEPTION) {
1215 ::close(fd);
1216 return;
1217 }
1218
1219 if (*sizep == 0) {
1220 size = sharedmem_filesize(fd, CHECK);
1221 } else {
1222 size = *sizep;
1223 }
1224
1225 assert(size > 0, "unexpected size <= 0");
1226
1227 mapAddress = (char*)::mmap((char*)0, size, mmap_prot, MAP_SHARED, fd, 0);
1228
1229 result = ::close(fd);
1230 assert(result != OS_ERR, "could not close file");
1231
1232 if (mapAddress == MAP_FAILED) {
1233 if (PrintMiscellaneous && Verbose) {
1234 warning("mmap failed: %s\n", strerror(errno));
1235 }
1236 THROW_MSG(vmSymbols::java_lang_OutOfMemoryError(),
1237 "Could not map PerfMemory");
1238 }
1239
1240 // it does not go through os api, the operation has to record from here.
1241 MemTracker::record_virtual_memory_reserve((address)mapAddress, size, CURRENT_PC, mtInternal);
1242
1243 *addr = mapAddress;
1244 *sizep = size;
1245
1246 if (PerfTraceMemOps) {
1247 tty->print("mapped " SIZE_FORMAT " bytes for vmid %d at "
1248 INTPTR_FORMAT "\n", size, vmid, p2i((void*)mapAddress));
1249 }
1250 }
1251
1252
1253
1254
1255 // create the PerfData memory region
1256 //
1257 // This method creates the memory region used to store performance
1258 // data for the JVM. The memory may be created in standard or
1259 // shared memory.
1260 //
1261 void PerfMemory::create_memory_region(size_t size) {
1262
1263 if (PerfDisableSharedMem) {
1264 // do not share the memory for the performance data.
1265 _start = create_standard_memory(size);
1266 }
1267 else {
1268 _start = create_shared_memory(size);
|