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