1 /*
2 * Copyright (c) 2001, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
414 //
415 static char* get_user_name(int vmid, TRAPS) {
416
417 char psinfo_name[PATH_MAX];
418 int result;
419
420 snprintf(psinfo_name, PATH_MAX, "/proc/%d/psinfo", vmid);
421
422 RESTARTABLE(::open(psinfo_name, O_RDONLY), result);
423
424 if (result != OS_ERR) {
425 int fd = result;
426
427 psinfo_t psinfo;
428 char* addr = (char*)&psinfo;
429
430 for (size_t remaining = sizeof(psinfo_t); remaining > 0;) {
431
432 RESTARTABLE(::read(fd, addr, remaining), result);
433 if (result == OS_ERR) {
434 THROW_MSG_0(vmSymbols::java_io_IOException(), "Read error");
435 }
436 remaining-=result;
437 addr+=result;
438 }
439
440 ::close(fd);
441
442 // get the user name for the effective user id of the process
443 char* user_name = get_user_name(psinfo.pr_euid);
444
445 return user_name;
446 }
447
448 if (result == OS_ERR && errno == EACCES) {
449
450 // In this case, the psinfo file for the process id existed,
451 // but we didn't have permission to access it.
452 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(),
453 strerror(errno));
454 }
455
456 // at this point, we don't know if the process id itself doesn't
457 // exist or if the psinfo file doesn't exit. If the psinfo file
458 // doesn't exist, then we are running on Solaris 2.5.1 or earlier.
889 FREE_C_HEAP_ARRAY(char, dirname, mtInternal);
890 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
891 "Process not found");
892 }
893
894 char* filename = get_sharedmem_filename(dirname, vmid);
895
896 // copy heap memory to resource memory. the open_sharedmem_file
897 // method below need to use the filename, but could throw an
898 // exception. using a resource array prevents the leak that
899 // would otherwise occur.
900 char* rfilename = NEW_RESOURCE_ARRAY(char, strlen(filename) + 1);
901 strcpy(rfilename, filename);
902
903 // free the c heap resources that are no longer needed
904 if (luser != user) FREE_C_HEAP_ARRAY(char, luser, mtInternal);
905 FREE_C_HEAP_ARRAY(char, dirname, mtInternal);
906 FREE_C_HEAP_ARRAY(char, filename, mtInternal);
907
908 // open the shared memory file for the give vmid
909 fd = open_sharedmem_file(rfilename, file_flags, CHECK);
910 assert(fd != OS_ERR, "unexpected value");
911
912 if (*sizep == 0) {
913 size = sharedmem_filesize(fd, CHECK);
914 } else {
915 size = *sizep;
916 }
917
918 assert(size > 0, "unexpected size <= 0");
919
920 mapAddress = (char*)::mmap((char*)0, size, mmap_prot, MAP_SHARED, fd, 0);
921
922 result = ::close(fd);
923 assert(result != OS_ERR, "could not close file");
924
925 if (mapAddress == MAP_FAILED) {
926 if (PrintMiscellaneous && Verbose) {
927 warning("mmap failed: %s\n", strerror(errno));
928 }
929 THROW_MSG(vmSymbols::java_lang_OutOfMemoryError(),
930 "Could not map PerfMemory");
931 }
|
1 /*
2 * Copyright (c) 2001, 2014, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
414 //
415 static char* get_user_name(int vmid, TRAPS) {
416
417 char psinfo_name[PATH_MAX];
418 int result;
419
420 snprintf(psinfo_name, PATH_MAX, "/proc/%d/psinfo", vmid);
421
422 RESTARTABLE(::open(psinfo_name, O_RDONLY), result);
423
424 if (result != OS_ERR) {
425 int fd = result;
426
427 psinfo_t psinfo;
428 char* addr = (char*)&psinfo;
429
430 for (size_t remaining = sizeof(psinfo_t); remaining > 0;) {
431
432 RESTARTABLE(::read(fd, addr, remaining), result);
433 if (result == OS_ERR) {
434 ::close(fd);
435 THROW_MSG_0(vmSymbols::java_io_IOException(), "Read error");
436 } else {
437 remaining-=result;
438 addr+=result;
439 }
440 }
441
442 ::close(fd);
443
444 // get the user name for the effective user id of the process
445 char* user_name = get_user_name(psinfo.pr_euid);
446
447 return user_name;
448 }
449
450 if (result == OS_ERR && errno == EACCES) {
451
452 // In this case, the psinfo file for the process id existed,
453 // but we didn't have permission to access it.
454 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(),
455 strerror(errno));
456 }
457
458 // at this point, we don't know if the process id itself doesn't
459 // exist or if the psinfo file doesn't exit. If the psinfo file
460 // doesn't exist, then we are running on Solaris 2.5.1 or earlier.
891 FREE_C_HEAP_ARRAY(char, dirname, mtInternal);
892 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
893 "Process not found");
894 }
895
896 char* filename = get_sharedmem_filename(dirname, vmid);
897
898 // copy heap memory to resource memory. the open_sharedmem_file
899 // method below need to use the filename, but could throw an
900 // exception. using a resource array prevents the leak that
901 // would otherwise occur.
902 char* rfilename = NEW_RESOURCE_ARRAY(char, strlen(filename) + 1);
903 strcpy(rfilename, filename);
904
905 // free the c heap resources that are no longer needed
906 if (luser != user) FREE_C_HEAP_ARRAY(char, luser, mtInternal);
907 FREE_C_HEAP_ARRAY(char, dirname, mtInternal);
908 FREE_C_HEAP_ARRAY(char, filename, mtInternal);
909
910 // open the shared memory file for the give vmid
911 fd = open_sharedmem_file(rfilename, file_flags, THREAD);
912
913 if (fd == OS_ERR) {
914 return;
915 }
916
917 if (HAS_PENDING_EXCEPTION) {
918 ::close(fd);
919 return;
920 }
921
922 if (*sizep == 0) {
923 size = sharedmem_filesize(fd, CHECK);
924 } else {
925 size = *sizep;
926 }
927
928 assert(size > 0, "unexpected size <= 0");
929
930 mapAddress = (char*)::mmap((char*)0, size, mmap_prot, MAP_SHARED, fd, 0);
931
932 result = ::close(fd);
933 assert(result != OS_ERR, "could not close file");
934
935 if (mapAddress == MAP_FAILED) {
936 if (PrintMiscellaneous && Verbose) {
937 warning("mmap failed: %s\n", strerror(errno));
938 }
939 THROW_MSG(vmSymbols::java_lang_OutOfMemoryError(),
940 "Could not map PerfMemory");
941 }
|