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