Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/solaris/native/sun/awt/awt_LoadLibrary.c
32287 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 "gdefs.h"3435#include <sys/param.h>36#include <sys/utsname.h>3738#ifdef AIX39#include "porting_aix.h" /* For the 'dladdr' function. */40#endif4142#ifdef DEBUG43#define VERBOSE_AWT_DEBUG44#endif4546static void *awtHandle = NULL;4748typedef jint JNICALL JNI_OnLoad_type(JavaVM *vm, void *reserved);4950/* Initialize the Java VM instance variable when the library is51first loaded */52JavaVM *jvm;5354JNIEXPORT jboolean JNICALL AWTIsHeadless() {55static JNIEnv *env = NULL;56static jboolean isHeadless;57jmethodID headlessFn;58jclass graphicsEnvClass;5960if (env == NULL) {61env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);62graphicsEnvClass = (*env)->FindClass(env,63"java/awt/GraphicsEnvironment");64if (graphicsEnvClass == NULL) {65return JNI_TRUE;66}67headlessFn = (*env)->GetStaticMethodID(env,68graphicsEnvClass, "isHeadless", "()Z");69if (headlessFn == NULL) {70return JNI_TRUE;71}72isHeadless = (*env)->CallStaticBooleanMethod(env, graphicsEnvClass,73headlessFn);74}75return isHeadless;76}7778#define CHECK_EXCEPTION_FATAL(env, message) \79if ((*env)->ExceptionCheck(env)) { \80(*env)->ExceptionClear(env); \81(*env)->FatalError(env, message); \82}8384/*85* Pathnames to the various awt toolkits86*/8788#ifdef MACOSX89#define LWAWT_PATH "/libawt_lwawt.dylib"90#define DEFAULT_PATH LWAWT_PATH91#else92#define XAWT_PATH "/libawt_xawt.so"93#define DEFAULT_PATH XAWT_PATH94#define HEADLESS_PATH "/libawt_headless.so"95#endif96static bool read_so_path_from_maps(const char* so_name, char* buf) {97FILE *fp = fopen("/proc/self/maps", "r");98if (!fp) {99return false;100}101102char maps_buffer[2048];103while (fgets(maps_buffer, 2048, fp) != NULL) {104if (strstr(maps_buffer, so_name) == NULL) {105continue;106}107108char *so_path = strchr(maps_buffer, '/');109so_path[strlen(so_path) - 1] = '\0'; // Cut trailing \n110strcpy(buf,so_path);111fclose(fp);112return true;113}114115fclose(fp);116return false;117}118119jint120AWT_OnLoad(JavaVM *vm, void *reserved)121{122Dl_info dlinfo;123char buf[MAXPATHLEN];124int32_t len;125char *p, *tk;126JNI_OnLoad_type *JNI_OnLoad_ptr;127struct utsname name;128JNIEnv *env = (JNIEnv *)JNU_GetEnv(vm, JNI_VERSION_1_2);129void *v;130jstring fmanager = NULL;131jstring fmProp = NULL;132133if (awtHandle != NULL) {134/* Avoid several loading attempts */135return JNI_VERSION_1_2;136}137138jvm = vm;139140/* Get address of this library and the directory containing it. */141dladdr((void *)AWT_OnLoad, &dlinfo);142if (strrchr(dlinfo.dli_fname, '/') != NULL) {143realpath((char *)dlinfo.dli_fname, buf);144} else {145read_so_path_from_maps(dlinfo.dli_fname,buf);146}147len = strlen(buf);148p = strrchr(buf, '/');149150/*151* The code below is responsible for:152* 1. Loading appropriate awt library, i.e. libawt_xawt or libawt_headless153* 2. Set the "sun.font.fontmanager" system property.154*/155156fmProp = (*env)->NewStringUTF(env, "sun.font.fontmanager");157CHECK_EXCEPTION_FATAL(env, "Could not allocate font manager property");158159#ifdef MACOSX160fmanager = (*env)->NewStringUTF(env, "sun.font.CFontManager");161tk = LWAWT_PATH;162#else163fmanager = (*env)->NewStringUTF(env, "sun.awt.X11FontManager");164tk = XAWT_PATH;165#endif166CHECK_EXCEPTION_FATAL(env, "Could not allocate font manager name");167168if (fmanager && fmProp) {169JNU_CallStaticMethodByName(env, NULL, "java/lang/System", "setProperty",170"(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;",171fmProp, fmanager);172CHECK_EXCEPTION_FATAL(env, "Could not allocate set properties");173}174175#ifndef MACOSX176if (AWTIsHeadless()) {177tk = HEADLESS_PATH;178}179#endif180181/* Calculate library name to load */182strncpy(p, tk, MAXPATHLEN-len-1);183184if (fmProp) {185(*env)->DeleteLocalRef(env, fmProp);186}187if (fmanager) {188(*env)->DeleteLocalRef(env, fmanager);189}190191jstring jbuf = JNU_NewStringPlatform(env, buf);192CHECK_EXCEPTION_FATAL(env, "Could not allocate library name");193JNU_CallStaticMethodByName(env, NULL, "java/lang/System", "load",194"(Ljava/lang/String;)V",195jbuf);196197awtHandle = dlopen(buf, RTLD_LAZY | RTLD_GLOBAL);198199return JNI_VERSION_1_2;200}201202JNIEXPORT jint JNICALL203JNI_OnLoad(JavaVM *vm, void *reserved)204{205return AWT_OnLoad(vm, reserved);206}207208/*209* This entry point must remain in libawt.so as part of a contract210* with the CDE variant of Java Media Framework. (sdtjmplay)211* Reflect this call over to the correct libawt_<toolkit>.so.212*/213JNIEXPORT void JNICALL214Java_sun_awt_motif_XsessionWMcommand(JNIEnv *env, jobject this,215jobject frame, jstring jcommand)216{217/* type of the old backdoor function */218typedef void JNICALL219XsessionWMcommand_type(JNIEnv *env, jobject this,220jobject frame, jstring jcommand);221222static XsessionWMcommand_type *XsessionWMcommand = NULL;223224if (XsessionWMcommand == NULL && awtHandle == NULL) {225return;226}227228XsessionWMcommand = (XsessionWMcommand_type *)229dlsym(awtHandle, "Java_sun_awt_motif_XsessionWMcommand");230231if (XsessionWMcommand == NULL)232return;233234(*XsessionWMcommand)(env, this, frame, jcommand);235}236237238/*239* This entry point must remain in libawt.so as part of a contract240* with the CDE variant of Java Media Framework. (sdtjmplay)241* Reflect this call over to the correct libawt_<toolkit>.so.242*/243JNIEXPORT void JNICALL244Java_sun_awt_motif_XsessionWMcommand_New(JNIEnv *env, jobjectArray jargv)245{246typedef void JNICALL247XsessionWMcommand_New_type(JNIEnv *env, jobjectArray jargv);248249static XsessionWMcommand_New_type *XsessionWMcommand = NULL;250251if (XsessionWMcommand == NULL && awtHandle == NULL) {252return;253}254255XsessionWMcommand = (XsessionWMcommand_New_type *)256dlsym(awtHandle, "Java_sun_awt_motif_XsessionWMcommand_New");257258if (XsessionWMcommand == NULL)259return;260261(*XsessionWMcommand)(env, jargv);262}263264265