blob: b18b92dca39f8f5e7c0a06af67f920c6aefabf7c [file] [log] [blame]
duke6e45e102007-12-01 00:00:00 +00001/*
ohair2283b9d2010-05-25 15:58:33 -07002 * Copyright (c) 1998, 2009, Oracle and/or its affiliates. All rights reserved.
duke6e45e102007-12-01 00:00:00 +00003 * 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
ohair2283b9d2010-05-25 15:58:33 -07007 * published by the Free Software Foundation. Oracle designates this
duke6e45e102007-12-01 00:00:00 +00008 * particular file as subject to the "Classpath" exception as provided
ohair2283b9d2010-05-25 15:58:33 -07009 * by Oracle in the LICENSE file that accompanied this code.
duke6e45e102007-12-01 00:00:00 +000010 *
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 *
ohair2283b9d2010-05-25 15:58:33 -070021 * 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.
duke6e45e102007-12-01 00:00:00 +000024 */
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 __linux__
41#include <pthread.h>
42#else
43#include <thread.h>
44#endif
45
46#define JVM_DLL "libjvm.so"
47#define JAVA_DLL "libjava.so"
48
49/*
50 * If a processor / os combination has the ability to run binaries of
51 * two data models and cohabitation of jre/jdk bits with both data
52 * models is supported, then DUAL_MODE is defined. When DUAL_MODE is
53 * defined, the architecture names for the narrow and wide version of
ksrini11e7f1b2009-11-20 11:01:32 -080054 * the architecture are defined in LIBARCH64NAME and LIBARCH32NAME.
55 * Currently only Solaris on sparc/sparcv9 and i586/amd64 is DUAL_MODE;
56 * linux i586/amd64 could be defined as DUAL_MODE but that is not the
duke6e45e102007-12-01 00:00:00 +000057 * current policy.
58 */
59
60#ifdef __solaris__
61# define DUAL_MODE
62# ifndef LIBARCH32NAME
63# error "The macro LIBARCH32NAME was not defined on the compile line"
64# endif
65# ifndef LIBARCH64NAME
66# error "The macro LIBARCH64NAME was not defined on the compile line"
67# endif
68# include <sys/systeminfo.h>
69# include <sys/elf.h>
70# include <stdio.h>
71#endif
72
73/* pointer to environment */
74extern char **environ;
75
76/*
77 * A collection of useful strings. One should think of these as #define
78 * entries, but actual strings can be more efficient (with many compilers).
79 */
80#ifdef __linux__
81static const char *system_dir = "/usr/java";
82static const char *user_dir = "/java";
83#else /* Solaris */
84static const char *system_dir = "/usr/jdk";
85static const char *user_dir = "/jdk";
86#endif
87
88/* Store the name of the executable once computed */
89static char *execname = NULL;
90
91/*
92 * Flowchart of launcher execs and options processing on unix
93 *
94 * The selection of the proper vm shared library to open depends on
95 * several classes of command line options, including vm "flavor"
96 * options (-client, -server) and the data model options, -d32 and
97 * -d64, as well as a version specification which may have come from
98 * the command line or from the manifest of an executable jar file.
99 * The vm selection options are not passed to the running
100 * virtual machine; they must be screened out by the launcher.
101 *
102 * The version specification (if any) is processed first by the
103 * platform independent routine SelectVersion. This may result in
104 * the exec of the specified launcher version.
105 *
ksrini11e7f1b2009-11-20 11:01:32 -0800106 * Previously the launcher modified the LD_LIBRARY_PATH appropriately for the
107 * desired data model path, regardless if data models matched or not. The
108 * launcher subsequently exec'ed the desired executable, in order to make the
109 * LD_LIBRARY_PATH path available for the runtime linker. This is no longer the
110 * case, the launcher dlopens the target libjvm.so. All other required
111 * libraries are loaded by the runtime linker, by virtue of the $ORIGIN paths
112 * baked into the shared libraries, by the build infrastructure at compile time.
duke6e45e102007-12-01 00:00:00 +0000113 *
ksrini11e7f1b2009-11-20 11:01:32 -0800114 * Main
115 * (incoming argv)
duke6e45e102007-12-01 00:00:00 +0000116 * |
117 * \|/
ksrini11e7f1b2009-11-20 11:01:32 -0800118 * SelectVersion
119 * (selects the JRE version, note: not data model)
120 * |
121 * \|/
122 * CreateExecutionEnvironment
123 * (determines desired data model)
124 * |
125 * |
126 * \|/
127 * Have Desired Model ? --> NO --> Is Dual-Mode ? --> NO --> Exit(with error)
duke6e45e102007-12-01 00:00:00 +0000128 * | |
ksrini11e7f1b2009-11-20 11:01:32 -0800129 * | |
duke6e45e102007-12-01 00:00:00 +0000130 * | \|/
ksrini11e7f1b2009-11-20 11:01:32 -0800131 * | YES
132 * | |
133 * | |
134 * | \|/
135 * | CheckJvmType
136 * | (removes -client, -server etc.)
137 * | |
138 * | |
139 * \|/ \|/
140 * YES (find the desired executable and exec child)
141 * | |
142 * | |
143 * \|/ \|/
144 * CheckJvmType Main
145 * (removes -client, -server, etc.)
146 * |
duke6e45e102007-12-01 00:00:00 +0000147 * |
148 * \|/
149 * TranslateDashJArgs...
150 * (Prepare to pass args to vm)
151 * |
152 * |
duke6e45e102007-12-01 00:00:00 +0000153 * \|/
154 * ParseArguments
ksrini11e7f1b2009-11-20 11:01:32 -0800155 * (removes -d32 and -d64 if any,
duke6e45e102007-12-01 00:00:00 +0000156 * processes version options,
157 * creates argument list for vm,
158 * etc.)
159 *
160 */
161
162static const char * SetExecname(char **argv);
163static jboolean GetJVMPath(const char *jrepath, const char *jvmtype,
164 char *jvmpath, jint jvmpathsize, const char * arch);
165static jboolean GetJREPath(char *path, jint pathsize, const char * arch, jboolean speculative);
166
167
168#define GetArch() GetArchPath(CURRENT_DATA_MODEL)
169
170const char *
171GetArchPath(int nbits)
172{
173 switch(nbits) {
174#ifdef DUAL_MODE
175 case 32:
176 return LIBARCH32NAME;
177 case 64:
178 return LIBARCH64NAME;
179#endif /* DUAL_MODE */
180 default:
181 return LIBARCHNAME;
182 }
183}
184
185void
186CreateExecutionEnvironment(int *_argcp,
187 char ***_argvp,
188 char jrepath[],
189 jint so_jrepath,
190 char jvmpath[],
191 jint so_jvmpath,
192 char **original_argv) {
193 /*
194 * First, determine if we are running the desired data model. If we
195 * are running the desired data model, all the error messages
196 * associated with calling GetJREPath, ReadKnownVMs, etc. should be
197 * output. However, if we are not running the desired data model,
198 * some of the errors should be suppressed since it is more
199 * informative to issue an error message based on whether or not the
200 * os/processor combination has dual mode capabilities.
201 */
202
203 int original_argc = *_argcp;
204 jboolean jvmpathExists;
205
206 /* Compute/set the name of the executable */
207 SetExecname(*_argvp);
208
ksrini11e7f1b2009-11-20 11:01:32 -0800209 /* Check data model flags, and exec process, if needed */
duke6e45e102007-12-01 00:00:00 +0000210 {
211 char *arch = (char *)GetArch(); /* like sparc or sparcv9 */
212 char * jvmtype = NULL;
213 int argc = *_argcp;
214 char **argv = original_argv;
215
duke6e45e102007-12-01 00:00:00 +0000216 int running = CURRENT_DATA_MODEL;
217
218 int wanted = running; /* What data mode is being
219 asked for? Current model is
220 fine unless another model
221 is asked for */
222
duke6e45e102007-12-01 00:00:00 +0000223 char** newargv = NULL;
224 int newargc = 0;
duke6e45e102007-12-01 00:00:00 +0000225
226 /*
227 * Starting in 1.5, all unix platforms accept the -d32 and -d64
228 * options. On platforms where only one data-model is supported
229 * (e.g. ia-64 Linux), using the flag for the other data model is
230 * an error and will terminate the program.
231 */
232
233 { /* open new scope to declare local variables */
234 int i;
235
236 newargv = (char **)JLI_MemAlloc((argc+1) * sizeof(*newargv));
237 newargv[newargc++] = argv[0];
238
239 /* scan for data model arguments and remove from argument list;
240 last occurrence determines desired data model */
241 for (i=1; i < argc; i++) {
242
243 if (JLI_StrCmp(argv[i], "-J-d64") == 0 || JLI_StrCmp(argv[i], "-d64") == 0) {
244 wanted = 64;
245 continue;
246 }
247 if (JLI_StrCmp(argv[i], "-J-d32") == 0 || JLI_StrCmp(argv[i], "-d32") == 0) {
248 wanted = 32;
249 continue;
250 }
251 newargv[newargc++] = argv[i];
252
253 if (IsJavaArgs()) {
254 if (argv[i][0] != '-') continue;
255 } else {
256 if (JLI_StrCmp(argv[i], "-classpath") == 0 || JLI_StrCmp(argv[i], "-cp") == 0) {
257 i++;
258 if (i >= argc) break;
259 newargv[newargc++] = argv[i];
260 continue;
261 }
262 if (argv[i][0] != '-') { i++; break; }
263 }
264 }
265
266 /* copy rest of args [i .. argc) */
267 while (i < argc) {
268 newargv[newargc++] = argv[i++];
269 }
270 newargv[newargc] = NULL;
271
272 /*
273 * newargv has all proper arguments here
274 */
275
276 argc = newargc;
277 argv = newargv;
278 }
279
280 /* If the data model is not changing, it is an error if the
281 jvmpath does not exist */
282 if (wanted == running) {
283 /* Find out where the JRE is that we will be using. */
284 if (!GetJREPath(jrepath, so_jrepath, arch, JNI_FALSE) ) {
ksrini0e817162008-08-26 10:21:20 -0700285 JLI_ReportErrorMessage(JRE_ERROR1);
duke6e45e102007-12-01 00:00:00 +0000286 exit(2);
287 }
288
289 /* Find the specified JVM type */
290 if (ReadKnownVMs(jrepath, arch, JNI_FALSE) < 1) {
ksrini0e817162008-08-26 10:21:20 -0700291 JLI_ReportErrorMessage(CFG_ERROR7);
duke6e45e102007-12-01 00:00:00 +0000292 exit(1);
293 }
294
295 jvmpath[0] = '\0';
296 jvmtype = CheckJvmType(_argcp, _argvp, JNI_FALSE);
297
298 if (!GetJVMPath(jrepath, jvmtype, jvmpath, so_jvmpath, arch )) {
ksrini0e817162008-08-26 10:21:20 -0700299 JLI_ReportErrorMessage(CFG_ERROR8, jvmtype, jvmpath);
duke6e45e102007-12-01 00:00:00 +0000300 exit(4);
301 }
ksrini11e7f1b2009-11-20 11:01:32 -0800302 /*
303 * we seem to have everything we need, so without further ado
304 * we return back.
305 */
306 return;
duke6e45e102007-12-01 00:00:00 +0000307 } else { /* do the same speculatively or exit */
308#ifdef DUAL_MODE
309 if (running != wanted) {
310 /* Find out where the JRE is that we will be using. */
311 if (!GetJREPath(jrepath, so_jrepath, GetArchPath(wanted), JNI_TRUE)) {
312 goto EndDataModelSpeculate;
313 }
314
315 /*
316 * Read in jvm.cfg for target data model and process vm
317 * selection options.
318 */
319 if (ReadKnownVMs(jrepath, GetArchPath(wanted), JNI_TRUE) < 1) {
320 goto EndDataModelSpeculate;
321 }
322 jvmpath[0] = '\0';
323 jvmtype = CheckJvmType(_argcp, _argvp, JNI_TRUE);
324 /* exec child can do error checking on the existence of the path */
325 jvmpathExists = GetJVMPath(jrepath, jvmtype, jvmpath, so_jvmpath, GetArchPath(wanted));
326
327 }
328 EndDataModelSpeculate: /* give up and let other code report error message */
329 ;
330#else
ksrini0e817162008-08-26 10:21:20 -0700331 JLI_ReportErrorMessage(JRE_ERROR2, wanted);
duke6e45e102007-12-01 00:00:00 +0000332 exit(1);
333#endif
334 }
335
duke6e45e102007-12-01 00:00:00 +0000336 {
337 char *newexec = execname;
338#ifdef DUAL_MODE
339 /*
340 * If the data model is being changed, the path to the
341 * executable must be updated accordingly; the executable name
342 * and directory the executable resides in are separate. In the
343 * case of 32 => 64, the new bits are assumed to reside in, e.g.
344 * "olddir/LIBARCH64NAME/execname"; in the case of 64 => 32,
345 * the bits are assumed to be in "olddir/../execname". For example,
346 *
347 * olddir/sparcv9/execname
348 * olddir/amd64/execname
349 *
350 * for Solaris SPARC and Linux amd64, respectively.
351 */
352
353 if (running != wanted) {
354 char *oldexec = JLI_StrCpy(JLI_MemAlloc(JLI_StrLen(execname) + 1), execname);
355 char *olddir = oldexec;
356 char *oldbase = JLI_StrRChr(oldexec, '/');
357
358
359 newexec = JLI_MemAlloc(JLI_StrLen(execname) + 20);
360 *oldbase++ = 0;
361 sprintf(newexec, "%s/%s/%s", olddir,
362 ((wanted==64) ? LIBARCH64NAME : ".."), oldbase);
363 argv[0] = newexec;
364 }
365#endif
ksrini11e7f1b2009-11-20 11:01:32 -0800366 JLI_TraceLauncher("TRACER_MARKER:About to EXEC\n");
duke6e45e102007-12-01 00:00:00 +0000367 (void)fflush(stdout);
368 (void)fflush(stderr);
ksrini11e7f1b2009-11-20 11:01:32 -0800369 execv(newexec, argv);
ksrini0e817162008-08-26 10:21:20 -0700370 JLI_ReportErrorMessageSys(JRE_ERROR4, newexec);
duke6e45e102007-12-01 00:00:00 +0000371
372#ifdef DUAL_MODE
373 if (running != wanted) {
ksrini0e817162008-08-26 10:21:20 -0700374 JLI_ReportErrorMessage(JRE_ERROR5, wanted, running);
duke6e45e102007-12-01 00:00:00 +0000375# ifdef __solaris__
duke6e45e102007-12-01 00:00:00 +0000376# ifdef __sparc
ksrini0e817162008-08-26 10:21:20 -0700377 JLI_ReportErrorMessage(JRE_ERROR6);
duke6e45e102007-12-01 00:00:00 +0000378# else
ksrini0e817162008-08-26 10:21:20 -0700379 JLI_ReportErrorMessage(JRE_ERROR7);
duke6e45e102007-12-01 00:00:00 +0000380# endif
381 }
382# endif
383#endif
384
385 }
duke6e45e102007-12-01 00:00:00 +0000386 exit(1);
387 }
388
389}
390
duke6e45e102007-12-01 00:00:00 +0000391/*
392 * On Solaris VM choosing is done by the launcher (java.c).
393 */
394static jboolean
395GetJVMPath(const char *jrepath, const char *jvmtype,
396 char *jvmpath, jint jvmpathsize, const char * arch)
397{
398 struct stat s;
399
400 if (JLI_StrChr(jvmtype, '/')) {
401 sprintf(jvmpath, "%s/" JVM_DLL, jvmtype);
402 } else {
403 sprintf(jvmpath, "%s/lib/%s/%s/" JVM_DLL, jrepath, arch, jvmtype);
404 }
405
406 JLI_TraceLauncher("Does `%s' exist ... ", jvmpath);
407
408 if (stat(jvmpath, &s) == 0) {
409 JLI_TraceLauncher("yes.\n");
410 return JNI_TRUE;
411 } else {
412 JLI_TraceLauncher("no.\n");
413 return JNI_FALSE;
414 }
415}
416
417/*
418 * Find path to JRE based on .exe's location or registry settings.
419 */
420static jboolean
421GetJREPath(char *path, jint pathsize, const char * arch, jboolean speculative)
422{
423 char libjava[MAXPATHLEN];
424
425 if (GetApplicationHome(path, pathsize)) {
426 /* Is JRE co-located with the application? */
427 sprintf(libjava, "%s/lib/%s/" JAVA_DLL, path, arch);
428 if (access(libjava, F_OK) == 0) {
429 goto found;
430 }
431
432 /* Does the app ship a private JRE in <apphome>/jre directory? */
433 sprintf(libjava, "%s/jre/lib/%s/" JAVA_DLL, path, arch);
434 if (access(libjava, F_OK) == 0) {
435 JLI_StrCat(path, "/jre");
436 goto found;
437 }
438 }
439
440 if (!speculative)
ksrini0e817162008-08-26 10:21:20 -0700441 JLI_ReportErrorMessage(JRE_ERROR8 JAVA_DLL);
duke6e45e102007-12-01 00:00:00 +0000442 return JNI_FALSE;
443
444 found:
445 JLI_TraceLauncher("JRE path is %s\n", path);
446 return JNI_TRUE;
447}
448
449jboolean
450LoadJavaVM(const char *jvmpath, InvocationFunctions *ifn)
451{
452 Dl_info dlinfo;
453 void *libjvm;
454
455 JLI_TraceLauncher("JVM path is %s\n", jvmpath);
456
457 libjvm = dlopen(jvmpath, RTLD_NOW + RTLD_GLOBAL);
458 if (libjvm == NULL) {
459#if defined(__solaris__) && defined(__sparc) && !defined(_LP64) /* i.e. 32-bit sparc */
460 FILE * fp;
461 Elf32_Ehdr elf_head;
462 int count;
463 int location;
464
465 fp = fopen(jvmpath, "r");
466 if(fp == NULL)
467 goto error;
468
469 /* read in elf header */
470 count = fread((void*)(&elf_head), sizeof(Elf32_Ehdr), 1, fp);
471 fclose(fp);
472 if(count < 1)
473 goto error;
474
475 /*
476 * Check for running a server vm (compiled with -xarch=v8plus)
477 * on a stock v8 processor. In this case, the machine type in
478 * the elf header would not be included the architecture list
479 * provided by the isalist command, which is turn is gotten from
480 * sysinfo. This case cannot occur on 64-bit hardware and thus
481 * does not have to be checked for in binaries with an LP64 data
482 * model.
483 */
484 if(elf_head.e_machine == EM_SPARC32PLUS) {
485 char buf[257]; /* recommended buffer size from sysinfo man
486 page */
487 long length;
488 char* location;
489
490 length = sysinfo(SI_ISALIST, buf, 257);
491 if(length > 0) {
492 location = JLI_StrStr(buf, "sparcv8plus ");
493 if(location == NULL) {
ksrini0e817162008-08-26 10:21:20 -0700494 JLI_ReportErrorMessage(JVM_ERROR3);
duke6e45e102007-12-01 00:00:00 +0000495 return JNI_FALSE;
496 }
497 }
498 }
499#endif
ksrini0e817162008-08-26 10:21:20 -0700500 JLI_ReportErrorMessage(DLL_ERROR1, __LINE__);
duke6e45e102007-12-01 00:00:00 +0000501 goto error;
502 }
503
504 ifn->CreateJavaVM = (CreateJavaVM_t)
505 dlsym(libjvm, "JNI_CreateJavaVM");
506 if (ifn->CreateJavaVM == NULL)
507 goto error;
508
509 ifn->GetDefaultJavaVMInitArgs = (GetDefaultJavaVMInitArgs_t)
510 dlsym(libjvm, "JNI_GetDefaultJavaVMInitArgs");
511 if (ifn->GetDefaultJavaVMInitArgs == NULL)
512 goto error;
513
514 return JNI_TRUE;
515
516error:
ksrini0e817162008-08-26 10:21:20 -0700517 JLI_ReportErrorMessage(DLL_ERROR2, jvmpath, dlerror());
duke6e45e102007-12-01 00:00:00 +0000518 return JNI_FALSE;
519}
520
521/*
522 * If app is "/foo/bin/javac", or "/foo/bin/sparcv9/javac" then put
523 * "/foo" into buf.
524 */
525jboolean
526GetApplicationHome(char *buf, jint bufsize)
527{
528 if (execname != NULL) {
529 JLI_StrNCpy(buf, execname, bufsize-1);
530 buf[bufsize-1] = '\0';
531 } else {
532 return JNI_FALSE;
533 }
534
535 if (JLI_StrRChr(buf, '/') == 0) {
536 buf[0] = '\0';
537 return JNI_FALSE;
538 }
539 *(JLI_StrRChr(buf, '/')) = '\0'; /* executable file */
540 if (JLI_StrLen(buf) < 4 || JLI_StrRChr(buf, '/') == 0) {
541 buf[0] = '\0';
542 return JNI_FALSE;
543 }
544 if (JLI_StrCmp("/bin", buf + JLI_StrLen(buf) - 4) != 0)
545 *(JLI_StrRChr(buf, '/')) = '\0'; /* sparcv9 or amd64 */
546 if (JLI_StrLen(buf) < 4 || JLI_StrCmp("/bin", buf + JLI_StrLen(buf) - 4) != 0) {
547 buf[0] = '\0';
548 return JNI_FALSE;
549 }
550 *(JLI_StrRChr(buf, '/')) = '\0'; /* bin */
551
552 return JNI_TRUE;
553}
554
555
556/*
557 * Return true if the named program exists
558 */
559static int
560ProgramExists(char *name)
561{
562 struct stat sb;
563 if (stat(name, &sb) != 0) return 0;
564 if (S_ISDIR(sb.st_mode)) return 0;
565 return (sb.st_mode & S_IEXEC) != 0;
566}
567
568
569/*
570 * Find a command in a directory, returning the path.
571 */
572static char *
573Resolve(char *indir, char *cmd)
574{
575 char name[PATH_MAX + 2], *real;
576
577 if ((JLI_StrLen(indir) + JLI_StrLen(cmd) + 1) > PATH_MAX) return 0;
578 sprintf(name, "%s%c%s", indir, FILE_SEPARATOR, cmd);
579 if (!ProgramExists(name)) return 0;
580 real = JLI_MemAlloc(PATH_MAX + 2);
581 if (!realpath(name, real))
582 JLI_StrCpy(real, name);
583 return real;
584}
585
586
587/*
588 * Find a path for the executable
589 */
590static char *
591FindExecName(char *program)
592{
593 char cwdbuf[PATH_MAX+2];
594 char *path;
595 char *tmp_path;
596 char *f;
597 char *result = NULL;
598
599 /* absolute path? */
600 if (*program == FILE_SEPARATOR ||
601 (FILE_SEPARATOR=='\\' && JLI_StrRChr(program, ':')))
602 return Resolve("", program+1);
603
604 /* relative path? */
605 if (JLI_StrRChr(program, FILE_SEPARATOR) != 0) {
606 char buf[PATH_MAX+2];
607 return Resolve(getcwd(cwdbuf, sizeof(cwdbuf)), program);
608 }
609
610 /* from search path? */
611 path = getenv("PATH");
612 if (!path || !*path) path = ".";
613 tmp_path = JLI_MemAlloc(JLI_StrLen(path) + 2);
614 JLI_StrCpy(tmp_path, path);
615
616 for (f=tmp_path; *f && result==0; ) {
617 char *s = f;
618 while (*f && (*f != PATH_SEPARATOR)) ++f;
619 if (*f) *f++ = 0;
620 if (*s == FILE_SEPARATOR)
621 result = Resolve(s, program);
622 else {
623 /* relative path element */
624 char dir[2*PATH_MAX];
625 sprintf(dir, "%s%c%s", getcwd(cwdbuf, sizeof(cwdbuf)),
626 FILE_SEPARATOR, s);
627 result = Resolve(dir, program);
628 }
629 if (result != 0) break;
630 }
631
632 JLI_MemFree(tmp_path);
633 return result;
634}
635
636
637
638/*
639 * Compute the name of the executable
640 *
641 * In order to re-exec securely we need the absolute path of the
642 * executable. On Solaris getexecname(3c) may not return an absolute
643 * path so we use dladdr to get the filename of the executable and
644 * then use realpath to derive an absolute path. From Solaris 9
645 * onwards the filename returned in DL_info structure from dladdr is
646 * an absolute pathname so technically realpath isn't required.
647 * On Linux we read the executable name from /proc/self/exe.
648 * As a fallback, and for platforms other than Solaris and Linux,
649 * we use FindExecName to compute the executable name.
650 */
651static const char*
652SetExecname(char **argv)
653{
654 char* exec_path = NULL;
655#if defined(__solaris__)
656 {
657 Dl_info dlinfo;
658 int (*fptr)();
659
660 fptr = (int (*)())dlsym(RTLD_DEFAULT, "main");
661 if (fptr == NULL) {
ksrini0e817162008-08-26 10:21:20 -0700662 JLI_ReportErrorMessage(DLL_ERROR3, dlerror());
duke6e45e102007-12-01 00:00:00 +0000663 return JNI_FALSE;
664 }
665
666 if (dladdr((void*)fptr, &dlinfo)) {
667 char *resolved = (char*)JLI_MemAlloc(PATH_MAX+1);
668 if (resolved != NULL) {
669 exec_path = realpath(dlinfo.dli_fname, resolved);
670 if (exec_path == NULL) {
671 JLI_MemFree(resolved);
672 }
673 }
674 }
675 }
676#elif defined(__linux__)
677 {
678 const char* self = "/proc/self/exe";
679 char buf[PATH_MAX+1];
680 int len = readlink(self, buf, PATH_MAX);
681 if (len >= 0) {
682 buf[len] = '\0'; /* readlink doesn't nul terminate */
683 exec_path = JLI_StringDup(buf);
684 }
685 }
686#else /* !__solaris__ && !__linux */
687 {
688 /* Not implemented */
689 }
690#endif
691
692 if (exec_path == NULL) {
693 exec_path = FindExecName(argv[0]);
694 }
695 execname = exec_path;
696 return exec_path;
697}
698
ksrini0e817162008-08-26 10:21:20 -0700699void JLI_ReportErrorMessage(const char* fmt, ...) {
duke6e45e102007-12-01 00:00:00 +0000700 va_list vl;
701 va_start(vl, fmt);
702 vfprintf(stderr, fmt, vl);
703 fprintf(stderr, "\n");
704 va_end(vl);
705}
706
ksrini0e817162008-08-26 10:21:20 -0700707void JLI_ReportErrorMessageSys(const char* fmt, ...) {
duke6e45e102007-12-01 00:00:00 +0000708 va_list vl;
709 char *emsg;
710
711 /*
712 * TODO: its safer to use strerror_r but is not available on
713 * Solaris 8. Until then....
714 */
715 emsg = strerror(errno);
716 if (emsg != NULL) {
717 fprintf(stderr, "%s\n", emsg);
718 }
719
720 va_start(vl, fmt);
721 vfprintf(stderr, fmt, vl);
722 fprintf(stderr, "\n");
723 va_end(vl);
724}
725
ksrini0e817162008-08-26 10:21:20 -0700726void JLI_ReportExceptionDescription(JNIEnv * env) {
duke6e45e102007-12-01 00:00:00 +0000727 (*env)->ExceptionDescribe(env);
728}
729
730/*
731 * Since using the file system as a registry is a bit risky, perform
732 * additional sanity checks on the identified directory to validate
733 * it as a valid jre/sdk.
734 *
735 * Return 0 if the tests fail; otherwise return non-zero (true).
736 *
737 * Note that checking for anything more than the existence of an
738 * executable object at bin/java relative to the path being checked
739 * will break the regression tests.
740 */
741static int
742CheckSanity(char *path, char *dir)
743{
744 char buffer[PATH_MAX];
745
746 if (JLI_StrLen(path) + JLI_StrLen(dir) + 11 > PATH_MAX)
747 return (0); /* Silently reject "impossibly" long paths */
748
749 sprintf(buffer, "%s/%s/bin/java", path, dir);
750 return ((access(buffer, X_OK) == 0) ? 1 : 0);
751}
752
753/*
754 * Determine if there is an acceptable JRE in the directory dirname.
755 * Upon locating the "best" one, return a fully qualified path to
756 * it. "Best" is defined as the most advanced JRE meeting the
757 * constraints contained in the manifest_info. If no JRE in this
758 * directory meets the constraints, return NULL.
759 *
760 * Note that we don't check for errors in reading the directory
761 * (which would be done by checking errno). This is because it
762 * doesn't matter if we get an error reading the directory, or
763 * we just don't find anything interesting in the directory. We
764 * just return NULL in either case.
765 *
766 * The historical names of j2sdk and j2re were changed to jdk and
767 * jre respecively as part of the 1.5 rebranding effort. Since the
768 * former names are legacy on Linux, they must be recognized for
769 * all time. Fortunately, this is a minor cost.
770 */
771static char
772*ProcessDir(manifest_info *info, char *dirname)
773{
774 DIR *dirp;
775 struct dirent *dp;
776 char *best = NULL;
777 int offset;
778 int best_offset = 0;
779 char *ret_str = NULL;
780 char buffer[PATH_MAX];
781
782 if ((dirp = opendir(dirname)) == NULL)
783 return (NULL);
784
785 do {
786 if ((dp = readdir(dirp)) != NULL) {
787 offset = 0;
788 if ((JLI_StrNCmp(dp->d_name, "jre", 3) == 0) ||
789 (JLI_StrNCmp(dp->d_name, "jdk", 3) == 0))
790 offset = 3;
791 else if (JLI_StrNCmp(dp->d_name, "j2re", 4) == 0)
792 offset = 4;
793 else if (JLI_StrNCmp(dp->d_name, "j2sdk", 5) == 0)
794 offset = 5;
795 if (offset > 0) {
796 if ((JLI_AcceptableRelease(dp->d_name + offset,
797 info->jre_version)) && CheckSanity(dirname, dp->d_name))
798 if ((best == NULL) || (JLI_ExactVersionId(
799 dp->d_name + offset, best + best_offset) > 0)) {
800 if (best != NULL)
801 JLI_MemFree(best);
802 best = JLI_StringDup(dp->d_name);
803 best_offset = offset;
804 }
805 }
806 }
807 } while (dp != NULL);
808 (void) closedir(dirp);
809 if (best == NULL)
810 return (NULL);
811 else {
812 ret_str = JLI_MemAlloc(JLI_StrLen(dirname) + JLI_StrLen(best) + 2);
813 sprintf(ret_str, "%s/%s", dirname, best);
814 JLI_MemFree(best);
815 return (ret_str);
816 }
817}
818
819/*
820 * This is the global entry point. It examines the host for the optimal
821 * JRE to be used by scanning a set of directories. The set of directories
822 * is platform dependent and can be overridden by the environment
823 * variable JAVA_VERSION_PATH.
824 *
825 * This routine itself simply determines the set of appropriate
826 * directories before passing control onto ProcessDir().
827 */
828char*
829LocateJRE(manifest_info* info)
830{
831 char *path;
832 char *home;
833 char *target = NULL;
834 char *dp;
835 char *cp;
836
837 /*
838 * Start by getting JAVA_VERSION_PATH
839 */
840 if (info->jre_restrict_search) {
841 path = JLI_StringDup(system_dir);
842 } else if ((path = getenv("JAVA_VERSION_PATH")) != NULL) {
843 path = JLI_StringDup(path);
844 } else {
845 if ((home = getenv("HOME")) != NULL) {
846 path = (char *)JLI_MemAlloc(JLI_StrLen(home) + \
847 JLI_StrLen(system_dir) + JLI_StrLen(user_dir) + 2);
848 sprintf(path, "%s%s:%s", home, user_dir, system_dir);
849 } else {
850 path = JLI_StringDup(system_dir);
851 }
852 }
853
854 /*
855 * Step through each directory on the path. Terminate the scan with
856 * the first directory with an acceptable JRE.
857 */
858 cp = dp = path;
859 while (dp != NULL) {
860 cp = JLI_StrChr(dp, (int)':');
861 if (cp != NULL)
862 *cp = (char)NULL;
863 if ((target = ProcessDir(info, dp)) != NULL)
864 break;
865 dp = cp;
866 if (dp != NULL)
867 dp++;
868 }
869 JLI_MemFree(path);
870 return (target);
871}
872
873/*
874 * Given a path to a jre to execute, this routine checks if this process
875 * is indeed that jre. If not, it exec's that jre.
876 *
877 * We want to actually check the paths rather than just the version string
878 * built into the executable, so that given version specification (and
879 * JAVA_VERSION_PATH) will yield the exact same Java environment, regardless
880 * of the version of the arbitrary launcher we start with.
881 */
882void
883ExecJRE(char *jre, char **argv)
884{
885 char wanted[PATH_MAX];
886 const char* progname = GetProgramName();
887
888 /*
889 * Resolve the real path to the directory containing the selected JRE.
890 */
891 if (realpath(jre, wanted) == NULL) {
ksrini0e817162008-08-26 10:21:20 -0700892 JLI_ReportErrorMessage(JRE_ERROR9, jre);
duke6e45e102007-12-01 00:00:00 +0000893 exit(1);
894 }
895
896 /*
897 * Resolve the real path to the currently running launcher.
898 */
899 SetExecname(argv);
900 if (execname == NULL) {
ksrini0e817162008-08-26 10:21:20 -0700901 JLI_ReportErrorMessage(JRE_ERROR10);
duke6e45e102007-12-01 00:00:00 +0000902 exit(1);
903 }
904
905 /*
906 * If the path to the selected JRE directory is a match to the initial
907 * portion of the path to the currently executing JRE, we have a winner!
908 * If so, just return.
909 */
910 if (JLI_StrNCmp(wanted, execname, JLI_StrLen(wanted)) == 0)
911 return; /* I am the droid you were looking for */
912
913
914 /*
915 * This should never happen (because of the selection code in SelectJRE),
916 * but check for "impossibly" long path names just because buffer overruns
917 * can be so deadly.
918 */
919 if (JLI_StrLen(wanted) + JLI_StrLen(progname) + 6 > PATH_MAX) {
ksrini0e817162008-08-26 10:21:20 -0700920 JLI_ReportErrorMessage(JRE_ERROR11);
duke6e45e102007-12-01 00:00:00 +0000921 exit(1);
922 }
923
924 /*
925 * Construct the path and exec it.
926 */
927 (void)JLI_StrCat(JLI_StrCat(wanted, "/bin/"), progname);
928 argv[0] = JLI_StringDup(progname);
929 if (JLI_IsTraceLauncher()) {
930 int i;
931 printf("ReExec Command: %s (%s)\n", wanted, argv[0]);
932 printf("ReExec Args:");
933 for (i = 1; argv[i] != NULL; i++)
934 printf(" %s", argv[i]);
935 printf("\n");
936 }
ksrini11e7f1b2009-11-20 11:01:32 -0800937 JLI_TraceLauncher("TRACER_MARKER:About to EXEC\n");
duke6e45e102007-12-01 00:00:00 +0000938 (void)fflush(stdout);
939 (void)fflush(stderr);
940 execv(wanted, argv);
ksrini0e817162008-08-26 10:21:20 -0700941 JLI_ReportErrorMessageSys(JRE_ERROR12, wanted);
duke6e45e102007-12-01 00:00:00 +0000942 exit(1);
943}
944
945/*
946 * "Borrowed" from Solaris 10 where the unsetenv() function is being added
947 * to libc thanks to SUSv3 (Standard Unix Specification, version 3). As
948 * such, in the fullness of time this will appear in libc on all relevant
949 * Solaris/Linux platforms and maybe even the Windows platform. At that
950 * time, this stub can be removed.
951 *
952 * This implementation removes the environment locking for multithreaded
953 * applications. (We don't have access to these mutexes within libc and
954 * the launcher isn't multithreaded.) Note that what remains is platform
955 * independent, because it only relies on attributes that a POSIX environment
956 * defines.
957 *
958 * Returns 0 on success, -1 on failure.
959 *
960 * Also removed was the setting of errno. The only value of errno set
961 * was EINVAL ("Invalid Argument").
962 */
963
964/*
965 * s1(environ) is name=value
966 * s2(name) is name(not the form of name=value).
967 * if names match, return value of 1, else return 0
968 */
969static int
970match_noeq(const char *s1, const char *s2)
971{
972 while (*s1 == *s2++) {
973 if (*s1++ == '=')
974 return (1);
975 }
976 if (*s1 == '=' && s2[-1] == '\0')
977 return (1);
978 return (0);
979}
980
981/*
982 * added for SUSv3 standard
983 *
984 * Delete entry from environ.
985 * Do not free() memory! Other threads may be using it.
986 * Keep it around forever.
987 */
988static int
989borrowed_unsetenv(const char *name)
990{
991 long idx; /* index into environ */
992
993 if (name == NULL || *name == '\0' ||
994 JLI_StrChr(name, '=') != NULL) {
995 return (-1);
996 }
997
998 for (idx = 0; environ[idx] != NULL; idx++) {
999 if (match_noeq(environ[idx], name))
1000 break;
1001 }
1002 if (environ[idx] == NULL) {
1003 /* name not found but still a success */
1004 return (0);
1005 }
1006 /* squeeze up one entry */
1007 do {
1008 environ[idx] = environ[idx+1];
1009 } while (environ[++idx] != NULL);
1010
1011 return (0);
1012}
1013/* --- End of "borrowed" code --- */
1014
1015/*
1016 * Wrapper for unsetenv() function.
1017 */
1018int
1019UnsetEnv(char *name)
1020{
1021 return(borrowed_unsetenv(name));
1022}
1023
1024/* --- Splash Screen shared library support --- */
1025
1026static const char* SPLASHSCREEN_SO = "libsplashscreen.so";
1027
1028static void* hSplashLib = NULL;
1029
1030void* SplashProcAddress(const char* name) {
1031 if (!hSplashLib) {
1032 hSplashLib = dlopen(SPLASHSCREEN_SO, RTLD_LAZY | RTLD_GLOBAL);
1033 }
1034 if (hSplashLib) {
1035 void* sym = dlsym(hSplashLib, name);
1036 return sym;
1037 } else {
1038 return NULL;
1039 }
1040}
1041
1042void SplashFreeLibrary() {
1043 if (hSplashLib) {
1044 dlclose(hSplashLib);
1045 hSplashLib = NULL;
1046 }
1047}
1048
1049const char *
1050jlong_format_specifier() {
1051 return "%lld";
1052}
1053
1054
1055
1056/*
1057 * Block current thread and continue execution in a new thread
1058 */
1059int
1060ContinueInNewThread0(int (JNICALL *continuation)(void *), jlong stack_size, void * args) {
1061 int rslt;
1062#ifdef __linux__
1063 pthread_t tid;
1064 pthread_attr_t attr;
1065 pthread_attr_init(&attr);
1066 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
1067
1068 if (stack_size > 0) {
1069 pthread_attr_setstacksize(&attr, stack_size);
1070 }
1071
1072 if (pthread_create(&tid, &attr, (void *(*)(void*))continuation, (void*)args) == 0) {
1073 void * tmp;
1074 pthread_join(tid, &tmp);
1075 rslt = (int)tmp;
1076 } else {
1077 /*
1078 * Continue execution in current thread if for some reason (e.g. out of
1079 * memory/LWP) a new thread can't be created. This will likely fail
1080 * later in continuation as JNI_CreateJavaVM needs to create quite a
1081 * few new threads, anyway, just give it a try..
1082 */
1083 rslt = continuation(args);
1084 }
1085
1086 pthread_attr_destroy(&attr);
1087#else
1088 thread_t tid;
1089 long flags = 0;
1090 if (thr_create(NULL, stack_size, (void *(*)(void *))continuation, args, flags, &tid) == 0) {
1091 void * tmp;
1092 thr_join(tid, NULL, &tmp);
1093 rslt = (int)tmp;
1094 } else {
1095 /* See above. Continue in current thread if thr_create() failed */
1096 rslt = continuation(args);
1097 }
1098#endif
1099 return rslt;
1100}
1101
1102/* Coarse estimation of number of digits assuming the worst case is a 64-bit pid. */
1103#define MAX_PID_STR_SZ 20
1104
1105void SetJavaLauncherPlatformProps() {
1106 /* Linux only */
1107#ifdef __linux__
1108 const char *substr = "-Dsun.java.launcher.pid=";
1109 char *pid_prop_str = (char *)JLI_MemAlloc(JLI_StrLen(substr) + MAX_PID_STR_SZ + 1);
1110 sprintf(pid_prop_str, "%s%d", substr, getpid());
1111 AddOption(pid_prop_str, NULL);
1112#endif
1113}
duke6e45e102007-12-01 00:00:00 +00001114
1115jboolean
1116IsJavaw()
1117{
1118 /* noop on UNIX */
1119 return JNI_FALSE;
1120}
ksrini52cded22008-03-06 07:51:28 -08001121
1122void
1123InitLauncher(jboolean javaw)
1124{
1125 JLI_SetTraceLauncher();
1126}
ksrini20a64b22008-09-24 15:07:41 -07001127
1128/*
1129 * The implementation for finding classes from the bootstrap
1130 * class loader, refer to java.h
1131 */
1132static FindClassFromBootLoader_t *findBootClass = NULL;
1133
1134jclass
1135FindBootStrapClass(JNIEnv *env, const char* classname)
1136{
1137 if (findBootClass == NULL) {
1138 findBootClass = (FindClassFromBootLoader_t *)dlsym(RTLD_DEFAULT,
mchungb2ce9412009-08-06 16:35:24 -07001139 "JVM_FindClassFromBootLoader");
ksrini20a64b22008-09-24 15:07:41 -07001140 if (findBootClass == NULL) {
1141 JLI_ReportErrorMessage(DLL_ERROR4,
mchungb2ce9412009-08-06 16:35:24 -07001142 "JVM_FindClassFromBootLoader");
ksrini20a64b22008-09-24 15:07:41 -07001143 return NULL;
1144 }
1145 }
mchungb2ce9412009-08-06 16:35:24 -07001146 return findBootClass(env, classname);
ksrini20a64b22008-09-24 15:07:41 -07001147}