1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/os/linux/launcher/java_md.c Sat Dec 01 00:00:00 2007 +0000 1.3 @@ -0,0 +1,1828 @@ 1.4 +/* 1.5 + * Copyright 1999-2005 Sun Microsystems, Inc. All Rights Reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 1.23 + * CA 95054 USA or visit www.sun.com if you need additional information or 1.24 + * have any questions. 1.25 + * 1.26 + */ 1.27 + 1.28 +/* 1.29 + * Gamma (Hotspot internal engineering test) launcher based on 1.6.0-b28 JDK, 1.30 + * search "GAMMA" for gamma specific changes. 1.31 + */ 1.32 + 1.33 +#include "java.h" 1.34 +#include <dirent.h> 1.35 +#include <dlfcn.h> 1.36 +#include <fcntl.h> 1.37 +#include <inttypes.h> 1.38 +#include <stdio.h> 1.39 +#include <string.h> 1.40 +#include <stdlib.h> 1.41 +#include <limits.h> 1.42 +#include <sys/stat.h> 1.43 +#include <unistd.h> 1.44 +#include <sys/types.h> 1.45 + 1.46 +#ifndef GAMMA 1.47 +#include "manifest_info.h" 1.48 +#include "version_comp.h" 1.49 +#endif 1.50 + 1.51 +#define JVM_DLL "libjvm.so" 1.52 +#define JAVA_DLL "libjava.so" 1.53 + 1.54 +#ifndef GAMMA /* launcher.make defines ARCH */ 1.55 + 1.56 +/* 1.57 + * If a processor / os combination has the ability to run binaries of 1.58 + * two data models and cohabitation of jre/jdk bits with both data 1.59 + * models is supported, then DUAL_MODE is defined. When DUAL_MODE is 1.60 + * defined, the architecture names for the narrow and wide version of 1.61 + * the architecture are defined in BIG_ARCH and SMALL_ARCH. Currently 1.62 + * only Solaris on sparc/sparcv9 and i586/amd64 is DUAL_MODE; linux 1.63 + * i586/amd64 could be defined as DUAL_MODE but that is not the 1.64 + * current policy. 1.65 + */ 1.66 + 1.67 +#ifdef _LP64 1.68 + 1.69 +# ifdef ia64 1.70 +# define ARCH "ia64" 1.71 +# elif defined(amd64) 1.72 +# define ARCH "amd64" 1.73 +# elif defined(__sparc) 1.74 +# define ARCH "sparcv9" 1.75 +# else 1.76 +# define ARCH "unknown" /* unknown 64-bit architecture */ 1.77 +# endif 1.78 + 1.79 +#else /* 32-bit data model */ 1.80 + 1.81 +# ifdef i586 1.82 +# define ARCH "i386" 1.83 +# elif defined(__sparc) 1.84 +# define ARCH "sparc" 1.85 +# endif 1.86 + 1.87 +#endif /* _LP64 */ 1.88 + 1.89 +#ifdef __sun 1.90 +# define DUAL_MODE 1.91 +# ifdef __sparc 1.92 +# define BIG_ARCH "sparcv9" 1.93 +# define SMALL_ARCH "sparc" 1.94 +# else 1.95 +# define BIG_ARCH "amd64" 1.96 +# define SMALL_ARCH "i386" 1.97 +# endif 1.98 +# include <sys/systeminfo.h> 1.99 +# include <sys/elf.h> 1.100 +# include <stdio.h> 1.101 +#else 1.102 +# ifndef ARCH 1.103 +# include <sys/systeminfo.h> 1.104 +# endif 1.105 +#endif 1.106 + 1.107 +#endif /* ifndef GAMMA */ 1.108 + 1.109 +/* pointer to environment */ 1.110 +extern char **environ; 1.111 + 1.112 +#ifndef GAMMA 1.113 + 1.114 +/* 1.115 + * A collection of useful strings. One should think of these as #define 1.116 + * entries, but actual strings can be more efficient (with many compilers). 1.117 + */ 1.118 +#ifdef __linux__ 1.119 +static const char *system_dir = "/usr/java"; 1.120 +static const char *user_dir = "/java"; 1.121 +#else /* Solaris */ 1.122 +static const char *system_dir = "/usr/jdk"; 1.123 +static const char *user_dir = "/jdk"; 1.124 +#endif 1.125 + 1.126 +#endif /* ifndef GAMMA */ 1.127 + 1.128 +/* 1.129 + * Flowchart of launcher execs and options processing on unix 1.130 + * 1.131 + * The selection of the proper vm shared library to open depends on 1.132 + * several classes of command line options, including vm "flavor" 1.133 + * options (-client, -server) and the data model options, -d32 and 1.134 + * -d64, as well as a version specification which may have come from 1.135 + * the command line or from the manifest of an executable jar file. 1.136 + * The vm selection options are not passed to the running 1.137 + * virtual machine; they must be screened out by the launcher. 1.138 + * 1.139 + * The version specification (if any) is processed first by the 1.140 + * platform independent routine SelectVersion. This may result in 1.141 + * the exec of the specified launcher version. 1.142 + * 1.143 + * Typically, the launcher execs at least once to ensure a suitable 1.144 + * LD_LIBRARY_PATH is in effect for the process. The first exec 1.145 + * screens out all the data model options; leaving the choice of data 1.146 + * model implicit in the binary selected to run. However, in case no 1.147 + * exec is done, the data model options are screened out before the vm 1.148 + * is invoked. 1.149 + * 1.150 + * incoming argv ------------------------------ 1.151 + * | | 1.152 + * \|/ | 1.153 + * CheckJVMType | 1.154 + * (removes -client, -server, etc.) | 1.155 + * \|/ 1.156 + * CreateExecutionEnvironment 1.157 + * (removes -d32 and -d64, 1.158 + * determines desired data model, 1.159 + * sets up LD_LIBRARY_PATH, 1.160 + * and exec's) 1.161 + * | 1.162 + * -------------------------------------------- 1.163 + * | 1.164 + * \|/ 1.165 + * exec child 1 incoming argv ----------------- 1.166 + * | | 1.167 + * \|/ | 1.168 + * CheckJVMType | 1.169 + * (removes -client, -server, etc.) | 1.170 + * | \|/ 1.171 + * | CreateExecutionEnvironment 1.172 + * | (verifies desired data model 1.173 + * | is running and acceptable 1.174 + * | LD_LIBRARY_PATH; 1.175 + * | no-op in child) 1.176 + * | 1.177 + * \|/ 1.178 + * TranslateDashJArgs... 1.179 + * (Prepare to pass args to vm) 1.180 + * | 1.181 + * | 1.182 + * | 1.183 + * \|/ 1.184 + * ParseArguments 1.185 + * (ignores -d32 and -d64, 1.186 + * processes version options, 1.187 + * creates argument list for vm, 1.188 + * etc.) 1.189 + * 1.190 + */ 1.191 + 1.192 +static char *SetExecname(char **argv); 1.193 +static char * GetExecname(); 1.194 +static jboolean GetJVMPath(const char *jrepath, const char *jvmtype, 1.195 + char *jvmpath, jint jvmpathsize, char * arch); 1.196 +static jboolean GetJREPath(char *path, jint pathsize, char * arch, jboolean speculative); 1.197 + 1.198 +const char * 1.199 +GetArch() 1.200 +{ 1.201 + static char *arch = NULL; 1.202 + static char buf[12]; 1.203 + if (arch) { 1.204 + return arch; 1.205 + } 1.206 + 1.207 +#ifdef ARCH 1.208 + strcpy(buf, ARCH); 1.209 +#else 1.210 + sysinfo(SI_ARCHITECTURE, buf, sizeof(buf)); 1.211 +#endif 1.212 + arch = buf; 1.213 + return arch; 1.214 +} 1.215 + 1.216 +void 1.217 +CreateExecutionEnvironment(int *_argcp, 1.218 + char ***_argvp, 1.219 + char jrepath[], 1.220 + jint so_jrepath, 1.221 + char jvmpath[], 1.222 + jint so_jvmpath, 1.223 + char **original_argv) { 1.224 + /* 1.225 + * First, determine if we are running the desired data model. If we 1.226 + * are running the desired data model, all the error messages 1.227 + * associated with calling GetJREPath, ReadKnownVMs, etc. should be 1.228 + * output. However, if we are not running the desired data model, 1.229 + * some of the errors should be suppressed since it is more 1.230 + * informative to issue an error message based on whether or not the 1.231 + * os/processor combination has dual mode capabilities. 1.232 + */ 1.233 + 1.234 + char *execname = NULL; 1.235 + int original_argc = *_argcp; 1.236 + jboolean jvmpathExists; 1.237 + 1.238 + /* Compute the name of the executable */ 1.239 + execname = SetExecname(*_argvp); 1.240 + 1.241 +#ifndef GAMMA 1.242 + /* Set the LD_LIBRARY_PATH environment variable, check data model 1.243 + flags, and exec process, if needed */ 1.244 + { 1.245 + char *arch = (char *)GetArch(); /* like sparc or sparcv9 */ 1.246 + char * jvmtype = NULL; 1.247 + int argc = *_argcp; 1.248 + char **argv = original_argv; 1.249 + 1.250 + char *runpath = NULL; /* existing effective LD_LIBRARY_PATH 1.251 + setting */ 1.252 + 1.253 + int running = /* What data model is being ILP32 => 1.254 + 32 bit vm; LP64 => 64 bit vm */ 1.255 +#ifdef _LP64 1.256 + 64; 1.257 +#else 1.258 + 32; 1.259 +#endif 1.260 + 1.261 + int wanted = running; /* What data mode is being 1.262 + asked for? Current model is 1.263 + fine unless another model 1.264 + is asked for */ 1.265 + 1.266 + char* new_runpath = NULL; /* desired new LD_LIBRARY_PATH string */ 1.267 + char* newpath = NULL; /* path on new LD_LIBRARY_PATH */ 1.268 + char* lastslash = NULL; 1.269 + 1.270 + char** newenvp = NULL; /* current environment */ 1.271 + 1.272 + char** newargv = NULL; 1.273 + int newargc = 0; 1.274 +#ifdef __sun 1.275 + char* dmpath = NULL; /* data model specific LD_LIBRARY_PATH, 1.276 + Solaris only */ 1.277 +#endif 1.278 + 1.279 + /* 1.280 + * Starting in 1.5, all unix platforms accept the -d32 and -d64 1.281 + * options. On platforms where only one data-model is supported 1.282 + * (e.g. ia-64 Linux), using the flag for the other data model is 1.283 + * an error and will terminate the program. 1.284 + */ 1.285 + 1.286 + { /* open new scope to declare local variables */ 1.287 + int i; 1.288 + 1.289 + newargv = (char **)MemAlloc((argc+1) * sizeof(*newargv)); 1.290 + newargv[newargc++] = argv[0]; 1.291 + 1.292 + /* scan for data model arguments and remove from argument list; 1.293 + last occurrence determines desired data model */ 1.294 + for (i=1; i < argc; i++) { 1.295 + 1.296 + if (strcmp(argv[i], "-J-d64") == 0 || strcmp(argv[i], "-d64") == 0) { 1.297 + wanted = 64; 1.298 + continue; 1.299 + } 1.300 + if (strcmp(argv[i], "-J-d32") == 0 || strcmp(argv[i], "-d32") == 0) { 1.301 + wanted = 32; 1.302 + continue; 1.303 + } 1.304 + newargv[newargc++] = argv[i]; 1.305 + 1.306 +#ifdef JAVA_ARGS 1.307 + if (argv[i][0] != '-') 1.308 + continue; 1.309 +#else 1.310 + if (strcmp(argv[i], "-classpath") == 0 || strcmp(argv[i], "-cp") == 0) { 1.311 + i++; 1.312 + if (i >= argc) break; 1.313 + newargv[newargc++] = argv[i]; 1.314 + continue; 1.315 + } 1.316 + if (argv[i][0] != '-') { i++; break; } 1.317 +#endif 1.318 + } 1.319 + 1.320 + /* copy rest of args [i .. argc) */ 1.321 + while (i < argc) { 1.322 + newargv[newargc++] = argv[i++]; 1.323 + } 1.324 + newargv[newargc] = NULL; 1.325 + 1.326 + /* 1.327 + * newargv has all proper arguments here 1.328 + */ 1.329 + 1.330 + argc = newargc; 1.331 + argv = newargv; 1.332 + } 1.333 + 1.334 + /* If the data model is not changing, it is an error if the 1.335 + jvmpath does not exist */ 1.336 + if (wanted == running) { 1.337 + /* Find out where the JRE is that we will be using. */ 1.338 + if (!GetJREPath(jrepath, so_jrepath, arch, JNI_FALSE) ) { 1.339 + fprintf(stderr, "Error: could not find Java 2 Runtime Environment.\n"); 1.340 + exit(2); 1.341 + } 1.342 + 1.343 + /* Find the specified JVM type */ 1.344 + if (ReadKnownVMs(jrepath, arch, JNI_FALSE) < 1) { 1.345 + fprintf(stderr, "Error: no known VMs. (check for corrupt jvm.cfg file)\n"); 1.346 + exit(1); 1.347 + } 1.348 + 1.349 + jvmpath[0] = '\0'; 1.350 + jvmtype = CheckJvmType(_argcp, _argvp, JNI_FALSE); 1.351 + 1.352 + if (!GetJVMPath(jrepath, jvmtype, jvmpath, so_jvmpath, arch )) { 1.353 + fprintf(stderr, "Error: no `%s' JVM at `%s'.\n", jvmtype, jvmpath); 1.354 + exit(4); 1.355 + } 1.356 + } else { /* do the same speculatively or exit */ 1.357 +#ifdef DUAL_MODE 1.358 + if (running != wanted) { 1.359 + /* Find out where the JRE is that we will be using. */ 1.360 + if (!GetJREPath(jrepath, so_jrepath, ((wanted==64)?BIG_ARCH:SMALL_ARCH), JNI_TRUE)) { 1.361 + goto EndDataModelSpeculate; 1.362 + } 1.363 + 1.364 + /* 1.365 + * Read in jvm.cfg for target data model and process vm 1.366 + * selection options. 1.367 + */ 1.368 + if (ReadKnownVMs(jrepath, ((wanted==64)?BIG_ARCH:SMALL_ARCH), JNI_TRUE) < 1) { 1.369 + goto EndDataModelSpeculate; 1.370 + } 1.371 + jvmpath[0] = '\0'; 1.372 + jvmtype = CheckJvmType(_argcp, _argvp, JNI_TRUE); 1.373 + /* exec child can do error checking on the existence of the path */ 1.374 + jvmpathExists = GetJVMPath(jrepath, jvmtype, jvmpath, so_jvmpath, 1.375 + ((wanted==64)?BIG_ARCH:SMALL_ARCH)); 1.376 + 1.377 + } 1.378 + EndDataModelSpeculate: /* give up and let other code report error message */ 1.379 + ; 1.380 +#else 1.381 + fprintf(stderr, "Running a %d-bit JVM is not supported on this platform.\n", wanted); 1.382 + exit(1); 1.383 +#endif 1.384 + } 1.385 + 1.386 + /* 1.387 + * We will set the LD_LIBRARY_PATH as follows: 1.388 + * 1.389 + * o $JVMPATH (directory portion only) 1.390 + * o $JRE/lib/$ARCH 1.391 + * o $JRE/../lib/$ARCH 1.392 + * 1.393 + * followed by the user's previous effective LD_LIBRARY_PATH, if 1.394 + * any. 1.395 + */ 1.396 + 1.397 +#ifdef __sun 1.398 + /* 1.399 + * Starting in Solaris 7, ld.so.1 supports three LD_LIBRARY_PATH 1.400 + * variables: 1.401 + * 1.402 + * 1. LD_LIBRARY_PATH -- used for 32 and 64 bit searches if 1.403 + * data-model specific variables are not set. 1.404 + * 1.405 + * 2. LD_LIBRARY_PATH_64 -- overrides and replaces LD_LIBRARY_PATH 1.406 + * for 64-bit binaries. 1.407 + * 1.408 + * 3. LD_LIBRARY_PATH_32 -- overrides and replaces LD_LIBRARY_PATH 1.409 + * for 32-bit binaries. 1.410 + * 1.411 + * The vm uses LD_LIBRARY_PATH to set the java.library.path system 1.412 + * property. To shield the vm from the complication of multiple 1.413 + * LD_LIBRARY_PATH variables, if the appropriate data model 1.414 + * specific variable is set, we will act as if LD_LIBRARY_PATH had 1.415 + * the value of the data model specific variant and the data model 1.416 + * specific variant will be unset. Note that the variable for the 1.417 + * *wanted* data model must be used (if it is set), not simply the 1.418 + * current running data model. 1.419 + */ 1.420 + 1.421 + switch(wanted) { 1.422 + case 0: 1.423 + if(running == 32) { 1.424 + dmpath = getenv("LD_LIBRARY_PATH_32"); 1.425 + wanted = 32; 1.426 + } 1.427 + else { 1.428 + dmpath = getenv("LD_LIBRARY_PATH_64"); 1.429 + wanted = 64; 1.430 + } 1.431 + break; 1.432 + 1.433 + case 32: 1.434 + dmpath = getenv("LD_LIBRARY_PATH_32"); 1.435 + break; 1.436 + 1.437 + case 64: 1.438 + dmpath = getenv("LD_LIBRARY_PATH_64"); 1.439 + break; 1.440 + 1.441 + default: 1.442 + fprintf(stderr, "Improper value at line %d.", __LINE__); 1.443 + exit(1); /* unknown value in wanted */ 1.444 + break; 1.445 + } 1.446 + 1.447 + /* 1.448 + * If dmpath is NULL, the relevant data model specific variable is 1.449 + * not set and normal LD_LIBRARY_PATH should be used. 1.450 + */ 1.451 + if( dmpath == NULL) { 1.452 + runpath = getenv("LD_LIBRARY_PATH"); 1.453 + } 1.454 + else { 1.455 + runpath = dmpath; 1.456 + } 1.457 +#else 1.458 + /* 1.459 + * If not on Solaris, assume only a single LD_LIBRARY_PATH 1.460 + * variable. 1.461 + */ 1.462 + runpath = getenv("LD_LIBRARY_PATH"); 1.463 +#endif /* __sun */ 1.464 + 1.465 +#ifdef __linux 1.466 + /* 1.467 + * On linux, if a binary is running as sgid or suid, glibc sets 1.468 + * LD_LIBRARY_PATH to the empty string for security purposes. (In 1.469 + * contrast, on Solaris the LD_LIBRARY_PATH variable for a 1.470 + * privileged binary does not lose its settings; but the dynamic 1.471 + * linker does apply more scrutiny to the path.) The launcher uses 1.472 + * the value of LD_LIBRARY_PATH to prevent an exec loop. 1.473 + * Therefore, if we are running sgid or suid, this function's 1.474 + * setting of LD_LIBRARY_PATH will be ineffective and we should 1.475 + * return from the function now. Getting the right libraries to 1.476 + * be found must be handled through other mechanisms. 1.477 + */ 1.478 + if((getgid() != getegid()) || (getuid() != geteuid()) ) { 1.479 + return; 1.480 + } 1.481 +#endif 1.482 + 1.483 + /* runpath contains current effective LD_LIBRARY_PATH setting */ 1.484 + 1.485 + jvmpath = strdup(jvmpath); 1.486 + new_runpath = MemAlloc( ((runpath!=NULL)?strlen(runpath):0) + 1.487 + 2*strlen(jrepath) + 2*strlen(arch) + 1.488 + strlen(jvmpath) + 52); 1.489 + newpath = new_runpath + strlen("LD_LIBRARY_PATH="); 1.490 + 1.491 + 1.492 + /* 1.493 + * Create desired LD_LIBRARY_PATH value for target data model. 1.494 + */ 1.495 + { 1.496 + /* remove the name of the .so from the JVM path */ 1.497 + lastslash = strrchr(jvmpath, '/'); 1.498 + if (lastslash) 1.499 + *lastslash = '\0'; 1.500 + 1.501 + 1.502 + /* jvmpath, ((running != wanted)?((wanted==64)?"/"BIG_ARCH:"/.."):""), */ 1.503 + 1.504 + sprintf(new_runpath, "LD_LIBRARY_PATH=" 1.505 + "%s:" 1.506 + "%s/lib/%s:" 1.507 + "%s/../lib/%s", 1.508 + jvmpath, 1.509 +#ifdef DUAL_MODE 1.510 + jrepath, ((wanted==64)?BIG_ARCH:SMALL_ARCH), 1.511 + jrepath, ((wanted==64)?BIG_ARCH:SMALL_ARCH) 1.512 +#else 1.513 + jrepath, arch, 1.514 + jrepath, arch 1.515 +#endif 1.516 + ); 1.517 + 1.518 + 1.519 + /* 1.520 + * Check to make sure that the prefix of the current path is the 1.521 + * desired environment variable setting. 1.522 + */ 1.523 + if (runpath != NULL && 1.524 + strncmp(newpath, runpath, strlen(newpath))==0 && 1.525 + (runpath[strlen(newpath)] == 0 || runpath[strlen(newpath)] == ':') && 1.526 + (running == wanted) /* data model does not have to be changed */ 1.527 +#ifdef __sun 1.528 + && (dmpath == NULL) /* data model specific variables not set */ 1.529 +#endif 1.530 + ) { 1.531 + 1.532 + return; 1.533 + 1.534 + } 1.535 + } 1.536 + 1.537 + /* 1.538 + * Place the desired environment setting onto the prefix of 1.539 + * LD_LIBRARY_PATH. Note that this prevents any possible infinite 1.540 + * loop of execv() because we test for the prefix, above. 1.541 + */ 1.542 + if (runpath != 0) { 1.543 + strcat(new_runpath, ":"); 1.544 + strcat(new_runpath, runpath); 1.545 + } 1.546 + 1.547 + if( putenv(new_runpath) != 0) { 1.548 + exit(1); /* problem allocating memory; LD_LIBRARY_PATH not set 1.549 + properly */ 1.550 + } 1.551 + 1.552 + /* 1.553 + * Unix systems document that they look at LD_LIBRARY_PATH only 1.554 + * once at startup, so we have to re-exec the current executable 1.555 + * to get the changed environment variable to have an effect. 1.556 + */ 1.557 + 1.558 +#ifdef __sun 1.559 + /* 1.560 + * If dmpath is not NULL, remove the data model specific string 1.561 + * in the environment for the exec'ed child. 1.562 + */ 1.563 + 1.564 + if( dmpath != NULL) 1.565 + (void)UnsetEnv((wanted==32)?"LD_LIBRARY_PATH_32":"LD_LIBRARY_PATH_64"); 1.566 +#endif 1.567 + 1.568 + newenvp = environ; 1.569 + 1.570 + { 1.571 + char *newexec = execname; 1.572 +#ifdef DUAL_MODE 1.573 + /* 1.574 + * If the data model is being changed, the path to the 1.575 + * executable must be updated accordingly; the executable name 1.576 + * and directory the executable resides in are separate. In the 1.577 + * case of 32 => 64, the new bits are assumed to reside in, e.g. 1.578 + * "olddir/BIGARCH/execname"; in the case of 64 => 32, 1.579 + * the bits are assumed to be in "olddir/../execname". For example, 1.580 + * 1.581 + * olddir/sparcv9/execname 1.582 + * olddir/amd64/execname 1.583 + * 1.584 + * for Solaris SPARC and Linux amd64, respectively. 1.585 + */ 1.586 + 1.587 + if (running != wanted) { 1.588 + char *oldexec = strcpy(MemAlloc(strlen(execname) + 1), execname); 1.589 + char *olddir = oldexec; 1.590 + char *oldbase = strrchr(oldexec, '/'); 1.591 + 1.592 + 1.593 + newexec = MemAlloc(strlen(execname) + 20); 1.594 + *oldbase++ = 0; 1.595 + sprintf(newexec, "%s/%s/%s", olddir, 1.596 + ((wanted==64) ? BIG_ARCH : ".."), oldbase); 1.597 + argv[0] = newexec; 1.598 + } 1.599 +#endif 1.600 + 1.601 + execve(newexec, argv, newenvp); 1.602 + perror("execve()"); 1.603 + 1.604 + fprintf(stderr, "Error trying to exec %s.\n", newexec); 1.605 + fprintf(stderr, "Check if file exists and permissions are set correctly.\n"); 1.606 + 1.607 +#ifdef DUAL_MODE 1.608 + if (running != wanted) { 1.609 + fprintf(stderr, "Failed to start a %d-bit JVM process from a %d-bit JVM.\n", 1.610 + wanted, running); 1.611 +# ifdef __sun 1.612 + 1.613 +# ifdef __sparc 1.614 + fprintf(stderr, "Verify all necessary J2SE components have been installed.\n" ); 1.615 + fprintf(stderr, 1.616 + "(Solaris SPARC 64-bit components must be installed after 32-bit components.)\n" ); 1.617 +# else 1.618 + fprintf(stderr, "Either 64-bit processes are not supported by this platform\n"); 1.619 + fprintf(stderr, "or the 64-bit components have not been installed.\n"); 1.620 +# endif 1.621 + } 1.622 +# endif 1.623 +#endif 1.624 + 1.625 + } 1.626 + 1.627 + exit(1); 1.628 + } 1.629 + 1.630 +#else /* ifndef GAMMA */ 1.631 + 1.632 + /* gamma launcher is simpler in that it doesn't handle VM flavors, data */ 1.633 + /* model, LD_LIBRARY_PATH, etc. Assuming everything is set-up correctly */ 1.634 + /* all we need to do here is to return correct path names. See also */ 1.635 + /* GetJVMPath() and GetApplicationHome(). */ 1.636 + 1.637 + { char *arch = (char *)GetArch(); /* like sparc or sparcv9 */ 1.638 + char *p; 1.639 + 1.640 + if (!GetJREPath(jrepath, so_jrepath, arch, JNI_FALSE) ) { 1.641 + fprintf(stderr, "Error: could not find Java 2 Runtime Environment.\n"); 1.642 + exit(2); 1.643 + } 1.644 + 1.645 + if (!GetJVMPath(jrepath, NULL, jvmpath, so_jvmpath, arch )) { 1.646 + fprintf(stderr, "Error: no JVM at `%s'.\n", jvmpath); 1.647 + exit(4); 1.648 + } 1.649 + } 1.650 + 1.651 +#endif /* ifndef GAMMA */ 1.652 +} 1.653 + 1.654 + 1.655 +/* 1.656 + * On Solaris VM choosing is done by the launcher (java.c). 1.657 + */ 1.658 +static jboolean 1.659 +GetJVMPath(const char *jrepath, const char *jvmtype, 1.660 + char *jvmpath, jint jvmpathsize, char * arch) 1.661 +{ 1.662 + struct stat s; 1.663 + 1.664 +#ifndef GAMMA 1.665 + if (strchr(jvmtype, '/')) { 1.666 + sprintf(jvmpath, "%s/" JVM_DLL, jvmtype); 1.667 + } else { 1.668 + sprintf(jvmpath, "%s/lib/%s/%s/" JVM_DLL, jrepath, arch, jvmtype); 1.669 + } 1.670 +#else 1.671 + /* For gamma launcher, JVM is either built-in or in the same directory. */ 1.672 + /* Either way we return "<exe_path>/libjvm.so" where <exe_path> is the */ 1.673 + /* directory where gamma launcher is located. */ 1.674 + 1.675 + char *p; 1.676 + 1.677 + snprintf(jvmpath, jvmpathsize, "%s", GetExecname()); 1.678 + p = strrchr(jvmpath, '/'); 1.679 + if (p) { 1.680 + /* replace executable name with libjvm.so */ 1.681 + snprintf(p + 1, jvmpathsize - (p + 1 - jvmpath), "%s", JVM_DLL); 1.682 + } else { 1.683 + /* this case shouldn't happen */ 1.684 + snprintf(jvmpath, jvmpathsize, "%s", JVM_DLL); 1.685 + } 1.686 +#endif 1.687 + 1.688 + if (_launcher_debug) 1.689 + printf("Does `%s' exist ... ", jvmpath); 1.690 + 1.691 + if (stat(jvmpath, &s) == 0) { 1.692 + if (_launcher_debug) 1.693 + printf("yes.\n"); 1.694 + return JNI_TRUE; 1.695 + } else { 1.696 + if (_launcher_debug) 1.697 + printf("no.\n"); 1.698 + return JNI_FALSE; 1.699 + } 1.700 +} 1.701 + 1.702 +/* 1.703 + * Find path to JRE based on .exe's location or registry settings. 1.704 + */ 1.705 +static jboolean 1.706 +GetJREPath(char *path, jint pathsize, char * arch, jboolean speculative) 1.707 +{ 1.708 + char libjava[MAXPATHLEN]; 1.709 + 1.710 + if (GetApplicationHome(path, pathsize)) { 1.711 + /* Is JRE co-located with the application? */ 1.712 + sprintf(libjava, "%s/lib/%s/" JAVA_DLL, path, arch); 1.713 + if (access(libjava, F_OK) == 0) { 1.714 + goto found; 1.715 + } 1.716 + 1.717 + /* Does the app ship a private JRE in <apphome>/jre directory? */ 1.718 + sprintf(libjava, "%s/jre/lib/%s/" JAVA_DLL, path, arch); 1.719 + if (access(libjava, F_OK) == 0) { 1.720 + strcat(path, "/jre"); 1.721 + goto found; 1.722 + } 1.723 + } 1.724 + 1.725 + if (!speculative) 1.726 + fprintf(stderr, "Error: could not find " JAVA_DLL "\n"); 1.727 + return JNI_FALSE; 1.728 + 1.729 + found: 1.730 + if (_launcher_debug) 1.731 + printf("JRE path is %s\n", path); 1.732 + return JNI_TRUE; 1.733 +} 1.734 + 1.735 +jboolean 1.736 +LoadJavaVM(const char *jvmpath, InvocationFunctions *ifn) 1.737 +{ 1.738 +#ifdef GAMMA 1.739 + /* JVM is directly linked with gamma launcher; no dlopen() */ 1.740 + ifn->CreateJavaVM = JNI_CreateJavaVM; 1.741 + ifn->GetDefaultJavaVMInitArgs = JNI_GetDefaultJavaVMInitArgs; 1.742 + return JNI_TRUE; 1.743 +#else 1.744 + Dl_info dlinfo; 1.745 + void *libjvm; 1.746 + 1.747 + if (_launcher_debug) { 1.748 + printf("JVM path is %s\n", jvmpath); 1.749 + } 1.750 + 1.751 + libjvm = dlopen(jvmpath, RTLD_NOW + RTLD_GLOBAL); 1.752 + if (libjvm == NULL) { 1.753 +#if defined(__sparc) && !defined(_LP64) /* i.e. 32-bit sparc */ 1.754 + FILE * fp; 1.755 + Elf32_Ehdr elf_head; 1.756 + int count; 1.757 + int location; 1.758 + 1.759 + fp = fopen(jvmpath, "r"); 1.760 + if(fp == NULL) 1.761 + goto error; 1.762 + 1.763 + /* read in elf header */ 1.764 + count = fread((void*)(&elf_head), sizeof(Elf32_Ehdr), 1, fp); 1.765 + fclose(fp); 1.766 + if(count < 1) 1.767 + goto error; 1.768 + 1.769 + /* 1.770 + * Check for running a server vm (compiled with -xarch=v8plus) 1.771 + * on a stock v8 processor. In this case, the machine type in 1.772 + * the elf header would not be included the architecture list 1.773 + * provided by the isalist command, which is turn is gotten from 1.774 + * sysinfo. This case cannot occur on 64-bit hardware and thus 1.775 + * does not have to be checked for in binaries with an LP64 data 1.776 + * model. 1.777 + */ 1.778 + if(elf_head.e_machine == EM_SPARC32PLUS) { 1.779 + char buf[257]; /* recommended buffer size from sysinfo man 1.780 + page */ 1.781 + long length; 1.782 + char* location; 1.783 + 1.784 + length = sysinfo(SI_ISALIST, buf, 257); 1.785 + if(length > 0) { 1.786 + location = strstr(buf, "sparcv8plus "); 1.787 + if(location == NULL) { 1.788 + fprintf(stderr, "SPARC V8 processor detected; Server compiler requires V9 or better.\n"); 1.789 + fprintf(stderr, "Use Client compiler on V8 processors.\n"); 1.790 + fprintf(stderr, "Could not create the Java virtual machine.\n"); 1.791 + return JNI_FALSE; 1.792 + } 1.793 + } 1.794 + } 1.795 +#endif 1.796 + fprintf(stderr, "dl failure on line %d", __LINE__); 1.797 + goto error; 1.798 + } 1.799 + 1.800 + ifn->CreateJavaVM = (CreateJavaVM_t) 1.801 + dlsym(libjvm, "JNI_CreateJavaVM"); 1.802 + if (ifn->CreateJavaVM == NULL) 1.803 + goto error; 1.804 + 1.805 + ifn->GetDefaultJavaVMInitArgs = (GetDefaultJavaVMInitArgs_t) 1.806 + dlsym(libjvm, "JNI_GetDefaultJavaVMInitArgs"); 1.807 + if (ifn->GetDefaultJavaVMInitArgs == NULL) 1.808 + goto error; 1.809 + 1.810 + return JNI_TRUE; 1.811 + 1.812 +error: 1.813 + fprintf(stderr, "Error: failed %s, because %s\n", jvmpath, dlerror()); 1.814 + return JNI_FALSE; 1.815 +#endif /* GAMMA */ 1.816 +} 1.817 + 1.818 +/* 1.819 + * Get the path to the file that has the usage message for -X options. 1.820 + */ 1.821 +void 1.822 +GetXUsagePath(char *buf, jint bufsize) 1.823 +{ 1.824 + static const char Xusage_txt[] = "/Xusage.txt"; 1.825 + Dl_info dlinfo; 1.826 + 1.827 + /* we use RTLD_NOW because of problems with ld.so.1 and green threads */ 1.828 + dladdr(dlsym(dlopen(JVM_DLL, RTLD_NOW), "JNI_CreateJavaVM"), &dlinfo); 1.829 + strncpy(buf, (char *)dlinfo.dli_fname, bufsize - sizeof(Xusage_txt)); 1.830 + 1.831 + buf[bufsize-1] = '\0'; 1.832 + strcpy(strrchr(buf, '/'), Xusage_txt); 1.833 +} 1.834 + 1.835 +/* 1.836 + * If app is "/foo/bin/javac", or "/foo/bin/sparcv9/javac" then put 1.837 + * "/foo" into buf. 1.838 + */ 1.839 +jboolean 1.840 +GetApplicationHome(char *buf, jint bufsize) 1.841 +{ 1.842 +#ifdef __linux__ 1.843 + char *execname = GetExecname(); 1.844 + if (execname) { 1.845 + strncpy(buf, execname, bufsize-1); 1.846 + buf[bufsize-1] = '\0'; 1.847 + } else { 1.848 + return JNI_FALSE; 1.849 + } 1.850 +#else 1.851 + Dl_info dlinfo; 1.852 + 1.853 + dladdr((void *)GetApplicationHome, &dlinfo); 1.854 + if (realpath(dlinfo.dli_fname, buf) == NULL) { 1.855 + fprintf(stderr, "Error: realpath(`%s') failed.\n", dlinfo.dli_fname); 1.856 + return JNI_FALSE; 1.857 + } 1.858 +#endif 1.859 + 1.860 +#ifdef GAMMA 1.861 + { 1.862 + /* gamma launcher uses JAVA_HOME environment variable to find JDK/JRE */ 1.863 + char* java_home_var = getenv("JAVA_HOME"); 1.864 + if (java_home_var == NULL) { 1.865 + printf("JAVA_HOME must point to a valid JDK/JRE to run gamma\n"); 1.866 + return JNI_FALSE; 1.867 + } 1.868 + snprintf(buf, bufsize, "%s", java_home_var); 1.869 + } 1.870 +#else 1.871 + if (strrchr(buf, '/') == 0) { 1.872 + buf[0] = '\0'; 1.873 + return JNI_FALSE; 1.874 + } 1.875 + *(strrchr(buf, '/')) = '\0'; /* executable file */ 1.876 + if (strlen(buf) < 4 || strrchr(buf, '/') == 0) { 1.877 + buf[0] = '\0'; 1.878 + return JNI_FALSE; 1.879 + } 1.880 + if (strcmp("/bin", buf + strlen(buf) - 4) != 0) 1.881 + *(strrchr(buf, '/')) = '\0'; /* sparcv9 or amd64 */ 1.882 + if (strlen(buf) < 4 || strcmp("/bin", buf + strlen(buf) - 4) != 0) { 1.883 + buf[0] = '\0'; 1.884 + return JNI_FALSE; 1.885 + } 1.886 + *(strrchr(buf, '/')) = '\0'; /* bin */ 1.887 +#endif /* GAMMA */ 1.888 + 1.889 + return JNI_TRUE; 1.890 +} 1.891 + 1.892 + 1.893 +/* 1.894 + * Return true if the named program exists 1.895 + */ 1.896 +static int 1.897 +ProgramExists(char *name) 1.898 +{ 1.899 + struct stat sb; 1.900 + if (stat(name, &sb) != 0) return 0; 1.901 + if (S_ISDIR(sb.st_mode)) return 0; 1.902 + return (sb.st_mode & S_IEXEC) != 0; 1.903 +} 1.904 + 1.905 + 1.906 +/* 1.907 + * Find a command in a directory, returning the path. 1.908 + */ 1.909 +static char * 1.910 +Resolve(char *indir, char *cmd) 1.911 +{ 1.912 + char name[PATH_MAX + 2], *real; 1.913 + 1.914 + if ((strlen(indir) + strlen(cmd) + 1) > PATH_MAX) return 0; 1.915 + sprintf(name, "%s%c%s", indir, FILE_SEPARATOR, cmd); 1.916 + if (!ProgramExists(name)) return 0; 1.917 + real = MemAlloc(PATH_MAX + 2); 1.918 + if (!realpath(name, real)) 1.919 + strcpy(real, name); 1.920 + return real; 1.921 +} 1.922 + 1.923 + 1.924 +/* 1.925 + * Find a path for the executable 1.926 + */ 1.927 +static char * 1.928 +FindExecName(char *program) 1.929 +{ 1.930 + char cwdbuf[PATH_MAX+2]; 1.931 + char *path; 1.932 + char *tmp_path; 1.933 + char *f; 1.934 + char *result = NULL; 1.935 + 1.936 + /* absolute path? */ 1.937 + if (*program == FILE_SEPARATOR || 1.938 + (FILE_SEPARATOR=='\\' && strrchr(program, ':'))) 1.939 + return Resolve("", program+1); 1.940 + 1.941 + /* relative path? */ 1.942 + if (strrchr(program, FILE_SEPARATOR) != 0) { 1.943 + char buf[PATH_MAX+2]; 1.944 + return Resolve(getcwd(cwdbuf, sizeof(cwdbuf)), program); 1.945 + } 1.946 + 1.947 + /* from search path? */ 1.948 + path = getenv("PATH"); 1.949 + if (!path || !*path) path = "."; 1.950 + tmp_path = MemAlloc(strlen(path) + 2); 1.951 + strcpy(tmp_path, path); 1.952 + 1.953 + for (f=tmp_path; *f && result==0; ) { 1.954 + char *s = f; 1.955 + while (*f && (*f != PATH_SEPARATOR)) ++f; 1.956 + if (*f) *f++ = 0; 1.957 + if (*s == FILE_SEPARATOR) 1.958 + result = Resolve(s, program); 1.959 + else { 1.960 + /* relative path element */ 1.961 + char dir[2*PATH_MAX]; 1.962 + sprintf(dir, "%s%c%s", getcwd(cwdbuf, sizeof(cwdbuf)), 1.963 + FILE_SEPARATOR, s); 1.964 + result = Resolve(dir, program); 1.965 + } 1.966 + if (result != 0) break; 1.967 + } 1.968 + 1.969 + free(tmp_path); 1.970 + return result; 1.971 +} 1.972 + 1.973 + 1.974 +/* Store the name of the executable once computed */ 1.975 +static char *execname = NULL; 1.976 + 1.977 +/* 1.978 + * Compute the name of the executable 1.979 + * 1.980 + * In order to re-exec securely we need the absolute path of the 1.981 + * executable. On Solaris getexecname(3c) may not return an absolute 1.982 + * path so we use dladdr to get the filename of the executable and 1.983 + * then use realpath to derive an absolute path. From Solaris 9 1.984 + * onwards the filename returned in DL_info structure from dladdr is 1.985 + * an absolute pathname so technically realpath isn't required. 1.986 + * On Linux we read the executable name from /proc/self/exe. 1.987 + * As a fallback, and for platforms other than Solaris and Linux, 1.988 + * we use FindExecName to compute the executable name. 1.989 + */ 1.990 +static char * 1.991 +SetExecname(char **argv) 1.992 +{ 1.993 + char* exec_path = NULL; 1.994 + 1.995 + if (execname != NULL) /* Already determined */ 1.996 + return (execname); 1.997 + 1.998 +#if defined(__sun) 1.999 + { 1.1000 + Dl_info dlinfo; 1.1001 + if (dladdr((void*)&SetExecname, &dlinfo)) { 1.1002 + char *resolved = (char*)MemAlloc(PATH_MAX+1); 1.1003 + if (resolved != NULL) { 1.1004 + exec_path = realpath(dlinfo.dli_fname, resolved); 1.1005 + if (exec_path == NULL) { 1.1006 + free(resolved); 1.1007 + } 1.1008 + } 1.1009 + } 1.1010 + } 1.1011 +#elif defined(__linux__) 1.1012 + { 1.1013 + const char* self = "/proc/self/exe"; 1.1014 + char buf[PATH_MAX+1]; 1.1015 + int len = readlink(self, buf, PATH_MAX); 1.1016 + if (len >= 0) { 1.1017 + buf[len] = '\0'; /* readlink doesn't nul terminate */ 1.1018 + exec_path = strdup(buf); 1.1019 + } 1.1020 + } 1.1021 +#else /* !__sun && !__linux */ 1.1022 + { 1.1023 + /* Not implemented */ 1.1024 + } 1.1025 +#endif 1.1026 + 1.1027 + if (exec_path == NULL) { 1.1028 + exec_path = FindExecName(argv[0]); 1.1029 + } 1.1030 + execname = exec_path; 1.1031 + return exec_path; 1.1032 +} 1.1033 + 1.1034 +/* 1.1035 + * Return the name of the executable. Used in java_md.c to find the JRE area. 1.1036 + */ 1.1037 +static char * 1.1038 +GetExecname() { 1.1039 + return execname; 1.1040 +} 1.1041 + 1.1042 +void ReportErrorMessage(char * message, jboolean always) { 1.1043 + if (always) { 1.1044 + fprintf(stderr, "%s\n", message); 1.1045 + } 1.1046 +} 1.1047 + 1.1048 +void ReportErrorMessage2(char * format, char * string, jboolean always) { 1.1049 + if (always) { 1.1050 + fprintf(stderr, format, string); 1.1051 + fprintf(stderr, "\n"); 1.1052 + } 1.1053 +} 1.1054 + 1.1055 +void ReportExceptionDescription(JNIEnv * env) { 1.1056 + (*env)->ExceptionDescribe(env); 1.1057 +} 1.1058 + 1.1059 +/* 1.1060 + * Return JNI_TRUE for an option string that has no effect but should 1.1061 + * _not_ be passed on to the vm; return JNI_FALSE otherwise. On 1.1062 + * Solaris SPARC, this screening needs to be done if: 1.1063 + * 1) LD_LIBRARY_PATH does _not_ need to be reset and 1.1064 + * 2) -d32 or -d64 is passed to a binary with a matching data model 1.1065 + * (the exec in SetLibraryPath removes -d<n> options and points the 1.1066 + * exec to the proper binary). When this exec is not done, these options 1.1067 + * would end up getting passed onto the vm. 1.1068 + */ 1.1069 +jboolean RemovableMachineDependentOption(char * option) { 1.1070 + /* 1.1071 + * Unconditionally remove both -d32 and -d64 options since only 1.1072 + * the last such options has an effect; e.g. 1.1073 + * java -d32 -d64 -d32 -version 1.1074 + * is equivalent to 1.1075 + * java -d32 -version 1.1076 + */ 1.1077 + 1.1078 + if( (strcmp(option, "-d32") == 0 ) || 1.1079 + (strcmp(option, "-d64") == 0 )) 1.1080 + return JNI_TRUE; 1.1081 + else 1.1082 + return JNI_FALSE; 1.1083 +} 1.1084 + 1.1085 +void PrintMachineDependentOptions() { 1.1086 + fprintf(stdout, 1.1087 + " -d32 use a 32-bit data model if available\n" 1.1088 + "\n" 1.1089 + " -d64 use a 64-bit data model if available\n"); 1.1090 + return; 1.1091 +} 1.1092 + 1.1093 +#ifndef GAMMA /* gamma launcher does not have ergonomics */ 1.1094 + 1.1095 +/* 1.1096 + * The following methods (down to ServerClassMachine()) answer 1.1097 + * the question about whether a machine is a "server-class" 1.1098 + * machine. A server-class machine is loosely defined as one 1.1099 + * with 2 or more processors and 2 gigabytes or more physical 1.1100 + * memory. The definition of a processor is a physical package, 1.1101 + * not a hyperthreaded chip masquerading as a multi-processor. 1.1102 + * The definition of memory is also somewhat fuzzy, since x86 1.1103 + * machines seem not to report all the memory in their DIMMs, we 1.1104 + * think because of memory mapping of graphics cards, etc. 1.1105 + * 1.1106 + * This code is somewhat more confused with #ifdef's than we'd 1.1107 + * like because this file is used by both Solaris and Linux 1.1108 + * platforms, and so needs to be parameterized for SPARC and 1.1109 + * i586 hardware. The other Linux platforms (amd64 and ia64) 1.1110 + * don't even ask this question, because they only come with 1.1111 + * server JVMs. */ 1.1112 + 1.1113 +# define KB (1024UL) 1.1114 +# define MB (1024UL * KB) 1.1115 +# define GB (1024UL * MB) 1.1116 + 1.1117 +/* Compute physical memory by asking the OS */ 1.1118 +uint64_t 1.1119 +physical_memory(void) { 1.1120 + const uint64_t pages = (uint64_t) sysconf(_SC_PHYS_PAGES); 1.1121 + const uint64_t page_size = (uint64_t) sysconf(_SC_PAGESIZE); 1.1122 + const uint64_t result = pages * page_size; 1.1123 +# define UINT64_FORMAT "%" PRIu64 1.1124 + 1.1125 + if (_launcher_debug) { 1.1126 + printf("pages: " UINT64_FORMAT 1.1127 + " page_size: " UINT64_FORMAT 1.1128 + " physical memory: " UINT64_FORMAT " (%.3fGB)\n", 1.1129 + pages, page_size, result, result / (double) GB); 1.1130 + } 1.1131 + return result; 1.1132 +} 1.1133 + 1.1134 +#if defined(__sun) && defined(__sparc) 1.1135 + 1.1136 +/* Methods for solaris-sparc: these are easy. */ 1.1137 + 1.1138 +/* Ask the OS how many processors there are. */ 1.1139 +unsigned long 1.1140 +physical_processors(void) { 1.1141 + const unsigned long sys_processors = sysconf(_SC_NPROCESSORS_CONF); 1.1142 + 1.1143 + if (_launcher_debug) { 1.1144 + printf("sysconf(_SC_NPROCESSORS_CONF): %lu\n", sys_processors); 1.1145 + } 1.1146 + return sys_processors; 1.1147 +} 1.1148 + 1.1149 +/* The solaris-sparc version of the "server-class" predicate. */ 1.1150 +jboolean 1.1151 +solaris_sparc_ServerClassMachine(void) { 1.1152 + jboolean result = JNI_FALSE; 1.1153 + /* How big is a server class machine? */ 1.1154 + const unsigned long server_processors = 2UL; 1.1155 + const uint64_t server_memory = 2UL * GB; 1.1156 + const uint64_t actual_memory = physical_memory(); 1.1157 + 1.1158 + /* Is this a server class machine? */ 1.1159 + if (actual_memory >= server_memory) { 1.1160 + const unsigned long actual_processors = physical_processors(); 1.1161 + if (actual_processors >= server_processors) { 1.1162 + result = JNI_TRUE; 1.1163 + } 1.1164 + } 1.1165 + if (_launcher_debug) { 1.1166 + printf("solaris_" ARCH "_ServerClassMachine: %s\n", 1.1167 + (result == JNI_TRUE ? "JNI_TRUE" : "JNI_FALSE")); 1.1168 + } 1.1169 + return result; 1.1170 +} 1.1171 + 1.1172 +#endif /* __sun && __sparc */ 1.1173 + 1.1174 +#if defined(__sun) && defined(i586) 1.1175 + 1.1176 +/* 1.1177 + * A utility method for asking the CPU about itself. 1.1178 + * There's a corresponding version of linux-i586 1.1179 + * because the compilers are different. 1.1180 + */ 1.1181 +void 1.1182 +get_cpuid(uint32_t arg, 1.1183 + uint32_t* eaxp, 1.1184 + uint32_t* ebxp, 1.1185 + uint32_t* ecxp, 1.1186 + uint32_t* edxp) { 1.1187 +#ifdef _LP64 1.1188 + asm( 1.1189 + /* rbx is a callee-saved register */ 1.1190 + " movq %rbx, %r11 \n" 1.1191 + /* rdx and rcx are 3rd and 4th argument registers */ 1.1192 + " movq %rdx, %r10 \n" 1.1193 + " movq %rcx, %r9 \n" 1.1194 + " movl %edi, %eax \n" 1.1195 + " cpuid \n" 1.1196 + " movl %eax, (%rsi)\n" 1.1197 + " movl %ebx, (%r10)\n" 1.1198 + " movl %ecx, (%r9) \n" 1.1199 + " movl %edx, (%r8) \n" 1.1200 + /* Restore rbx */ 1.1201 + " movq %r11, %rbx"); 1.1202 +#else 1.1203 + /* EBX is a callee-saved register */ 1.1204 + asm(" pushl %ebx"); 1.1205 + /* Need ESI for storing through arguments */ 1.1206 + asm(" pushl %esi"); 1.1207 + asm(" movl 8(%ebp), %eax \n" 1.1208 + " cpuid \n" 1.1209 + " movl 12(%ebp), %esi \n" 1.1210 + " movl %eax, (%esi) \n" 1.1211 + " movl 16(%ebp), %esi \n" 1.1212 + " movl %ebx, (%esi) \n" 1.1213 + " movl 20(%ebp), %esi \n" 1.1214 + " movl %ecx, (%esi) \n" 1.1215 + " movl 24(%ebp), %esi \n" 1.1216 + " movl %edx, (%esi) "); 1.1217 + /* Restore ESI and EBX */ 1.1218 + asm(" popl %esi"); 1.1219 + /* Restore EBX */ 1.1220 + asm(" popl %ebx"); 1.1221 +#endif 1.1222 +} 1.1223 + 1.1224 +#endif /* __sun && i586 */ 1.1225 + 1.1226 +#if defined(__linux__) && defined(i586) 1.1227 + 1.1228 +/* 1.1229 + * A utility method for asking the CPU about itself. 1.1230 + * There's a corresponding version of solaris-i586 1.1231 + * because the compilers are different. 1.1232 + */ 1.1233 +void 1.1234 +get_cpuid(uint32_t arg, 1.1235 + uint32_t* eaxp, 1.1236 + uint32_t* ebxp, 1.1237 + uint32_t* ecxp, 1.1238 + uint32_t* edxp) { 1.1239 +#ifdef _LP64 1.1240 + __asm__ volatile (/* Instructions */ 1.1241 + " movl %4, %%eax \n" 1.1242 + " cpuid \n" 1.1243 + " movl %%eax, (%0)\n" 1.1244 + " movl %%ebx, (%1)\n" 1.1245 + " movl %%ecx, (%2)\n" 1.1246 + " movl %%edx, (%3)\n" 1.1247 + : /* Outputs */ 1.1248 + : /* Inputs */ 1.1249 + "r" (eaxp), 1.1250 + "r" (ebxp), 1.1251 + "r" (ecxp), 1.1252 + "r" (edxp), 1.1253 + "r" (arg) 1.1254 + : /* Clobbers */ 1.1255 + "%rax", "%rbx", "%rcx", "%rdx", "memory" 1.1256 + ); 1.1257 +#else 1.1258 + uint32_t value_of_eax = 0; 1.1259 + uint32_t value_of_ebx = 0; 1.1260 + uint32_t value_of_ecx = 0; 1.1261 + uint32_t value_of_edx = 0; 1.1262 + __asm__ volatile (/* Instructions */ 1.1263 + /* ebx is callee-save, so push it */ 1.1264 + /* even though it's in the clobbers section */ 1.1265 + " pushl %%ebx \n" 1.1266 + " movl %4, %%eax \n" 1.1267 + " cpuid \n" 1.1268 + " movl %%eax, %0 \n" 1.1269 + " movl %%ebx, %1 \n" 1.1270 + " movl %%ecx, %2 \n" 1.1271 + " movl %%edx, %3 \n" 1.1272 + /* restore ebx */ 1.1273 + " popl %%ebx \n" 1.1274 + 1.1275 + : /* Outputs */ 1.1276 + "=m" (value_of_eax), 1.1277 + "=m" (value_of_ebx), 1.1278 + "=m" (value_of_ecx), 1.1279 + "=m" (value_of_edx) 1.1280 + : /* Inputs */ 1.1281 + "m" (arg) 1.1282 + : /* Clobbers */ 1.1283 + "%eax", "%ebx", "%ecx", "%edx" 1.1284 + ); 1.1285 + *eaxp = value_of_eax; 1.1286 + *ebxp = value_of_ebx; 1.1287 + *ecxp = value_of_ecx; 1.1288 + *edxp = value_of_edx; 1.1289 +#endif 1.1290 +} 1.1291 + 1.1292 +#endif /* __linux__ && i586 */ 1.1293 + 1.1294 +#ifdef i586 1.1295 +/* 1.1296 + * Routines shared by solaris-i586 and linux-i586. 1.1297 + */ 1.1298 + 1.1299 +enum HyperThreadingSupport_enum { 1.1300 + hts_supported = 1, 1.1301 + hts_too_soon_to_tell = 0, 1.1302 + hts_not_supported = -1, 1.1303 + hts_not_pentium4 = -2, 1.1304 + hts_not_intel = -3 1.1305 +}; 1.1306 +typedef enum HyperThreadingSupport_enum HyperThreadingSupport; 1.1307 + 1.1308 +/* Determine if hyperthreading is supported */ 1.1309 +HyperThreadingSupport 1.1310 +hyperthreading_support(void) { 1.1311 + HyperThreadingSupport result = hts_too_soon_to_tell; 1.1312 + /* Bits 11 through 8 is family processor id */ 1.1313 +# define FAMILY_ID_SHIFT 8 1.1314 +# define FAMILY_ID_MASK 0xf 1.1315 + /* Bits 23 through 20 is extended family processor id */ 1.1316 +# define EXT_FAMILY_ID_SHIFT 20 1.1317 +# define EXT_FAMILY_ID_MASK 0xf 1.1318 + /* Pentium 4 family processor id */ 1.1319 +# define PENTIUM4_FAMILY_ID 0xf 1.1320 + /* Bit 28 indicates Hyper-Threading Technology support */ 1.1321 +# define HT_BIT_SHIFT 28 1.1322 +# define HT_BIT_MASK 1 1.1323 + uint32_t vendor_id[3] = { 0U, 0U, 0U }; 1.1324 + uint32_t value_of_eax = 0U; 1.1325 + uint32_t value_of_edx = 0U; 1.1326 + uint32_t dummy = 0U; 1.1327 + 1.1328 + /* Yes, this is supposed to be [0], [2], [1] */ 1.1329 + get_cpuid(0, &dummy, &vendor_id[0], &vendor_id[2], &vendor_id[1]); 1.1330 + if (_launcher_debug) { 1.1331 + printf("vendor: %c %c %c %c %c %c %c %c %c %c %c %c \n", 1.1332 + ((vendor_id[0] >> 0) & 0xff), 1.1333 + ((vendor_id[0] >> 8) & 0xff), 1.1334 + ((vendor_id[0] >> 16) & 0xff), 1.1335 + ((vendor_id[0] >> 24) & 0xff), 1.1336 + ((vendor_id[1] >> 0) & 0xff), 1.1337 + ((vendor_id[1] >> 8) & 0xff), 1.1338 + ((vendor_id[1] >> 16) & 0xff), 1.1339 + ((vendor_id[1] >> 24) & 0xff), 1.1340 + ((vendor_id[2] >> 0) & 0xff), 1.1341 + ((vendor_id[2] >> 8) & 0xff), 1.1342 + ((vendor_id[2] >> 16) & 0xff), 1.1343 + ((vendor_id[2] >> 24) & 0xff)); 1.1344 + } 1.1345 + get_cpuid(1, &value_of_eax, &dummy, &dummy, &value_of_edx); 1.1346 + if (_launcher_debug) { 1.1347 + printf("value_of_eax: 0x%x value_of_edx: 0x%x\n", 1.1348 + value_of_eax, value_of_edx); 1.1349 + } 1.1350 + if ((((value_of_eax >> FAMILY_ID_SHIFT) & FAMILY_ID_MASK) == PENTIUM4_FAMILY_ID) || 1.1351 + (((value_of_eax >> EXT_FAMILY_ID_SHIFT) & EXT_FAMILY_ID_MASK) != 0)) { 1.1352 + if ((((vendor_id[0] >> 0) & 0xff) == 'G') && 1.1353 + (((vendor_id[0] >> 8) & 0xff) == 'e') && 1.1354 + (((vendor_id[0] >> 16) & 0xff) == 'n') && 1.1355 + (((vendor_id[0] >> 24) & 0xff) == 'u') && 1.1356 + (((vendor_id[1] >> 0) & 0xff) == 'i') && 1.1357 + (((vendor_id[1] >> 8) & 0xff) == 'n') && 1.1358 + (((vendor_id[1] >> 16) & 0xff) == 'e') && 1.1359 + (((vendor_id[1] >> 24) & 0xff) == 'I') && 1.1360 + (((vendor_id[2] >> 0) & 0xff) == 'n') && 1.1361 + (((vendor_id[2] >> 8) & 0xff) == 't') && 1.1362 + (((vendor_id[2] >> 16) & 0xff) == 'e') && 1.1363 + (((vendor_id[2] >> 24) & 0xff) == 'l')) { 1.1364 + if (((value_of_edx >> HT_BIT_SHIFT) & HT_BIT_MASK) == HT_BIT_MASK) { 1.1365 + if (_launcher_debug) { 1.1366 + printf("Hyperthreading supported\n"); 1.1367 + } 1.1368 + result = hts_supported; 1.1369 + } else { 1.1370 + if (_launcher_debug) { 1.1371 + printf("Hyperthreading not supported\n"); 1.1372 + } 1.1373 + result = hts_not_supported; 1.1374 + } 1.1375 + } else { 1.1376 + if (_launcher_debug) { 1.1377 + printf("Not GenuineIntel\n"); 1.1378 + } 1.1379 + result = hts_not_intel; 1.1380 + } 1.1381 + } else { 1.1382 + if (_launcher_debug) { 1.1383 + printf("not Pentium 4 or extended\n"); 1.1384 + } 1.1385 + result = hts_not_pentium4; 1.1386 + } 1.1387 + return result; 1.1388 +} 1.1389 + 1.1390 +/* Determine how many logical processors there are per CPU */ 1.1391 +unsigned int 1.1392 +logical_processors_per_package(void) { 1.1393 + /* 1.1394 + * After CPUID with EAX==1, register EBX bits 23 through 16 1.1395 + * indicate the number of logical processors per package 1.1396 + */ 1.1397 +# define NUM_LOGICAL_SHIFT 16 1.1398 +# define NUM_LOGICAL_MASK 0xff 1.1399 + unsigned int result = 1U; 1.1400 + const HyperThreadingSupport hyperthreading = hyperthreading_support(); 1.1401 + 1.1402 + if (hyperthreading == hts_supported) { 1.1403 + uint32_t value_of_ebx = 0U; 1.1404 + uint32_t dummy = 0U; 1.1405 + 1.1406 + get_cpuid(1, &dummy, &value_of_ebx, &dummy, &dummy); 1.1407 + result = (value_of_ebx >> NUM_LOGICAL_SHIFT) & NUM_LOGICAL_MASK; 1.1408 + if (_launcher_debug) { 1.1409 + printf("logical processors per package: %u\n", result); 1.1410 + } 1.1411 + } 1.1412 + return result; 1.1413 +} 1.1414 + 1.1415 +/* Compute the number of physical processors, not logical processors */ 1.1416 +unsigned long 1.1417 +physical_processors(void) { 1.1418 + const long sys_processors = sysconf(_SC_NPROCESSORS_CONF); 1.1419 + unsigned long result = sys_processors; 1.1420 + 1.1421 + if (_launcher_debug) { 1.1422 + printf("sysconf(_SC_NPROCESSORS_CONF): %lu\n", sys_processors); 1.1423 + } 1.1424 + if (sys_processors > 1) { 1.1425 + unsigned int logical_processors = logical_processors_per_package(); 1.1426 + if (logical_processors > 1) { 1.1427 + result = (unsigned long) sys_processors / logical_processors; 1.1428 + } 1.1429 + } 1.1430 + if (_launcher_debug) { 1.1431 + printf("physical processors: %lu\n", result); 1.1432 + } 1.1433 + return result; 1.1434 +} 1.1435 + 1.1436 +#endif /* i586 */ 1.1437 + 1.1438 +#if defined(__sun) && defined(i586) 1.1439 + 1.1440 +/* The definition of a server-class machine for solaris-i586/amd64 */ 1.1441 +jboolean 1.1442 +solaris_i586_ServerClassMachine(void) { 1.1443 + jboolean result = JNI_FALSE; 1.1444 + /* How big is a server class machine? */ 1.1445 + const unsigned long server_processors = 2UL; 1.1446 + const uint64_t server_memory = 2UL * GB; 1.1447 + /* 1.1448 + * We seem not to get our full complement of memory. 1.1449 + * We allow some part (1/8?) of the memory to be "missing", 1.1450 + * based on the sizes of DIMMs, and maybe graphics cards. 1.1451 + */ 1.1452 + const uint64_t missing_memory = 256UL * MB; 1.1453 + const uint64_t actual_memory = physical_memory(); 1.1454 + 1.1455 + /* Is this a server class machine? */ 1.1456 + if (actual_memory >= (server_memory - missing_memory)) { 1.1457 + const unsigned long actual_processors = physical_processors(); 1.1458 + if (actual_processors >= server_processors) { 1.1459 + result = JNI_TRUE; 1.1460 + } 1.1461 + } 1.1462 + if (_launcher_debug) { 1.1463 + printf("solaris_" ARCH "_ServerClassMachine: %s\n", 1.1464 + (result == JNI_TRUE ? "true" : "false")); 1.1465 + } 1.1466 + return result; 1.1467 +} 1.1468 + 1.1469 +#endif /* __sun && i586 */ 1.1470 + 1.1471 +#if defined(__linux__) && defined(i586) 1.1472 + 1.1473 +/* The definition of a server-class machine for linux-i586 */ 1.1474 +jboolean 1.1475 +linux_i586_ServerClassMachine(void) { 1.1476 + jboolean result = JNI_FALSE; 1.1477 + /* How big is a server class machine? */ 1.1478 + const unsigned long server_processors = 2UL; 1.1479 + const uint64_t server_memory = 2UL * GB; 1.1480 + /* 1.1481 + * We seem not to get our full complement of memory. 1.1482 + * We allow some part (1/8?) of the memory to be "missing", 1.1483 + * based on the sizes of DIMMs, and maybe graphics cards. 1.1484 + */ 1.1485 + const uint64_t missing_memory = 256UL * MB; 1.1486 + const uint64_t actual_memory = physical_memory(); 1.1487 + 1.1488 + /* Is this a server class machine? */ 1.1489 + if (actual_memory >= (server_memory - missing_memory)) { 1.1490 + const unsigned long actual_processors = physical_processors(); 1.1491 + if (actual_processors >= server_processors) { 1.1492 + result = JNI_TRUE; 1.1493 + } 1.1494 + } 1.1495 + if (_launcher_debug) { 1.1496 + printf("linux_" ARCH "_ServerClassMachine: %s\n", 1.1497 + (result == JNI_TRUE ? "true" : "false")); 1.1498 + } 1.1499 + return result; 1.1500 +} 1.1501 + 1.1502 +#endif /* __linux__ && i586 */ 1.1503 + 1.1504 +/* Dispatch to the platform-specific definition of "server-class" */ 1.1505 +jboolean 1.1506 +ServerClassMachine(void) { 1.1507 + jboolean result = JNI_FALSE; 1.1508 +#if defined(__sun) && defined(__sparc) 1.1509 + result = solaris_sparc_ServerClassMachine(); 1.1510 +#elif defined(__sun) && defined(i586) 1.1511 + result = solaris_i586_ServerClassMachine(); 1.1512 +#elif defined(__linux__) && defined(i586) 1.1513 + result = linux_i586_ServerClassMachine(); 1.1514 +#else 1.1515 + if (_launcher_debug) { 1.1516 + printf("ServerClassMachine: returns default value of %s\n", 1.1517 + (result == JNI_TRUE ? "true" : "false")); 1.1518 + } 1.1519 +#endif 1.1520 + return result; 1.1521 +} 1.1522 + 1.1523 +#endif /* ifndef GAMMA */ 1.1524 + 1.1525 +#ifndef GAMMA /* gamma launcher does not choose JDK/JRE/JVM */ 1.1526 + 1.1527 +/* 1.1528 + * Since using the file system as a registry is a bit risky, perform 1.1529 + * additional sanity checks on the identified directory to validate 1.1530 + * it as a valid jre/sdk. 1.1531 + * 1.1532 + * Return 0 if the tests fail; otherwise return non-zero (true). 1.1533 + * 1.1534 + * Note that checking for anything more than the existence of an 1.1535 + * executable object at bin/java relative to the path being checked 1.1536 + * will break the regression tests. 1.1537 + */ 1.1538 +static int 1.1539 +CheckSanity(char *path, char *dir) 1.1540 +{ 1.1541 + char buffer[PATH_MAX]; 1.1542 + 1.1543 + if (strlen(path) + strlen(dir) + 11 > PATH_MAX) 1.1544 + return (0); /* Silently reject "impossibly" long paths */ 1.1545 + 1.1546 + (void)strcat(strcat(strcat(strcpy(buffer, path), "/"), dir), "/bin/java"); 1.1547 + return ((access(buffer, X_OK) == 0) ? 1 : 0); 1.1548 +} 1.1549 + 1.1550 +/* 1.1551 + * Determine if there is an acceptable JRE in the directory dirname. 1.1552 + * Upon locating the "best" one, return a fully qualified path to 1.1553 + * it. "Best" is defined as the most advanced JRE meeting the 1.1554 + * constraints contained in the manifest_info. If no JRE in this 1.1555 + * directory meets the constraints, return NULL. 1.1556 + * 1.1557 + * Note that we don't check for errors in reading the directory 1.1558 + * (which would be done by checking errno). This is because it 1.1559 + * doesn't matter if we get an error reading the directory, or 1.1560 + * we just don't find anything interesting in the directory. We 1.1561 + * just return NULL in either case. 1.1562 + * 1.1563 + * The historical names of j2sdk and j2re were changed to jdk and 1.1564 + * jre respecively as part of the 1.5 rebranding effort. Since the 1.1565 + * former names are legacy on Linux, they must be recognized for 1.1566 + * all time. Fortunately, this is a minor cost. 1.1567 + */ 1.1568 +static char 1.1569 +*ProcessDir(manifest_info *info, char *dirname) 1.1570 +{ 1.1571 + DIR *dirp; 1.1572 + struct dirent *dp; 1.1573 + char *best = NULL; 1.1574 + int offset; 1.1575 + int best_offset = 0; 1.1576 + char *ret_str = NULL; 1.1577 + char buffer[PATH_MAX]; 1.1578 + 1.1579 + if ((dirp = opendir(dirname)) == NULL) 1.1580 + return (NULL); 1.1581 + 1.1582 + do { 1.1583 + if ((dp = readdir(dirp)) != NULL) { 1.1584 + offset = 0; 1.1585 + if ((strncmp(dp->d_name, "jre", 3) == 0) || 1.1586 + (strncmp(dp->d_name, "jdk", 3) == 0)) 1.1587 + offset = 3; 1.1588 + else if (strncmp(dp->d_name, "j2re", 4) == 0) 1.1589 + offset = 4; 1.1590 + else if (strncmp(dp->d_name, "j2sdk", 5) == 0) 1.1591 + offset = 5; 1.1592 + if (offset > 0) { 1.1593 + if ((acceptable_release(dp->d_name + offset, 1.1594 + info->jre_version)) && CheckSanity(dirname, dp->d_name)) 1.1595 + if ((best == NULL) || (exact_version_id( 1.1596 + dp->d_name + offset, best + best_offset) > 0)) { 1.1597 + if (best != NULL) 1.1598 + free(best); 1.1599 + best = strdup(dp->d_name); 1.1600 + best_offset = offset; 1.1601 + } 1.1602 + } 1.1603 + } 1.1604 + } while (dp != NULL); 1.1605 + (void) closedir(dirp); 1.1606 + if (best == NULL) 1.1607 + return (NULL); 1.1608 + else { 1.1609 + ret_str = MemAlloc(strlen(dirname) + strlen(best) + 2); 1.1610 + ret_str = strcat(strcat(strcpy(ret_str, dirname), "/"), best); 1.1611 + free(best); 1.1612 + return (ret_str); 1.1613 + } 1.1614 +} 1.1615 + 1.1616 +/* 1.1617 + * This is the global entry point. It examines the host for the optimal 1.1618 + * JRE to be used by scanning a set of directories. The set of directories 1.1619 + * is platform dependent and can be overridden by the environment 1.1620 + * variable JAVA_VERSION_PATH. 1.1621 + * 1.1622 + * This routine itself simply determines the set of appropriate 1.1623 + * directories before passing control onto ProcessDir(). 1.1624 + */ 1.1625 +char* 1.1626 +LocateJRE(manifest_info* info) 1.1627 +{ 1.1628 + char *path; 1.1629 + char *home; 1.1630 + char *target = NULL; 1.1631 + char *dp; 1.1632 + char *cp; 1.1633 + 1.1634 + /* 1.1635 + * Start by getting JAVA_VERSION_PATH 1.1636 + */ 1.1637 + if (info->jre_restrict_search) 1.1638 + path = strdup(system_dir); 1.1639 + else if ((path = getenv("JAVA_VERSION_PATH")) != NULL) 1.1640 + path = strdup(path); 1.1641 + else 1.1642 + if ((home = getenv("HOME")) != NULL) { 1.1643 + path = (char *)MemAlloc(strlen(home) + 13); 1.1644 + path = strcat(strcat(strcat(strcpy(path, home), 1.1645 + user_dir), ":"), system_dir); 1.1646 + } else 1.1647 + path = strdup(system_dir); 1.1648 + 1.1649 + /* 1.1650 + * Step through each directory on the path. Terminate the scan with 1.1651 + * the first directory with an acceptable JRE. 1.1652 + */ 1.1653 + cp = dp = path; 1.1654 + while (dp != NULL) { 1.1655 + cp = strchr(dp, (int)':'); 1.1656 + if (cp != NULL) 1.1657 + *cp = (char)NULL; 1.1658 + if ((target = ProcessDir(info, dp)) != NULL) 1.1659 + break; 1.1660 + dp = cp; 1.1661 + if (dp != NULL) 1.1662 + dp++; 1.1663 + } 1.1664 + free(path); 1.1665 + return (target); 1.1666 +} 1.1667 + 1.1668 +/* 1.1669 + * Given a path to a jre to execute, this routine checks if this process 1.1670 + * is indeed that jre. If not, it exec's that jre. 1.1671 + * 1.1672 + * We want to actually check the paths rather than just the version string 1.1673 + * built into the executable, so that given version specification (and 1.1674 + * JAVA_VERSION_PATH) will yield the exact same Java environment, regardless 1.1675 + * of the version of the arbitrary launcher we start with. 1.1676 + */ 1.1677 +void 1.1678 +ExecJRE(char *jre, char **argv) 1.1679 +{ 1.1680 + char wanted[PATH_MAX]; 1.1681 + char *execname; 1.1682 + char *progname; 1.1683 + 1.1684 + /* 1.1685 + * Resolve the real path to the directory containing the selected JRE. 1.1686 + */ 1.1687 + if (realpath(jre, wanted) == NULL) { 1.1688 + fprintf(stderr, "Unable to resolve %s\n", jre); 1.1689 + exit(1); 1.1690 + } 1.1691 + 1.1692 + /* 1.1693 + * Resolve the real path to the currently running launcher. 1.1694 + */ 1.1695 + execname = SetExecname(argv); 1.1696 + if (execname == NULL) { 1.1697 + fprintf(stderr, "Unable to resolve current executable\n"); 1.1698 + exit(1); 1.1699 + } 1.1700 + 1.1701 + /* 1.1702 + * If the path to the selected JRE directory is a match to the initial 1.1703 + * portion of the path to the currently executing JRE, we have a winner! 1.1704 + * If so, just return. 1.1705 + */ 1.1706 + if (strncmp(wanted, execname, strlen(wanted)) == 0) 1.1707 + return; /* I am the droid you were looking for */ 1.1708 + 1.1709 + /* 1.1710 + * If this isn't the selected version, exec the selected version. 1.1711 + */ 1.1712 +#ifdef JAVA_ARGS /* javac, jar and friends. */ 1.1713 + progname = "java"; 1.1714 +#else /* java, oldjava, javaw and friends */ 1.1715 +#ifdef PROGNAME 1.1716 + progname = PROGNAME; 1.1717 +#else 1.1718 + progname = *argv; 1.1719 + if ((s = strrchr(progname, FILE_SEPARATOR)) != 0) { 1.1720 + progname = s + 1; 1.1721 + } 1.1722 +#endif /* PROGNAME */ 1.1723 +#endif /* JAVA_ARGS */ 1.1724 + 1.1725 + /* 1.1726 + * This should never happen (because of the selection code in SelectJRE), 1.1727 + * but check for "impossibly" long path names just because buffer overruns 1.1728 + * can be so deadly. 1.1729 + */ 1.1730 + if (strlen(wanted) + strlen(progname) + 6 > PATH_MAX) { 1.1731 + fprintf(stderr, "Path length exceeds maximum length (PATH_MAX)\n"); 1.1732 + exit(1); 1.1733 + } 1.1734 + 1.1735 + /* 1.1736 + * Construct the path and exec it. 1.1737 + */ 1.1738 + (void)strcat(strcat(wanted, "/bin/"), progname); 1.1739 + argv[0] = progname; 1.1740 + if (_launcher_debug) { 1.1741 + int i; 1.1742 + printf("execv(\"%s\"", wanted); 1.1743 + for (i = 0; argv[i] != NULL; i++) 1.1744 + printf(", \"%s\"", argv[i]); 1.1745 + printf(")\n"); 1.1746 + } 1.1747 + execv(wanted, argv); 1.1748 + fprintf(stderr, "Exec of %s failed\n", wanted); 1.1749 + exit(1); 1.1750 +} 1.1751 + 1.1752 +#endif /* ifndef GAMMA */ 1.1753 + 1.1754 +/* 1.1755 + * "Borrowed" from Solaris 10 where the unsetenv() function is being added 1.1756 + * to libc thanks to SUSv3 (Standard Unix Specification, version 3). As 1.1757 + * such, in the fullness of time this will appear in libc on all relevant 1.1758 + * Solaris/Linux platforms and maybe even the Windows platform. At that 1.1759 + * time, this stub can be removed. 1.1760 + * 1.1761 + * This implementation removes the environment locking for multithreaded 1.1762 + * applications. (We don't have access to these mutexes within libc and 1.1763 + * the launcher isn't multithreaded.) Note that what remains is platform 1.1764 + * independent, because it only relies on attributes that a POSIX environment 1.1765 + * defines. 1.1766 + * 1.1767 + * Returns 0 on success, -1 on failure. 1.1768 + * 1.1769 + * Also removed was the setting of errno. The only value of errno set 1.1770 + * was EINVAL ("Invalid Argument"). 1.1771 + */ 1.1772 + 1.1773 +/* 1.1774 + * s1(environ) is name=value 1.1775 + * s2(name) is name(not the form of name=value). 1.1776 + * if names match, return value of 1, else return 0 1.1777 + */ 1.1778 +static int 1.1779 +match_noeq(const char *s1, const char *s2) 1.1780 +{ 1.1781 + while (*s1 == *s2++) { 1.1782 + if (*s1++ == '=') 1.1783 + return (1); 1.1784 + } 1.1785 + if (*s1 == '=' && s2[-1] == '\0') 1.1786 + return (1); 1.1787 + return (0); 1.1788 +} 1.1789 + 1.1790 +/* 1.1791 + * added for SUSv3 standard 1.1792 + * 1.1793 + * Delete entry from environ. 1.1794 + * Do not free() memory! Other threads may be using it. 1.1795 + * Keep it around forever. 1.1796 + */ 1.1797 +static int 1.1798 +borrowed_unsetenv(const char *name) 1.1799 +{ 1.1800 + long idx; /* index into environ */ 1.1801 + 1.1802 + if (name == NULL || *name == '\0' || 1.1803 + strchr(name, '=') != NULL) { 1.1804 + return (-1); 1.1805 + } 1.1806 + 1.1807 + for (idx = 0; environ[idx] != NULL; idx++) { 1.1808 + if (match_noeq(environ[idx], name)) 1.1809 + break; 1.1810 + } 1.1811 + if (environ[idx] == NULL) { 1.1812 + /* name not found but still a success */ 1.1813 + return (0); 1.1814 + } 1.1815 + /* squeeze up one entry */ 1.1816 + do { 1.1817 + environ[idx] = environ[idx+1]; 1.1818 + } while (environ[++idx] != NULL); 1.1819 + 1.1820 + return (0); 1.1821 +} 1.1822 +/* --- End of "borrowed" code --- */ 1.1823 + 1.1824 +/* 1.1825 + * Wrapper for unsetenv() function. 1.1826 + */ 1.1827 +int 1.1828 +UnsetEnv(char *name) 1.1829 +{ 1.1830 + return(borrowed_unsetenv(name)); 1.1831 +}