Path: blob/jdk8u272-b10-aarch32-20201026/jdk/src/solaris/native/sun/awt/awt_LoadLibrary.c
48789 views
/*1* Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425#include <stdio.h>26#include <dlfcn.h>27#include <string.h>28#include <stdlib.h>29#include <jni.h>30#include <jni_util.h>31#include <jvm.h>32#include <stdbool.h>33#include <assert.h>34#include "gdefs.h"3536#include <sys/param.h>37#include <sys/utsname.h>3839#ifdef AIX40#include "porting_aix.h" /* For the 'dladdr' function. */41#endif4243#ifdef DEBUG44#define VERBOSE_AWT_DEBUG45#endif4647static void *awtHandle = NULL;4849typedef jint JNICALL JNI_OnLoad_type(JavaVM *vm, void *reserved);5051/* Initialize the Java VM instance variable when the library is52first loaded */53JavaVM *jvm;5455JNIEXPORT jboolean JNICALL AWTIsHeadless() {56static JNIEnv *env = NULL;57static jboolean isHeadless;58jmethodID headlessFn;59jclass graphicsEnvClass;6061if (env == NULL) {62env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);63graphicsEnvClass = (*env)->FindClass(env,64"java/awt/GraphicsEnvironment");65if (graphicsEnvClass == NULL) {66return JNI_TRUE;67}68headlessFn = (*env)->GetStaticMethodID(env,69graphicsEnvClass, "isHeadless", "()Z");70if (headlessFn == NULL) {71return JNI_TRUE;72}73isHeadless = (*env)->CallStaticBooleanMethod(env, graphicsEnvClass,74headlessFn);75}76return isHeadless;77}7879#define CHECK_EXCEPTION_FATAL(env, message) \80if ((*env)->ExceptionCheck(env)) { \81(*env)->ExceptionClear(env); \82(*env)->FatalError(env, message); \83}8485/*86* Pathnames to the various awt toolkits87*/8889#ifdef MACOSX90#define LWAWT_PATH "/libawt_lwawt.dylib"91#define DEFAULT_PATH LWAWT_PATH92#else93#define XAWT_PATH "/libawt_xawt.so"94#define DEFAULT_PATH XAWT_PATH95#define HEADLESS_PATH "/libawt_headless.so"96#endif97static bool read_so_path_from_maps(const char* so_name, char* buf) {98FILE *fp = fopen("/proc/self/maps", "r");99if (!fp) {100return false;101}102103char maps_buffer[2048];104while (fgets(maps_buffer, 2048, fp) != NULL) {105if (strstr(maps_buffer, so_name) == NULL) {106continue;107}108109char *so_path = strchr(maps_buffer, '/');110so_path[strlen(so_path) - 1] = '\0'; // Cut trailing \n111strcpy(buf,so_path);112fclose(fp);113return true;114}115116fclose(fp);117return false;118}119120jint121AWT_OnLoad(JavaVM *vm, void *reserved)122{123Dl_info dlinfo;124char buf[MAXPATHLEN];125int32_t len;126char *p, *tk;127JNI_OnLoad_type *JNI_OnLoad_ptr;128struct utsname name;129JNIEnv *env = (JNIEnv *)JNU_GetEnv(vm, JNI_VERSION_1_2);130void *v;131jstring fmanager = NULL;132jstring fmProp = NULL;133134if (awtHandle != NULL) {135/* Avoid several loading attempts */136return JNI_VERSION_1_2;137}138139jvm = vm;140141/* Get address of this library and the directory containing it. */142dladdr((void *)AWT_OnLoad, &dlinfo);143if (strrchr(dlinfo.dli_fname, '/') != NULL) {144realpath((char *)dlinfo.dli_fname, buf);145}else{146read_so_path_from_maps(dlinfo.dli_fname,buf);147}148len = strlen(buf);149p = strrchr(buf, '/');150151/*152* The code below is responsible for:153* 1. Loading appropriate awt library, i.e. libawt_xawt or libawt_headless154* 2. Set the "sun.font.fontmanager" system property.155*/156157fmProp = (*env)->NewStringUTF(env, "sun.font.fontmanager");158CHECK_EXCEPTION_FATAL(env, "Could not allocate font manager property");159160#ifdef MACOSX161fmanager = (*env)->NewStringUTF(env, "sun.font.CFontManager");162tk = LWAWT_PATH;163#else164fmanager = (*env)->NewStringUTF(env, "sun.awt.X11FontManager");165tk = XAWT_PATH;166#endif167CHECK_EXCEPTION_FATAL(env, "Could not allocate font manager name");168169if (fmanager && fmProp) {170JNU_CallStaticMethodByName(env, NULL, "java/lang/System", "setProperty",171"(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;",172fmProp, fmanager);173CHECK_EXCEPTION_FATAL(env, "Could not allocate set properties");174}175176#ifndef MACOSX177if (AWTIsHeadless()) {178tk = HEADLESS_PATH;179}180#endif181182/* Calculate library name to load */183strncpy(p, tk, MAXPATHLEN-len-1);184185if (fmProp) {186(*env)->DeleteLocalRef(env, fmProp);187}188if (fmanager) {189(*env)->DeleteLocalRef(env, fmanager);190}191192jstring jbuf = JNU_NewStringPlatform(env, buf);193CHECK_EXCEPTION_FATAL(env, "Could not allocate library name");194JNU_CallStaticMethodByName(env, NULL, "java/lang/System", "load",195"(Ljava/lang/String;)V",196jbuf);197198awtHandle = dlopen(buf, RTLD_LAZY | RTLD_GLOBAL);199200return JNI_VERSION_1_2;201}202203JNIEXPORT jint JNICALL204JNI_OnLoad(JavaVM *vm, void *reserved)205{206return AWT_OnLoad(vm, reserved);207}208209/*210* This entry point must remain in libawt.so as part of a contract211* with the CDE variant of Java Media Framework. (sdtjmplay)212* Reflect this call over to the correct libawt_<toolkit>.so.213*/214JNIEXPORT void JNICALL215Java_sun_awt_motif_XsessionWMcommand(JNIEnv *env, jobject this,216jobject frame, jstring jcommand)217{218/* type of the old backdoor function */219typedef void JNICALL220XsessionWMcommand_type(JNIEnv *env, jobject this,221jobject frame, jstring jcommand);222223static XsessionWMcommand_type *XsessionWMcommand = NULL;224225if (XsessionWMcommand == NULL && awtHandle == NULL) {226return;227}228229XsessionWMcommand = (XsessionWMcommand_type *)230dlsym(awtHandle, "Java_sun_awt_motif_XsessionWMcommand");231232if (XsessionWMcommand == NULL)233return;234235(*XsessionWMcommand)(env, this, frame, jcommand);236}237238239/*240* This entry point must remain in libawt.so as part of a contract241* with the CDE variant of Java Media Framework. (sdtjmplay)242* Reflect this call over to the correct libawt_<toolkit>.so.243*/244JNIEXPORT void JNICALL245Java_sun_awt_motif_XsessionWMcommand_New(JNIEnv *env, jobjectArray jargv)246{247typedef void JNICALL248XsessionWMcommand_New_type(JNIEnv *env, jobjectArray jargv);249250static XsessionWMcommand_New_type *XsessionWMcommand = NULL;251252if (XsessionWMcommand == NULL && awtHandle == NULL) {253return;254}255256XsessionWMcommand = (XsessionWMcommand_New_type *)257dlsym(awtHandle, "Java_sun_awt_motif_XsessionWMcommand_New");258259if (XsessionWMcommand == NULL)260return;261262(*XsessionWMcommand)(env, jargv);263}264265266