Path: blob/master/src/java.desktop/unix/native/libawt/awt/awt_LoadLibrary.c
41152 views
/*1* Copyright (c) 2000, 2021, 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 */52JNIEXPORT JavaVM *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);74if ((*env)->ExceptionCheck(env)) {75(*env)->ExceptionClear(env);76return JNI_TRUE;77}78}79return isHeadless;80}8182#define CHECK_EXCEPTION_FATAL(env, message) \83if ((*env)->ExceptionCheck(env)) { \84(*env)->ExceptionClear(env); \85(*env)->FatalError(env, message); \86}8788/*89* Pathnames to the various awt toolkits90*/9192#ifdef MACOSX93#define LWAWT_PATH "/libawt_lwawt.dylib"94#define DEFAULT_PATH LWAWT_PATH95#else96#define XAWT_PATH "/libawt_xawt.so"97#define DEFAULT_PATH XAWT_PATH98#define HEADLESS_PATH "/libawt_headless.so"99#endif100101static bool read_so_path_from_maps(const char* so_name, char* buf) {102FILE *fp = fopen("/proc/self/maps", "r");103104if (!fp) {105return false;106}107108char maps_buffer[2048];109while (fgets(maps_buffer, 2048, fp) != NULL) {110if (strstr(maps_buffer, so_name) == NULL) {111continue;112}113114char *so_path = strchr(maps_buffer, '/');115so_path[strlen(so_path) - 1] = '\0'; // Cut trailing \n116strcpy(buf,so_path);117fclose(fp);118return true;119}120121fclose(fp);122return false;123}124125jint126AWT_OnLoad(JavaVM *vm, void *reserved)127{128Dl_info dlinfo;129char buf[MAXPATHLEN];130int32_t len;131char *p, *tk;132JNI_OnLoad_type *JNI_OnLoad_ptr;133struct utsname name;134JNIEnv *env = (JNIEnv *)JNU_GetEnv(vm, JNI_VERSION_1_2);135void *v;136jstring fmanager = NULL;137jstring fmProp = NULL;138139if (awtHandle != NULL) {140/* Avoid several loading attempts */141return JNI_VERSION_1_2;142}143144jvm = vm;145#ifndef STATIC_BUILD146/* Get address of this library and the directory containing it. */147dladdr((void *)AWT_OnLoad, &dlinfo);148if (strrchr(dlinfo.dli_fname, '/') != NULL) {149realpath((char *)dlinfo.dli_fname, buf);150}else{151read_so_path_from_maps(dlinfo.dli_fname,buf);152}153len = strlen(buf);154p = strrchr(buf, '/');155#endif156/*157* The code below is responsible for:158* 1. Loading appropriate awt library, i.e. libawt_xawt or libawt_headless159* 2. Set the "sun.font.fontmanager" system property.160*/161162fmProp = (*env)->NewStringUTF(env, "sun.font.fontmanager");163CHECK_EXCEPTION_FATAL(env, "Could not allocate font manager property");164165#ifdef MACOSX166fmanager = (*env)->NewStringUTF(env, "sun.font.CFontManager");167tk = LWAWT_PATH;168#else169fmanager = (*env)->NewStringUTF(env, "sun.awt.X11FontManager");170tk = XAWT_PATH;171#endif172CHECK_EXCEPTION_FATAL(env, "Could not allocate font manager name");173174if (fmanager && fmProp) {175JNU_CallStaticMethodByName(env, NULL, "java/lang/System", "setProperty",176"(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;",177fmProp, fmanager);178CHECK_EXCEPTION_FATAL(env, "Could not allocate set properties");179}180181#ifndef MACOSX182if (AWTIsHeadless()) {183tk = HEADLESS_PATH;184}185#endif186187#ifndef STATIC_BUILD188/* Calculate library name to load */189strncpy(p, tk, MAXPATHLEN-len-1);190#endif191192if (fmProp) {193(*env)->DeleteLocalRef(env, fmProp);194}195if (fmanager) {196(*env)->DeleteLocalRef(env, fmanager);197}198199200#ifndef STATIC_BUILD201jstring jbuf = JNU_NewStringPlatform(env, buf);202CHECK_EXCEPTION_FATAL(env, "Could not allocate library name");203JNU_CallStaticMethodByName(env, NULL, "java/lang/System", "load",204"(Ljava/lang/String;)V",205jbuf);206207awtHandle = dlopen(buf, RTLD_LAZY | RTLD_GLOBAL);208#endif209return JNI_VERSION_1_2;210}211212JNIEXPORT jint JNICALL213DEF_JNI_OnLoad(JavaVM *vm, void *reserved)214{215return AWT_OnLoad(vm, reserved);216}217218219