src/os/posix/launcher/java_md.c

Tue, 21 Dec 2010 23:39:42 -0500

author
dholmes
date
Tue, 21 Dec 2010 23:39:42 -0500
changeset 2390
e58d06a8037e
parent 2369
aa6e219afbf1
child 3156
f08d439fab8c
permissions
-rw-r--r--

7008444: Remove unnecessary include of stdint.h in java_md.c
Summary: Remove unnecessary include of stdint.h in java_md.c
Reviewed-by: brutisso, kvn

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

mercurial