1 /* 2 * Copyright (c) 2013, 2015, 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 */ 23 24 package jdk.testlibrary; 25 import java.util.regex.Pattern; 26 import java.io.RandomAccessFile; 27 import java.io.FileNotFoundException; 28 import java.io.IOException; 29 30 /** 31 * @deprecated This class is deprecated. Use the one from 32 * {@code <root>/test/lib/share/classes/jdk/test/lib} 33 */ 34 @Deprecated 35 public class Platform { 36 private static final String osName = System.getProperty("os.name"); 37 private static final String dataModel = System.getProperty("sun.arch.data.model"); 38 private static final String vmVersion = System.getProperty("java.vm.version"); 39 private static final String jdkDebug = System.getProperty("jdk.debug"); 40 private static final String osArch = System.getProperty("os.arch"); 41 private static final String vmName = System.getProperty("java.vm.name"); 42 private static final String userName = System.getProperty("user.name"); 43 private static final String compiler = System.getProperty("sun.management.compiler"); 44 45 public static boolean isClient() { 46 return vmName.endsWith(" Client VM"); 47 } 48 49 public static boolean isServer() { 50 return vmName.endsWith(" Server VM"); 51 } 52 53 public static boolean isGraal() { 54 return vmName.endsWith(" Graal VM"); 55 } 56 57 public static boolean isMinimal() { 58 return vmName.endsWith(" Minimal VM"); 59 } 60 61 public static boolean isEmbedded() { 62 return vmName.contains("Embedded"); 63 } 64 65 public static boolean isTieredSupported() { 66 return compiler.contains("Tiered Compilers"); 67 } 68 69 70 public static boolean is32bit() { 71 return dataModel.equals("32"); 72 } 73 74 public static boolean is64bit() { 75 return dataModel.equals("64"); 76 } 77 78 public static boolean isAix() { 79 return isOs("aix"); 80 } 81 82 public static boolean isLinux() { 83 return isOs("linux"); 84 } 85 86 public static boolean isOSX() { 87 return isOs("mac"); 88 } 89 90 public static boolean isSolaris() { 91 return isOs("sunos"); 92 } 93 94 public static boolean isWindows() { 95 return isOs("win"); 96 } 97 98 private static boolean isOs(String osname) { 99 return osName.toLowerCase().startsWith(osname.toLowerCase()); 100 } 101 102 public static String getOsName() { 103 return osName; 104 } 105 106 public static boolean isDebugBuild() { 107 return (jdkDebug.toLowerCase().contains("debug")); 108 } 109 110 public static String getVMVersion() { 111 return vmVersion; 112 } 113 114 // Returns true for sparc and sparcv9. 115 public static boolean isSparc() { 116 return isArch("sparc.*"); 117 } 118 119 public static boolean isARM() { 120 return isArch("arm.*"); 121 } 122 123 public static boolean isPPC() { 124 return isArch("ppc.*"); 125 } 126 127 public static boolean isX86() { 128 // On Linux it's 'i386', Windows 'x86' without '_64' suffix. 129 return isArch("(i386)|(x86(?!_64))"); 130 } 131 132 public static boolean isX64() { 133 // On OSX it's 'x86_64' and on other (Linux, Windows and Solaris) platforms it's 'amd64' 134 return isArch("(amd64)|(x86_64)"); 135 } 136 137 private static boolean isArch(String archnameRE) { 138 return Pattern.compile(archnameRE, Pattern.CASE_INSENSITIVE) 139 .matcher(osArch) 140 .matches(); 141 } 142 143 public static String getOsArch() { 144 return osArch; 145 } 146 147 /** 148 * Return a boolean for whether we expect to be able to attach 149 * the SA to our own processes on this system. 150 */ 151 public static boolean shouldSAAttach() 152 throws IOException { 153 154 if (isAix()) { 155 return false; // SA not implemented. 156 } else if (isLinux()) { 157 return canPtraceAttachLinux(); 158 } else if (isOSX()) { 159 return canAttachOSX(); 160 } else { 161 // Other platforms expected to work: 162 return true; 163 } 164 } 165 166 /** 167 * On Linux, first check the SELinux boolean "deny_ptrace" and return false 168 * as we expect to be denied if that is "1". 169 */ 170 public static boolean canPtraceAttachLinux() 171 throws IOException { 172 173 // SELinux deny_ptrace: 174 try(RandomAccessFile file = new RandomAccessFile("/sys/fs/selinux/booleans/deny_ptrace", "r")) { 175 if (file.readByte() != '0') { 176 return false; 177 } 178 } 179 catch(FileNotFoundException ex) { 180 // Ignored 181 } 182 183 // YAMA enhanced security ptrace_scope: 184 // 0 - a process can PTRACE_ATTACH to any other process running under the same uid 185 // 1 - restricted ptrace: a process must be a children of the inferior or user is root 186 // 2 - only processes with CAP_SYS_PTRACE may use ptrace or user is root 187 // 3 - no attach: no processes may use ptrace with PTRACE_ATTACH 188 189 try(RandomAccessFile file = new RandomAccessFile("/proc/sys/kernel/yama/ptrace_scope", "r")) { 190 byte yama_scope = file.readByte(); 191 if (yama_scope == '3') { 192 return false; 193 } 194 195 if (!userName.equals("root") && yama_scope != '0') { 196 return false; 197 } 198 } 199 catch(FileNotFoundException ex) { 200 // Ignored 201 } 202 203 // Otherwise expect to be permitted: 204 return true; 205 } 206 207 /** 208 * On OSX, expect permission to attach only if we are root. 209 */ 210 public static boolean canAttachOSX() { 211 return userName.equals("root"); 212 } 213 }