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