1 /* |
1 /* |
2 * Copyright (c) 2001, 2015, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 2001, 2016, Oracle and/or its affiliates. All rights reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
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 |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. |
7 * published by the Free Software Foundation. |
27 #include "memory/allocation.inline.hpp" |
27 #include "memory/allocation.inline.hpp" |
28 #include "memory/resourceArea.hpp" |
28 #include "memory/resourceArea.hpp" |
29 #include "oops/oop.inline.hpp" |
29 #include "oops/oop.inline.hpp" |
30 #include "os_bsd.inline.hpp" |
30 #include "os_bsd.inline.hpp" |
31 #include "runtime/handles.inline.hpp" |
31 #include "runtime/handles.inline.hpp" |
|
32 #include "runtime/os.hpp" |
32 #include "runtime/perfMemory.hpp" |
33 #include "runtime/perfMemory.hpp" |
33 #include "services/memTracker.hpp" |
34 #include "services/memTracker.hpp" |
34 #include "utilities/exceptions.hpp" |
35 #include "utilities/exceptions.hpp" |
35 |
36 |
36 // put OS-includes here |
37 // put OS-includes here |
98 RESTARTABLE(::open(destfile, O_CREAT|O_WRONLY|O_TRUNC, S_IREAD|S_IWRITE), |
99 RESTARTABLE(::open(destfile, O_CREAT|O_WRONLY|O_TRUNC, S_IREAD|S_IWRITE), |
99 result);; |
100 result);; |
100 if (result == OS_ERR) { |
101 if (result == OS_ERR) { |
101 if (PrintMiscellaneous && Verbose) { |
102 if (PrintMiscellaneous && Verbose) { |
102 warning("Could not create Perfdata save file: %s: %s\n", |
103 warning("Could not create Perfdata save file: %s: %s\n", |
103 destfile, strerror(errno)); |
104 destfile, os::strerror(errno)); |
104 } |
105 } |
105 } else { |
106 } else { |
106 int fd = result; |
107 int fd = result; |
107 |
108 |
108 for (size_t remaining = size; remaining > 0;) { |
109 for (size_t remaining = size; remaining > 0;) { |
109 |
110 |
110 RESTARTABLE(::write(fd, addr, remaining), result); |
111 RESTARTABLE(::write(fd, addr, remaining), result); |
111 if (result == OS_ERR) { |
112 if (result == OS_ERR) { |
112 if (PrintMiscellaneous && Verbose) { |
113 if (PrintMiscellaneous && Verbose) { |
113 warning("Could not write Perfdata save file: %s: %s\n", |
114 warning("Could not write Perfdata save file: %s: %s\n", |
114 destfile, strerror(errno)); |
115 destfile, os::strerror(errno)); |
115 } |
116 } |
116 break; |
117 break; |
117 } |
118 } |
118 |
119 |
119 remaining -= (size_t)result; |
120 remaining -= (size_t)result; |
121 } |
122 } |
122 |
123 |
123 result = ::close(fd); |
124 result = ::close(fd); |
124 if (PrintMiscellaneous && Verbose) { |
125 if (PrintMiscellaneous && Verbose) { |
125 if (result == OS_ERR) { |
126 if (result == OS_ERR) { |
126 warning("Could not close %s: %s\n", destfile, strerror(errno)); |
127 warning("Could not close %s: %s\n", destfile, os::strerror(errno)); |
127 } |
128 } |
128 } |
129 } |
129 } |
130 } |
130 FREE_C_HEAP_ARRAY(char, destfile); |
131 FREE_C_HEAP_ARRAY(char, destfile); |
131 } |
132 } |
307 // Directory doesn't exist or is a symlink, so there is nothing to cleanup. |
308 // Directory doesn't exist or is a symlink, so there is nothing to cleanup. |
308 if (PrintMiscellaneous && Verbose) { |
309 if (PrintMiscellaneous && Verbose) { |
309 if (errno == ELOOP) { |
310 if (errno == ELOOP) { |
310 warning("directory %s is a symlink and is not secure\n", dirname); |
311 warning("directory %s is a symlink and is not secure\n", dirname); |
311 } else { |
312 } else { |
312 warning("could not open directory %s: %s\n", dirname, strerror(errno)); |
313 warning("could not open directory %s: %s\n", dirname, os::strerror(errno)); |
313 } |
314 } |
314 } |
315 } |
315 return dirp; |
316 return dirp; |
316 } |
317 } |
317 int fd = result; |
318 int fd = result; |
418 |
419 |
419 // Determine if the file is secure. |
420 // Determine if the file is secure. |
420 RESTARTABLE(::fstat(fd, &statbuf), result); |
421 RESTARTABLE(::fstat(fd, &statbuf), result); |
421 if (result == OS_ERR) { |
422 if (result == OS_ERR) { |
422 if (PrintMiscellaneous && Verbose) { |
423 if (PrintMiscellaneous && Verbose) { |
423 warning("fstat failed on %s: %s\n", filename, strerror(errno)); |
424 warning("fstat failed on %s: %s\n", filename, os::strerror(errno)); |
424 } |
425 } |
425 return false; |
426 return false; |
426 } |
427 } |
427 if (statbuf.st_nlink > 1) { |
428 if (statbuf.st_nlink > 1) { |
428 // A file with multiple links is not expected. |
429 // A file with multiple links is not expected. |
457 |
458 |
458 if (result != 0 || p == NULL || p->pw_name == NULL || *(p->pw_name) == '\0') { |
459 if (result != 0 || p == NULL || p->pw_name == NULL || *(p->pw_name) == '\0') { |
459 if (PrintMiscellaneous && Verbose) { |
460 if (PrintMiscellaneous && Verbose) { |
460 if (result != 0) { |
461 if (result != 0) { |
461 warning("Could not retrieve passwd entry: %s\n", |
462 warning("Could not retrieve passwd entry: %s\n", |
462 strerror(result)); |
463 os::strerror(result)); |
463 } |
464 } |
464 else if (p == NULL) { |
465 else if (p == NULL) { |
465 // this check is added to protect against an observed problem |
466 // this check is added to protect against an observed problem |
466 // with getpwuid_r() on RedHat 9 where getpwuid_r returns 0, |
467 // with getpwuid_r() on RedHat 9 where getpwuid_r returns 0, |
467 // indicating success, but has p == NULL. This was observed when |
468 // indicating success, but has p == NULL. This was observed when |
471 // is safe under any condition, but the use of errno in the output |
472 // is safe under any condition, but the use of errno in the output |
472 // message may result in an erroneous message. |
473 // message may result in an erroneous message. |
473 // Bug Id 89052 was opened with RedHat. |
474 // Bug Id 89052 was opened with RedHat. |
474 // |
475 // |
475 warning("Could not retrieve passwd entry: %s\n", |
476 warning("Could not retrieve passwd entry: %s\n", |
476 strerror(errno)); |
477 os::strerror(errno)); |
477 } |
478 } |
478 else { |
479 else { |
479 warning("Could not determine user name: %s\n", |
480 warning("Could not determine user name: %s\n", |
480 p->pw_name == NULL ? "pw_name = NULL" : |
481 p->pw_name == NULL ? "pw_name = NULL" : |
481 "pw_name zero length"); |
482 "pw_name zero length"); |
507 if (errno == ESRCH) { |
508 if (errno == ESRCH) { |
508 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), |
509 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), |
509 "Process not found"); |
510 "Process not found"); |
510 } |
511 } |
511 else /* EPERM */ { |
512 else /* EPERM */ { |
512 THROW_MSG_0(vmSymbols::java_io_IOException(), strerror(errno)); |
513 THROW_MSG_0(vmSymbols::java_io_IOException(), os::strerror(errno)); |
513 } |
514 } |
514 } |
515 } |
515 |
516 |
516 // directory search |
517 // directory search |
517 char* oldest_user = NULL; |
518 char* oldest_user = NULL; |
650 // |
651 // |
651 RESTARTABLE(::unlink(path), result); |
652 RESTARTABLE(::unlink(path), result); |
652 if (PrintMiscellaneous && Verbose && result == OS_ERR) { |
653 if (PrintMiscellaneous && Verbose && result == OS_ERR) { |
653 if (errno != ENOENT) { |
654 if (errno != ENOENT) { |
654 warning("Could not unlink shared memory backing" |
655 warning("Could not unlink shared memory backing" |
655 " store file %s : %s\n", path, strerror(errno)); |
656 " store file %s : %s\n", path, os::strerror(errno)); |
656 } |
657 } |
657 } |
658 } |
658 } |
659 } |
659 |
660 |
660 |
661 |
760 // we encountered some other failure while attempting |
761 // we encountered some other failure while attempting |
761 // to create the directory |
762 // to create the directory |
762 // |
763 // |
763 if (PrintMiscellaneous && Verbose) { |
764 if (PrintMiscellaneous && Verbose) { |
764 warning("could not create directory %s: %s\n", |
765 warning("could not create directory %s: %s\n", |
765 dirname, strerror(errno)); |
766 dirname, os::strerror(errno)); |
766 } |
767 } |
767 return false; |
768 return false; |
768 } |
769 } |
769 } |
770 } |
770 return true; |
771 return true; |
802 if (result == OS_ERR) { |
803 if (result == OS_ERR) { |
803 if (PrintMiscellaneous && Verbose) { |
804 if (PrintMiscellaneous && Verbose) { |
804 if (errno == ELOOP) { |
805 if (errno == ELOOP) { |
805 warning("file %s is a symlink and is not secure\n", filename); |
806 warning("file %s is a symlink and is not secure\n", filename); |
806 } else { |
807 } else { |
807 warning("could not create file %s: %s\n", filename, strerror(errno)); |
808 warning("could not create file %s: %s\n", filename, os::strerror(errno)); |
808 } |
809 } |
809 } |
810 } |
810 // close the directory and reset the current working directory |
811 // close the directory and reset the current working directory |
811 close_directory_secure_cwd(dirp, saved_cwd_fd); |
812 close_directory_secure_cwd(dirp, saved_cwd_fd); |
812 |
813 |
826 |
827 |
827 // truncate the file to get rid of any existing data |
828 // truncate the file to get rid of any existing data |
828 RESTARTABLE(::ftruncate(fd, (off_t)0), result); |
829 RESTARTABLE(::ftruncate(fd, (off_t)0), result); |
829 if (result == OS_ERR) { |
830 if (result == OS_ERR) { |
830 if (PrintMiscellaneous && Verbose) { |
831 if (PrintMiscellaneous && Verbose) { |
831 warning("could not truncate shared memory file: %s\n", strerror(errno)); |
832 warning("could not truncate shared memory file: %s\n", os::strerror(errno)); |
832 } |
833 } |
833 ::close(fd); |
834 ::close(fd); |
834 return -1; |
835 return -1; |
835 } |
836 } |
836 // set the file size |
837 // set the file size |
837 RESTARTABLE(::ftruncate(fd, (off_t)size), result); |
838 RESTARTABLE(::ftruncate(fd, (off_t)size), result); |
838 if (result == OS_ERR) { |
839 if (result == OS_ERR) { |
839 if (PrintMiscellaneous && Verbose) { |
840 if (PrintMiscellaneous && Verbose) { |
840 warning("could not set shared memory file size: %s\n", strerror(errno)); |
841 warning("could not set shared memory file size: %s\n", os::strerror(errno)); |
841 } |
842 } |
842 ::close(fd); |
843 ::close(fd); |
843 return -1; |
844 return -1; |
844 } |
845 } |
845 |
846 |
885 else if (errno == EACCES) { |
886 else if (errno == EACCES) { |
886 THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(), |
887 THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(), |
887 "Permission denied", OS_ERR); |
888 "Permission denied", OS_ERR); |
888 } |
889 } |
889 else { |
890 else { |
890 THROW_MSG_(vmSymbols::java_io_IOException(), strerror(errno), OS_ERR); |
891 THROW_MSG_(vmSymbols::java_io_IOException(), os::strerror(errno), OS_ERR); |
891 } |
892 } |
892 } |
893 } |
893 int fd = result; |
894 int fd = result; |
894 |
895 |
895 // check to see if the file is secure |
896 // check to see if the file is secure |
959 result = ::close(fd); |
960 result = ::close(fd); |
960 assert(result != OS_ERR, "could not close file"); |
961 assert(result != OS_ERR, "could not close file"); |
961 |
962 |
962 if (mapAddress == MAP_FAILED) { |
963 if (mapAddress == MAP_FAILED) { |
963 if (PrintMiscellaneous && Verbose) { |
964 if (PrintMiscellaneous && Verbose) { |
964 warning("mmap failed - %s\n", strerror(errno)); |
965 warning("mmap failed - %s\n", os::strerror(errno)); |
965 } |
966 } |
966 remove_file(filename); |
967 remove_file(filename); |
967 FREE_C_HEAP_ARRAY(char, filename); |
968 FREE_C_HEAP_ARRAY(char, filename); |
968 return NULL; |
969 return NULL; |
969 } |
970 } |
1023 int result; |
1024 int result; |
1024 |
1025 |
1025 RESTARTABLE(::fstat(fd, &statbuf), result); |
1026 RESTARTABLE(::fstat(fd, &statbuf), result); |
1026 if (result == OS_ERR) { |
1027 if (result == OS_ERR) { |
1027 if (PrintMiscellaneous && Verbose) { |
1028 if (PrintMiscellaneous && Verbose) { |
1028 warning("fstat failed: %s\n", strerror(errno)); |
1029 warning("fstat failed: %s\n", os::strerror(errno)); |
1029 } |
1030 } |
1030 THROW_MSG_0(vmSymbols::java_io_IOException(), |
1031 THROW_MSG_0(vmSymbols::java_io_IOException(), |
1031 "Could not determine PerfMemory size"); |
1032 "Could not determine PerfMemory size"); |
1032 } |
1033 } |
1033 |
1034 |
1134 result = ::close(fd); |
1135 result = ::close(fd); |
1135 assert(result != OS_ERR, "could not close file"); |
1136 assert(result != OS_ERR, "could not close file"); |
1136 |
1137 |
1137 if (mapAddress == MAP_FAILED) { |
1138 if (mapAddress == MAP_FAILED) { |
1138 if (PrintMiscellaneous && Verbose) { |
1139 if (PrintMiscellaneous && Verbose) { |
1139 warning("mmap failed: %s\n", strerror(errno)); |
1140 warning("mmap failed: %s\n", os::strerror(errno)); |
1140 } |
1141 } |
1141 THROW_MSG(vmSymbols::java_lang_OutOfMemoryError(), |
1142 THROW_MSG(vmSymbols::java_lang_OutOfMemoryError(), |
1142 "Could not map PerfMemory"); |
1143 "Could not map PerfMemory"); |
1143 } |
1144 } |
1144 |
1145 |