1 /* |
|
2 * Copyright (c) 1998, 2011, 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 <dirent.h> |
|
28 #include <dlfcn.h> |
|
29 #include <fcntl.h> |
|
30 #include <inttypes.h> |
|
31 #include <stdio.h> |
|
32 #include <string.h> |
|
33 #include <stdlib.h> |
|
34 #include <sys/stat.h> |
|
35 #include <unistd.h> |
|
36 #include <sys/types.h> |
|
37 #include "manifest_info.h" |
|
38 #include "version_comp.h" |
|
39 |
|
40 #ifdef __solaris__ |
|
41 #include <thread.h> |
|
42 #else |
|
43 #include <pthread.h> |
|
44 #endif |
|
45 |
|
46 #define JVM_DLL "libjvm.so" |
|
47 #define JAVA_DLL "libjava.so" |
|
48 |
|
49 /* help jettison the LD_LIBRARY_PATH settings in the future */ |
|
50 #ifndef SETENV_REQUIRED |
|
51 #define SETENV_REQUIRED |
|
52 #endif |
|
53 /* |
|
54 * If a processor / os combination has the ability to run binaries of |
|
55 * two data models and cohabitation of jre/jdk bits with both data |
|
56 * models is supported, then DUAL_MODE is defined. When DUAL_MODE is |
|
57 * defined, the architecture names for the narrow and wide version of |
|
58 * the architecture are defined in LIBARCH64NAME and LIBARCH32NAME. |
|
59 * Currently only Solaris on sparc/sparcv9 and i586/amd64 is DUAL_MODE; |
|
60 * linux i586/amd64 could be defined as DUAL_MODE but that is not the |
|
61 * current policy. |
|
62 */ |
|
63 |
|
64 #ifdef __solaris__ |
|
65 # define DUAL_MODE |
|
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 /* pointer to environment */ |
|
78 extern char **environ; |
|
79 |
|
80 /* |
|
81 * A collection of useful strings. One should think of these as #define |
|
82 * entries, but actual strings can be more efficient (with many compilers). |
|
83 */ |
|
84 #ifdef __linux__ |
|
85 static const char *system_dir = "/usr/java"; |
|
86 static const char *user_dir = "/java"; |
|
87 #else /* Solaris */ |
|
88 static const char *system_dir = "/usr/jdk"; |
|
89 static const char *user_dir = "/jdk"; |
|
90 #endif |
|
91 |
|
92 /* Store the name of the executable once computed */ |
|
93 static char *execname = NULL; |
|
94 |
|
95 /* |
|
96 * Flowchart of launcher execs and options processing on unix |
|
97 * |
|
98 * The selection of the proper vm shared library to open depends on |
|
99 * several classes of command line options, including vm "flavor" |
|
100 * options (-client, -server) and the data model options, -d32 and |
|
101 * -d64, as well as a version specification which may have come from |
|
102 * the command line or from the manifest of an executable jar file. |
|
103 * The vm selection options are not passed to the running |
|
104 * virtual machine; they must be screened out by the launcher. |
|
105 * |
|
106 * The version specification (if any) is processed first by the |
|
107 * platform independent routine SelectVersion. This may result in |
|
108 * the exec of the specified launcher version. |
|
109 * |
|
110 * Previously the launcher modified the LD_LIBRARY_PATH appropriately for the |
|
111 * desired data model path, regardless if data models matched or not. The |
|
112 * launcher subsequently exec'ed the desired executable, in order to make the |
|
113 * LD_LIBRARY_PATH path available, for the runtime linker. |
|
114 * |
|
115 * Now, in most cases,the launcher will dlopen the target libjvm.so. All |
|
116 * required libraries are loaded by the runtime linker, using the |
|
117 * $RPATH/$ORIGIN baked into the shared libraries at compile time. Therefore, |
|
118 * in most cases, the launcher will only exec, if the data models are |
|
119 * mismatched, and will not set any environment variables, regardless of the |
|
120 * data models. |
|
121 * |
|
122 * However, if the environment contains a LD_LIBRARY_PATH, this will cause the |
|
123 * launcher to inspect the LD_LIBRARY_PATH. The launcher will check |
|
124 * a. if the LD_LIBRARY_PATH's first component is the the path to the desired |
|
125 * libjvm.so |
|
126 * b. if any other libjvm.so is found in any of the paths. |
|
127 * If case b is true, then the launcher will set the LD_LIBRARY_PATH to the |
|
128 * desired JRE and reexec, in order to propagate the environment. |
|
129 * |
|
130 * Main |
|
131 * (incoming argv) |
|
132 * | |
|
133 * \|/ |
|
134 * SelectVersion |
|
135 * (selects the JRE version, note: not data model) |
|
136 * | |
|
137 * \|/ |
|
138 * CreateExecutionEnvironment |
|
139 * (determines desired data model) |
|
140 * | |
|
141 * | |
|
142 * \|/ |
|
143 * Have Desired Model ? --> NO --> Is Dual-Mode ? --> NO --> Exit(with error) |
|
144 * | | |
|
145 * | | |
|
146 * | \|/ |
|
147 * | YES |
|
148 * | | |
|
149 * | | |
|
150 * | \|/ |
|
151 * | CheckJvmType |
|
152 * | (removes -client, -server etc.) |
|
153 * | | |
|
154 * | | |
|
155 * \|/ \|/ |
|
156 * YES Find the desired executable/library |
|
157 * | | |
|
158 * | | |
|
159 * \|/ \|/ |
|
160 * CheckJvmType RequiresSetenv |
|
161 * (removes -client, -server, etc.) |
|
162 * | |
|
163 * | |
|
164 * \|/ |
|
165 * TranslateDashJArgs... |
|
166 * (Prepare to pass args to vm) |
|
167 * | |
|
168 * | |
|
169 * \|/ |
|
170 * ParseArguments |
|
171 * (removes -d32 and -d64 if any, |
|
172 * processes version options, |
|
173 * creates argument list for vm, |
|
174 * etc.) |
|
175 * | |
|
176 * | |
|
177 * \|/ |
|
178 * RequiresSetenv |
|
179 * Is LD_LIBRARY_PATH |
|
180 * and friends set ? --> NO --> Have Desired Model ? NO --> Re-exec --> Main |
|
181 * YES YES --> Continue |
|
182 * | |
|
183 * | |
|
184 * \|/ |
|
185 * Path is desired JRE ? YES --> Have Desired Model ? NO --> Re-exec --> Main |
|
186 * NO YES --> Continue |
|
187 * | |
|
188 * | |
|
189 * \|/ |
|
190 * Paths have well known |
|
191 * jvm paths ? --> NO --> Have Desired Model ? NO --> Re-exec --> Main |
|
192 * YES YES --> Continue |
|
193 * | |
|
194 * | |
|
195 * \|/ |
|
196 * Does libjvm.so exit |
|
197 * in any of them ? --> NO --> Have Desired Model ? NO --> Re-exec --> Main |
|
198 * YES YES --> Continue |
|
199 * | |
|
200 * | |
|
201 * \|/ |
|
202 * Set the LD_LIBRARY_PATH |
|
203 * | |
|
204 * | |
|
205 * \|/ |
|
206 * Re-exec |
|
207 * | |
|
208 * | |
|
209 * \|/ |
|
210 * Main |
|
211 */ |
|
212 |
|
213 static const char * SetExecname(char **argv); |
|
214 static jboolean GetJVMPath(const char *jrepath, const char *jvmtype, |
|
215 char *jvmpath, jint jvmpathsize, const char * arch); |
|
216 static jboolean GetJREPath(char *path, jint pathsize, const char * arch, jboolean speculative); |
|
217 |
|
218 |
|
219 #define GetArch() GetArchPath(CURRENT_DATA_MODEL) |
|
220 |
|
221 const char * |
|
222 GetArchPath(int nbits) |
|
223 { |
|
224 switch(nbits) { |
|
225 #ifdef DUAL_MODE |
|
226 case 32: |
|
227 return LIBARCH32NAME; |
|
228 case 64: |
|
229 return LIBARCH64NAME; |
|
230 #endif /* DUAL_MODE */ |
|
231 default: |
|
232 return LIBARCHNAME; |
|
233 } |
|
234 } |
|
235 |
|
236 #ifdef SETENV_REQUIRED |
|
237 static jboolean |
|
238 JvmExists(const char *path) { |
|
239 char tmp[PATH_MAX + 1]; |
|
240 struct stat statbuf; |
|
241 JLI_Snprintf(tmp, PATH_MAX, "%s/%s", path, JVM_DLL); |
|
242 if (stat(tmp, &statbuf) == 0) { |
|
243 return JNI_TRUE; |
|
244 } |
|
245 return JNI_FALSE; |
|
246 } |
|
247 /* |
|
248 * contains a lib/$LIBARCH/{server,client}/libjvm.so ? |
|
249 */ |
|
250 static jboolean |
|
251 ContainsLibJVM(int wanted, const char *env) { |
|
252 char clientPattern[PATH_MAX + 1]; |
|
253 char serverPattern[PATH_MAX + 1]; |
|
254 char *envpath; |
|
255 char *path; |
|
256 jboolean clientPatternFound; |
|
257 jboolean serverPatternFound; |
|
258 |
|
259 /* fastest path */ |
|
260 if (env == NULL) { |
|
261 return JNI_FALSE; |
|
262 } |
|
263 |
|
264 /* the usual suspects */ |
|
265 JLI_Snprintf(clientPattern, PATH_MAX, "lib/%s/client", GetArchPath(wanted)); |
|
266 JLI_Snprintf(serverPattern, PATH_MAX, "lib/%s/server", GetArchPath(wanted)); |
|
267 |
|
268 /* to optimize for time, test if any of our usual suspects are present. */ |
|
269 clientPatternFound = JLI_StrStr(env, clientPattern) != NULL; |
|
270 serverPatternFound = JLI_StrStr(env, serverPattern) != NULL; |
|
271 if (clientPatternFound == JNI_FALSE && serverPatternFound == JNI_FALSE) { |
|
272 return JNI_FALSE; |
|
273 } |
|
274 |
|
275 /* |
|
276 * we have a suspicious path component, check if it contains a libjvm.so |
|
277 */ |
|
278 envpath = JLI_StringDup(env); |
|
279 for (path = JLI_StrTok(envpath, ":"); path != NULL; path = JLI_StrTok(NULL, ":")) { |
|
280 if (clientPatternFound && JLI_StrStr(path, clientPattern) != NULL) { |
|
281 if (JvmExists(path)) { |
|
282 JLI_MemFree(envpath); |
|
283 return JNI_TRUE; |
|
284 } |
|
285 } |
|
286 if (serverPatternFound && JLI_StrStr(path, serverPattern) != NULL) { |
|
287 if (JvmExists(path)) { |
|
288 JLI_MemFree(envpath); |
|
289 return JNI_TRUE; |
|
290 } |
|
291 } |
|
292 } |
|
293 JLI_MemFree(envpath); |
|
294 return JNI_FALSE; |
|
295 } |
|
296 |
|
297 /* |
|
298 * Test whether the environment variable needs to be set, see flowchart. |
|
299 */ |
|
300 static jboolean |
|
301 RequiresSetenv(int wanted, const char *jvmpath) { |
|
302 char jpath[PATH_MAX + 1]; |
|
303 char *llp; |
|
304 char *dmllp = NULL; |
|
305 char *p; /* a utility pointer */ |
|
306 |
|
307 llp = getenv("LD_LIBRARY_PATH"); |
|
308 #ifdef __solaris__ |
|
309 dmllp = (CURRENT_DATA_MODEL == 32) |
|
310 ? getenv("LD_LIBRARY_PATH_32") |
|
311 : getenv("LD_LIBRARY_PATH_64"); |
|
312 #endif /* __solaris__ */ |
|
313 /* no environment variable is a good environment variable */ |
|
314 if (llp == NULL && dmllp == NULL) { |
|
315 return JNI_FALSE; |
|
316 } |
|
317 #ifdef __linux |
|
318 /* |
|
319 * On linux, if a binary is running as sgid or suid, glibc sets |
|
320 * LD_LIBRARY_PATH to the empty string for security purposes. (In contrast, |
|
321 * on Solaris the LD_LIBRARY_PATH variable for a privileged binary does not |
|
322 * lose its settings; but the dynamic linker does apply more scrutiny to the |
|
323 * path.) The launcher uses the value of LD_LIBRARY_PATH to prevent an exec |
|
324 * loop, here and further downstream. Therefore, if we are running sgid or |
|
325 * suid, this function's setting of LD_LIBRARY_PATH will be ineffective and |
|
326 * we should case a return from the calling function. Getting the right |
|
327 * libraries will be handled by the RPATH. In reality, this check is |
|
328 * redundant, as the previous check for a non-null LD_LIBRARY_PATH will |
|
329 * return back to the calling function forthwith, it is left here to safe |
|
330 * guard against any changes, in the glibc's existing security policy. |
|
331 */ |
|
332 if ((getgid() != getegid()) || (getuid() != geteuid())) { |
|
333 return JNI_FALSE; |
|
334 } |
|
335 #endif /* __linux */ |
|
336 |
|
337 /* |
|
338 * Prevent recursions. Since LD_LIBRARY_PATH is the one which will be set by |
|
339 * previous versions of the JRE, thus it is the only path that matters here. |
|
340 * So we check to see if the desired JRE is set. |
|
341 */ |
|
342 JLI_StrNCpy(jpath, jvmpath, PATH_MAX); |
|
343 p = JLI_StrRChr(jpath, '/'); |
|
344 *p = '\0'; |
|
345 if (llp != NULL && JLI_StrNCmp(llp, jpath, JLI_StrLen(jpath)) == 0) { |
|
346 return JNI_FALSE; |
|
347 } |
|
348 |
|
349 /* scrutinize all the paths further */ |
|
350 if (llp != NULL && ContainsLibJVM(wanted, llp)) { |
|
351 return JNI_TRUE; |
|
352 } |
|
353 if (dmllp != NULL && ContainsLibJVM(wanted, dmllp)) { |
|
354 return JNI_TRUE; |
|
355 } |
|
356 return JNI_FALSE; |
|
357 } |
|
358 #endif /* SETENV_REQUIRED */ |
|
359 |
|
360 void |
|
361 CreateExecutionEnvironment(int *pargc, char ***pargv, |
|
362 char jrepath[], jint so_jrepath, |
|
363 char jvmpath[], jint so_jvmpath) { |
|
364 /* |
|
365 * First, determine if we are running the desired data model. If we |
|
366 * are running the desired data model, all the error messages |
|
367 * associated with calling GetJREPath, ReadKnownVMs, etc. should be |
|
368 * output. However, if we are not running the desired data model, |
|
369 * some of the errors should be suppressed since it is more |
|
370 * informative to issue an error message based on whether or not the |
|
371 * os/processor combination has dual mode capabilities. |
|
372 */ |
|
373 jboolean jvmpathExists; |
|
374 |
|
375 /* Compute/set the name of the executable */ |
|
376 SetExecname(*pargv); |
|
377 |
|
378 /* Check data model flags, and exec process, if needed */ |
|
379 { |
|
380 char *arch = (char *)GetArch(); /* like sparc or sparcv9 */ |
|
381 char * jvmtype = NULL; |
|
382 int argc = *pargc; |
|
383 char **argv = *pargv; |
|
384 int running = CURRENT_DATA_MODEL; |
|
385 |
|
386 int wanted = running; /* What data mode is being |
|
387 asked for? Current model is |
|
388 fine unless another model |
|
389 is asked for */ |
|
390 #ifdef SETENV_REQUIRED |
|
391 jboolean mustsetenv = JNI_FALSE; |
|
392 char *runpath = NULL; /* existing effective LD_LIBRARY_PATH setting */ |
|
393 char* new_runpath = NULL; /* desired new LD_LIBRARY_PATH string */ |
|
394 char* newpath = NULL; /* path on new LD_LIBRARY_PATH */ |
|
395 char* lastslash = NULL; |
|
396 char** newenvp = NULL; /* current environment */ |
|
397 #ifdef __solaris__ |
|
398 char* dmpath = NULL; /* data model specific LD_LIBRARY_PATH, |
|
399 Solaris only */ |
|
400 #endif /* __solaris__ */ |
|
401 #endif /* SETENV_REQUIRED */ |
|
402 |
|
403 char** newargv = NULL; |
|
404 int newargc = 0; |
|
405 |
|
406 /* |
|
407 * Starting in 1.5, all unix platforms accept the -d32 and -d64 |
|
408 * options. On platforms where only one data-model is supported |
|
409 * (e.g. ia-64 Linux), using the flag for the other data model is |
|
410 * an error and will terminate the program. |
|
411 */ |
|
412 |
|
413 { /* open new scope to declare local variables */ |
|
414 int i; |
|
415 |
|
416 newargv = (char **)JLI_MemAlloc((argc+1) * sizeof(char*)); |
|
417 newargv[newargc++] = argv[0]; |
|
418 |
|
419 /* scan for data model arguments and remove from argument list; |
|
420 last occurrence determines desired data model */ |
|
421 for (i=1; i < argc; i++) { |
|
422 |
|
423 if (JLI_StrCmp(argv[i], "-J-d64") == 0 || JLI_StrCmp(argv[i], "-d64") == 0) { |
|
424 wanted = 64; |
|
425 continue; |
|
426 } |
|
427 if (JLI_StrCmp(argv[i], "-J-d32") == 0 || JLI_StrCmp(argv[i], "-d32") == 0) { |
|
428 wanted = 32; |
|
429 continue; |
|
430 } |
|
431 newargv[newargc++] = argv[i]; |
|
432 |
|
433 if (IsJavaArgs()) { |
|
434 if (argv[i][0] != '-') continue; |
|
435 } else { |
|
436 if (JLI_StrCmp(argv[i], "-classpath") == 0 || JLI_StrCmp(argv[i], "-cp") == 0) { |
|
437 i++; |
|
438 if (i >= argc) break; |
|
439 newargv[newargc++] = argv[i]; |
|
440 continue; |
|
441 } |
|
442 if (argv[i][0] != '-') { i++; break; } |
|
443 } |
|
444 } |
|
445 |
|
446 /* copy rest of args [i .. argc) */ |
|
447 while (i < argc) { |
|
448 newargv[newargc++] = argv[i++]; |
|
449 } |
|
450 newargv[newargc] = NULL; |
|
451 |
|
452 /* |
|
453 * newargv has all proper arguments here |
|
454 */ |
|
455 |
|
456 argc = newargc; |
|
457 argv = newargv; |
|
458 } |
|
459 |
|
460 /* If the data model is not changing, it is an error if the |
|
461 jvmpath does not exist */ |
|
462 if (wanted == running) { |
|
463 /* Find out where the JRE is that we will be using. */ |
|
464 if (!GetJREPath(jrepath, so_jrepath, arch, JNI_FALSE) ) { |
|
465 JLI_ReportErrorMessage(JRE_ERROR1); |
|
466 exit(2); |
|
467 } |
|
468 |
|
469 /* Find the specified JVM type */ |
|
470 if (ReadKnownVMs(jrepath, arch, JNI_FALSE) < 1) { |
|
471 JLI_ReportErrorMessage(CFG_ERROR7); |
|
472 exit(1); |
|
473 } |
|
474 |
|
475 jvmpath[0] = '\0'; |
|
476 jvmtype = CheckJvmType(pargc, pargv, JNI_FALSE); |
|
477 if (JLI_StrCmp(jvmtype, "ERROR") == 0) { |
|
478 JLI_ReportErrorMessage(CFG_ERROR9); |
|
479 exit(4); |
|
480 } |
|
481 |
|
482 if (!GetJVMPath(jrepath, jvmtype, jvmpath, so_jvmpath, arch )) { |
|
483 JLI_ReportErrorMessage(CFG_ERROR8, jvmtype, jvmpath); |
|
484 exit(4); |
|
485 } |
|
486 /* |
|
487 * we seem to have everything we need, so without further ado |
|
488 * we return back, otherwise proceed to set the environment. |
|
489 */ |
|
490 #ifdef SETENV_REQUIRED |
|
491 mustsetenv = RequiresSetenv(wanted, jvmpath); |
|
492 JLI_TraceLauncher("mustsetenv: %s\n", mustsetenv ? "TRUE" : "FALSE"); |
|
493 |
|
494 if (mustsetenv == JNI_FALSE) { |
|
495 return; |
|
496 } |
|
497 #else |
|
498 return; |
|
499 #endif /* SETENV_REQUIRED */ |
|
500 } else { /* do the same speculatively or exit */ |
|
501 #ifdef DUAL_MODE |
|
502 if (running != wanted) { |
|
503 /* Find out where the JRE is that we will be using. */ |
|
504 if (!GetJREPath(jrepath, so_jrepath, GetArchPath(wanted), JNI_TRUE)) { |
|
505 /* give up and let other code report error message */ |
|
506 JLI_ReportErrorMessage(JRE_ERROR2, wanted); |
|
507 exit(1); |
|
508 } |
|
509 |
|
510 /* |
|
511 * Read in jvm.cfg for target data model and process vm |
|
512 * selection options. |
|
513 */ |
|
514 if (ReadKnownVMs(jrepath, GetArchPath(wanted), JNI_TRUE) < 1) { |
|
515 /* give up and let other code report error message */ |
|
516 JLI_ReportErrorMessage(JRE_ERROR2, wanted); |
|
517 exit(1); |
|
518 } |
|
519 jvmpath[0] = '\0'; |
|
520 jvmtype = CheckJvmType(pargc, pargv, JNI_TRUE); |
|
521 if (JLI_StrCmp(jvmtype, "ERROR") == 0) { |
|
522 JLI_ReportErrorMessage(CFG_ERROR9); |
|
523 exit(4); |
|
524 } |
|
525 |
|
526 /* exec child can do error checking on the existence of the path */ |
|
527 jvmpathExists = GetJVMPath(jrepath, jvmtype, jvmpath, so_jvmpath, GetArchPath(wanted)); |
|
528 #ifdef SETENV_REQUIRED |
|
529 mustsetenv = RequiresSetenv(wanted, jvmpath); |
|
530 #endif /* SETENV_REQUIRED */ |
|
531 } |
|
532 #else |
|
533 JLI_ReportErrorMessage(JRE_ERROR2, wanted); |
|
534 exit(1); |
|
535 #endif |
|
536 } |
|
537 #ifdef SETENV_REQUIRED |
|
538 if (mustsetenv) { |
|
539 /* |
|
540 * We will set the LD_LIBRARY_PATH as follows: |
|
541 * |
|
542 * o $JVMPATH (directory portion only) |
|
543 * o $JRE/lib/$LIBARCHNAME |
|
544 * o $JRE/../lib/$LIBARCHNAME |
|
545 * |
|
546 * followed by the user's previous effective LD_LIBRARY_PATH, if |
|
547 * any. |
|
548 */ |
|
549 |
|
550 #ifdef __solaris__ |
|
551 /* |
|
552 * Starting in Solaris 7, ld.so.1 supports three LD_LIBRARY_PATH |
|
553 * variables: |
|
554 * |
|
555 * 1. LD_LIBRARY_PATH -- used for 32 and 64 bit searches if |
|
556 * data-model specific variables are not set. |
|
557 * |
|
558 * 2. LD_LIBRARY_PATH_64 -- overrides and replaces LD_LIBRARY_PATH |
|
559 * for 64-bit binaries. |
|
560 * |
|
561 * 3. LD_LIBRARY_PATH_32 -- overrides and replaces LD_LIBRARY_PATH |
|
562 * for 32-bit binaries. |
|
563 * |
|
564 * The vm uses LD_LIBRARY_PATH to set the java.library.path system |
|
565 * property. To shield the vm from the complication of multiple |
|
566 * LD_LIBRARY_PATH variables, if the appropriate data model |
|
567 * specific variable is set, we will act as if LD_LIBRARY_PATH had |
|
568 * the value of the data model specific variant and the data model |
|
569 * specific variant will be unset. Note that the variable for the |
|
570 * *wanted* data model must be used (if it is set), not simply the |
|
571 * current running data model. |
|
572 */ |
|
573 |
|
574 switch (wanted) { |
|
575 case 0: |
|
576 if (running == 32) { |
|
577 dmpath = getenv("LD_LIBRARY_PATH_32"); |
|
578 wanted = 32; |
|
579 } else { |
|
580 dmpath = getenv("LD_LIBRARY_PATH_64"); |
|
581 wanted = 64; |
|
582 } |
|
583 break; |
|
584 |
|
585 case 32: |
|
586 dmpath = getenv("LD_LIBRARY_PATH_32"); |
|
587 break; |
|
588 |
|
589 case 64: |
|
590 dmpath = getenv("LD_LIBRARY_PATH_64"); |
|
591 break; |
|
592 |
|
593 default: |
|
594 JLI_ReportErrorMessage(JRE_ERROR3, __LINE__); |
|
595 exit(1); /* unknown value in wanted */ |
|
596 break; |
|
597 } |
|
598 |
|
599 /* |
|
600 * If dmpath is NULL, the relevant data model specific variable is |
|
601 * not set and normal LD_LIBRARY_PATH should be used. |
|
602 */ |
|
603 if (dmpath == NULL) { |
|
604 runpath = getenv("LD_LIBRARY_PATH"); |
|
605 } else { |
|
606 runpath = dmpath; |
|
607 } |
|
608 #else |
|
609 /* |
|
610 * If not on Solaris, assume only a single LD_LIBRARY_PATH |
|
611 * variable. |
|
612 */ |
|
613 runpath = getenv("LD_LIBRARY_PATH"); |
|
614 #endif /* __solaris__ */ |
|
615 |
|
616 /* runpath contains current effective LD_LIBRARY_PATH setting */ |
|
617 |
|
618 jvmpath = JLI_StringDup(jvmpath); |
|
619 new_runpath = JLI_MemAlloc(((runpath != NULL) ? JLI_StrLen(runpath) : 0) + |
|
620 2 * JLI_StrLen(jrepath) + 2 * JLI_StrLen(arch) + |
|
621 JLI_StrLen(jvmpath) + 52); |
|
622 newpath = new_runpath + JLI_StrLen("LD_LIBRARY_PATH="); |
|
623 |
|
624 |
|
625 /* |
|
626 * Create desired LD_LIBRARY_PATH value for target data model. |
|
627 */ |
|
628 { |
|
629 /* remove the name of the .so from the JVM path */ |
|
630 lastslash = JLI_StrRChr(jvmpath, '/'); |
|
631 if (lastslash) |
|
632 *lastslash = '\0'; |
|
633 |
|
634 sprintf(new_runpath, "LD_LIBRARY_PATH=" |
|
635 "%s:" |
|
636 "%s/lib/%s:" |
|
637 "%s/../lib/%s", |
|
638 jvmpath, |
|
639 #ifdef DUAL_MODE |
|
640 jrepath, GetArchPath(wanted), |
|
641 jrepath, GetArchPath(wanted) |
|
642 #else |
|
643 jrepath, arch, |
|
644 jrepath, arch |
|
645 #endif |
|
646 ); |
|
647 |
|
648 |
|
649 /* |
|
650 * Check to make sure that the prefix of the current path is the |
|
651 * desired environment variable setting, though the RequiresSetenv |
|
652 * checks if the desired runpath exists, this logic does a more |
|
653 * comprehensive check. |
|
654 */ |
|
655 if (runpath != NULL && |
|
656 JLI_StrNCmp(newpath, runpath, JLI_StrLen(newpath)) == 0 && |
|
657 (runpath[JLI_StrLen(newpath)] == 0 || runpath[JLI_StrLen(newpath)] == ':') && |
|
658 (running == wanted) /* data model does not have to be changed */ |
|
659 #ifdef __solaris__ |
|
660 && (dmpath == NULL) /* data model specific variables not set */ |
|
661 #endif |
|
662 ) { |
|
663 |
|
664 return; |
|
665 |
|
666 } |
|
667 } |
|
668 |
|
669 /* |
|
670 * Place the desired environment setting onto the prefix of |
|
671 * LD_LIBRARY_PATH. Note that this prevents any possible infinite |
|
672 * loop of execv() because we test for the prefix, above. |
|
673 */ |
|
674 if (runpath != 0) { |
|
675 JLI_StrCat(new_runpath, ":"); |
|
676 JLI_StrCat(new_runpath, runpath); |
|
677 } |
|
678 |
|
679 if (putenv(new_runpath) != 0) { |
|
680 exit(1); /* problem allocating memory; LD_LIBRARY_PATH not set |
|
681 properly */ |
|
682 } |
|
683 |
|
684 /* |
|
685 * Unix systems document that they look at LD_LIBRARY_PATH only |
|
686 * once at startup, so we have to re-exec the current executable |
|
687 * to get the changed environment variable to have an effect. |
|
688 */ |
|
689 |
|
690 #ifdef __solaris__ |
|
691 /* |
|
692 * If dmpath is not NULL, remove the data model specific string |
|
693 * in the environment for the exec'ed child. |
|
694 */ |
|
695 if (dmpath != NULL) |
|
696 (void)UnsetEnv((wanted == 32) ? "LD_LIBRARY_PATH_32" : "LD_LIBRARY_PATH_64"); |
|
697 #endif |
|
698 |
|
699 newenvp = environ; |
|
700 } |
|
701 #endif /* SETENV_REQUIRED */ |
|
702 { |
|
703 char *newexec = execname; |
|
704 #ifdef DUAL_MODE |
|
705 /* |
|
706 * If the data model is being changed, the path to the |
|
707 * executable must be updated accordingly; the executable name |
|
708 * and directory the executable resides in are separate. In the |
|
709 * case of 32 => 64, the new bits are assumed to reside in, e.g. |
|
710 * "olddir/LIBARCH64NAME/execname"; in the case of 64 => 32, |
|
711 * the bits are assumed to be in "olddir/../execname". For example, |
|
712 * |
|
713 * olddir/sparcv9/execname |
|
714 * olddir/amd64/execname |
|
715 * |
|
716 * for Solaris SPARC and Linux amd64, respectively. |
|
717 */ |
|
718 |
|
719 if (running != wanted) { |
|
720 char *oldexec = JLI_StrCpy(JLI_MemAlloc(JLI_StrLen(execname) + 1), execname); |
|
721 char *olddir = oldexec; |
|
722 char *oldbase = JLI_StrRChr(oldexec, '/'); |
|
723 |
|
724 |
|
725 newexec = JLI_MemAlloc(JLI_StrLen(execname) + 20); |
|
726 *oldbase++ = 0; |
|
727 sprintf(newexec, "%s/%s/%s", olddir, |
|
728 ((wanted == 64) ? LIBARCH64NAME : ".."), oldbase); |
|
729 argv[0] = newexec; |
|
730 } |
|
731 #endif /* DUAL_MODE */ |
|
732 JLI_TraceLauncher("TRACER_MARKER:About to EXEC\n"); |
|
733 (void) fflush(stdout); |
|
734 (void) fflush(stderr); |
|
735 #ifdef SETENV_REQUIRED |
|
736 if (mustsetenv) { |
|
737 execve(newexec, argv, newenvp); |
|
738 } else { |
|
739 execv(newexec, argv); |
|
740 } |
|
741 #else |
|
742 execv(newexec, argv); |
|
743 #endif /* SETENV_REQUIRED */ |
|
744 JLI_ReportErrorMessageSys(JRE_ERROR4, newexec); |
|
745 |
|
746 #ifdef DUAL_MODE |
|
747 if (running != wanted) { |
|
748 JLI_ReportErrorMessage(JRE_ERROR5, wanted, running); |
|
749 #ifdef __solaris__ |
|
750 #ifdef __sparc |
|
751 JLI_ReportErrorMessage(JRE_ERROR6); |
|
752 #else |
|
753 JLI_ReportErrorMessage(JRE_ERROR7); |
|
754 #endif /* __sparc */ |
|
755 } |
|
756 #endif /* __solaris__ */ |
|
757 #endif /* DUAL_MODE */ |
|
758 |
|
759 } |
|
760 exit(1); |
|
761 } |
|
762 } |
|
763 |
|
764 /* |
|
765 * On Solaris VM choosing is done by the launcher (java.c). |
|
766 */ |
|
767 static jboolean |
|
768 GetJVMPath(const char *jrepath, const char *jvmtype, |
|
769 char *jvmpath, jint jvmpathsize, const char * arch) |
|
770 { |
|
771 struct stat s; |
|
772 |
|
773 if (JLI_StrChr(jvmtype, '/')) { |
|
774 JLI_Snprintf(jvmpath, jvmpathsize, "%s/" JVM_DLL, jvmtype); |
|
775 } else { |
|
776 JLI_Snprintf(jvmpath, jvmpathsize, "%s/lib/%s/%s/" JVM_DLL, jrepath, arch, jvmtype); |
|
777 } |
|
778 |
|
779 JLI_TraceLauncher("Does `%s' exist ... ", jvmpath); |
|
780 |
|
781 if (stat(jvmpath, &s) == 0) { |
|
782 JLI_TraceLauncher("yes.\n"); |
|
783 return JNI_TRUE; |
|
784 } else { |
|
785 JLI_TraceLauncher("no.\n"); |
|
786 return JNI_FALSE; |
|
787 } |
|
788 } |
|
789 |
|
790 /* |
|
791 * Find path to JRE based on .exe's location or registry settings. |
|
792 */ |
|
793 static jboolean |
|
794 GetJREPath(char *path, jint pathsize, const char * arch, jboolean speculative) |
|
795 { |
|
796 char libjava[MAXPATHLEN]; |
|
797 |
|
798 if (GetApplicationHome(path, pathsize)) { |
|
799 /* Is JRE co-located with the application? */ |
|
800 JLI_Snprintf(libjava, sizeof(libjava), "%s/lib/%s/" JAVA_DLL, path, arch); |
|
801 if (access(libjava, F_OK) == 0) { |
|
802 JLI_TraceLauncher("JRE path is %s\n", path); |
|
803 return JNI_TRUE; |
|
804 } |
|
805 |
|
806 /* Does the app ship a private JRE in <apphome>/jre directory? */ |
|
807 JLI_Snprintf(libjava, sizeof(libjava), "%s/jre/lib/%s/" JAVA_DLL, path, arch); |
|
808 if (access(libjava, F_OK) == 0) { |
|
809 JLI_StrCat(path, "/jre"); |
|
810 JLI_TraceLauncher("JRE path is %s\n", path); |
|
811 return JNI_TRUE; |
|
812 } |
|
813 } |
|
814 |
|
815 if (!speculative) |
|
816 JLI_ReportErrorMessage(JRE_ERROR8 JAVA_DLL); |
|
817 return JNI_FALSE; |
|
818 } |
|
819 |
|
820 jboolean |
|
821 LoadJavaVM(const char *jvmpath, InvocationFunctions *ifn) |
|
822 { |
|
823 void *libjvm; |
|
824 |
|
825 JLI_TraceLauncher("JVM path is %s\n", jvmpath); |
|
826 |
|
827 libjvm = dlopen(jvmpath, RTLD_NOW + RTLD_GLOBAL); |
|
828 if (libjvm == NULL) { |
|
829 #if defined(__solaris__) && defined(__sparc) && !defined(_LP64) /* i.e. 32-bit sparc */ |
|
830 FILE * fp; |
|
831 Elf32_Ehdr elf_head; |
|
832 int count; |
|
833 int location; |
|
834 |
|
835 fp = fopen(jvmpath, "r"); |
|
836 if (fp == NULL) { |
|
837 JLI_ReportErrorMessage(DLL_ERROR2, jvmpath, dlerror()); |
|
838 return JNI_FALSE; |
|
839 } |
|
840 |
|
841 /* read in elf header */ |
|
842 count = fread((void*)(&elf_head), sizeof(Elf32_Ehdr), 1, fp); |
|
843 fclose(fp); |
|
844 if (count < 1) { |
|
845 JLI_ReportErrorMessage(DLL_ERROR2, jvmpath, dlerror()); |
|
846 return JNI_FALSE; |
|
847 } |
|
848 |
|
849 /* |
|
850 * Check for running a server vm (compiled with -xarch=v8plus) |
|
851 * on a stock v8 processor. In this case, the machine type in |
|
852 * the elf header would not be included the architecture list |
|
853 * provided by the isalist command, which is turn is gotten from |
|
854 * sysinfo. This case cannot occur on 64-bit hardware and thus |
|
855 * does not have to be checked for in binaries with an LP64 data |
|
856 * model. |
|
857 */ |
|
858 if (elf_head.e_machine == EM_SPARC32PLUS) { |
|
859 char buf[257]; /* recommended buffer size from sysinfo man |
|
860 page */ |
|
861 long length; |
|
862 char* location; |
|
863 |
|
864 length = sysinfo(SI_ISALIST, buf, 257); |
|
865 if (length > 0) { |
|
866 location = JLI_StrStr(buf, "sparcv8plus "); |
|
867 if (location == NULL) { |
|
868 JLI_ReportErrorMessage(JVM_ERROR3); |
|
869 return JNI_FALSE; |
|
870 } |
|
871 } |
|
872 } |
|
873 #endif |
|
874 JLI_ReportErrorMessage(DLL_ERROR1, __LINE__); |
|
875 JLI_ReportErrorMessage(DLL_ERROR2, jvmpath, dlerror()); |
|
876 return JNI_FALSE; |
|
877 } |
|
878 |
|
879 ifn->CreateJavaVM = (CreateJavaVM_t) |
|
880 dlsym(libjvm, "JNI_CreateJavaVM"); |
|
881 if (ifn->CreateJavaVM == NULL) { |
|
882 JLI_ReportErrorMessage(DLL_ERROR2, jvmpath, dlerror()); |
|
883 return JNI_FALSE; |
|
884 } |
|
885 |
|
886 ifn->GetDefaultJavaVMInitArgs = (GetDefaultJavaVMInitArgs_t) |
|
887 dlsym(libjvm, "JNI_GetDefaultJavaVMInitArgs"); |
|
888 if (ifn->GetDefaultJavaVMInitArgs == NULL) { |
|
889 JLI_ReportErrorMessage(DLL_ERROR2, jvmpath, dlerror()); |
|
890 return JNI_FALSE; |
|
891 } |
|
892 |
|
893 return JNI_TRUE; |
|
894 } |
|
895 |
|
896 /* |
|
897 * If app is "/foo/bin/javac", or "/foo/bin/sparcv9/javac" then put |
|
898 * "/foo" into buf. |
|
899 */ |
|
900 jboolean |
|
901 GetApplicationHome(char *buf, jint bufsize) |
|
902 { |
|
903 if (execname != NULL) { |
|
904 JLI_Snprintf(buf, bufsize, "%s", execname); |
|
905 buf[bufsize-1] = '\0'; |
|
906 } else { |
|
907 return JNI_FALSE; |
|
908 } |
|
909 |
|
910 if (JLI_StrRChr(buf, '/') == 0) { |
|
911 buf[0] = '\0'; |
|
912 return JNI_FALSE; |
|
913 } |
|
914 *(JLI_StrRChr(buf, '/')) = '\0'; /* executable file */ |
|
915 if (JLI_StrLen(buf) < 4 || JLI_StrRChr(buf, '/') == 0) { |
|
916 buf[0] = '\0'; |
|
917 return JNI_FALSE; |
|
918 } |
|
919 if (JLI_StrCmp("/bin", buf + JLI_StrLen(buf) - 4) != 0) |
|
920 *(JLI_StrRChr(buf, '/')) = '\0'; /* sparcv9 or amd64 */ |
|
921 if (JLI_StrLen(buf) < 4 || JLI_StrCmp("/bin", buf + JLI_StrLen(buf) - 4) != 0) { |
|
922 buf[0] = '\0'; |
|
923 return JNI_FALSE; |
|
924 } |
|
925 *(JLI_StrRChr(buf, '/')) = '\0'; /* bin */ |
|
926 |
|
927 return JNI_TRUE; |
|
928 } |
|
929 |
|
930 |
|
931 /* |
|
932 * Return true if the named program exists |
|
933 */ |
|
934 static int |
|
935 ProgramExists(char *name) |
|
936 { |
|
937 struct stat sb; |
|
938 if (stat(name, &sb) != 0) return 0; |
|
939 if (S_ISDIR(sb.st_mode)) return 0; |
|
940 return (sb.st_mode & S_IEXEC) != 0; |
|
941 } |
|
942 |
|
943 |
|
944 /* |
|
945 * Find a command in a directory, returning the path. |
|
946 */ |
|
947 static char * |
|
948 Resolve(char *indir, char *cmd) |
|
949 { |
|
950 char name[PATH_MAX + 2], *real; |
|
951 |
|
952 if ((JLI_StrLen(indir) + JLI_StrLen(cmd) + 1) > PATH_MAX) return 0; |
|
953 JLI_Snprintf(name, sizeof(name), "%s%c%s", indir, FILE_SEPARATOR, cmd); |
|
954 if (!ProgramExists(name)) return 0; |
|
955 real = JLI_MemAlloc(PATH_MAX + 2); |
|
956 if (!realpath(name, real)) |
|
957 JLI_StrCpy(real, name); |
|
958 return real; |
|
959 } |
|
960 |
|
961 |
|
962 /* |
|
963 * Find a path for the executable |
|
964 */ |
|
965 static char * |
|
966 FindExecName(char *program) |
|
967 { |
|
968 char cwdbuf[PATH_MAX+2]; |
|
969 char *path; |
|
970 char *tmp_path; |
|
971 char *f; |
|
972 char *result = NULL; |
|
973 |
|
974 /* absolute path? */ |
|
975 if (*program == FILE_SEPARATOR || |
|
976 (FILE_SEPARATOR=='\\' && JLI_StrRChr(program, ':'))) |
|
977 return Resolve("", program+1); |
|
978 |
|
979 /* relative path? */ |
|
980 if (JLI_StrRChr(program, FILE_SEPARATOR) != 0) { |
|
981 char buf[PATH_MAX+2]; |
|
982 return Resolve(getcwd(cwdbuf, sizeof(cwdbuf)), program); |
|
983 } |
|
984 |
|
985 /* from search path? */ |
|
986 path = getenv("PATH"); |
|
987 if (!path || !*path) path = "."; |
|
988 tmp_path = JLI_MemAlloc(JLI_StrLen(path) + 2); |
|
989 JLI_StrCpy(tmp_path, path); |
|
990 |
|
991 for (f=tmp_path; *f && result==0; ) { |
|
992 char *s = f; |
|
993 while (*f && (*f != PATH_SEPARATOR)) ++f; |
|
994 if (*f) *f++ = 0; |
|
995 if (*s == FILE_SEPARATOR) |
|
996 result = Resolve(s, program); |
|
997 else { |
|
998 /* relative path element */ |
|
999 char dir[2*PATH_MAX]; |
|
1000 JLI_Snprintf(dir, sizeof(dir), "%s%c%s", getcwd(cwdbuf, sizeof(cwdbuf)), |
|
1001 FILE_SEPARATOR, s); |
|
1002 result = Resolve(dir, program); |
|
1003 } |
|
1004 if (result != 0) break; |
|
1005 } |
|
1006 |
|
1007 JLI_MemFree(tmp_path); |
|
1008 return result; |
|
1009 } |
|
1010 |
|
1011 |
|
1012 |
|
1013 /* |
|
1014 * Compute the name of the executable |
|
1015 * |
|
1016 * In order to re-exec securely we need the absolute path of the |
|
1017 * executable. On Solaris getexecname(3c) may not return an absolute |
|
1018 * path so we use dladdr to get the filename of the executable and |
|
1019 * then use realpath to derive an absolute path. From Solaris 9 |
|
1020 * onwards the filename returned in DL_info structure from dladdr is |
|
1021 * an absolute pathname so technically realpath isn't required. |
|
1022 * On Linux we read the executable name from /proc/self/exe. |
|
1023 * As a fallback, and for platforms other than Solaris and Linux, |
|
1024 * we use FindExecName to compute the executable name. |
|
1025 */ |
|
1026 static const char* |
|
1027 SetExecname(char **argv) |
|
1028 { |
|
1029 char* exec_path = NULL; |
|
1030 #if defined(__solaris__) |
|
1031 { |
|
1032 Dl_info dlinfo; |
|
1033 int (*fptr)(); |
|
1034 |
|
1035 fptr = (int (*)())dlsym(RTLD_DEFAULT, "main"); |
|
1036 if (fptr == NULL) { |
|
1037 JLI_ReportErrorMessage(DLL_ERROR3, dlerror()); |
|
1038 return JNI_FALSE; |
|
1039 } |
|
1040 |
|
1041 if (dladdr((void*)fptr, &dlinfo)) { |
|
1042 char *resolved = (char*)JLI_MemAlloc(PATH_MAX+1); |
|
1043 if (resolved != NULL) { |
|
1044 exec_path = realpath(dlinfo.dli_fname, resolved); |
|
1045 if (exec_path == NULL) { |
|
1046 JLI_MemFree(resolved); |
|
1047 } |
|
1048 } |
|
1049 } |
|
1050 } |
|
1051 #elif defined(__linux__) |
|
1052 { |
|
1053 const char* self = "/proc/self/exe"; |
|
1054 char buf[PATH_MAX+1]; |
|
1055 int len = readlink(self, buf, PATH_MAX); |
|
1056 if (len >= 0) { |
|
1057 buf[len] = '\0'; /* readlink doesn't nul terminate */ |
|
1058 exec_path = JLI_StringDup(buf); |
|
1059 } |
|
1060 } |
|
1061 #else /* !__solaris__ && !__linux */ |
|
1062 { |
|
1063 /* Not implemented */ |
|
1064 } |
|
1065 #endif |
|
1066 |
|
1067 if (exec_path == NULL) { |
|
1068 exec_path = FindExecName(argv[0]); |
|
1069 } |
|
1070 execname = exec_path; |
|
1071 return exec_path; |
|
1072 } |
|
1073 |
|
1074 void JLI_ReportErrorMessage(const char* fmt, ...) { |
|
1075 va_list vl; |
|
1076 va_start(vl, fmt); |
|
1077 vfprintf(stderr, fmt, vl); |
|
1078 fprintf(stderr, "\n"); |
|
1079 va_end(vl); |
|
1080 } |
|
1081 |
|
1082 void JLI_ReportErrorMessageSys(const char* fmt, ...) { |
|
1083 va_list vl; |
|
1084 char *emsg; |
|
1085 |
|
1086 /* |
|
1087 * TODO: its safer to use strerror_r but is not available on |
|
1088 * Solaris 8. Until then.... |
|
1089 */ |
|
1090 emsg = strerror(errno); |
|
1091 if (emsg != NULL) { |
|
1092 fprintf(stderr, "%s\n", emsg); |
|
1093 } |
|
1094 |
|
1095 va_start(vl, fmt); |
|
1096 vfprintf(stderr, fmt, vl); |
|
1097 fprintf(stderr, "\n"); |
|
1098 va_end(vl); |
|
1099 } |
|
1100 |
|
1101 void JLI_ReportExceptionDescription(JNIEnv * env) { |
|
1102 (*env)->ExceptionDescribe(env); |
|
1103 } |
|
1104 |
|
1105 /* |
|
1106 * Since using the file system as a registry is a bit risky, perform |
|
1107 * additional sanity checks on the identified directory to validate |
|
1108 * it as a valid jre/sdk. |
|
1109 * |
|
1110 * Return 0 if the tests fail; otherwise return non-zero (true). |
|
1111 * |
|
1112 * Note that checking for anything more than the existence of an |
|
1113 * executable object at bin/java relative to the path being checked |
|
1114 * will break the regression tests. |
|
1115 */ |
|
1116 static int |
|
1117 CheckSanity(char *path, char *dir) |
|
1118 { |
|
1119 char buffer[PATH_MAX]; |
|
1120 |
|
1121 if (JLI_StrLen(path) + JLI_StrLen(dir) + 11 > PATH_MAX) |
|
1122 return (0); /* Silently reject "impossibly" long paths */ |
|
1123 |
|
1124 JLI_Snprintf(buffer, sizeof(buffer), "%s/%s/bin/java", path, dir); |
|
1125 return ((access(buffer, X_OK) == 0) ? 1 : 0); |
|
1126 } |
|
1127 |
|
1128 /* |
|
1129 * Determine if there is an acceptable JRE in the directory dirname. |
|
1130 * Upon locating the "best" one, return a fully qualified path to |
|
1131 * it. "Best" is defined as the most advanced JRE meeting the |
|
1132 * constraints contained in the manifest_info. If no JRE in this |
|
1133 * directory meets the constraints, return NULL. |
|
1134 * |
|
1135 * Note that we don't check for errors in reading the directory |
|
1136 * (which would be done by checking errno). This is because it |
|
1137 * doesn't matter if we get an error reading the directory, or |
|
1138 * we just don't find anything interesting in the directory. We |
|
1139 * just return NULL in either case. |
|
1140 * |
|
1141 * The historical names of j2sdk and j2re were changed to jdk and |
|
1142 * jre respecively as part of the 1.5 rebranding effort. Since the |
|
1143 * former names are legacy on Linux, they must be recognized for |
|
1144 * all time. Fortunately, this is a minor cost. |
|
1145 */ |
|
1146 static char |
|
1147 *ProcessDir(manifest_info *info, char *dirname) |
|
1148 { |
|
1149 DIR *dirp; |
|
1150 struct dirent *dp; |
|
1151 char *best = NULL; |
|
1152 int offset; |
|
1153 int best_offset = 0; |
|
1154 char *ret_str = NULL; |
|
1155 char buffer[PATH_MAX]; |
|
1156 |
|
1157 if ((dirp = opendir(dirname)) == NULL) |
|
1158 return (NULL); |
|
1159 |
|
1160 do { |
|
1161 if ((dp = readdir(dirp)) != NULL) { |
|
1162 offset = 0; |
|
1163 if ((JLI_StrNCmp(dp->d_name, "jre", 3) == 0) || |
|
1164 (JLI_StrNCmp(dp->d_name, "jdk", 3) == 0)) |
|
1165 offset = 3; |
|
1166 else if (JLI_StrNCmp(dp->d_name, "j2re", 4) == 0) |
|
1167 offset = 4; |
|
1168 else if (JLI_StrNCmp(dp->d_name, "j2sdk", 5) == 0) |
|
1169 offset = 5; |
|
1170 if (offset > 0) { |
|
1171 if ((JLI_AcceptableRelease(dp->d_name + offset, |
|
1172 info->jre_version)) && CheckSanity(dirname, dp->d_name)) |
|
1173 if ((best == NULL) || (JLI_ExactVersionId( |
|
1174 dp->d_name + offset, best + best_offset) > 0)) { |
|
1175 if (best != NULL) |
|
1176 JLI_MemFree(best); |
|
1177 best = JLI_StringDup(dp->d_name); |
|
1178 best_offset = offset; |
|
1179 } |
|
1180 } |
|
1181 } |
|
1182 } while (dp != NULL); |
|
1183 (void) closedir(dirp); |
|
1184 if (best == NULL) |
|
1185 return (NULL); |
|
1186 else { |
|
1187 ret_str = JLI_MemAlloc(JLI_StrLen(dirname) + JLI_StrLen(best) + 2); |
|
1188 sprintf(ret_str, "%s/%s", dirname, best); |
|
1189 JLI_MemFree(best); |
|
1190 return (ret_str); |
|
1191 } |
|
1192 } |
|
1193 |
|
1194 /* |
|
1195 * This is the global entry point. It examines the host for the optimal |
|
1196 * JRE to be used by scanning a set of directories. The set of directories |
|
1197 * is platform dependent and can be overridden by the environment |
|
1198 * variable JAVA_VERSION_PATH. |
|
1199 * |
|
1200 * This routine itself simply determines the set of appropriate |
|
1201 * directories before passing control onto ProcessDir(). |
|
1202 */ |
|
1203 char* |
|
1204 LocateJRE(manifest_info* info) |
|
1205 { |
|
1206 char *path; |
|
1207 char *home; |
|
1208 char *target = NULL; |
|
1209 char *dp; |
|
1210 char *cp; |
|
1211 |
|
1212 /* |
|
1213 * Start by getting JAVA_VERSION_PATH |
|
1214 */ |
|
1215 if (info->jre_restrict_search) { |
|
1216 path = JLI_StringDup(system_dir); |
|
1217 } else if ((path = getenv("JAVA_VERSION_PATH")) != NULL) { |
|
1218 path = JLI_StringDup(path); |
|
1219 } else { |
|
1220 if ((home = getenv("HOME")) != NULL) { |
|
1221 path = (char *)JLI_MemAlloc(JLI_StrLen(home) + \ |
|
1222 JLI_StrLen(system_dir) + JLI_StrLen(user_dir) + 2); |
|
1223 sprintf(path, "%s%s:%s", home, user_dir, system_dir); |
|
1224 } else { |
|
1225 path = JLI_StringDup(system_dir); |
|
1226 } |
|
1227 } |
|
1228 |
|
1229 /* |
|
1230 * Step through each directory on the path. Terminate the scan with |
|
1231 * the first directory with an acceptable JRE. |
|
1232 */ |
|
1233 cp = dp = path; |
|
1234 while (dp != NULL) { |
|
1235 cp = JLI_StrChr(dp, (int)':'); |
|
1236 if (cp != NULL) |
|
1237 *cp = '\0'; |
|
1238 if ((target = ProcessDir(info, dp)) != NULL) |
|
1239 break; |
|
1240 dp = cp; |
|
1241 if (dp != NULL) |
|
1242 dp++; |
|
1243 } |
|
1244 JLI_MemFree(path); |
|
1245 return (target); |
|
1246 } |
|
1247 |
|
1248 /* |
|
1249 * Given a path to a jre to execute, this routine checks if this process |
|
1250 * is indeed that jre. If not, it exec's that jre. |
|
1251 * |
|
1252 * We want to actually check the paths rather than just the version string |
|
1253 * built into the executable, so that given version specification (and |
|
1254 * JAVA_VERSION_PATH) will yield the exact same Java environment, regardless |
|
1255 * of the version of the arbitrary launcher we start with. |
|
1256 */ |
|
1257 void |
|
1258 ExecJRE(char *jre, char **argv) |
|
1259 { |
|
1260 char wanted[PATH_MAX]; |
|
1261 const char* progname = GetProgramName(); |
|
1262 |
|
1263 /* |
|
1264 * Resolve the real path to the directory containing the selected JRE. |
|
1265 */ |
|
1266 if (realpath(jre, wanted) == NULL) { |
|
1267 JLI_ReportErrorMessage(JRE_ERROR9, jre); |
|
1268 exit(1); |
|
1269 } |
|
1270 |
|
1271 /* |
|
1272 * Resolve the real path to the currently running launcher. |
|
1273 */ |
|
1274 SetExecname(argv); |
|
1275 if (execname == NULL) { |
|
1276 JLI_ReportErrorMessage(JRE_ERROR10); |
|
1277 exit(1); |
|
1278 } |
|
1279 |
|
1280 /* |
|
1281 * If the path to the selected JRE directory is a match to the initial |
|
1282 * portion of the path to the currently executing JRE, we have a winner! |
|
1283 * If so, just return. |
|
1284 */ |
|
1285 if (JLI_StrNCmp(wanted, execname, JLI_StrLen(wanted)) == 0) |
|
1286 return; /* I am the droid you were looking for */ |
|
1287 |
|
1288 |
|
1289 /* |
|
1290 * This should never happen (because of the selection code in SelectJRE), |
|
1291 * but check for "impossibly" long path names just because buffer overruns |
|
1292 * can be so deadly. |
|
1293 */ |
|
1294 if (JLI_StrLen(wanted) + JLI_StrLen(progname) + 6 > PATH_MAX) { |
|
1295 JLI_ReportErrorMessage(JRE_ERROR11); |
|
1296 exit(1); |
|
1297 } |
|
1298 |
|
1299 /* |
|
1300 * Construct the path and exec it. |
|
1301 */ |
|
1302 (void)JLI_StrCat(JLI_StrCat(wanted, "/bin/"), progname); |
|
1303 argv[0] = JLI_StringDup(progname); |
|
1304 if (JLI_IsTraceLauncher()) { |
|
1305 int i; |
|
1306 printf("ReExec Command: %s (%s)\n", wanted, argv[0]); |
|
1307 printf("ReExec Args:"); |
|
1308 for (i = 1; argv[i] != NULL; i++) |
|
1309 printf(" %s", argv[i]); |
|
1310 printf("\n"); |
|
1311 } |
|
1312 JLI_TraceLauncher("TRACER_MARKER:About to EXEC\n"); |
|
1313 (void)fflush(stdout); |
|
1314 (void)fflush(stderr); |
|
1315 execv(wanted, argv); |
|
1316 JLI_ReportErrorMessageSys(JRE_ERROR12, wanted); |
|
1317 exit(1); |
|
1318 } |
|
1319 |
|
1320 /* |
|
1321 * "Borrowed" from Solaris 10 where the unsetenv() function is being added |
|
1322 * to libc thanks to SUSv3 (Standard Unix Specification, version 3). As |
|
1323 * such, in the fullness of time this will appear in libc on all relevant |
|
1324 * Solaris/Linux platforms and maybe even the Windows platform. At that |
|
1325 * time, this stub can be removed. |
|
1326 * |
|
1327 * This implementation removes the environment locking for multithreaded |
|
1328 * applications. (We don't have access to these mutexes within libc and |
|
1329 * the launcher isn't multithreaded.) Note that what remains is platform |
|
1330 * independent, because it only relies on attributes that a POSIX environment |
|
1331 * defines. |
|
1332 * |
|
1333 * Returns 0 on success, -1 on failure. |
|
1334 * |
|
1335 * Also removed was the setting of errno. The only value of errno set |
|
1336 * was EINVAL ("Invalid Argument"). |
|
1337 */ |
|
1338 |
|
1339 /* |
|
1340 * s1(environ) is name=value |
|
1341 * s2(name) is name(not the form of name=value). |
|
1342 * if names match, return value of 1, else return 0 |
|
1343 */ |
|
1344 static int |
|
1345 match_noeq(const char *s1, const char *s2) |
|
1346 { |
|
1347 while (*s1 == *s2++) { |
|
1348 if (*s1++ == '=') |
|
1349 return (1); |
|
1350 } |
|
1351 if (*s1 == '=' && s2[-1] == '\0') |
|
1352 return (1); |
|
1353 return (0); |
|
1354 } |
|
1355 |
|
1356 /* |
|
1357 * added for SUSv3 standard |
|
1358 * |
|
1359 * Delete entry from environ. |
|
1360 * Do not free() memory! Other threads may be using it. |
|
1361 * Keep it around forever. |
|
1362 */ |
|
1363 static int |
|
1364 borrowed_unsetenv(const char *name) |
|
1365 { |
|
1366 long idx; /* index into environ */ |
|
1367 |
|
1368 if (name == NULL || *name == '\0' || |
|
1369 JLI_StrChr(name, '=') != NULL) { |
|
1370 return (-1); |
|
1371 } |
|
1372 |
|
1373 for (idx = 0; environ[idx] != NULL; idx++) { |
|
1374 if (match_noeq(environ[idx], name)) |
|
1375 break; |
|
1376 } |
|
1377 if (environ[idx] == NULL) { |
|
1378 /* name not found but still a success */ |
|
1379 return (0); |
|
1380 } |
|
1381 /* squeeze up one entry */ |
|
1382 do { |
|
1383 environ[idx] = environ[idx+1]; |
|
1384 } while (environ[++idx] != NULL); |
|
1385 |
|
1386 return (0); |
|
1387 } |
|
1388 /* --- End of "borrowed" code --- */ |
|
1389 |
|
1390 /* |
|
1391 * Wrapper for unsetenv() function. |
|
1392 */ |
|
1393 int |
|
1394 UnsetEnv(char *name) |
|
1395 { |
|
1396 return(borrowed_unsetenv(name)); |
|
1397 } |
|
1398 |
|
1399 /* --- Splash Screen shared library support --- */ |
|
1400 |
|
1401 static const char* SPLASHSCREEN_SO = "libsplashscreen.so"; |
|
1402 |
|
1403 static void* hSplashLib = NULL; |
|
1404 |
|
1405 void* SplashProcAddress(const char* name) { |
|
1406 if (!hSplashLib) { |
|
1407 hSplashLib = dlopen(SPLASHSCREEN_SO, RTLD_LAZY | RTLD_GLOBAL); |
|
1408 } |
|
1409 if (hSplashLib) { |
|
1410 void* sym = dlsym(hSplashLib, name); |
|
1411 return sym; |
|
1412 } else { |
|
1413 return NULL; |
|
1414 } |
|
1415 } |
|
1416 |
|
1417 void SplashFreeLibrary() { |
|
1418 if (hSplashLib) { |
|
1419 dlclose(hSplashLib); |
|
1420 hSplashLib = NULL; |
|
1421 } |
|
1422 } |
|
1423 |
|
1424 const char * |
|
1425 jlong_format_specifier() { |
|
1426 return "%lld"; |
|
1427 } |
|
1428 |
|
1429 |
|
1430 |
|
1431 /* |
|
1432 * Block current thread and continue execution in a new thread |
|
1433 */ |
|
1434 int |
|
1435 ContinueInNewThread0(int (JNICALL *continuation)(void *), jlong stack_size, void * args) { |
|
1436 int rslt; |
|
1437 #ifdef __solaris__ |
|
1438 thread_t tid; |
|
1439 long flags = 0; |
|
1440 if (thr_create(NULL, stack_size, (void *(*)(void *))continuation, args, flags, &tid) == 0) { |
|
1441 void * tmp; |
|
1442 thr_join(tid, NULL, &tmp); |
|
1443 rslt = (int)tmp; |
|
1444 } else { |
|
1445 /* See below. Continue in current thread if thr_create() failed */ |
|
1446 rslt = continuation(args); |
|
1447 } |
|
1448 #else |
|
1449 pthread_t tid; |
|
1450 pthread_attr_t attr; |
|
1451 pthread_attr_init(&attr); |
|
1452 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); |
|
1453 |
|
1454 if (stack_size > 0) { |
|
1455 pthread_attr_setstacksize(&attr, stack_size); |
|
1456 } |
|
1457 |
|
1458 if (pthread_create(&tid, &attr, (void *(*)(void*))continuation, (void*)args) == 0) { |
|
1459 void * tmp; |
|
1460 pthread_join(tid, &tmp); |
|
1461 rslt = (int)tmp; |
|
1462 } else { |
|
1463 /* |
|
1464 * Continue execution in current thread if for some reason (e.g. out of |
|
1465 * memory/LWP) a new thread can't be created. This will likely fail |
|
1466 * later in continuation as JNI_CreateJavaVM needs to create quite a |
|
1467 * few new threads, anyway, just give it a try.. |
|
1468 */ |
|
1469 rslt = continuation(args); |
|
1470 } |
|
1471 |
|
1472 pthread_attr_destroy(&attr); |
|
1473 #endif |
|
1474 return rslt; |
|
1475 } |
|
1476 |
|
1477 /* Coarse estimation of number of digits assuming the worst case is a 64-bit pid. */ |
|
1478 #define MAX_PID_STR_SZ 20 |
|
1479 |
|
1480 void SetJavaLauncherPlatformProps() { |
|
1481 /* Linux only */ |
|
1482 #ifdef __linux__ |
|
1483 const char *substr = "-Dsun.java.launcher.pid="; |
|
1484 char *pid_prop_str = (char *)JLI_MemAlloc(JLI_StrLen(substr) + MAX_PID_STR_SZ + 1); |
|
1485 sprintf(pid_prop_str, "%s%d", substr, getpid()); |
|
1486 AddOption(pid_prop_str, NULL); |
|
1487 #endif |
|
1488 } |
|
1489 |
|
1490 jboolean |
|
1491 IsJavaw() |
|
1492 { |
|
1493 /* noop on UNIX */ |
|
1494 return JNI_FALSE; |
|
1495 } |
|
1496 |
|
1497 void |
|
1498 InitLauncher(jboolean javaw) |
|
1499 { |
|
1500 JLI_SetTraceLauncher(); |
|
1501 } |
|
1502 |
|
1503 /* |
|
1504 * The implementation for finding classes from the bootstrap |
|
1505 * class loader, refer to java.h |
|
1506 */ |
|
1507 static FindClassFromBootLoader_t *findBootClass = NULL; |
|
1508 |
|
1509 jclass |
|
1510 FindBootStrapClass(JNIEnv *env, const char* classname) |
|
1511 { |
|
1512 if (findBootClass == NULL) { |
|
1513 findBootClass = (FindClassFromBootLoader_t *)dlsym(RTLD_DEFAULT, |
|
1514 "JVM_FindClassFromBootLoader"); |
|
1515 if (findBootClass == NULL) { |
|
1516 JLI_ReportErrorMessage(DLL_ERROR4, |
|
1517 "JVM_FindClassFromBootLoader"); |
|
1518 return NULL; |
|
1519 } |
|
1520 } |
|
1521 return findBootClass(env, classname); |
|
1522 } |
|