1 /*
   2  * Copyright (c) 2005, 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.  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 #include "jni.h"
  27 #include "jni_util.h"
  28 #include "jvm.h"
  29 #include "jdk_util.h"
  30 
  31 #include "sun_misc_Version.h"
  32 
  33 static void setStaticIntField(JNIEnv* env, jclass cls, const char* name, jint value)
  34 {
  35     jfieldID fid;
  36     fid = (*env)->GetStaticFieldID(env, cls, name, "I");
  37     if (fid != 0) {
  38         (*env)->SetStaticIntField(env, cls, fid, value);
  39     }
  40 }
  41 
  42 typedef void (JNICALL *GetJvmVersionInfo_fp)(JNIEnv*, jvm_version_info*, size_t);
  43 
  44 JNIEXPORT jboolean JNICALL
  45 Java_sun_misc_Version_getJvmVersionInfo(JNIEnv *env, jclass cls)
  46 {
  47     jvm_version_info info;
  48     GetJvmVersionInfo_fp func_p;
  49 
  50     if (!JDK_InitJvmHandle()) {
  51         JNU_ThrowInternalError(env, "Handle for JVM not found for symbol lookup");
  52         return JNI_FALSE;
  53     }
  54     func_p = (GetJvmVersionInfo_fp) JDK_FindJvmEntry("JVM_GetVersionInfo");
  55     if (func_p == NULL) {
  56         return JNI_FALSE;
  57     }
  58 
  59     (*func_p)(env, &info, sizeof(info));
  60     setStaticIntField(env, cls, "jvm_major_version", JVM_VERSION_MAJOR(info.jvm_version));
  61     JNU_CHECK_EXCEPTION_RETURN(env, JNI_FALSE);
  62     setStaticIntField(env, cls, "jvm_minor_version", JVM_VERSION_MINOR(info.jvm_version));
  63     JNU_CHECK_EXCEPTION_RETURN(env, JNI_FALSE);
  64     setStaticIntField(env, cls, "jvm_security_version", JVM_VERSION_SECURITY(info.jvm_version));
  65     JNU_CHECK_EXCEPTION_RETURN(env, JNI_FALSE);
  66     setStaticIntField(env, cls, "jvm_build_number", JVM_VERSION_BUILD(info.jvm_version));
  67     JNU_CHECK_EXCEPTION_RETURN(env, JNI_FALSE);
  68     setStaticIntField(env, cls, "jvm_patch_version", info.patch_version);
  69     JNU_CHECK_EXCEPTION_RETURN(env, JNI_FALSE);
  70 
  71     return JNI_TRUE;
  72 }
  73 
  74 JNIEXPORT void JNICALL
  75 Java_sun_misc_Version_getJdkVersionInfo(JNIEnv *env, jclass cls)
  76 {
  77     jdk_version_info info;
  78 
  79     JDK_GetVersionInfo0(&info, sizeof(info));
  80     setStaticIntField(env, cls, "jdk_major_version", JDK_VERSION_MAJOR(info.jdk_version));
  81     JNU_CHECK_EXCEPTION(env);
  82     setStaticIntField(env, cls, "jdk_minor_version", JDK_VERSION_MINOR(info.jdk_version));
  83     JNU_CHECK_EXCEPTION(env);
  84     setStaticIntField(env, cls, "jdk_security_version", JDK_VERSION_SECURITY(info.jdk_version));
  85     JNU_CHECK_EXCEPTION(env);
  86     setStaticIntField(env, cls, "jdk_build_number", JDK_VERSION_BUILD(info.jdk_version));
  87     JNU_CHECK_EXCEPTION(env);
  88     setStaticIntField(env, cls, "jdk_patch_version", info.patch_version);
  89     JNU_CHECK_EXCEPTION(env);
  90 }