comparison src/os/solaris/launcher/java_md.c @ 0:a61af66fc99e jdk7-b24

Initial load
author duke
date Sat, 01 Dec 2007 00:00:00 +0000
parents
children f008d3631bd1
comparison
equal deleted inserted replaced
-1:000000000000 0:a61af66fc99e
1 /*
2 * Copyright 1998-2005 Sun Microsystems, Inc. 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.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 *
23 */
24
25 /*
26 * Gamma (Hotspot internal engineering test) launcher based on 1.6.0-b28 JDK,
27 * search "GAMMA" for gamma specific changes.
28 */
29
30 #include "java.h"
31 #include <dirent.h>
32 #include <dlfcn.h>
33 #include <fcntl.h>
34 #include <inttypes.h>
35 #include <stdio.h>
36 #include <string.h>
37 #include <stdlib.h>
38 #include <limits.h>
39 #include <sys/stat.h>
40 #include <unistd.h>
41 #include <sys/types.h>
42
43 #ifndef GAMMA
44 #include "manifest_info.h"
45 #include "version_comp.h"
46 #endif
47
48 #define JVM_DLL "libjvm.so"
49 #define JAVA_DLL "libjava.so"
50
51 #ifndef GAMMA /* launcher.make defines ARCH */
52
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 BIG_ARCH and SMALL_ARCH. Currently
59 * only Solaris on sparc/sparcv9 and i586/amd64 is DUAL_MODE; linux
60 * i586/amd64 could be defined as DUAL_MODE but that is not the
61 * current policy.
62 */
63
64 #ifdef _LP64
65
66 # ifdef ia64
67 # define ARCH "ia64"
68 # elif defined(amd64)
69 # define ARCH "amd64"
70 # elif defined(__sparc)
71 # define ARCH "sparcv9"
72 # else
73 # define ARCH "unknown" /* unknown 64-bit architecture */
74 # endif
75
76 #else /* 32-bit data model */
77
78 # ifdef i586
79 # define ARCH "i386"
80 # elif defined(__sparc)
81 # define ARCH "sparc"
82 # endif
83
84 #endif /* _LP64 */
85
86 #ifdef __sun
87 # define DUAL_MODE
88 # ifdef __sparc
89 # define BIG_ARCH "sparcv9"
90 # define SMALL_ARCH "sparc"
91 # else
92 # define BIG_ARCH "amd64"
93 # define SMALL_ARCH "i386"
94 # endif
95 # include <sys/systeminfo.h>
96 # include <sys/elf.h>
97 # include <stdio.h>
98 #else
99 # ifndef ARCH
100 # include <sys/systeminfo.h>
101 # endif
102 #endif
103
104 #endif /* ifndef GAMMA */
105
106 /* pointer to environment */
107 extern char **environ;
108
109 #ifndef GAMMA
110
111 /*
112 * A collection of useful strings. One should think of these as #define
113 * entries, but actual strings can be more efficient (with many compilers).
114 */
115 #ifdef __linux__
116 static const char *system_dir = "/usr/java";
117 static const char *user_dir = "/java";
118 #else /* Solaris */
119 static const char *system_dir = "/usr/jdk";
120 static const char *user_dir = "/jdk";
121 #endif
122
123 #endif /* ifndef GAMMA */
124
125 /*
126 * Flowchart of launcher execs and options processing on unix
127 *
128 * The selection of the proper vm shared library to open depends on
129 * several classes of command line options, including vm "flavor"
130 * options (-client, -server) and the data model options, -d32 and
131 * -d64, as well as a version specification which may have come from
132 * the command line or from the manifest of an executable jar file.
133 * The vm selection options are not passed to the running
134 * virtual machine; they must be screened out by the launcher.
135 *
136 * The version specification (if any) is processed first by the
137 * platform independent routine SelectVersion. This may result in
138 * the exec of the specified launcher version.
139 *
140 * Typically, the launcher execs at least once to ensure a suitable
141 * LD_LIBRARY_PATH is in effect for the process. The first exec
142 * screens out all the data model options; leaving the choice of data
143 * model implicit in the binary selected to run. However, in case no
144 * exec is done, the data model options are screened out before the vm
145 * is invoked.
146 *
147 * incoming argv ------------------------------
148 * | |
149 * \|/ |
150 * CheckJVMType |
151 * (removes -client, -server, etc.) |
152 * \|/
153 * CreateExecutionEnvironment
154 * (removes -d32 and -d64,
155 * determines desired data model,
156 * sets up LD_LIBRARY_PATH,
157 * and exec's)
158 * |
159 * --------------------------------------------
160 * |
161 * \|/
162 * exec child 1 incoming argv -----------------
163 * | |
164 * \|/ |
165 * CheckJVMType |
166 * (removes -client, -server, etc.) |
167 * | \|/
168 * | CreateExecutionEnvironment
169 * | (verifies desired data model
170 * | is running and acceptable
171 * | LD_LIBRARY_PATH;
172 * | no-op in child)
173 * |
174 * \|/
175 * TranslateDashJArgs...
176 * (Prepare to pass args to vm)
177 * |
178 * |
179 * |
180 * \|/
181 * ParseArguments
182 * (ignores -d32 and -d64,
183 * processes version options,
184 * creates argument list for vm,
185 * etc.)
186 *
187 */
188
189 static char *SetExecname(char **argv);
190 static char * GetExecname();
191 static jboolean GetJVMPath(const char *jrepath, const char *jvmtype,
192 char *jvmpath, jint jvmpathsize, char * arch);
193 static jboolean GetJREPath(char *path, jint pathsize, char * arch, jboolean speculative);
194
195 const char *
196 GetArch()
197 {
198 static char *arch = NULL;
199 static char buf[12];
200 if (arch) {
201 return arch;
202 }
203
204 #ifdef ARCH
205 strcpy(buf, ARCH);
206 #else
207 sysinfo(SI_ARCHITECTURE, buf, sizeof(buf));
208 #endif
209 arch = buf;
210 return arch;
211 }
212
213 void
214 CreateExecutionEnvironment(int *_argcp,
215 char ***_argvp,
216 char jrepath[],
217 jint so_jrepath,
218 char jvmpath[],
219 jint so_jvmpath,
220 char **original_argv) {
221 /*
222 * First, determine if we are running the desired data model. If we
223 * are running the desired data model, all the error messages
224 * associated with calling GetJREPath, ReadKnownVMs, etc. should be
225 * output. However, if we are not running the desired data model,
226 * some of the errors should be suppressed since it is more
227 * informative to issue an error message based on whether or not the
228 * os/processor combination has dual mode capabilities.
229 */
230
231 char *execname = NULL;
232 int original_argc = *_argcp;
233 jboolean jvmpathExists;
234
235 /* Compute the name of the executable */
236 execname = SetExecname(*_argvp);
237
238 #ifndef GAMMA
239 /* Set the LD_LIBRARY_PATH environment variable, check data model
240 flags, and exec process, if needed */
241 {
242 char *arch = (char *)GetArch(); /* like sparc or sparcv9 */
243 char * jvmtype = NULL;
244 int argc = *_argcp;
245 char **argv = original_argv;
246
247 char *runpath = NULL; /* existing effective LD_LIBRARY_PATH
248 setting */
249
250 int running = /* What data model is being ILP32 =>
251 32 bit vm; LP64 => 64 bit vm */
252 #ifdef _LP64
253 64;
254 #else
255 32;
256 #endif
257
258 int wanted = running; /* What data mode is being
259 asked for? Current model is
260 fine unless another model
261 is asked for */
262
263 char* new_runpath = NULL; /* desired new LD_LIBRARY_PATH string */
264 char* newpath = NULL; /* path on new LD_LIBRARY_PATH */
265 char* lastslash = NULL;
266
267 char** newenvp = NULL; /* current environment */
268
269 char** newargv = NULL;
270 int newargc = 0;
271 #ifdef __sun
272 char* dmpath = NULL; /* data model specific LD_LIBRARY_PATH,
273 Solaris only */
274 #endif
275
276 /*
277 * Starting in 1.5, all unix platforms accept the -d32 and -d64
278 * options. On platforms where only one data-model is supported
279 * (e.g. ia-64 Linux), using the flag for the other data model is
280 * an error and will terminate the program.
281 */
282
283 { /* open new scope to declare local variables */
284 int i;
285
286 newargv = (char **)MemAlloc((argc+1) * sizeof(*newargv));
287 newargv[newargc++] = argv[0];
288
289 /* scan for data model arguments and remove from argument list;
290 last occurrence determines desired data model */
291 for (i=1; i < argc; i++) {
292
293 if (strcmp(argv[i], "-J-d64") == 0 || strcmp(argv[i], "-d64") == 0) {
294 wanted = 64;
295 continue;
296 }
297 if (strcmp(argv[i], "-J-d32") == 0 || strcmp(argv[i], "-d32") == 0) {
298 wanted = 32;
299 continue;
300 }
301 newargv[newargc++] = argv[i];
302
303 #ifdef JAVA_ARGS
304 if (argv[i][0] != '-')
305 continue;
306 #else
307 if (strcmp(argv[i], "-classpath") == 0 || strcmp(argv[i], "-cp") == 0) {
308 i++;
309 if (i >= argc) break;
310 newargv[newargc++] = argv[i];
311 continue;
312 }
313 if (argv[i][0] != '-') { i++; break; }
314 #endif
315 }
316
317 /* copy rest of args [i .. argc) */
318 while (i < argc) {
319 newargv[newargc++] = argv[i++];
320 }
321 newargv[newargc] = NULL;
322
323 /*
324 * newargv has all proper arguments here
325 */
326
327 argc = newargc;
328 argv = newargv;
329 }
330
331 /* If the data model is not changing, it is an error if the
332 jvmpath does not exist */
333 if (wanted == running) {
334 /* Find out where the JRE is that we will be using. */
335 if (!GetJREPath(jrepath, so_jrepath, arch, JNI_FALSE) ) {
336 fprintf(stderr, "Error: could not find Java 2 Runtime Environment.\n");
337 exit(2);
338 }
339
340 /* Find the specified JVM type */
341 if (ReadKnownVMs(jrepath, arch, JNI_FALSE) < 1) {
342 fprintf(stderr, "Error: no known VMs. (check for corrupt jvm.cfg file)\n");
343 exit(1);
344 }
345
346 jvmpath[0] = '\0';
347 jvmtype = CheckJvmType(_argcp, _argvp, JNI_FALSE);
348
349 if (!GetJVMPath(jrepath, jvmtype, jvmpath, so_jvmpath, arch )) {
350 fprintf(stderr, "Error: no `%s' JVM at `%s'.\n", jvmtype, jvmpath);
351 exit(4);
352 }
353 } else { /* do the same speculatively or exit */
354 #ifdef DUAL_MODE
355 if (running != wanted) {
356 /* Find out where the JRE is that we will be using. */
357 if (!GetJREPath(jrepath, so_jrepath, ((wanted==64)?BIG_ARCH:SMALL_ARCH), JNI_TRUE)) {
358 goto EndDataModelSpeculate;
359 }
360
361 /*
362 * Read in jvm.cfg for target data model and process vm
363 * selection options.
364 */
365 if (ReadKnownVMs(jrepath, ((wanted==64)?BIG_ARCH:SMALL_ARCH), JNI_TRUE) < 1) {
366 goto EndDataModelSpeculate;
367 }
368 jvmpath[0] = '\0';
369 jvmtype = CheckJvmType(_argcp, _argvp, JNI_TRUE);
370 /* exec child can do error checking on the existence of the path */
371 jvmpathExists = GetJVMPath(jrepath, jvmtype, jvmpath, so_jvmpath,
372 ((wanted==64)?BIG_ARCH:SMALL_ARCH));
373
374 }
375 EndDataModelSpeculate: /* give up and let other code report error message */
376 ;
377 #else
378 fprintf(stderr, "Running a %d-bit JVM is not supported on this platform.\n", wanted);
379 exit(1);
380 #endif
381 }
382
383 /*
384 * We will set the LD_LIBRARY_PATH as follows:
385 *
386 * o $JVMPATH (directory portion only)
387 * o $JRE/lib/$ARCH
388 * o $JRE/../lib/$ARCH
389 *
390 * followed by the user's previous effective LD_LIBRARY_PATH, if
391 * any.
392 */
393
394 #ifdef __sun
395 /*
396 * Starting in Solaris 7, ld.so.1 supports three LD_LIBRARY_PATH
397 * variables:
398 *
399 * 1. LD_LIBRARY_PATH -- used for 32 and 64 bit searches if
400 * data-model specific variables are not set.
401 *
402 * 2. LD_LIBRARY_PATH_64 -- overrides and replaces LD_LIBRARY_PATH
403 * for 64-bit binaries.
404 *
405 * 3. LD_LIBRARY_PATH_32 -- overrides and replaces LD_LIBRARY_PATH
406 * for 32-bit binaries.
407 *
408 * The vm uses LD_LIBRARY_PATH to set the java.library.path system
409 * property. To shield the vm from the complication of multiple
410 * LD_LIBRARY_PATH variables, if the appropriate data model
411 * specific variable is set, we will act as if LD_LIBRARY_PATH had
412 * the value of the data model specific variant and the data model
413 * specific variant will be unset. Note that the variable for the
414 * *wanted* data model must be used (if it is set), not simply the
415 * current running data model.
416 */
417
418 switch(wanted) {
419 case 0:
420 if(running == 32) {
421 dmpath = getenv("LD_LIBRARY_PATH_32");
422 wanted = 32;
423 }
424 else {
425 dmpath = getenv("LD_LIBRARY_PATH_64");
426 wanted = 64;
427 }
428 break;
429
430 case 32:
431 dmpath = getenv("LD_LIBRARY_PATH_32");
432 break;
433
434 case 64:
435 dmpath = getenv("LD_LIBRARY_PATH_64");
436 break;
437
438 default:
439 fprintf(stderr, "Improper value at line %d.", __LINE__);
440 exit(1); /* unknown value in wanted */
441 break;
442 }
443
444 /*
445 * If dmpath is NULL, the relevant data model specific variable is
446 * not set and normal LD_LIBRARY_PATH should be used.
447 */
448 if( dmpath == NULL) {
449 runpath = getenv("LD_LIBRARY_PATH");
450 }
451 else {
452 runpath = dmpath;
453 }
454 #else
455 /*
456 * If not on Solaris, assume only a single LD_LIBRARY_PATH
457 * variable.
458 */
459 runpath = getenv("LD_LIBRARY_PATH");
460 #endif /* __sun */
461
462 #ifdef __linux
463 /*
464 * On linux, if a binary is running as sgid or suid, glibc sets
465 * LD_LIBRARY_PATH to the empty string for security purposes. (In
466 * contrast, on Solaris the LD_LIBRARY_PATH variable for a
467 * privileged binary does not lose its settings; but the dynamic
468 * linker does apply more scrutiny to the path.) The launcher uses
469 * the value of LD_LIBRARY_PATH to prevent an exec loop.
470 * Therefore, if we are running sgid or suid, this function's
471 * setting of LD_LIBRARY_PATH will be ineffective and we should
472 * return from the function now. Getting the right libraries to
473 * be found must be handled through other mechanisms.
474 */
475 if((getgid() != getegid()) || (getuid() != geteuid()) ) {
476 return;
477 }
478 #endif
479
480 /* runpath contains current effective LD_LIBRARY_PATH setting */
481
482 jvmpath = strdup(jvmpath);
483 new_runpath = MemAlloc( ((runpath!=NULL)?strlen(runpath):0) +
484 2*strlen(jrepath) + 2*strlen(arch) +
485 strlen(jvmpath) + 52);
486 newpath = new_runpath + strlen("LD_LIBRARY_PATH=");
487
488
489 /*
490 * Create desired LD_LIBRARY_PATH value for target data model.
491 */
492 {
493 /* remove the name of the .so from the JVM path */
494 lastslash = strrchr(jvmpath, '/');
495 if (lastslash)
496 *lastslash = '\0';
497
498
499 /* jvmpath, ((running != wanted)?((wanted==64)?"/"BIG_ARCH:"/.."):""), */
500
501 sprintf(new_runpath, "LD_LIBRARY_PATH="
502 "%s:"
503 "%s/lib/%s:"
504 "%s/../lib/%s",
505 jvmpath,
506 #ifdef DUAL_MODE
507 jrepath, ((wanted==64)?BIG_ARCH:SMALL_ARCH),
508 jrepath, ((wanted==64)?BIG_ARCH:SMALL_ARCH)
509 #else
510 jrepath, arch,
511 jrepath, arch
512 #endif
513 );
514
515
516 /*
517 * Check to make sure that the prefix of the current path is the
518 * desired environment variable setting.
519 */
520 if (runpath != NULL &&
521 strncmp(newpath, runpath, strlen(newpath))==0 &&
522 (runpath[strlen(newpath)] == 0 || runpath[strlen(newpath)] == ':') &&
523 (running == wanted) /* data model does not have to be changed */
524 #ifdef __sun
525 && (dmpath == NULL) /* data model specific variables not set */
526 #endif
527 ) {
528
529 return;
530
531 }
532 }
533
534 /*
535 * Place the desired environment setting onto the prefix of
536 * LD_LIBRARY_PATH. Note that this prevents any possible infinite
537 * loop of execv() because we test for the prefix, above.
538 */
539 if (runpath != 0) {
540 strcat(new_runpath, ":");
541 strcat(new_runpath, runpath);
542 }
543
544 if( putenv(new_runpath) != 0) {
545 exit(1); /* problem allocating memory; LD_LIBRARY_PATH not set
546 properly */
547 }
548
549 /*
550 * Unix systems document that they look at LD_LIBRARY_PATH only
551 * once at startup, so we have to re-exec the current executable
552 * to get the changed environment variable to have an effect.
553 */
554
555 #ifdef __sun
556 /*
557 * If dmpath is not NULL, remove the data model specific string
558 * in the environment for the exec'ed child.
559 */
560
561 if( dmpath != NULL)
562 (void)UnsetEnv((wanted==32)?"LD_LIBRARY_PATH_32":"LD_LIBRARY_PATH_64");
563 #endif
564
565 newenvp = environ;
566
567 {
568 char *newexec = execname;
569 #ifdef DUAL_MODE
570 /*
571 * If the data model is being changed, the path to the
572 * executable must be updated accordingly; the executable name
573 * and directory the executable resides in are separate. In the
574 * case of 32 => 64, the new bits are assumed to reside in, e.g.
575 * "olddir/BIGARCH/execname"; in the case of 64 => 32,
576 * the bits are assumed to be in "olddir/../execname". For example,
577 *
578 * olddir/sparcv9/execname
579 * olddir/amd64/execname
580 *
581 * for Solaris SPARC and Linux amd64, respectively.
582 */
583
584 if (running != wanted) {
585 char *oldexec = strcpy(MemAlloc(strlen(execname) + 1), execname);
586 char *olddir = oldexec;
587 char *oldbase = strrchr(oldexec, '/');
588
589
590 newexec = MemAlloc(strlen(execname) + 20);
591 *oldbase++ = 0;
592 sprintf(newexec, "%s/%s/%s", olddir,
593 ((wanted==64) ? BIG_ARCH : ".."), oldbase);
594 argv[0] = newexec;
595 }
596 #endif
597
598 execve(newexec, argv, newenvp);
599 perror("execve()");
600
601 fprintf(stderr, "Error trying to exec %s.\n", newexec);
602 fprintf(stderr, "Check if file exists and permissions are set correctly.\n");
603
604 #ifdef DUAL_MODE
605 if (running != wanted) {
606 fprintf(stderr, "Failed to start a %d-bit JVM process from a %d-bit JVM.\n",
607 wanted, running);
608 # ifdef __sun
609
610 # ifdef __sparc
611 fprintf(stderr, "Verify all necessary J2SE components have been installed.\n" );
612 fprintf(stderr,
613 "(Solaris SPARC 64-bit components must be installed after 32-bit components.)\n" );
614 # else
615 fprintf(stderr, "Either 64-bit processes are not supported by this platform\n");
616 fprintf(stderr, "or the 64-bit components have not been installed.\n");
617 # endif
618 }
619 # endif
620 #endif
621
622 }
623
624 exit(1);
625 }
626
627 #else /* ifndef GAMMA */
628
629 /* gamma launcher is simpler in that it doesn't handle VM flavors, data */
630 /* model, LD_LIBRARY_PATH, etc. Assuming everything is set-up correctly */
631 /* all we need to do here is to return correct path names. See also */
632 /* GetJVMPath() and GetApplicationHome(). */
633
634 { char *arch = (char *)GetArch(); /* like sparc or sparcv9 */
635 char *p;
636
637 if (!GetJREPath(jrepath, so_jrepath, arch, JNI_FALSE) ) {
638 fprintf(stderr, "Error: could not find Java 2 Runtime Environment.\n");
639 exit(2);
640 }
641
642 if (!GetJVMPath(jrepath, NULL, jvmpath, so_jvmpath, arch )) {
643 fprintf(stderr, "Error: no JVM at `%s'.\n", jvmpath);
644 exit(4);
645 }
646 }
647
648 #endif /* ifndef GAMMA */
649 }
650
651
652 /*
653 * On Solaris VM choosing is done by the launcher (java.c).
654 */
655 static jboolean
656 GetJVMPath(const char *jrepath, const char *jvmtype,
657 char *jvmpath, jint jvmpathsize, char * arch)
658 {
659 struct stat s;
660
661 #ifndef GAMMA
662 if (strchr(jvmtype, '/')) {
663 sprintf(jvmpath, "%s/" JVM_DLL, jvmtype);
664 } else {
665 sprintf(jvmpath, "%s/lib/%s/%s/" JVM_DLL, jrepath, arch, jvmtype);
666 }
667 #else
668 /* For gamma launcher, JVM is either built-in or in the same directory. */
669 /* Either way we return "<exe_path>/libjvm.so" where <exe_path> is the */
670 /* directory where gamma launcher is located. */
671
672 char *p;
673
674 snprintf(jvmpath, jvmpathsize, "%s", GetExecname());
675 p = strrchr(jvmpath, '/');
676 if (p) {
677 /* replace executable name with libjvm.so */
678 snprintf(p + 1, jvmpathsize - (p + 1 - jvmpath), "%s", JVM_DLL);
679 } else {
680 /* this case shouldn't happen */
681 snprintf(jvmpath, jvmpathsize, "%s", JVM_DLL);
682 }
683 #endif
684
685 if (_launcher_debug)
686 printf("Does `%s' exist ... ", jvmpath);
687
688 if (stat(jvmpath, &s) == 0) {
689 if (_launcher_debug)
690 printf("yes.\n");
691 return JNI_TRUE;
692 } else {
693 if (_launcher_debug)
694 printf("no.\n");
695 return JNI_FALSE;
696 }
697 }
698
699 /*
700 * Find path to JRE based on .exe's location or registry settings.
701 */
702 static jboolean
703 GetJREPath(char *path, jint pathsize, char * arch, jboolean speculative)
704 {
705 char libjava[MAXPATHLEN];
706
707 if (GetApplicationHome(path, pathsize)) {
708 /* Is JRE co-located with the application? */
709 sprintf(libjava, "%s/lib/%s/" JAVA_DLL, path, arch);
710 if (access(libjava, F_OK) == 0) {
711 goto found;
712 }
713
714 /* Does the app ship a private JRE in <apphome>/jre directory? */
715 sprintf(libjava, "%s/jre/lib/%s/" JAVA_DLL, path, arch);
716 if (access(libjava, F_OK) == 0) {
717 strcat(path, "/jre");
718 goto found;
719 }
720 }
721
722 if (!speculative)
723 fprintf(stderr, "Error: could not find " JAVA_DLL "\n");
724 return JNI_FALSE;
725
726 found:
727 if (_launcher_debug)
728 printf("JRE path is %s\n", path);
729 return JNI_TRUE;
730 }
731
732 jboolean
733 LoadJavaVM(const char *jvmpath, InvocationFunctions *ifn)
734 {
735 #ifdef GAMMA
736 /* JVM is directly linked with gamma launcher; no dlopen() */
737 ifn->CreateJavaVM = JNI_CreateJavaVM;
738 ifn->GetDefaultJavaVMInitArgs = JNI_GetDefaultJavaVMInitArgs;
739 return JNI_TRUE;
740 #else
741 Dl_info dlinfo;
742 void *libjvm;
743
744 if (_launcher_debug) {
745 printf("JVM path is %s\n", jvmpath);
746 }
747
748 libjvm = dlopen(jvmpath, RTLD_NOW + RTLD_GLOBAL);
749 if (libjvm == NULL) {
750 #if defined(__sparc) && !defined(_LP64) /* i.e. 32-bit sparc */
751 FILE * fp;
752 Elf32_Ehdr elf_head;
753 int count;
754 int location;
755
756 fp = fopen(jvmpath, "r");
757 if(fp == NULL)
758 goto error;
759
760 /* read in elf header */
761 count = fread((void*)(&elf_head), sizeof(Elf32_Ehdr), 1, fp);
762 fclose(fp);
763 if(count < 1)
764 goto error;
765
766 /*
767 * Check for running a server vm (compiled with -xarch=v8plus)
768 * on a stock v8 processor. In this case, the machine type in
769 * the elf header would not be included the architecture list
770 * provided by the isalist command, which is turn is gotten from
771 * sysinfo. This case cannot occur on 64-bit hardware and thus
772 * does not have to be checked for in binaries with an LP64 data
773 * model.
774 */
775 if(elf_head.e_machine == EM_SPARC32PLUS) {
776 char buf[257]; /* recommended buffer size from sysinfo man
777 page */
778 long length;
779 char* location;
780
781 length = sysinfo(SI_ISALIST, buf, 257);
782 if(length > 0) {
783 location = strstr(buf, "sparcv8plus ");
784 if(location == NULL) {
785 fprintf(stderr, "SPARC V8 processor detected; Server compiler requires V9 or better.\n");
786 fprintf(stderr, "Use Client compiler on V8 processors.\n");
787 fprintf(stderr, "Could not create the Java virtual machine.\n");
788 return JNI_FALSE;
789 }
790 }
791 }
792 #endif
793 fprintf(stderr, "dl failure on line %d", __LINE__);
794 goto error;
795 }
796
797 ifn->CreateJavaVM = (CreateJavaVM_t)
798 dlsym(libjvm, "JNI_CreateJavaVM");
799 if (ifn->CreateJavaVM == NULL)
800 goto error;
801
802 ifn->GetDefaultJavaVMInitArgs = (GetDefaultJavaVMInitArgs_t)
803 dlsym(libjvm, "JNI_GetDefaultJavaVMInitArgs");
804 if (ifn->GetDefaultJavaVMInitArgs == NULL)
805 goto error;
806
807 return JNI_TRUE;
808
809 error:
810 fprintf(stderr, "Error: failed %s, because %s\n", jvmpath, dlerror());
811 return JNI_FALSE;
812 #endif /* GAMMA */
813 }
814
815 /*
816 * Get the path to the file that has the usage message for -X options.
817 */
818 void
819 GetXUsagePath(char *buf, jint bufsize)
820 {
821 static const char Xusage_txt[] = "/Xusage.txt";
822 Dl_info dlinfo;
823
824 /* we use RTLD_NOW because of problems with ld.so.1 and green threads */
825 dladdr(dlsym(dlopen(JVM_DLL, RTLD_NOW), "JNI_CreateJavaVM"), &dlinfo);
826 strncpy(buf, (char *)dlinfo.dli_fname, bufsize - sizeof(Xusage_txt));
827
828 buf[bufsize-1] = '\0';
829 strcpy(strrchr(buf, '/'), Xusage_txt);
830 }
831
832 /*
833 * If app is "/foo/bin/javac", or "/foo/bin/sparcv9/javac" then put
834 * "/foo" into buf.
835 */
836 jboolean
837 GetApplicationHome(char *buf, jint bufsize)
838 {
839 #ifdef __linux__
840 char *execname = GetExecname();
841 if (execname) {
842 strncpy(buf, execname, bufsize-1);
843 buf[bufsize-1] = '\0';
844 } else {
845 return JNI_FALSE;
846 }
847 #else
848 Dl_info dlinfo;
849
850 dladdr((void *)GetApplicationHome, &dlinfo);
851 if (realpath(dlinfo.dli_fname, buf) == NULL) {
852 fprintf(stderr, "Error: realpath(`%s') failed.\n", dlinfo.dli_fname);
853 return JNI_FALSE;
854 }
855 #endif
856
857 #ifdef GAMMA
858 {
859 /* gamma launcher uses JAVA_HOME environment variable to find JDK/JRE */
860 char* java_home_var = getenv("JAVA_HOME");
861 if (java_home_var == NULL) {
862 printf("JAVA_HOME must point to a valid JDK/JRE to run gamma\n");
863 return JNI_FALSE;
864 }
865 snprintf(buf, bufsize, "%s", java_home_var);
866 }
867 #else
868 if (strrchr(buf, '/') == 0) {
869 buf[0] = '\0';
870 return JNI_FALSE;
871 }
872 *(strrchr(buf, '/')) = '\0'; /* executable file */
873 if (strlen(buf) < 4 || strrchr(buf, '/') == 0) {
874 buf[0] = '\0';
875 return JNI_FALSE;
876 }
877 if (strcmp("/bin", buf + strlen(buf) - 4) != 0)
878 *(strrchr(buf, '/')) = '\0'; /* sparcv9 or amd64 */
879 if (strlen(buf) < 4 || strcmp("/bin", buf + strlen(buf) - 4) != 0) {
880 buf[0] = '\0';
881 return JNI_FALSE;
882 }
883 *(strrchr(buf, '/')) = '\0'; /* bin */
884 #endif /* GAMMA */
885
886 return JNI_TRUE;
887 }
888
889
890 /*
891 * Return true if the named program exists
892 */
893 static int
894 ProgramExists(char *name)
895 {
896 struct stat sb;
897 if (stat(name, &sb) != 0) return 0;
898 if (S_ISDIR(sb.st_mode)) return 0;
899 return (sb.st_mode & S_IEXEC) != 0;
900 }
901
902
903 /*
904 * Find a command in a directory, returning the path.
905 */
906 static char *
907 Resolve(char *indir, char *cmd)
908 {
909 char name[PATH_MAX + 2], *real;
910
911 if ((strlen(indir) + strlen(cmd) + 1) > PATH_MAX) return 0;
912 sprintf(name, "%s%c%s", indir, FILE_SEPARATOR, cmd);
913 if (!ProgramExists(name)) return 0;
914 real = MemAlloc(PATH_MAX + 2);
915 if (!realpath(name, real))
916 strcpy(real, name);
917 return real;
918 }
919
920
921 /*
922 * Find a path for the executable
923 */
924 static char *
925 FindExecName(char *program)
926 {
927 char cwdbuf[PATH_MAX+2];
928 char *path;
929 char *tmp_path;
930 char *f;
931 char *result = NULL;
932
933 /* absolute path? */
934 if (*program == FILE_SEPARATOR ||
935 (FILE_SEPARATOR=='\\' && strrchr(program, ':')))
936 return Resolve("", program+1);
937
938 /* relative path? */
939 if (strrchr(program, FILE_SEPARATOR) != 0) {
940 char buf[PATH_MAX+2];
941 return Resolve(getcwd(cwdbuf, sizeof(cwdbuf)), program);
942 }
943
944 /* from search path? */
945 path = getenv("PATH");
946 if (!path || !*path) path = ".";
947 tmp_path = MemAlloc(strlen(path) + 2);
948 strcpy(tmp_path, path);
949
950 for (f=tmp_path; *f && result==0; ) {
951 char *s = f;
952 while (*f && (*f != PATH_SEPARATOR)) ++f;
953 if (*f) *f++ = 0;
954 if (*s == FILE_SEPARATOR)
955 result = Resolve(s, program);
956 else {
957 /* relative path element */
958 char dir[2*PATH_MAX];
959 sprintf(dir, "%s%c%s", getcwd(cwdbuf, sizeof(cwdbuf)),
960 FILE_SEPARATOR, s);
961 result = Resolve(dir, program);
962 }
963 if (result != 0) break;
964 }
965
966 free(tmp_path);
967 return result;
968 }
969
970
971 /* Store the name of the executable once computed */
972 static char *execname = NULL;
973
974 /*
975 * Compute the name of the executable
976 *
977 * In order to re-exec securely we need the absolute path of the
978 * executable. On Solaris getexecname(3c) may not return an absolute
979 * path so we use dladdr to get the filename of the executable and
980 * then use realpath to derive an absolute path. From Solaris 9
981 * onwards the filename returned in DL_info structure from dladdr is
982 * an absolute pathname so technically realpath isn't required.
983 * On Linux we read the executable name from /proc/self/exe.
984 * As a fallback, and for platforms other than Solaris and Linux,
985 * we use FindExecName to compute the executable name.
986 */
987 static char *
988 SetExecname(char **argv)
989 {
990 char* exec_path = NULL;
991
992 if (execname != NULL) /* Already determined */
993 return (execname);
994
995 #if defined(__sun)
996 {
997 Dl_info dlinfo;
998 if (dladdr((void*)&SetExecname, &dlinfo)) {
999 char *resolved = (char*)MemAlloc(PATH_MAX+1);
1000 if (resolved != NULL) {
1001 exec_path = realpath(dlinfo.dli_fname, resolved);
1002 if (exec_path == NULL) {
1003 free(resolved);
1004 }
1005 }
1006 }
1007 }
1008 #elif defined(__linux__)
1009 {
1010 const char* self = "/proc/self/exe";
1011 char buf[PATH_MAX+1];
1012 int len = readlink(self, buf, PATH_MAX);
1013 if (len >= 0) {
1014 buf[len] = '\0'; /* readlink doesn't nul terminate */
1015 exec_path = strdup(buf);
1016 }
1017 }
1018 #else /* !__sun && !__linux */
1019 {
1020 /* Not implemented */
1021 }
1022 #endif
1023
1024 if (exec_path == NULL) {
1025 exec_path = FindExecName(argv[0]);
1026 }
1027 execname = exec_path;
1028 return exec_path;
1029 }
1030
1031 /*
1032 * Return the name of the executable. Used in java_md.c to find the JRE area.
1033 */
1034 static char *
1035 GetExecname() {
1036 return execname;
1037 }
1038
1039 void ReportErrorMessage(char * message, jboolean always) {
1040 if (always) {
1041 fprintf(stderr, "%s\n", message);
1042 }
1043 }
1044
1045 void ReportErrorMessage2(char * format, char * string, jboolean always) {
1046 if (always) {
1047 fprintf(stderr, format, string);
1048 fprintf(stderr, "\n");
1049 }
1050 }
1051
1052 void ReportExceptionDescription(JNIEnv * env) {
1053 (*env)->ExceptionDescribe(env);
1054 }
1055
1056 /*
1057 * Return JNI_TRUE for an option string that has no effect but should
1058 * _not_ be passed on to the vm; return JNI_FALSE otherwise. On
1059 * Solaris SPARC, this screening needs to be done if:
1060 * 1) LD_LIBRARY_PATH does _not_ need to be reset and
1061 * 2) -d32 or -d64 is passed to a binary with a matching data model
1062 * (the exec in SetLibraryPath removes -d<n> options and points the
1063 * exec to the proper binary). When this exec is not done, these options
1064 * would end up getting passed onto the vm.
1065 */
1066 jboolean RemovableMachineDependentOption(char * option) {
1067 /*
1068 * Unconditionally remove both -d32 and -d64 options since only
1069 * the last such options has an effect; e.g.
1070 * java -d32 -d64 -d32 -version
1071 * is equivalent to
1072 * java -d32 -version
1073 */
1074
1075 if( (strcmp(option, "-d32") == 0 ) ||
1076 (strcmp(option, "-d64") == 0 ))
1077 return JNI_TRUE;
1078 else
1079 return JNI_FALSE;
1080 }
1081
1082 void PrintMachineDependentOptions() {
1083 fprintf(stdout,
1084 " -d32 use a 32-bit data model if available\n"
1085 "\n"
1086 " -d64 use a 64-bit data model if available\n");
1087 return;
1088 }
1089
1090 #ifndef GAMMA /* gamma launcher does not have ergonomics */
1091
1092 /*
1093 * The following methods (down to ServerClassMachine()) answer
1094 * the question about whether a machine is a "server-class"
1095 * machine. A server-class machine is loosely defined as one
1096 * with 2 or more processors and 2 gigabytes or more physical
1097 * memory. The definition of a processor is a physical package,
1098 * not a hyperthreaded chip masquerading as a multi-processor.
1099 * The definition of memory is also somewhat fuzzy, since x86
1100 * machines seem not to report all the memory in their DIMMs, we
1101 * think because of memory mapping of graphics cards, etc.
1102 *
1103 * This code is somewhat more confused with #ifdef's than we'd
1104 * like because this file is used by both Solaris and Linux
1105 * platforms, and so needs to be parameterized for SPARC and
1106 * i586 hardware. The other Linux platforms (amd64 and ia64)
1107 * don't even ask this question, because they only come with
1108 * server JVMs. */
1109
1110 # define KB (1024UL)
1111 # define MB (1024UL * KB)
1112 # define GB (1024UL * MB)
1113
1114 /* Compute physical memory by asking the OS */
1115 uint64_t
1116 physical_memory(void) {
1117 const uint64_t pages = (uint64_t) sysconf(_SC_PHYS_PAGES);
1118 const uint64_t page_size = (uint64_t) sysconf(_SC_PAGESIZE);
1119 const uint64_t result = pages * page_size;
1120 # define UINT64_FORMAT "%" PRIu64
1121
1122 if (_launcher_debug) {
1123 printf("pages: " UINT64_FORMAT
1124 " page_size: " UINT64_FORMAT
1125 " physical memory: " UINT64_FORMAT " (%.3fGB)\n",
1126 pages, page_size, result, result / (double) GB);
1127 }
1128 return result;
1129 }
1130
1131 #if defined(__sun) && defined(__sparc)
1132
1133 /* Methods for solaris-sparc: these are easy. */
1134
1135 /* Ask the OS how many processors there are. */
1136 unsigned long
1137 physical_processors(void) {
1138 const unsigned long sys_processors = sysconf(_SC_NPROCESSORS_CONF);
1139
1140 if (_launcher_debug) {
1141 printf("sysconf(_SC_NPROCESSORS_CONF): %lu\n", sys_processors);
1142 }
1143 return sys_processors;
1144 }
1145
1146 /* The solaris-sparc version of the "server-class" predicate. */
1147 jboolean
1148 solaris_sparc_ServerClassMachine(void) {
1149 jboolean result = JNI_FALSE;
1150 /* How big is a server class machine? */
1151 const unsigned long server_processors = 2UL;
1152 const uint64_t server_memory = 2UL * GB;
1153 const uint64_t actual_memory = physical_memory();
1154
1155 /* Is this a server class machine? */
1156 if (actual_memory >= server_memory) {
1157 const unsigned long actual_processors = physical_processors();
1158 if (actual_processors >= server_processors) {
1159 result = JNI_TRUE;
1160 }
1161 }
1162 if (_launcher_debug) {
1163 printf("solaris_" ARCH "_ServerClassMachine: %s\n",
1164 (result == JNI_TRUE ? "JNI_TRUE" : "JNI_FALSE"));
1165 }
1166 return result;
1167 }
1168
1169 #endif /* __sun && __sparc */
1170
1171 #if defined(__sun) && defined(i586)
1172
1173 /*
1174 * A utility method for asking the CPU about itself.
1175 * There's a corresponding version of linux-i586
1176 * because the compilers are different.
1177 */
1178 void
1179 get_cpuid(uint32_t arg,
1180 uint32_t* eaxp,
1181 uint32_t* ebxp,
1182 uint32_t* ecxp,
1183 uint32_t* edxp) {
1184 #ifdef _LP64
1185 asm(
1186 /* rbx is a callee-saved register */
1187 " movq %rbx, %r11 \n"
1188 /* rdx and rcx are 3rd and 4th argument registers */
1189 " movq %rdx, %r10 \n"
1190 " movq %rcx, %r9 \n"
1191 " movl %edi, %eax \n"
1192 " cpuid \n"
1193 " movl %eax, (%rsi)\n"
1194 " movl %ebx, (%r10)\n"
1195 " movl %ecx, (%r9) \n"
1196 " movl %edx, (%r8) \n"
1197 /* Restore rbx */
1198 " movq %r11, %rbx");
1199 #else
1200 /* EBX is a callee-saved register */
1201 asm(" pushl %ebx");
1202 /* Need ESI for storing through arguments */
1203 asm(" pushl %esi");
1204 asm(" movl 8(%ebp), %eax \n"
1205 " cpuid \n"
1206 " movl 12(%ebp), %esi \n"
1207 " movl %eax, (%esi) \n"
1208 " movl 16(%ebp), %esi \n"
1209 " movl %ebx, (%esi) \n"
1210 " movl 20(%ebp), %esi \n"
1211 " movl %ecx, (%esi) \n"
1212 " movl 24(%ebp), %esi \n"
1213 " movl %edx, (%esi) ");
1214 /* Restore ESI and EBX */
1215 asm(" popl %esi");
1216 /* Restore EBX */
1217 asm(" popl %ebx");
1218 #endif
1219 }
1220
1221 #endif /* __sun && i586 */
1222
1223 #if defined(__linux__) && defined(i586)
1224
1225 /*
1226 * A utility method for asking the CPU about itself.
1227 * There's a corresponding version of solaris-i586
1228 * because the compilers are different.
1229 */
1230 void
1231 get_cpuid(uint32_t arg,
1232 uint32_t* eaxp,
1233 uint32_t* ebxp,
1234 uint32_t* ecxp,
1235 uint32_t* edxp) {
1236 #ifdef _LP64
1237 __asm__ volatile (/* Instructions */
1238 " movl %4, %%eax \n"
1239 " cpuid \n"
1240 " movl %%eax, (%0)\n"
1241 " movl %%ebx, (%1)\n"
1242 " movl %%ecx, (%2)\n"
1243 " movl %%edx, (%3)\n"
1244 : /* Outputs */
1245 : /* Inputs */
1246 "r" (eaxp),
1247 "r" (ebxp),
1248 "r" (ecxp),
1249 "r" (edxp),
1250 "r" (arg)
1251 : /* Clobbers */
1252 "%rax", "%rbx", "%rcx", "%rdx", "memory"
1253 );
1254 #else
1255 uint32_t value_of_eax = 0;
1256 uint32_t value_of_ebx = 0;
1257 uint32_t value_of_ecx = 0;
1258 uint32_t value_of_edx = 0;
1259 __asm__ volatile (/* Instructions */
1260 /* ebx is callee-save, so push it */
1261 /* even though it's in the clobbers section */
1262 " pushl %%ebx \n"
1263 " movl %4, %%eax \n"
1264 " cpuid \n"
1265 " movl %%eax, %0 \n"
1266 " movl %%ebx, %1 \n"
1267 " movl %%ecx, %2 \n"
1268 " movl %%edx, %3 \n"
1269 /* restore ebx */
1270 " popl %%ebx \n"
1271
1272 : /* Outputs */
1273 "=m" (value_of_eax),
1274 "=m" (value_of_ebx),
1275 "=m" (value_of_ecx),
1276 "=m" (value_of_edx)
1277 : /* Inputs */
1278 "m" (arg)
1279 : /* Clobbers */
1280 "%eax", "%ebx", "%ecx", "%edx"
1281 );
1282 *eaxp = value_of_eax;
1283 *ebxp = value_of_ebx;
1284 *ecxp = value_of_ecx;
1285 *edxp = value_of_edx;
1286 #endif
1287 }
1288
1289 #endif /* __linux__ && i586 */
1290
1291 #ifdef i586
1292 /*
1293 * Routines shared by solaris-i586 and linux-i586.
1294 */
1295
1296 enum HyperThreadingSupport_enum {
1297 hts_supported = 1,
1298 hts_too_soon_to_tell = 0,
1299 hts_not_supported = -1,
1300 hts_not_pentium4 = -2,
1301 hts_not_intel = -3
1302 };
1303 typedef enum HyperThreadingSupport_enum HyperThreadingSupport;
1304
1305 /* Determine if hyperthreading is supported */
1306 HyperThreadingSupport
1307 hyperthreading_support(void) {
1308 HyperThreadingSupport result = hts_too_soon_to_tell;
1309 /* Bits 11 through 8 is family processor id */
1310 # define FAMILY_ID_SHIFT 8
1311 # define FAMILY_ID_MASK 0xf
1312 /* Bits 23 through 20 is extended family processor id */
1313 # define EXT_FAMILY_ID_SHIFT 20
1314 # define EXT_FAMILY_ID_MASK 0xf
1315 /* Pentium 4 family processor id */
1316 # define PENTIUM4_FAMILY_ID 0xf
1317 /* Bit 28 indicates Hyper-Threading Technology support */
1318 # define HT_BIT_SHIFT 28
1319 # define HT_BIT_MASK 1
1320 uint32_t vendor_id[3] = { 0U, 0U, 0U };
1321 uint32_t value_of_eax = 0U;
1322 uint32_t value_of_edx = 0U;
1323 uint32_t dummy = 0U;
1324
1325 /* Yes, this is supposed to be [0], [2], [1] */
1326 get_cpuid(0, &dummy, &vendor_id[0], &vendor_id[2], &vendor_id[1]);
1327 if (_launcher_debug) {
1328 printf("vendor: %c %c %c %c %c %c %c %c %c %c %c %c \n",
1329 ((vendor_id[0] >> 0) & 0xff),
1330 ((vendor_id[0] >> 8) & 0xff),
1331 ((vendor_id[0] >> 16) & 0xff),
1332 ((vendor_id[0] >> 24) & 0xff),
1333 ((vendor_id[1] >> 0) & 0xff),
1334 ((vendor_id[1] >> 8) & 0xff),
1335 ((vendor_id[1] >> 16) & 0xff),
1336 ((vendor_id[1] >> 24) & 0xff),
1337 ((vendor_id[2] >> 0) & 0xff),
1338 ((vendor_id[2] >> 8) & 0xff),
1339 ((vendor_id[2] >> 16) & 0xff),
1340 ((vendor_id[2] >> 24) & 0xff));
1341 }
1342 get_cpuid(1, &value_of_eax, &dummy, &dummy, &value_of_edx);
1343 if (_launcher_debug) {
1344 printf("value_of_eax: 0x%x value_of_edx: 0x%x\n",
1345 value_of_eax, value_of_edx);
1346 }
1347 if ((((value_of_eax >> FAMILY_ID_SHIFT) & FAMILY_ID_MASK) == PENTIUM4_FAMILY_ID) ||
1348 (((value_of_eax >> EXT_FAMILY_ID_SHIFT) & EXT_FAMILY_ID_MASK) != 0)) {
1349 if ((((vendor_id[0] >> 0) & 0xff) == 'G') &&
1350 (((vendor_id[0] >> 8) & 0xff) == 'e') &&
1351 (((vendor_id[0] >> 16) & 0xff) == 'n') &&
1352 (((vendor_id[0] >> 24) & 0xff) == 'u') &&
1353 (((vendor_id[1] >> 0) & 0xff) == 'i') &&
1354 (((vendor_id[1] >> 8) & 0xff) == 'n') &&
1355 (((vendor_id[1] >> 16) & 0xff) == 'e') &&
1356 (((vendor_id[1] >> 24) & 0xff) == 'I') &&
1357 (((vendor_id[2] >> 0) & 0xff) == 'n') &&
1358 (((vendor_id[2] >> 8) & 0xff) == 't') &&
1359 (((vendor_id[2] >> 16) & 0xff) == 'e') &&
1360 (((vendor_id[2] >> 24) & 0xff) == 'l')) {
1361 if (((value_of_edx >> HT_BIT_SHIFT) & HT_BIT_MASK) == HT_BIT_MASK) {
1362 if (_launcher_debug) {
1363 printf("Hyperthreading supported\n");
1364 }
1365 result = hts_supported;
1366 } else {
1367 if (_launcher_debug) {
1368 printf("Hyperthreading not supported\n");
1369 }
1370 result = hts_not_supported;
1371 }
1372 } else {
1373 if (_launcher_debug) {
1374 printf("Not GenuineIntel\n");
1375 }
1376 result = hts_not_intel;
1377 }
1378 } else {
1379 if (_launcher_debug) {
1380 printf("not Pentium 4 or extended\n");
1381 }
1382 result = hts_not_pentium4;
1383 }
1384 return result;
1385 }
1386
1387 /* Determine how many logical processors there are per CPU */
1388 unsigned int
1389 logical_processors_per_package(void) {
1390 /*
1391 * After CPUID with EAX==1, register EBX bits 23 through 16
1392 * indicate the number of logical processors per package
1393 */
1394 # define NUM_LOGICAL_SHIFT 16
1395 # define NUM_LOGICAL_MASK 0xff
1396 unsigned int result = 1U;
1397 const HyperThreadingSupport hyperthreading = hyperthreading_support();
1398
1399 if (hyperthreading == hts_supported) {
1400 uint32_t value_of_ebx = 0U;
1401 uint32_t dummy = 0U;
1402
1403 get_cpuid(1, &dummy, &value_of_ebx, &dummy, &dummy);
1404 result = (value_of_ebx >> NUM_LOGICAL_SHIFT) & NUM_LOGICAL_MASK;
1405 if (_launcher_debug) {
1406 printf("logical processors per package: %u\n", result);
1407 }
1408 }
1409 return result;
1410 }
1411
1412 /* Compute the number of physical processors, not logical processors */
1413 unsigned long
1414 physical_processors(void) {
1415 const long sys_processors = sysconf(_SC_NPROCESSORS_CONF);
1416 unsigned long result = sys_processors;
1417
1418 if (_launcher_debug) {
1419 printf("sysconf(_SC_NPROCESSORS_CONF): %lu\n", sys_processors);
1420 }
1421 if (sys_processors > 1) {
1422 unsigned int logical_processors = logical_processors_per_package();
1423 if (logical_processors > 1) {
1424 result = (unsigned long) sys_processors / logical_processors;
1425 }
1426 }
1427 if (_launcher_debug) {
1428 printf("physical processors: %lu\n", result);
1429 }
1430 return result;
1431 }
1432
1433 #endif /* i586 */
1434
1435 #if defined(__sun) && defined(i586)
1436
1437 /* The definition of a server-class machine for solaris-i586/amd64 */
1438 jboolean
1439 solaris_i586_ServerClassMachine(void) {
1440 jboolean result = JNI_FALSE;
1441 /* How big is a server class machine? */
1442 const unsigned long server_processors = 2UL;
1443 const uint64_t server_memory = 2UL * GB;
1444 /*
1445 * We seem not to get our full complement of memory.
1446 * We allow some part (1/8?) of the memory to be "missing",
1447 * based on the sizes of DIMMs, and maybe graphics cards.
1448 */
1449 const uint64_t missing_memory = 256UL * MB;
1450 const uint64_t actual_memory = physical_memory();
1451
1452 /* Is this a server class machine? */
1453 if (actual_memory >= (server_memory - missing_memory)) {
1454 const unsigned long actual_processors = physical_processors();
1455 if (actual_processors >= server_processors) {
1456 result = JNI_TRUE;
1457 }
1458 }
1459 if (_launcher_debug) {
1460 printf("solaris_" ARCH "_ServerClassMachine: %s\n",
1461 (result == JNI_TRUE ? "true" : "false"));
1462 }
1463 return result;
1464 }
1465
1466 #endif /* __sun && i586 */
1467
1468 #if defined(__linux__) && defined(i586)
1469
1470 /* The definition of a server-class machine for linux-i586 */
1471 jboolean
1472 linux_i586_ServerClassMachine(void) {
1473 jboolean result = JNI_FALSE;
1474 /* How big is a server class machine? */
1475 const unsigned long server_processors = 2UL;
1476 const uint64_t server_memory = 2UL * GB;
1477 /*
1478 * We seem not to get our full complement of memory.
1479 * We allow some part (1/8?) of the memory to be "missing",
1480 * based on the sizes of DIMMs, and maybe graphics cards.
1481 */
1482 const uint64_t missing_memory = 256UL * MB;
1483 const uint64_t actual_memory = physical_memory();
1484
1485 /* Is this a server class machine? */
1486 if (actual_memory >= (server_memory - missing_memory)) {
1487 const unsigned long actual_processors = physical_processors();
1488 if (actual_processors >= server_processors) {
1489 result = JNI_TRUE;
1490 }
1491 }
1492 if (_launcher_debug) {
1493 printf("linux_" ARCH "_ServerClassMachine: %s\n",
1494 (result == JNI_TRUE ? "true" : "false"));
1495 }
1496 return result;
1497 }
1498
1499 #endif /* __linux__ && i586 */
1500
1501 /* Dispatch to the platform-specific definition of "server-class" */
1502 jboolean
1503 ServerClassMachine(void) {
1504 jboolean result = JNI_FALSE;
1505 #if defined(__sun) && defined(__sparc)
1506 result = solaris_sparc_ServerClassMachine();
1507 #elif defined(__sun) && defined(i586)
1508 result = solaris_i586_ServerClassMachine();
1509 #elif defined(__linux__) && defined(i586)
1510 result = linux_i586_ServerClassMachine();
1511 #else
1512 if (_launcher_debug) {
1513 printf("ServerClassMachine: returns default value of %s\n",
1514 (result == JNI_TRUE ? "true" : "false"));
1515 }
1516 #endif
1517 return result;
1518 }
1519
1520 #endif /* ifndef GAMMA */
1521
1522 #ifndef GAMMA /* gamma launcher does not choose JDK/JRE/JVM */
1523
1524 /*
1525 * Since using the file system as a registry is a bit risky, perform
1526 * additional sanity checks on the identified directory to validate
1527 * it as a valid jre/sdk.
1528 *
1529 * Return 0 if the tests fail; otherwise return non-zero (true).
1530 *
1531 * Note that checking for anything more than the existence of an
1532 * executable object at bin/java relative to the path being checked
1533 * will break the regression tests.
1534 */
1535 static int
1536 CheckSanity(char *path, char *dir)
1537 {
1538 char buffer[PATH_MAX];
1539
1540 if (strlen(path) + strlen(dir) + 11 > PATH_MAX)
1541 return (0); /* Silently reject "impossibly" long paths */
1542
1543 (void)strcat(strcat(strcat(strcpy(buffer, path), "/"), dir), "/bin/java");
1544 return ((access(buffer, X_OK) == 0) ? 1 : 0);
1545 }
1546
1547 /*
1548 * Determine if there is an acceptable JRE in the directory dirname.
1549 * Upon locating the "best" one, return a fully qualified path to
1550 * it. "Best" is defined as the most advanced JRE meeting the
1551 * constraints contained in the manifest_info. If no JRE in this
1552 * directory meets the constraints, return NULL.
1553 *
1554 * Note that we don't check for errors in reading the directory
1555 * (which would be done by checking errno). This is because it
1556 * doesn't matter if we get an error reading the directory, or
1557 * we just don't find anything interesting in the directory. We
1558 * just return NULL in either case.
1559 *
1560 * The historical names of j2sdk and j2re were changed to jdk and
1561 * jre respecively as part of the 1.5 rebranding effort. Since the
1562 * former names are legacy on Linux, they must be recognized for
1563 * all time. Fortunately, this is a minor cost.
1564 */
1565 static char
1566 *ProcessDir(manifest_info *info, char *dirname)
1567 {
1568 DIR *dirp;
1569 struct dirent *dp;
1570 char *best = NULL;
1571 int offset;
1572 int best_offset = 0;
1573 char *ret_str = NULL;
1574 char buffer[PATH_MAX];
1575
1576 if ((dirp = opendir(dirname)) == NULL)
1577 return (NULL);
1578
1579 do {
1580 if ((dp = readdir(dirp)) != NULL) {
1581 offset = 0;
1582 if ((strncmp(dp->d_name, "jre", 3) == 0) ||
1583 (strncmp(dp->d_name, "jdk", 3) == 0))
1584 offset = 3;
1585 else if (strncmp(dp->d_name, "j2re", 4) == 0)
1586 offset = 4;
1587 else if (strncmp(dp->d_name, "j2sdk", 5) == 0)
1588 offset = 5;
1589 if (offset > 0) {
1590 if ((acceptable_release(dp->d_name + offset,
1591 info->jre_version)) && CheckSanity(dirname, dp->d_name))
1592 if ((best == NULL) || (exact_version_id(
1593 dp->d_name + offset, best + best_offset) > 0)) {
1594 if (best != NULL)
1595 free(best);
1596 best = strdup(dp->d_name);
1597 best_offset = offset;
1598 }
1599 }
1600 }
1601 } while (dp != NULL);
1602 (void) closedir(dirp);
1603 if (best == NULL)
1604 return (NULL);
1605 else {
1606 ret_str = MemAlloc(strlen(dirname) + strlen(best) + 2);
1607 ret_str = strcat(strcat(strcpy(ret_str, dirname), "/"), best);
1608 free(best);
1609 return (ret_str);
1610 }
1611 }
1612
1613 /*
1614 * This is the global entry point. It examines the host for the optimal
1615 * JRE to be used by scanning a set of directories. The set of directories
1616 * is platform dependent and can be overridden by the environment
1617 * variable JAVA_VERSION_PATH.
1618 *
1619 * This routine itself simply determines the set of appropriate
1620 * directories before passing control onto ProcessDir().
1621 */
1622 char*
1623 LocateJRE(manifest_info* info)
1624 {
1625 char *path;
1626 char *home;
1627 char *target = NULL;
1628 char *dp;
1629 char *cp;
1630
1631 /*
1632 * Start by getting JAVA_VERSION_PATH
1633 */
1634 if (info->jre_restrict_search)
1635 path = strdup(system_dir);
1636 else if ((path = getenv("JAVA_VERSION_PATH")) != NULL)
1637 path = strdup(path);
1638 else
1639 if ((home = getenv("HOME")) != NULL) {
1640 path = (char *)MemAlloc(strlen(home) + 13);
1641 path = strcat(strcat(strcat(strcpy(path, home),
1642 user_dir), ":"), system_dir);
1643 } else
1644 path = strdup(system_dir);
1645
1646 /*
1647 * Step through each directory on the path. Terminate the scan with
1648 * the first directory with an acceptable JRE.
1649 */
1650 cp = dp = path;
1651 while (dp != NULL) {
1652 cp = strchr(dp, (int)':');
1653 if (cp != NULL)
1654 *cp = (char)NULL;
1655 if ((target = ProcessDir(info, dp)) != NULL)
1656 break;
1657 dp = cp;
1658 if (dp != NULL)
1659 dp++;
1660 }
1661 free(path);
1662 return (target);
1663 }
1664
1665 /*
1666 * Given a path to a jre to execute, this routine checks if this process
1667 * is indeed that jre. If not, it exec's that jre.
1668 *
1669 * We want to actually check the paths rather than just the version string
1670 * built into the executable, so that given version specification (and
1671 * JAVA_VERSION_PATH) will yield the exact same Java environment, regardless
1672 * of the version of the arbitrary launcher we start with.
1673 */
1674 void
1675 ExecJRE(char *jre, char **argv)
1676 {
1677 char wanted[PATH_MAX];
1678 char *execname;
1679 char *progname;
1680
1681 /*
1682 * Resolve the real path to the directory containing the selected JRE.
1683 */
1684 if (realpath(jre, wanted) == NULL) {
1685 fprintf(stderr, "Unable to resolve %s\n", jre);
1686 exit(1);
1687 }
1688
1689 /*
1690 * Resolve the real path to the currently running launcher.
1691 */
1692 execname = SetExecname(argv);
1693 if (execname == NULL) {
1694 fprintf(stderr, "Unable to resolve current executable\n");
1695 exit(1);
1696 }
1697
1698 /*
1699 * If the path to the selected JRE directory is a match to the initial
1700 * portion of the path to the currently executing JRE, we have a winner!
1701 * If so, just return.
1702 */
1703 if (strncmp(wanted, execname, strlen(wanted)) == 0)
1704 return; /* I am the droid you were looking for */
1705
1706 /*
1707 * If this isn't the selected version, exec the selected version.
1708 */
1709 #ifdef JAVA_ARGS /* javac, jar and friends. */
1710 progname = "java";
1711 #else /* java, oldjava, javaw and friends */
1712 #ifdef PROGNAME
1713 progname = PROGNAME;
1714 #else
1715 progname = *argv;
1716 if ((s = strrchr(progname, FILE_SEPARATOR)) != 0) {
1717 progname = s + 1;
1718 }
1719 #endif /* PROGNAME */
1720 #endif /* JAVA_ARGS */
1721
1722 /*
1723 * This should never happen (because of the selection code in SelectJRE),
1724 * but check for "impossibly" long path names just because buffer overruns
1725 * can be so deadly.
1726 */
1727 if (strlen(wanted) + strlen(progname) + 6 > PATH_MAX) {
1728 fprintf(stderr, "Path length exceeds maximum length (PATH_MAX)\n");
1729 exit(1);
1730 }
1731
1732 /*
1733 * Construct the path and exec it.
1734 */
1735 (void)strcat(strcat(wanted, "/bin/"), progname);
1736 argv[0] = progname;
1737 if (_launcher_debug) {
1738 int i;
1739 printf("execv(\"%s\"", wanted);
1740 for (i = 0; argv[i] != NULL; i++)
1741 printf(", \"%s\"", argv[i]);
1742 printf(")\n");
1743 }
1744 execv(wanted, argv);
1745 fprintf(stderr, "Exec of %s failed\n", wanted);
1746 exit(1);
1747 }
1748
1749 #endif /* ifndef GAMMA */
1750
1751 /*
1752 * "Borrowed" from Solaris 10 where the unsetenv() function is being added
1753 * to libc thanks to SUSv3 (Standard Unix Specification, version 3). As
1754 * such, in the fullness of time this will appear in libc on all relevant
1755 * Solaris/Linux platforms and maybe even the Windows platform. At that
1756 * time, this stub can be removed.
1757 *
1758 * This implementation removes the environment locking for multithreaded
1759 * applications. (We don't have access to these mutexes within libc and
1760 * the launcher isn't multithreaded.) Note that what remains is platform
1761 * independent, because it only relies on attributes that a POSIX environment
1762 * defines.
1763 *
1764 * Returns 0 on success, -1 on failure.
1765 *
1766 * Also removed was the setting of errno. The only value of errno set
1767 * was EINVAL ("Invalid Argument").
1768 */
1769
1770 /*
1771 * s1(environ) is name=value
1772 * s2(name) is name(not the form of name=value).
1773 * if names match, return value of 1, else return 0
1774 */
1775 static int
1776 match_noeq(const char *s1, const char *s2)
1777 {
1778 while (*s1 == *s2++) {
1779 if (*s1++ == '=')
1780 return (1);
1781 }
1782 if (*s1 == '=' && s2[-1] == '\0')
1783 return (1);
1784 return (0);
1785 }
1786
1787 /*
1788 * added for SUSv3 standard
1789 *
1790 * Delete entry from environ.
1791 * Do not free() memory! Other threads may be using it.
1792 * Keep it around forever.
1793 */
1794 static int
1795 borrowed_unsetenv(const char *name)
1796 {
1797 long idx; /* index into environ */
1798
1799 if (name == NULL || *name == '\0' ||
1800 strchr(name, '=') != NULL) {
1801 return (-1);
1802 }
1803
1804 for (idx = 0; environ[idx] != NULL; idx++) {
1805 if (match_noeq(environ[idx], name))
1806 break;
1807 }
1808 if (environ[idx] == NULL) {
1809 /* name not found but still a success */
1810 return (0);
1811 }
1812 /* squeeze up one entry */
1813 do {
1814 environ[idx] = environ[idx+1];
1815 } while (environ[++idx] != NULL);
1816
1817 return (0);
1818 }
1819 /* --- End of "borrowed" code --- */
1820
1821 /*
1822 * Wrapper for unsetenv() function.
1823 */
1824 int
1825 UnsetEnv(char *name)
1826 {
1827 return(borrowed_unsetenv(name));
1828 }