1 /*
   2  * Copyright (c) 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  */
  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 isSolaris() {
  65         return isOs("sunos");
  66     }
  67 
  68     public static boolean isWindows() {
  69         return isOs("win");
  70     }
  71 
  72     public static boolean isOSX() {
  73         return isOs("mac");
  74     }
  75 
  76     public static boolean isLinux() {
  77         return isOs("linux");
  78     }
  79 
  80     private static boolean isOs(String osname) {
  81         return osName.toLowerCase().startsWith(osname.toLowerCase());
  82     }
  83 
  84     public static String getOsName() {
  85         return osName;
  86     }
  87 
  88     public static boolean isDebugBuild() {
  89         return vmVersion.toLowerCase().contains("debug");
  90     }
  91 
  92     public static String getVMVersion() {
  93         return vmVersion;
  94     }
  95 
  96     // Returns true for sparc and sparcv9.
  97     public static boolean isSparc() {
  98         return isArch("sparc");
  99     }
 100 
 101     public static boolean isARM() {
 102         return isArch("arm");
 103     }
 104 
 105     public static boolean isPPC() {
 106         return isArch("ppc");
 107     }
 108 
 109     public static boolean isX86() {
 110         // On Linux it's 'i386', Windows 'x86'
 111         return (isArch("i386") || isArch("x86"));
 112     }
 113 
 114     public static boolean isX64() {
 115         // On OSX it's 'x86_64' and on other (Linux, Windows and Solaris) platforms it's 'amd64'
 116         return (isArch("amd64") || isArch("x86_64"));
 117     }
 118 
 119     private static boolean isArch(String archname) {
 120         return osArch.toLowerCase().startsWith(archname.toLowerCase());
 121     }
 122 
 123     public static String getOsArch() {
 124         return osArch;
 125     }
 126 
 127     /**
 128      * Return a boolean for whether we expect to be able to attach
 129      * the SA to our own processes on this system.
 130      */
 131     public static boolean shouldSAAttach() throws Exception {
 132 
 133         if (isLinux()) {
 134             return canPtraceAttachLinux();
 135         } else if (isOSX()) {
 136             return canAttachOSX();
 137         } else {
 138             // Other platforms expected to work:
 139             return true;
 140         }
 141     }
 142 
 143     /**
 144      * On Linux, first check the SELinux boolean "deny_ptrace" and return false
 145      * as we expect to be denied if that is "1".  Then expect permission to attach
 146      * if we are root, so return true.  Then return false for an expected denial
 147      * if "ptrace_scope" is 1, and true otherwise.
 148      */
 149     public static boolean canPtraceAttachLinux() throws Exception {
 150 
 151         // SELinux deny_ptrace:
 152         String deny_ptrace = Utils.fileAsString("/sys/fs/selinux/booleans/deny_ptrace");
 153         if (deny_ptrace != null && deny_ptrace.contains("1")) {
 154             // ptrace will be denied:
 155             return false;
 156         }
 157 
 158         if (userName.equals("root")) {
 159             return true;
 160         }
 161 
 162         // ptrace_scope:
 163         String ptrace_scope = Utils.fileAsString("/proc/sys/kernel/yama/ptrace_scope");
 164         if (ptrace_scope != null && ptrace_scope.contains("1")) {
 165             // ptrace will be denied:
 166             return false;
 167         }
 168 
 169         // Otherwise expect to be permitted:
 170         return true;
 171     }
 172 
 173     /**
 174      * On OSX, expect permission to attach only if we are root.
 175      */
 176     public static boolean canAttachOSX() throws Exception {
 177         return userName.equals("root");
 178     }
 179 }