Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/solaris/native/sun/awt/awt_LoadLibrary.c
32287 views
1
/*
2
* Copyright (c) 2000, 2014, 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 <stdio.h>
27
#include <dlfcn.h>
28
#include <string.h>
29
#include <stdlib.h>
30
#include <jni.h>
31
#include <jni_util.h>
32
#include <jvm.h>
33
#include <stdbool.h>
34
#include "gdefs.h"
35
36
#include <sys/param.h>
37
#include <sys/utsname.h>
38
39
#ifdef AIX
40
#include "porting_aix.h" /* For the 'dladdr' function. */
41
#endif
42
43
#ifdef DEBUG
44
#define VERBOSE_AWT_DEBUG
45
#endif
46
47
static void *awtHandle = NULL;
48
49
typedef jint JNICALL JNI_OnLoad_type(JavaVM *vm, void *reserved);
50
51
/* Initialize the Java VM instance variable when the library is
52
first loaded */
53
JavaVM *jvm;
54
55
JNIEXPORT jboolean JNICALL AWTIsHeadless() {
56
static JNIEnv *env = NULL;
57
static jboolean isHeadless;
58
jmethodID headlessFn;
59
jclass graphicsEnvClass;
60
61
if (env == NULL) {
62
env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
63
graphicsEnvClass = (*env)->FindClass(env,
64
"java/awt/GraphicsEnvironment");
65
if (graphicsEnvClass == NULL) {
66
return JNI_TRUE;
67
}
68
headlessFn = (*env)->GetStaticMethodID(env,
69
graphicsEnvClass, "isHeadless", "()Z");
70
if (headlessFn == NULL) {
71
return JNI_TRUE;
72
}
73
isHeadless = (*env)->CallStaticBooleanMethod(env, graphicsEnvClass,
74
headlessFn);
75
}
76
return isHeadless;
77
}
78
79
#define CHECK_EXCEPTION_FATAL(env, message) \
80
if ((*env)->ExceptionCheck(env)) { \
81
(*env)->ExceptionClear(env); \
82
(*env)->FatalError(env, message); \
83
}
84
85
/*
86
* Pathnames to the various awt toolkits
87
*/
88
89
#ifdef MACOSX
90
#define LWAWT_PATH "/libawt_lwawt.dylib"
91
#define DEFAULT_PATH LWAWT_PATH
92
#else
93
#define XAWT_PATH "/libawt_xawt.so"
94
#define DEFAULT_PATH XAWT_PATH
95
#define HEADLESS_PATH "/libawt_headless.so"
96
#endif
97
static bool read_so_path_from_maps(const char* so_name, char* buf) {
98
FILE *fp = fopen("/proc/self/maps", "r");
99
if (!fp) {
100
return false;
101
}
102
103
char maps_buffer[2048];
104
while (fgets(maps_buffer, 2048, fp) != NULL) {
105
if (strstr(maps_buffer, so_name) == NULL) {
106
continue;
107
}
108
109
char *so_path = strchr(maps_buffer, '/');
110
so_path[strlen(so_path) - 1] = '\0'; // Cut trailing \n
111
strcpy(buf,so_path);
112
fclose(fp);
113
return true;
114
}
115
116
fclose(fp);
117
return false;
118
}
119
120
jint
121
AWT_OnLoad(JavaVM *vm, void *reserved)
122
{
123
Dl_info dlinfo;
124
char buf[MAXPATHLEN];
125
int32_t len;
126
char *p, *tk;
127
JNI_OnLoad_type *JNI_OnLoad_ptr;
128
struct utsname name;
129
JNIEnv *env = (JNIEnv *)JNU_GetEnv(vm, JNI_VERSION_1_2);
130
void *v;
131
jstring fmanager = NULL;
132
jstring fmProp = NULL;
133
134
if (awtHandle != NULL) {
135
/* Avoid several loading attempts */
136
return JNI_VERSION_1_2;
137
}
138
139
jvm = vm;
140
141
/* Get address of this library and the directory containing it. */
142
dladdr((void *)AWT_OnLoad, &dlinfo);
143
if (strrchr(dlinfo.dli_fname, '/') != NULL) {
144
realpath((char *)dlinfo.dli_fname, buf);
145
} else {
146
read_so_path_from_maps(dlinfo.dli_fname,buf);
147
}
148
len = strlen(buf);
149
p = strrchr(buf, '/');
150
151
/*
152
* The code below is responsible for:
153
* 1. Loading appropriate awt library, i.e. libawt_xawt or libawt_headless
154
* 2. Set the "sun.font.fontmanager" system property.
155
*/
156
157
fmProp = (*env)->NewStringUTF(env, "sun.font.fontmanager");
158
CHECK_EXCEPTION_FATAL(env, "Could not allocate font manager property");
159
160
#ifdef MACOSX
161
fmanager = (*env)->NewStringUTF(env, "sun.font.CFontManager");
162
tk = LWAWT_PATH;
163
#else
164
fmanager = (*env)->NewStringUTF(env, "sun.awt.X11FontManager");
165
tk = XAWT_PATH;
166
#endif
167
CHECK_EXCEPTION_FATAL(env, "Could not allocate font manager name");
168
169
if (fmanager && fmProp) {
170
JNU_CallStaticMethodByName(env, NULL, "java/lang/System", "setProperty",
171
"(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;",
172
fmProp, fmanager);
173
CHECK_EXCEPTION_FATAL(env, "Could not allocate set properties");
174
}
175
176
#ifndef MACOSX
177
if (AWTIsHeadless()) {
178
tk = HEADLESS_PATH;
179
}
180
#endif
181
182
/* Calculate library name to load */
183
strncpy(p, tk, MAXPATHLEN-len-1);
184
185
if (fmProp) {
186
(*env)->DeleteLocalRef(env, fmProp);
187
}
188
if (fmanager) {
189
(*env)->DeleteLocalRef(env, fmanager);
190
}
191
192
jstring jbuf = JNU_NewStringPlatform(env, buf);
193
CHECK_EXCEPTION_FATAL(env, "Could not allocate library name");
194
JNU_CallStaticMethodByName(env, NULL, "java/lang/System", "load",
195
"(Ljava/lang/String;)V",
196
jbuf);
197
198
awtHandle = dlopen(buf, RTLD_LAZY | RTLD_GLOBAL);
199
200
return JNI_VERSION_1_2;
201
}
202
203
JNIEXPORT jint JNICALL
204
JNI_OnLoad(JavaVM *vm, void *reserved)
205
{
206
return AWT_OnLoad(vm, reserved);
207
}
208
209
/*
210
* This entry point must remain in libawt.so as part of a contract
211
* with the CDE variant of Java Media Framework. (sdtjmplay)
212
* Reflect this call over to the correct libawt_<toolkit>.so.
213
*/
214
JNIEXPORT void JNICALL
215
Java_sun_awt_motif_XsessionWMcommand(JNIEnv *env, jobject this,
216
jobject frame, jstring jcommand)
217
{
218
/* type of the old backdoor function */
219
typedef void JNICALL
220
XsessionWMcommand_type(JNIEnv *env, jobject this,
221
jobject frame, jstring jcommand);
222
223
static XsessionWMcommand_type *XsessionWMcommand = NULL;
224
225
if (XsessionWMcommand == NULL && awtHandle == NULL) {
226
return;
227
}
228
229
XsessionWMcommand = (XsessionWMcommand_type *)
230
dlsym(awtHandle, "Java_sun_awt_motif_XsessionWMcommand");
231
232
if (XsessionWMcommand == NULL)
233
return;
234
235
(*XsessionWMcommand)(env, this, frame, jcommand);
236
}
237
238
239
/*
240
* This entry point must remain in libawt.so as part of a contract
241
* with the CDE variant of Java Media Framework. (sdtjmplay)
242
* Reflect this call over to the correct libawt_<toolkit>.so.
243
*/
244
JNIEXPORT void JNICALL
245
Java_sun_awt_motif_XsessionWMcommand_New(JNIEnv *env, jobjectArray jargv)
246
{
247
typedef void JNICALL
248
XsessionWMcommand_New_type(JNIEnv *env, jobjectArray jargv);
249
250
static XsessionWMcommand_New_type *XsessionWMcommand = NULL;
251
252
if (XsessionWMcommand == NULL && awtHandle == NULL) {
253
return;
254
}
255
256
XsessionWMcommand = (XsessionWMcommand_New_type *)
257
dlsym(awtHandle, "Java_sun_awt_motif_XsessionWMcommand_New");
258
259
if (XsessionWMcommand == NULL)
260
return;
261
262
(*XsessionWMcommand)(env, jargv);
263
}
264
265