1 /*
   2  * Copyright (c) 2013, 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  */
  23 
  24 package com.oracle.java.testlibrary;
  25 
  26 import java.util.regex.Pattern;
  27 import com.oracle.java.testlibrary.Utils;
  28 
  29 public class Platform {
  30     private static final String osName      = System.getProperty("os.name");
  31     private static final String dataModel   = System.getProperty("sun.arch.data.model");
  32     private static final String vmVersion   = System.getProperty("java.vm.version");
  33     private static final String javaVersion = System.getProperty("java.version");
  34     private static final String osArch      = System.getProperty("os.arch");
  35     private static final String vmName      = System.getProperty("java.vm.name");
  36     private static final String userName    = System.getProperty("user.name");
  37 
  38     public static boolean isClient() {
  39         return vmName.endsWith(" Client VM");
  40     }
  41 
  42     public static boolean isServer() {
  43         return vmName.endsWith(" Server VM");
  44     }
  45 
  46     public static boolean isGraal() {
  47         return vmName.endsWith(" Graal VM");
  48     }
  49 
  50     public static boolean isMinimal() {
  51         return vmName.endsWith(" Minimal VM");
  52     }
  53 
  54     public static boolean isEmbedded() {
  55         return vmName.contains("Embedded");
  56     }
  57 
  58     public static boolean is32bit() {
  59         return dataModel.equals("32");
  60     }
  61 
  62     public static boolean is64bit() {
  63         return dataModel.equals("64");
  64     }
  65 
  66     public static boolean isSolaris() {
  67         return isOs("sunos");
  68     }
  69 
  70     public static boolean isWindows() {
  71         return isOs("win");
  72     }
  73 
  74     public static boolean isOSX() {
  75         return isOs("mac");
  76     }
  77 
  78     public static boolean isLinux() {
  79         return isOs("linux");
  80     }
  81 
  82     private static boolean isOs(String osname) {
  83         return osName.toLowerCase().startsWith(osname.toLowerCase());
  84     }
  85 
  86     public static String getOsName() {
  87         return osName;
  88     }
  89 
  90     public static boolean isDebugBuild() {
  91         return (vmVersion.toLowerCase().contains("debug") ||
  92                 javaVersion.toLowerCase().contains("debug"));
  93     }
  94 
  95     public static String getVMVersion() {
  96         return vmVersion;
  97     }
  98 
  99     // Returns true for sparc and sparcv9.
 100     public static boolean isSparc() {
 101         return isArch("sparc.*");
 102     }
 103 
 104     public static boolean isARM() {
 105         return isArch("arm.*");
 106     }
 107 
 108     public static boolean isPPC() {
 109         return isArch("ppc.*");
 110     }
 111 
 112     public static boolean isX86() {
 113         // On Linux it's 'i386', Windows 'x86' without '_64' suffix.
 114         return isArch("(i386)|(x86(?!_64))");
 115     }
 116 
 117     public static boolean isX64() {
 118         // On OSX it's 'x86_64' and on other (Linux, Windows and Solaris) platforms it's 'amd64'
 119         return isArch("(amd64)|(x86_64)");
 120     }
 121 
 122     private static boolean isArch(String archnameRE) {
 123         return Pattern.compile(archnameRE, Pattern.CASE_INSENSITIVE)
 124                 .matcher(osArch)
 125                 .matches();
 126     }
 127 
 128     public static String getOsArch() {
 129         return osArch;
 130     }
 131 
 132     /**
 133      * Return a boolean for whether we expect to be able to attach
 134      * the SA to our own processes on this system.
 135      */
 136     public static boolean shouldSAAttach() throws Exception {
 137 
 138         if (isLinux()) {
 139             return canPtraceAttachLinux();
 140         } else if (isOSX()) {
 141             return canAttachOSX();
 142         } else {
 143             // Other platforms expected to work:
 144             return true;
 145         }
 146     }
 147 
 148     /**
 149      * On Linux, first check the SELinux boolean "deny_ptrace" and return false
 150      * as we expect to be denied if that is "1".  Then expect permission to attach
 151      * if we are root, so return true.  Then return false for an expected denial
 152      * if "ptrace_scope" is 1, and true otherwise.
 153      */
 154     public static boolean canPtraceAttachLinux() throws Exception {
 155 
 156         // SELinux deny_ptrace:
 157         String deny_ptrace = Utils.fileAsString("/sys/fs/selinux/booleans/deny_ptrace");
 158         if (deny_ptrace != null && deny_ptrace.contains("1")) {
 159             // ptrace will be denied:
 160             return false;
 161         }
 162 
 163         if (userName.equals("root")) {
 164             return true;
 165         }
 166 
 167         // ptrace_scope:
 168         String ptrace_scope = Utils.fileAsString("/proc/sys/kernel/yama/ptrace_scope");
 169         if (ptrace_scope != null && ptrace_scope.contains("1")) {
 170             // ptrace will be denied:
 171             return false;
 172         }
 173 
 174         // Otherwise expect to be permitted:
 175         return true;
 176     }
 177 
 178     /**
 179      * On OSX, expect permission to attach only if we are root.
 180      */
 181     public static boolean canAttachOSX() throws Exception {
 182         return userName.equals("root");
 183     }
 184 }