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/bin/java_md_solinux.c
32285 views
1
/*
2
* Copyright (c) 1998, 2020, 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 "java.h"
27
#include "jvm_md.h"
28
#include <dirent.h>
29
#include <dlfcn.h>
30
#include <fcntl.h>
31
#include <inttypes.h>
32
#include <stdio.h>
33
#include <string.h>
34
#include <stdlib.h>
35
#include <sys/stat.h>
36
#include <unistd.h>
37
#include <sys/types.h>
38
#include "manifest_info.h"
39
#include "version_comp.h"
40
41
42
#define JVM_DLL "libjvm.so"
43
#define JAVA_DLL "libjava.so"
44
#ifdef AIX
45
#define LD_LIBRARY_PATH "LIBPATH"
46
#else
47
#define LD_LIBRARY_PATH "LD_LIBRARY_PATH"
48
#endif
49
50
/* help jettison the LD_LIBRARY_PATH settings in the future */
51
#ifndef SETENV_REQUIRED
52
#define SETENV_REQUIRED
53
#endif
54
/*
55
* If a processor / os combination has the ability to run binaries of
56
* two data models and cohabitation of jre/jdk bits with both data
57
* models is supported, then DUAL_MODE is defined. When DUAL_MODE is
58
* defined, the architecture names for the narrow and wide version of
59
* the architecture are defined in LIBARCH64NAME and LIBARCH32NAME.
60
* Currently only Solaris on sparc/sparcv9 and i586/amd64 is DUAL_MODE;
61
* linux i586/amd64 could be defined as DUAL_MODE but that is not the
62
* current policy.
63
*/
64
65
#ifdef __solaris__
66
# ifndef LIBARCH32NAME
67
# error "The macro LIBARCH32NAME was not defined on the compile line"
68
# endif
69
# ifndef LIBARCH64NAME
70
# error "The macro LIBARCH64NAME was not defined on the compile line"
71
# endif
72
# include <sys/systeminfo.h>
73
# include <sys/elf.h>
74
# include <stdio.h>
75
#endif
76
77
/*
78
* Flowchart of launcher execs and options processing on unix
79
*
80
* The selection of the proper vm shared library to open depends on
81
* several classes of command line options, including vm "flavor"
82
* options (-client, -server) and the data model options, -d32 and
83
* -d64, as well as a version specification which may have come from
84
* the command line or from the manifest of an executable jar file.
85
* The vm selection options are not passed to the running
86
* virtual machine; they must be screened out by the launcher.
87
*
88
* The version specification (if any) is processed first by the
89
* platform independent routine SelectVersion. This may result in
90
* the exec of the specified launcher version.
91
*
92
* Previously the launcher modified the LD_LIBRARY_PATH appropriately for the
93
* desired data model path, regardless if data models matched or not. The
94
* launcher subsequently exec'ed the desired executable, in order to make the
95
* LD_LIBRARY_PATH path available, for the runtime linker.
96
*
97
* Now, in most cases,the launcher will dlopen the target libjvm.so. All
98
* required libraries are loaded by the runtime linker, using the
99
* $RPATH/$ORIGIN baked into the shared libraries at compile time. Therefore,
100
* in most cases, the launcher will only exec, if the data models are
101
* mismatched, and will not set any environment variables, regardless of the
102
* data models.
103
*
104
* However, if the environment contains a LD_LIBRARY_PATH, this will cause the
105
* launcher to inspect the LD_LIBRARY_PATH. The launcher will check
106
* a. if the LD_LIBRARY_PATH's first component is the the path to the desired
107
* libjvm.so
108
* b. if any other libjvm.so is found in any of the paths.
109
* If case b is true, then the launcher will set the LD_LIBRARY_PATH to the
110
* desired JRE and reexec, in order to propagate the environment.
111
*
112
* Main
113
* (incoming argv)
114
* |
115
* \|/
116
* SelectVersion
117
* (selects the JRE version, note: not data model)
118
* |
119
* \|/
120
* CreateExecutionEnvironment
121
* (determines desired data model)
122
* |
123
* |
124
* \|/
125
* Have Desired Model ? --> NO --> Is Dual-Mode ? --> NO --> Exit(with error)
126
* | |
127
* | |
128
* | \|/
129
* | YES
130
* | |
131
* | |
132
* | \|/
133
* | CheckJvmType
134
* | (removes -client, -server etc.)
135
* | |
136
* | |
137
* \|/ \|/
138
* YES Find the desired executable/library
139
* | |
140
* | |
141
* \|/ \|/
142
* CheckJvmType RequiresSetenv
143
* (removes -client, -server, etc.)
144
* |
145
* |
146
* \|/
147
* TranslateDashJArgs...
148
* (Prepare to pass args to vm)
149
* |
150
* |
151
* \|/
152
* ParseArguments
153
* (removes -d32 and -d64 if any,
154
* processes version options,
155
* creates argument list for vm,
156
* etc.)
157
* |
158
* |
159
* \|/
160
* RequiresSetenv
161
* Is LD_LIBRARY_PATH
162
* and friends set ? --> NO --> Have Desired Model ? NO --> Re-exec --> Main
163
* YES YES --> Continue
164
* |
165
* |
166
* \|/
167
* Path is desired JRE ? YES --> Have Desired Model ? NO --> Re-exec --> Main
168
* NO YES --> Continue
169
* |
170
* |
171
* \|/
172
* Paths have well known
173
* jvm paths ? --> NO --> Have Desired Model ? NO --> Re-exec --> Main
174
* YES YES --> Continue
175
* |
176
* |
177
* \|/
178
* Does libjvm.so exit
179
* in any of them ? --> NO --> Have Desired Model ? NO --> Re-exec --> Main
180
* YES YES --> Continue
181
* |
182
* |
183
* \|/
184
* Set the LD_LIBRARY_PATH
185
* |
186
* |
187
* \|/
188
* Re-exec
189
* |
190
* |
191
* \|/
192
* Main
193
*/
194
195
#define GetArch() GetArchPath(CURRENT_DATA_MODEL)
196
197
/* Store the name of the executable once computed */
198
static char *execname = NULL;
199
200
/*
201
* execname accessor from other parts of platform dependent logic
202
*/
203
const char *
204
GetExecName() {
205
return execname;
206
}
207
208
const char *
209
GetArchPath(int nbits)
210
{
211
switch(nbits) {
212
#ifdef DUAL_MODE
213
case 32:
214
return LIBARCH32NAME;
215
case 64:
216
return LIBARCH64NAME;
217
#endif /* DUAL_MODE */
218
default:
219
return LIBARCHNAME;
220
}
221
}
222
223
#ifdef SETENV_REQUIRED
224
static jboolean
225
JvmExists(const char *path) {
226
char tmp[PATH_MAX + 1];
227
struct stat statbuf;
228
JLI_Snprintf(tmp, PATH_MAX, "%s/%s", path, JVM_DLL);
229
if (stat(tmp, &statbuf) == 0) {
230
return JNI_TRUE;
231
}
232
return JNI_FALSE;
233
}
234
/*
235
* contains a lib/$LIBARCH/{server,client}/libjvm.so ?
236
*/
237
static jboolean
238
ContainsLibJVM(int wanted, const char *env) {
239
char clientPattern[PATH_MAX + 1];
240
char serverPattern[PATH_MAX + 1];
241
char *envpath;
242
char *path;
243
char* save_ptr = NULL;
244
jboolean clientPatternFound;
245
jboolean serverPatternFound;
246
247
/* fastest path */
248
if (env == NULL) {
249
return JNI_FALSE;
250
}
251
252
/* the usual suspects */
253
JLI_Snprintf(clientPattern, PATH_MAX, "lib/%s/client", GetArchPath(wanted));
254
JLI_Snprintf(serverPattern, PATH_MAX, "lib/%s/server", GetArchPath(wanted));
255
256
/* to optimize for time, test if any of our usual suspects are present. */
257
clientPatternFound = JLI_StrStr(env, clientPattern) != NULL;
258
serverPatternFound = JLI_StrStr(env, serverPattern) != NULL;
259
if (clientPatternFound == JNI_FALSE && serverPatternFound == JNI_FALSE) {
260
return JNI_FALSE;
261
}
262
263
/*
264
* we have a suspicious path component, check if it contains a libjvm.so
265
*/
266
envpath = JLI_StringDup(env);
267
for (path = strtok_r(envpath, ":", &save_ptr); path != NULL; path = strtok_r(NULL, ":", &save_ptr)) {
268
if (clientPatternFound && JLI_StrStr(path, clientPattern) != NULL) {
269
if (JvmExists(path)) {
270
JLI_MemFree(envpath);
271
return JNI_TRUE;
272
}
273
}
274
if (serverPatternFound && JLI_StrStr(path, serverPattern) != NULL) {
275
if (JvmExists(path)) {
276
JLI_MemFree(envpath);
277
return JNI_TRUE;
278
}
279
}
280
}
281
JLI_MemFree(envpath);
282
return JNI_FALSE;
283
}
284
285
/*
286
* Test whether the environment variable needs to be set, see flowchart.
287
*/
288
static jboolean
289
RequiresSetenv(int wanted, const char *jvmpath) {
290
char jpath[PATH_MAX + 1];
291
char *llp;
292
char *dmllp = NULL;
293
char *p; /* a utility pointer */
294
295
#ifdef AIX
296
/* We always have to set the LIBPATH on AIX because ld doesn't support $ORIGIN. */
297
return JNI_TRUE;
298
#endif
299
300
llp = getenv("LD_LIBRARY_PATH");
301
#ifdef __solaris__
302
dmllp = (CURRENT_DATA_MODEL == 32)
303
? getenv("LD_LIBRARY_PATH_32")
304
: getenv("LD_LIBRARY_PATH_64");
305
#endif /* __solaris__ */
306
/* no environment variable is a good environment variable */
307
if (llp == NULL && dmllp == NULL) {
308
return JNI_FALSE;
309
}
310
#ifdef __linux
311
/*
312
* On linux, if a binary is running as sgid or suid, glibc sets
313
* LD_LIBRARY_PATH to the empty string for security purposes. (In contrast,
314
* on Solaris the LD_LIBRARY_PATH variable for a privileged binary does not
315
* lose its settings; but the dynamic linker does apply more scrutiny to the
316
* path.) The launcher uses the value of LD_LIBRARY_PATH to prevent an exec
317
* loop, here and further downstream. Therefore, if we are running sgid or
318
* suid, this function's setting of LD_LIBRARY_PATH will be ineffective and
319
* we should case a return from the calling function. Getting the right
320
* libraries will be handled by the RPATH. In reality, this check is
321
* redundant, as the previous check for a non-null LD_LIBRARY_PATH will
322
* return back to the calling function forthwith, it is left here to safe
323
* guard against any changes, in the glibc's existing security policy.
324
*/
325
if ((getgid() != getegid()) || (getuid() != geteuid())) {
326
return JNI_FALSE;
327
}
328
#endif /* __linux */
329
330
/*
331
* Prevent recursions. Since LD_LIBRARY_PATH is the one which will be set by
332
* previous versions of the JRE, thus it is the only path that matters here.
333
* So we check to see if the desired JRE is set.
334
*/
335
JLI_StrNCpy(jpath, jvmpath, PATH_MAX);
336
p = JLI_StrRChr(jpath, '/');
337
*p = '\0';
338
if (llp != NULL && JLI_StrNCmp(llp, jpath, JLI_StrLen(jpath)) == 0) {
339
return JNI_FALSE;
340
}
341
342
/* scrutinize all the paths further */
343
if (llp != NULL && ContainsLibJVM(wanted, llp)) {
344
return JNI_TRUE;
345
}
346
if (dmllp != NULL && ContainsLibJVM(wanted, dmllp)) {
347
return JNI_TRUE;
348
}
349
return JNI_FALSE;
350
}
351
#endif /* SETENV_REQUIRED */
352
353
void
354
CreateExecutionEnvironment(int *pargc, char ***pargv,
355
char jrepath[], jint so_jrepath,
356
char jvmpath[], jint so_jvmpath,
357
char jvmcfg[], jint so_jvmcfg) {
358
/*
359
* First, determine if we are running the desired data model. If we
360
* are running the desired data model, all the error messages
361
* associated with calling GetJREPath, ReadKnownVMs, etc. should be
362
* output. However, if we are not running the desired data model,
363
* some of the errors should be suppressed since it is more
364
* informative to issue an error message based on whether or not the
365
* os/processor combination has dual mode capabilities.
366
*/
367
jboolean jvmpathExists;
368
369
/* Compute/set the name of the executable */
370
SetExecname(*pargv);
371
372
/* Check data model flags, and exec process, if needed */
373
{
374
char *arch = (char *)GetArch(); /* like sparc or sparcv9 */
375
char * jvmtype = NULL;
376
int argc = *pargc;
377
char **argv = *pargv;
378
int running = CURRENT_DATA_MODEL;
379
380
int wanted = running; /* What data mode is being
381
asked for? Current model is
382
fine unless another model
383
is asked for */
384
#ifdef SETENV_REQUIRED
385
jboolean mustsetenv = JNI_FALSE;
386
char *runpath = NULL; /* existing effective LD_LIBRARY_PATH setting */
387
char* new_runpath = NULL; /* desired new LD_LIBRARY_PATH string */
388
char* newpath = NULL; /* path on new LD_LIBRARY_PATH */
389
char* lastslash = NULL;
390
char** newenvp = NULL; /* current environment */
391
#ifdef __solaris__
392
char* dmpath = NULL; /* data model specific LD_LIBRARY_PATH,
393
Solaris only */
394
#endif /* __solaris__ */
395
#endif /* SETENV_REQUIRED */
396
397
char** newargv = NULL;
398
int newargc = 0;
399
400
/*
401
* Starting in 1.5, all unix platforms accept the -d32 and -d64
402
* options. On platforms where only one data-model is supported
403
* (e.g. ia-64 Linux), using the flag for the other data model is
404
* an error and will terminate the program.
405
*/
406
407
{ /* open new scope to declare local variables */
408
int i;
409
410
newargv = (char **)JLI_MemAlloc((argc+1) * sizeof(char*));
411
newargv[newargc++] = argv[0];
412
413
/* scan for data model arguments and remove from argument list;
414
last occurrence determines desired data model */
415
for (i=1; i < argc; i++) {
416
417
if (JLI_StrCmp(argv[i], "-J-d64") == 0 || JLI_StrCmp(argv[i], "-d64") == 0) {
418
wanted = 64;
419
continue;
420
}
421
if (JLI_StrCmp(argv[i], "-J-d32") == 0 || JLI_StrCmp(argv[i], "-d32") == 0) {
422
wanted = 32;
423
continue;
424
}
425
newargv[newargc++] = argv[i];
426
427
if (IsJavaArgs()) {
428
if (argv[i][0] != '-') continue;
429
} else {
430
if (JLI_StrCmp(argv[i], "-classpath") == 0 || JLI_StrCmp(argv[i], "-cp") == 0) {
431
i++;
432
if (i >= argc) break;
433
newargv[newargc++] = argv[i];
434
continue;
435
}
436
if (argv[i][0] != '-') { i++; break; }
437
}
438
}
439
440
/* copy rest of args [i .. argc) */
441
while (i < argc) {
442
newargv[newargc++] = argv[i++];
443
}
444
newargv[newargc] = NULL;
445
446
/*
447
* newargv has all proper arguments here
448
*/
449
450
argc = newargc;
451
argv = newargv;
452
}
453
454
/* If the data model is not changing, it is an error if the
455
jvmpath does not exist */
456
if (wanted == running) {
457
/* Find out where the JRE is that we will be using. */
458
if (!GetJREPath(jrepath, so_jrepath, arch, JNI_FALSE) ) {
459
JLI_ReportErrorMessage(JRE_ERROR1);
460
exit(2);
461
}
462
JLI_Snprintf(jvmcfg, so_jvmcfg, "%s%slib%s%s%sjvm.cfg",
463
jrepath, FILESEP, FILESEP, arch, FILESEP);
464
/* Find the specified JVM type */
465
if (ReadKnownVMs(jvmcfg, JNI_FALSE) < 1) {
466
JLI_ReportErrorMessage(CFG_ERROR7);
467
exit(1);
468
}
469
470
jvmpath[0] = '\0';
471
jvmtype = CheckJvmType(pargc, pargv, JNI_FALSE);
472
if (JLI_StrCmp(jvmtype, "ERROR") == 0) {
473
JLI_ReportErrorMessage(CFG_ERROR9);
474
exit(4);
475
}
476
477
if (!GetJVMPath(jrepath, jvmtype, jvmpath, so_jvmpath, arch, 0 )) {
478
JLI_ReportErrorMessage(CFG_ERROR8, jvmtype, jvmpath);
479
exit(4);
480
}
481
/*
482
* we seem to have everything we need, so without further ado
483
* we return back, otherwise proceed to set the environment.
484
*/
485
#ifdef SETENV_REQUIRED
486
mustsetenv = RequiresSetenv(wanted, jvmpath);
487
JLI_TraceLauncher("mustsetenv: %s\n", mustsetenv ? "TRUE" : "FALSE");
488
489
if (mustsetenv == JNI_FALSE) {
490
JLI_MemFree(newargv);
491
return;
492
}
493
#else
494
JLI_MemFree(newargv);
495
return;
496
#endif /* SETENV_REQUIRED */
497
} else { /* do the same speculatively or exit */
498
#ifdef DUAL_MODE
499
if (running != wanted) {
500
/* Find out where the JRE is that we will be using. */
501
if (!GetJREPath(jrepath, so_jrepath, GetArchPath(wanted), JNI_TRUE)) {
502
/* give up and let other code report error message */
503
JLI_ReportErrorMessage(JRE_ERROR2, wanted);
504
exit(1);
505
}
506
JLI_Snprintf(jvmcfg, so_jvmcfg, "%s%slib%s%s%sjvm.cfg",
507
jrepath, FILESEP, FILESEP, GetArchPath(wanted), FILESEP);
508
/*
509
* Read in jvm.cfg for target data model and process vm
510
* selection options.
511
*/
512
if (ReadKnownVMs(jvmcfg, JNI_TRUE) < 1) {
513
/* give up and let other code report error message */
514
JLI_ReportErrorMessage(JRE_ERROR2, wanted);
515
exit(1);
516
}
517
jvmpath[0] = '\0';
518
jvmtype = CheckJvmType(pargc, pargv, JNI_TRUE);
519
if (JLI_StrCmp(jvmtype, "ERROR") == 0) {
520
JLI_ReportErrorMessage(CFG_ERROR9);
521
exit(4);
522
}
523
524
/* exec child can do error checking on the existence of the path */
525
jvmpathExists = GetJVMPath(jrepath, jvmtype, jvmpath, so_jvmpath, GetArchPath(wanted), 0);
526
#ifdef SETENV_REQUIRED
527
mustsetenv = RequiresSetenv(wanted, jvmpath);
528
#endif /* SETENV_REQUIRED */
529
}
530
#else /* ! DUALMODE */
531
JLI_ReportErrorMessage(JRE_ERROR2, wanted);
532
exit(1);
533
#endif /* DUAL_MODE */
534
}
535
#ifdef SETENV_REQUIRED
536
if (mustsetenv) {
537
/*
538
* We will set the LD_LIBRARY_PATH as follows:
539
*
540
* o $JVMPATH (directory portion only)
541
* o $JRE/lib/$LIBARCHNAME
542
* o $JRE/../lib/$LIBARCHNAME
543
*
544
* followed by the user's previous effective LD_LIBRARY_PATH, if
545
* any.
546
*/
547
548
#ifdef __solaris__
549
/*
550
* Starting in Solaris 7, ld.so.1 supports three LD_LIBRARY_PATH
551
* variables:
552
*
553
* 1. LD_LIBRARY_PATH -- used for 32 and 64 bit searches if
554
* data-model specific variables are not set.
555
*
556
* 2. LD_LIBRARY_PATH_64 -- overrides and replaces LD_LIBRARY_PATH
557
* for 64-bit binaries.
558
*
559
* 3. LD_LIBRARY_PATH_32 -- overrides and replaces LD_LIBRARY_PATH
560
* for 32-bit binaries.
561
*
562
* The vm uses LD_LIBRARY_PATH to set the java.library.path system
563
* property. To shield the vm from the complication of multiple
564
* LD_LIBRARY_PATH variables, if the appropriate data model
565
* specific variable is set, we will act as if LD_LIBRARY_PATH had
566
* the value of the data model specific variant and the data model
567
* specific variant will be unset. Note that the variable for the
568
* *wanted* data model must be used (if it is set), not simply the
569
* current running data model.
570
*/
571
572
switch (wanted) {
573
case 0:
574
if (running == 32) {
575
dmpath = getenv("LD_LIBRARY_PATH_32");
576
wanted = 32;
577
} else {
578
dmpath = getenv("LD_LIBRARY_PATH_64");
579
wanted = 64;
580
}
581
break;
582
583
case 32:
584
dmpath = getenv("LD_LIBRARY_PATH_32");
585
break;
586
587
case 64:
588
dmpath = getenv("LD_LIBRARY_PATH_64");
589
break;
590
591
default:
592
JLI_ReportErrorMessage(JRE_ERROR3, __LINE__);
593
exit(1); /* unknown value in wanted */
594
break;
595
}
596
597
/*
598
* If dmpath is NULL, the relevant data model specific variable is
599
* not set and normal LD_LIBRARY_PATH should be used.
600
*/
601
if (dmpath == NULL) {
602
runpath = getenv("LD_LIBRARY_PATH");
603
} else {
604
runpath = dmpath;
605
}
606
#else /* ! __solaris__ */
607
/*
608
* If not on Solaris, assume only a single LD_LIBRARY_PATH
609
* variable.
610
*/
611
runpath = getenv(LD_LIBRARY_PATH);
612
#endif /* __solaris__ */
613
614
/* runpath contains current effective LD_LIBRARY_PATH setting */
615
616
jvmpath = JLI_StringDup(jvmpath);
617
size_t new_runpath_size = ((runpath != NULL) ? JLI_StrLen(runpath) : 0) +
618
2 * JLI_StrLen(jrepath) + 2 * JLI_StrLen(arch) +
619
#ifdef AIX
620
/* On AIX we additionally need 'jli' in the path because ld doesn't support $ORIGIN. */
621
JLI_StrLen(jrepath) + JLI_StrLen(arch) + JLI_StrLen("/lib//jli:") +
622
#endif
623
JLI_StrLen(jvmpath) + 52;
624
new_runpath = JLI_MemAlloc(new_runpath_size);
625
newpath = new_runpath + JLI_StrLen(LD_LIBRARY_PATH "=");
626
627
628
/*
629
* Create desired LD_LIBRARY_PATH value for target data model.
630
*/
631
{
632
/* remove the name of the .so from the JVM path */
633
lastslash = JLI_StrRChr(jvmpath, '/');
634
if (lastslash)
635
*lastslash = '\0';
636
637
sprintf(new_runpath, LD_LIBRARY_PATH "="
638
"%s:"
639
"%s/lib/%s:"
640
#ifdef AIX
641
"%s/lib/%s/jli:" /* Needed on AIX because ld doesn't support $ORIGIN. */
642
#endif
643
"%s/../lib/%s",
644
jvmpath,
645
#ifdef DUAL_MODE
646
jrepath, GetArchPath(wanted),
647
jrepath, GetArchPath(wanted)
648
#else /* !DUAL_MODE */
649
jrepath, arch,
650
#ifdef AIX
651
jrepath, arch,
652
#endif
653
jrepath, arch
654
#endif /* DUAL_MODE */
655
);
656
657
658
/*
659
* Check to make sure that the prefix of the current path is the
660
* desired environment variable setting, though the RequiresSetenv
661
* checks if the desired runpath exists, this logic does a more
662
* comprehensive check.
663
*/
664
if (runpath != NULL &&
665
JLI_StrNCmp(newpath, runpath, JLI_StrLen(newpath)) == 0 &&
666
(runpath[JLI_StrLen(newpath)] == 0 || runpath[JLI_StrLen(newpath)] == ':') &&
667
(running == wanted) /* data model does not have to be changed */
668
#ifdef __solaris__
669
&& (dmpath == NULL) /* data model specific variables not set */
670
#endif /* __solaris__ */
671
) {
672
JLI_MemFree(newargv);
673
JLI_MemFree(new_runpath);
674
return;
675
}
676
}
677
678
/*
679
* Place the desired environment setting onto the prefix of
680
* LD_LIBRARY_PATH. Note that this prevents any possible infinite
681
* loop of execv() because we test for the prefix, above.
682
*/
683
if (runpath != 0) {
684
/* ensure storage for runpath + colon + NULL */
685
if ((JLI_StrLen(runpath) + 1 + 1) > new_runpath_size) {
686
JLI_ReportErrorMessageSys(JRE_ERROR11);
687
exit(1);
688
}
689
JLI_StrCat(new_runpath, ":");
690
JLI_StrCat(new_runpath, runpath);
691
}
692
693
if (putenv(new_runpath) != 0) {
694
exit(1); /* problem allocating memory; LD_LIBRARY_PATH not set
695
properly */
696
}
697
698
/*
699
* Unix systems document that they look at LD_LIBRARY_PATH only
700
* once at startup, so we have to re-exec the current executable
701
* to get the changed environment variable to have an effect.
702
*/
703
704
#ifdef __solaris__
705
/*
706
* If dmpath is not NULL, remove the data model specific string
707
* in the environment for the exec'ed child.
708
*/
709
if (dmpath != NULL)
710
(void)UnsetEnv((wanted == 32) ? "LD_LIBRARY_PATH_32" : "LD_LIBRARY_PATH_64");
711
#endif /* __solaris */
712
713
newenvp = environ;
714
}
715
#endif /* SETENV_REQUIRED */
716
{
717
char *newexec = execname;
718
#ifdef DUAL_MODE
719
/*
720
* If the data model is being changed, the path to the
721
* executable must be updated accordingly; the executable name
722
* and directory the executable resides in are separate. In the
723
* case of 32 => 64, the new bits are assumed to reside in, e.g.
724
* "olddir/LIBARCH64NAME/execname"; in the case of 64 => 32,
725
* the bits are assumed to be in "olddir/../execname". For example,
726
*
727
* olddir/sparcv9/execname
728
* olddir/amd64/execname
729
*
730
* for Solaris SPARC and Linux amd64, respectively.
731
*/
732
733
if (running != wanted) {
734
char *oldexec = JLI_StrCpy(JLI_MemAlloc(JLI_StrLen(execname) + 1), execname);
735
char *olddir = oldexec;
736
char *oldbase = JLI_StrRChr(oldexec, '/');
737
738
739
newexec = JLI_MemAlloc(JLI_StrLen(execname) + 20);
740
*oldbase++ = 0;
741
sprintf(newexec, "%s/%s/%s", olddir,
742
((wanted == 64) ? LIBARCH64NAME : ".."), oldbase);
743
argv[0] = newexec;
744
}
745
#endif /* DUAL_MODE */
746
JLI_TraceLauncher("TRACER_MARKER:About to EXEC\n");
747
(void) fflush(stdout);
748
(void) fflush(stderr);
749
#ifdef SETENV_REQUIRED
750
if (mustsetenv) {
751
execve(newexec, argv, newenvp);
752
} else {
753
execv(newexec, argv);
754
}
755
#else /* !SETENV_REQUIRED */
756
execv(newexec, argv);
757
#endif /* SETENV_REQUIRED */
758
JLI_ReportErrorMessageSys(JRE_ERROR4, newexec);
759
760
#ifdef DUAL_MODE
761
if (running != wanted) {
762
JLI_ReportErrorMessage(JRE_ERROR5, wanted, running);
763
#ifdef __solaris__
764
#ifdef __sparc
765
JLI_ReportErrorMessage(JRE_ERROR6);
766
#else /* ! __sparc__ */
767
JLI_ReportErrorMessage(JRE_ERROR7);
768
#endif /* __sparc */
769
#endif /* __solaris__ */
770
}
771
#endif /* DUAL_MODE */
772
773
}
774
exit(1);
775
}
776
}
777
778
/*
779
* On Solaris VM choosing is done by the launcher (java.c),
780
* bitsWanted is used by MacOSX, on Solaris and Linux this.
781
* parameter is unused.
782
*/
783
static jboolean
784
GetJVMPath(const char *jrepath, const char *jvmtype,
785
char *jvmpath, jint jvmpathsize, const char * arch, int bitsWanted)
786
{
787
struct stat s;
788
789
if (JLI_StrChr(jvmtype, '/')) {
790
JLI_Snprintf(jvmpath, jvmpathsize, "%s/" JVM_DLL, jvmtype);
791
} else {
792
JLI_Snprintf(jvmpath, jvmpathsize, "%s/lib/%s/%s/" JVM_DLL, jrepath, arch, jvmtype);
793
}
794
795
JLI_TraceLauncher("Does `%s' exist ... ", jvmpath);
796
797
if (stat(jvmpath, &s) == 0) {
798
JLI_TraceLauncher("yes.\n");
799
return JNI_TRUE;
800
} else {
801
JLI_TraceLauncher("no.\n");
802
return JNI_FALSE;
803
}
804
}
805
806
/*
807
* Find path to JRE based on .exe's location or registry settings.
808
*/
809
static jboolean
810
GetJREPath(char *path, jint pathsize, const char * arch, jboolean speculative)
811
{
812
char libjava[MAXPATHLEN];
813
814
if (GetApplicationHome(path, pathsize)) {
815
/* Is JRE co-located with the application? */
816
JLI_Snprintf(libjava, sizeof(libjava), "%s/lib/%s/" JAVA_DLL, path, arch);
817
if (access(libjava, F_OK) == 0) {
818
JLI_TraceLauncher("JRE path is %s\n", path);
819
return JNI_TRUE;
820
}
821
/* ensure storage for path + /jre + NULL */
822
if ((JLI_StrLen(path) + 4 + 1) > pathsize) {
823
JLI_TraceLauncher("Insufficient space to store JRE path\n");
824
return JNI_FALSE;
825
}
826
/* Does the app ship a private JRE in <apphome>/jre directory? */
827
JLI_Snprintf(libjava, sizeof(libjava), "%s/jre/lib/%s/" JAVA_DLL, path, arch);
828
if (access(libjava, F_OK) == 0) {
829
JLI_StrCat(path, "/jre");
830
JLI_TraceLauncher("JRE path is %s\n", path);
831
return JNI_TRUE;
832
}
833
}
834
835
if (!speculative)
836
JLI_ReportErrorMessage(JRE_ERROR8 JAVA_DLL);
837
return JNI_FALSE;
838
}
839
840
jboolean
841
LoadJavaVM(const char *jvmpath, InvocationFunctions *ifn)
842
{
843
void *libjvm;
844
845
JLI_TraceLauncher("JVM path is %s\n", jvmpath);
846
847
libjvm = dlopen(jvmpath, RTLD_NOW + RTLD_GLOBAL);
848
if (libjvm == NULL) {
849
#if defined(__solaris__) && defined(__sparc) && !defined(_LP64) /* i.e. 32-bit sparc */
850
FILE * fp;
851
Elf32_Ehdr elf_head;
852
int count;
853
int location;
854
855
fp = fopen(jvmpath, "r");
856
if (fp == NULL) {
857
JLI_ReportErrorMessage(DLL_ERROR2, jvmpath, dlerror());
858
return JNI_FALSE;
859
}
860
861
/* read in elf header */
862
count = fread((void*)(&elf_head), sizeof(Elf32_Ehdr), 1, fp);
863
fclose(fp);
864
if (count < 1) {
865
JLI_ReportErrorMessage(DLL_ERROR2, jvmpath, dlerror());
866
return JNI_FALSE;
867
}
868
869
/*
870
* Check for running a server vm (compiled with -xarch=v8plus)
871
* on a stock v8 processor. In this case, the machine type in
872
* the elf header would not be included the architecture list
873
* provided by the isalist command, which is turn is gotten from
874
* sysinfo. This case cannot occur on 64-bit hardware and thus
875
* does not have to be checked for in binaries with an LP64 data
876
* model.
877
*/
878
if (elf_head.e_machine == EM_SPARC32PLUS) {
879
char buf[257]; /* recommended buffer size from sysinfo man
880
page */
881
long length;
882
char* location;
883
884
length = sysinfo(SI_ISALIST, buf, 257);
885
if (length > 0) {
886
location = JLI_StrStr(buf, "sparcv8plus ");
887
if (location == NULL) {
888
JLI_ReportErrorMessage(JVM_ERROR3);
889
return JNI_FALSE;
890
}
891
}
892
}
893
#endif
894
JLI_ReportErrorMessage(DLL_ERROR1, __LINE__);
895
JLI_ReportErrorMessage(DLL_ERROR2, jvmpath, dlerror());
896
return JNI_FALSE;
897
}
898
899
ifn->CreateJavaVM = (CreateJavaVM_t)
900
dlsym(libjvm, "JNI_CreateJavaVM");
901
if (ifn->CreateJavaVM == NULL) {
902
JLI_ReportErrorMessage(DLL_ERROR2, jvmpath, dlerror());
903
return JNI_FALSE;
904
}
905
906
ifn->GetDefaultJavaVMInitArgs = (GetDefaultJavaVMInitArgs_t)
907
dlsym(libjvm, "JNI_GetDefaultJavaVMInitArgs");
908
if (ifn->GetDefaultJavaVMInitArgs == NULL) {
909
JLI_ReportErrorMessage(DLL_ERROR2, jvmpath, dlerror());
910
return JNI_FALSE;
911
}
912
913
ifn->GetCreatedJavaVMs = (GetCreatedJavaVMs_t)
914
dlsym(libjvm, "JNI_GetCreatedJavaVMs");
915
if (ifn->GetCreatedJavaVMs == NULL) {
916
JLI_ReportErrorMessage(DLL_ERROR2, jvmpath, dlerror());
917
return JNI_FALSE;
918
}
919
920
return JNI_TRUE;
921
}
922
923
/*
924
* Compute the name of the executable
925
*
926
* In order to re-exec securely we need the absolute path of the
927
* executable. On Solaris getexecname(3c) may not return an absolute
928
* path so we use dladdr to get the filename of the executable and
929
* then use realpath to derive an absolute path. From Solaris 9
930
* onwards the filename returned in DL_info structure from dladdr is
931
* an absolute pathname so technically realpath isn't required.
932
* On Linux we read the executable name from /proc/self/exe.
933
* As a fallback, and for platforms other than Solaris and Linux,
934
* we use FindExecName to compute the executable name.
935
*/
936
const char*
937
SetExecname(char **argv)
938
{
939
char* exec_path = NULL;
940
#if defined(__solaris__)
941
{
942
Dl_info dlinfo;
943
int (*fptr)();
944
945
fptr = (int (*)())dlsym(RTLD_DEFAULT, "main");
946
if (fptr == NULL) {
947
JLI_ReportErrorMessage(DLL_ERROR3, dlerror());
948
return JNI_FALSE;
949
}
950
951
if (dladdr((void*)fptr, &dlinfo)) {
952
char *resolved = (char*)JLI_MemAlloc(PATH_MAX+1);
953
if (resolved != NULL) {
954
exec_path = realpath(dlinfo.dli_fname, resolved);
955
if (exec_path == NULL) {
956
JLI_MemFree(resolved);
957
}
958
}
959
}
960
}
961
#elif defined(__ANDROID__)
962
char *__java_home = getenv("JAVA_HOME");
963
// From http://hg.openjdk.java.net/mobile/jdk9/jdk/file/17bb8a98d5e3/src/java.base/unix/native/libjli/java_md_solinux.c#l844
964
/* For Android, 'self' would point to /system/bin/app_process
965
* since we are really executing a Dalvik program at this point.
966
* argv[0] points to the Dalvik application name and we set the
967
* path to __java_home.
968
*/
969
char buf[PATH_MAX+1];
970
char *p = NULL;
971
if ((p = JLI_StrRChr(argv[0], '/')) != 0) {
972
/* may be running from command line */
973
p++;
974
if ((JLI_StrLen(p) == 4) && JLI_StrCmp(p, "java") == 0) {
975
/* started as 'java'. Must be command line */
976
JLI_TraceLauncher("SetExecName maybe command line = %s\n", argv[0]);
977
if (*argv[0] != '/') {
978
char *curdir = NULL;
979
/* get absolute path */
980
getcwd(buf, PATH_MAX);
981
curdir = JLI_StringDup(buf);
982
JLI_Snprintf(buf, PATH_MAX, "%s/%s", curdir, argv[0]);
983
JLI_MemFree(curdir);
984
} else {
985
JLI_Snprintf(buf, PATH_MAX, "%s", argv[0]);
986
}
987
} else {
988
/* Not command line, see if __java_home set */
989
if (__java_home != NULL) {
990
JLI_TraceLauncher("SetExecName not java = %s\n", __java_home);
991
JLI_Snprintf(buf, PATH_MAX, "%s/bin/java", __java_home);
992
} else {
993
/* Fake it as best we can or should we punt? */
994
JLI_TraceLauncher("SetExecName fake it = %s\n", argv[0]);
995
JLI_Snprintf(buf, PATH_MAX, "/data/data/%s/storage/jvm/bin/java",
996
argv[0]);
997
}
998
}
999
} else {
1000
/* Not started as 'java', see if __java_home set */
1001
if (__java_home != NULL) {
1002
JLI_TraceLauncher("SetExecName not command line = %s\n", __java_home);
1003
JLI_Snprintf(buf, PATH_MAX, "%s/bin/java", __java_home);
1004
} else {
1005
/* Fake it as best we can or should we punt? */
1006
JLI_TraceLauncher("SetExecName fake it 2 = %s\n", argv[0]);
1007
JLI_Snprintf(buf, PATH_MAX, "/data/data/%s/storage/jvm/bin/java",
1008
argv[0]);
1009
}
1010
}
1011
exec_path = JLI_StringDup(buf);
1012
#elif defined(__linux__)
1013
{
1014
const char* self = "/proc/self/exe";
1015
char buf[PATH_MAX+1];
1016
int len = readlink(self, buf, PATH_MAX);
1017
if (len >= 0) {
1018
buf[len] = '\0'; /* readlink(2) doesn't NUL terminate */
1019
exec_path = JLI_StringDup(buf);
1020
}
1021
}
1022
#else /* !__solaris__ && !__linux__ */
1023
{
1024
/* Not implemented */
1025
}
1026
#endif
1027
1028
if (exec_path == NULL) {
1029
exec_path = FindExecName(argv[0]);
1030
}
1031
execname = exec_path;
1032
return exec_path;
1033
}
1034
1035
/* --- Splash Screen shared library support --- */
1036
static const char* SPLASHSCREEN_SO = JNI_LIB_NAME("splashscreen");
1037
static void* hSplashLib = NULL;
1038
1039
void* SplashProcAddress(const char* name) {
1040
if (!hSplashLib) {
1041
int ret;
1042
char jrePath[MAXPATHLEN];
1043
char splashPath[MAXPATHLEN];
1044
1045
if (!GetJREPath(jrePath, sizeof(jrePath), GetArch(), JNI_FALSE)) {
1046
JLI_ReportErrorMessage(JRE_ERROR1);
1047
return NULL;
1048
}
1049
ret = JLI_Snprintf(splashPath, sizeof(splashPath), "%s/lib/%s/%s",
1050
jrePath, GetArch(), SPLASHSCREEN_SO);
1051
1052
if (ret >= (int) sizeof(splashPath)) {
1053
JLI_ReportErrorMessage(JRE_ERROR11);
1054
return NULL;
1055
}
1056
if (ret < 0) {
1057
JLI_ReportErrorMessage(JRE_ERROR13);
1058
return NULL;
1059
}
1060
hSplashLib = dlopen(splashPath, RTLD_LAZY | RTLD_GLOBAL);
1061
JLI_TraceLauncher("Info: loaded %s\n", splashPath);
1062
}
1063
if (hSplashLib) {
1064
void* sym = dlsym(hSplashLib, name);
1065
return sym;
1066
} else {
1067
return NULL;
1068
}
1069
}
1070
1071
void SplashFreeLibrary() {
1072
if (hSplashLib) {
1073
dlclose(hSplashLib);
1074
hSplashLib = NULL;
1075
}
1076
}
1077
1078
/*
1079
* Block current thread and continue execution in a new thread
1080
*/
1081
int
1082
ContinueInNewThread0(int (JNICALL *continuation)(void *), jlong stack_size, void * args) {
1083
int rslt;
1084
#ifndef __solaris__
1085
pthread_t tid;
1086
pthread_attr_t attr;
1087
pthread_attr_init(&attr);
1088
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
1089
1090
if (stack_size > 0) {
1091
pthread_attr_setstacksize(&attr, stack_size);
1092
}
1093
1094
if (pthread_create(&tid, &attr, (void *(*)(void*))continuation, (void*)args) == 0) {
1095
void * tmp;
1096
pthread_join(tid, &tmp);
1097
rslt = (int)tmp;
1098
} else {
1099
/*
1100
* Continue execution in current thread if for some reason (e.g. out of
1101
* memory/LWP) a new thread can't be created. This will likely fail
1102
* later in continuation as JNI_CreateJavaVM needs to create quite a
1103
* few new threads, anyway, just give it a try..
1104
*/
1105
rslt = continuation(args);
1106
}
1107
1108
pthread_attr_destroy(&attr);
1109
#else /* __solaris__ */
1110
thread_t tid;
1111
long flags = 0;
1112
if (thr_create(NULL, stack_size, (void *(*)(void *))continuation, args, flags, &tid) == 0) {
1113
void * tmp;
1114
thr_join(tid, NULL, &tmp);
1115
rslt = (int)tmp;
1116
} else {
1117
/* See above. Continue in current thread if thr_create() failed */
1118
rslt = continuation(args);
1119
}
1120
#endif /* !__solaris__ */
1121
return rslt;
1122
}
1123
1124
/* Coarse estimation of number of digits assuming the worst case is a 64-bit pid. */
1125
#define MAX_PID_STR_SZ 20
1126
1127
void SetJavaLauncherPlatformProps() {
1128
/* Linux only */
1129
#ifdef __linux__
1130
const char *substr = "-Dsun.java.launcher.pid=";
1131
char *pid_prop_str = (char *)JLI_MemAlloc(JLI_StrLen(substr) + MAX_PID_STR_SZ + 1);
1132
sprintf(pid_prop_str, "%s%d", substr, getpid());
1133
AddOption(pid_prop_str, NULL);
1134
#endif /* __linux__ */
1135
}
1136
1137
int
1138
JVMInit(InvocationFunctions* ifn, jlong threadStackSize,
1139
int argc, char **argv,
1140
int mode, char *what, int ret)
1141
{
1142
ShowSplashScreen();
1143
return ContinueInNewThread(ifn, threadStackSize, argc, argv, mode, what, ret);
1144
}
1145
1146
void
1147
PostJVMInit(JNIEnv *env, jstring mainClass, JavaVM *vm)
1148
{
1149
// stubbed out for windows and *nixes.
1150
}
1151
1152
void
1153
RegisterThread()
1154
{
1155
// stubbed out for windows and *nixes.
1156
}
1157
1158
/*
1159
* on unix, we return a false to indicate this option is not applicable
1160
*/
1161
jboolean
1162
ProcessPlatformOption(const char *arg)
1163
{
1164
return JNI_FALSE;
1165
}
1166
1167
#ifndef __solaris__
1168
1169
/*
1170
* Provide a CounterGet() implementation based on gettimeofday() which
1171
* is universally available, even though it may not be 'high resolution'
1172
* compared to platforms that provide gethrtime() (like Solaris). It is
1173
* also subject to time-of-day changes, but alternatives may not be
1174
* known to be available at either build time or run time.
1175
*/
1176
uint64_t CounterGet() {
1177
uint64_t result = 0;
1178
struct timeval tv;
1179
if (gettimeofday(&tv, NULL) != -1) {
1180
result = 1000000LL * (uint64_t)tv.tv_sec;
1181
result += (uint64_t)tv.tv_usec;
1182
}
1183
return result;
1184
}
1185
1186
#endif // !__solaris__
1187
1188