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