1 /*
   2  * Copyright (c) 2008, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.servicetag;
  27 
  28 // This class is a copy of the com.sun.scn.servicetags.SolarisSystemEnvironment
  29 // class from the Sun Connection source.
  30 //
  31 // The Service Tags team maintains the latest version of the implementation
  32 // for system environment data collection.  JDK will include a copy of
  33 // the most recent released version for a JDK release. We rename
  34 // the package to com.sun.servicetag so that the Sun Connection
  35 // product always uses the latest version from the com.sun.scn.servicetags
  36 // package. JDK and users of the com.sun.servicetag API
  37 // (e.g. NetBeans and SunStudio) will use the version in JDK.
  38 //
  39 // So we keep this class in src/share/classes instead of src/<os>/classes.
  40 
  41 import java.io.*;
  42 
  43 /**
  44  * Solaris implementation of the SystemEnvironment class.
  45  */
  46 class SolarisSystemEnvironment extends SystemEnvironment {
  47     private static final String ORACLE = "Oracle Corporation";
  48     SolarisSystemEnvironment() {
  49         setHostId(getCommandOutput("/usr/bin/hostid"));
  50         setSystemModel(getCommandOutput("/usr/bin/uname", "-i"));
  51         setSystemManufacturer(getSolarisSystemManufacturer());
  52         setCpuManufacturer(getSolarisCpuManufacturer());
  53         setSerialNumber(getSolarisSN());
  54     }
  55 
  56     /**
  57      * Tries to obtain the cpu manufacturer.
  58      * @return The cpu manufacturer (an empty string if not found or an error occurred)
  59      */
  60     private String getSolarisCpuManufacturer() {
  61         // not fully accurate, this could be another manufacturer (fujitsu for example)
  62         if ("sparc".equalsIgnoreCase(System.getProperty("os.arch"))) {
  63             return ORACLE;
  64         }
  65 
  66         // if we're here, then we'll try smbios (type 4)
  67         return getSmbiosData("4", "Manufacturer: ");
  68     }
  69 
  70     /**
  71      * Tries to obtain the system manufacturer.
  72      * @return The system manufacturer (an empty string if not found or an error occurred)
  73      */
  74     private String getSolarisSystemManufacturer() {
  75         // not fully accurate, this could be another manufacturer (fujitsu for example)
  76         if ("sparc".equalsIgnoreCase(System.getProperty("os.arch"))) {
  77             return ORACLE;
  78         }
  79 
  80         // if we're here, then we'll try smbios (type 1)
  81         return getSmbiosData("1", "Manufacturer: ");
  82     }
  83 
  84     /**
  85      * Tries to obtain the serial number.
  86      * @return The serial number (empty string if not found or an error occurred)
  87      */
  88     private String getSolarisSN() {
  89         // try to read from the psn file if it exists
  90         String tmp = getFileContent("/var/run/psn");
  91         if (tmp.length() > 0) {
  92             return tmp.trim();
  93         }
  94 
  95         // if we're here, then we'll try sneep
  96         String tmpSN = getSneepSN();
  97         if (tmpSN.length() > 0) {
  98             return tmpSN;
  99         }
 100 
 101         // if we're here, then we'll try smbios (type 1)
 102         tmpSN = getSmbiosData("1", "Serial Number: ");
 103         if (tmpSN.length() > 0) {
 104             return tmpSN;
 105         }
 106 
 107         // if we're here, then we'll try smbios (type 3)
 108         tmpSN = getSmbiosData("3", "Serial Number: ");
 109         if (tmpSN.length() > 0) {
 110             return tmpSN;
 111         }
 112 
 113         // give up and return
 114         return "";
 115     }
 116 
 117     // Sample smbios output segment:
 118     // ID    SIZE TYPE
 119     // 1     150  SMB_TYPE_SYSTEM (system information)
 120     //
 121     //   Manufacturer: Oracle Corporation
 122     //   Product: Sun Fire X4600
 123     //   Version: To Be Filled By O.E.M.
 124     //   Serial Number: 00:14:4F:45:0C:2A
 125     private String getSmbiosData(String type, String target) {
 126         String output = getCommandOutput("/usr/sbin/smbios", "-t", type);
 127         for (String s : output.split("\n")) {
 128             if (s.contains(target)) {
 129                 int indx = s.indexOf(target) + target.length();
 130                 if (indx < s.length()) {
 131                     String tmp = s.substring(indx).trim();
 132                     String lowerCaseStr = tmp.toLowerCase();
 133                     if (!lowerCaseStr.startsWith("not available")
 134                             && !lowerCaseStr.startsWith("to be filled by o.e.m")) {
 135                         return tmp;
 136                     }
 137                 }
 138             }
 139         }
 140 
 141         return "";
 142     }
 143 
 144     private String getSneepSN() {
 145         String basedir = getCommandOutput("pkgparam","SUNWsneep","BASEDIR");
 146         File f = new File(basedir + "/bin/sneep");
 147         if (f.exists()) {
 148             String sneepSN = getCommandOutput(basedir + "/bin/sneep");
 149             if (sneepSN.equalsIgnoreCase("unknown")) {
 150                 return "";
 151             } else {
 152                 return sneepSN;
 153             }
 154         } else {
 155             return "";
 156         }
 157     }
 158 
 159 }