Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/solaris/bin/jexec.c
32285 views
/*1* Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/*26* jexec for J2SE27*28* jexec is used by the system to allow execution of JAR files.29* Essentially jexec needs to run java and30* needs to be a native ISA executable (not a shell script), although31* this native ISA executable requirement was a mistake that will be fixed.32* (<ISA> is sparc or i386 or amd64).33*34* When you execute a jar file, jexec is executed by the system as follows:35* /usr/java/jre/lib/<ISA>/jexec -jar JARFILENAME36* so this just needs to be turned into:37* /usr/java/jre/bin/java -jar JARFILENAME38*39* Solaris systems (new 7's and all 8's) will be looking for jexec at:40* /usr/java/jre/lib/<ISA>/jexec41* Older systems may need to add this to their /etc/system file:42* set javaexec:jexec="/usr/java/jre/lib/<ISA>/jexec"43* and reboot the machine for this to work.44*45* This source should be compiled as:46* cc -o jexec jexec.c47*48* And jexec should be placed at the following location of the installation:49* <INSTALLATIONDIR>/jre/lib/<ISA>/jexec (for Solaris)50* <INSTALLATIONDIR>/lib/jexec (for Linux)51*52* NOTE: Unless <INSTALLATIONDIR> is the "default" JDK on the system53* (i.e. /usr/java -> <INSTALLATIONDIR>), this jexec will not be54* found. The 1.2 java is only the default on Solaris 8 and55* on systems where the 1.2 packages were installed and no 1.156* java was found.57*58* NOTE: You must use 1.2 jar to build your jar files. The system59* doesn't seem to pick up 1.1 jar files.60*61* NOTE: We don't need to set LD_LIBRARY_PATH here, even though we62* are running the actual java binary because the java binary will63* look for it's libraries through it's own runpath, which uses64* $ORIGIN.65*66* NOTE: This jexec should NOT have any special .so library needs because67* it appears that this executable will NOT get the $ORIGIN of jexec68* but the $ORIGIN of the jar file being executed. Be careful to keep69* this program simple and with no .so dependencies.70*/7172#include <stdlib.h>73#include <stdio.h>74#include <unistd.h>75#include <string.h>76#include <limits.h>77#include <errno.h>78#ifdef __linux__79# include <sys/types.h>80# include <sys/stat.h>81# include <fcntl.h>82# include "jni.h"83# include "manifest_info.h"84#endif8586static const int CRAZY_EXEC = ENOEXEC;87static const int BAD_MAGIC = ENOEXEC;8889static const char * BAD_EXEC_MSG = "jexec failed";90static const char * CRAZY_EXEC_MSG = "missing args";91static const char * MISSING_JAVA_MSG = "can't locate java";92static const char * BAD_ARG_MSG = "incorrect number of arguments";93static const char * MEM_FAILED_MSG = "memory allocation failed";94#ifdef __linux__95static const char * BAD_PATHNAME_MSG = "invalid path";96static const char * BAD_FILE_MSG = "invalid file";97static const char * BAD_MAGIC_MSG = "invalid file (bad magic number)";98#endif99static const char * UNKNOWN_ERROR = "unknown error";100101/* Define a constant that represents the number of directories to pop off the102* current location to find the java binary */103#ifdef __linux__104static const int RELATIVE_DEPTH = 2;105#else /* Solaris */106static const int RELATIVE_DEPTH = 3;107#endif108109/* path to java after popping */110static const char * BIN_PATH = "/bin/java";111112/* flag used when running JAR files */113static const char * JAR_FLAG = "-jar";114115116#ifdef __linux__117/* largest possible size for a local file header */118static const size_t CHUNK_SIZE = 65535;119120/* smallest possible size for a local file header */121static const ssize_t MIN_SIZE = LOCHDR + 1 + 4;122#endif123124125int main(int argc, const char * argv[]);126void errorExit(int error, const char * message);127int getJavaPath(const char * path, char * buf, int depth);128#ifdef __linux__129const char * isJar(const char * path);130#endif131132133/*134* This is the main entry point. This program (jexec) will attempt to execute135* a JAR file by finding the Java program (java), relative to its own location.136* The exact location of the Java program depends on the platform, i.e.137*138* <INSTALLATIONDIR>/jre/lib/<ISA>/jexec (for Solaris)139* <INSTALLATIONDIR>/lib/jexec (for Linux JDK)140*141* Once the Java program is found, this program copies any remaining arguments142* into another array, which is then used to exec the Java program.143*144* On Linux this program does some additional steps. When copying the array of145* args, it is necessary to insert the "-jar" flag between arg[0], the program146* name, and the original arg[1], which is presumed to be a path to a JAR file.147* It is also necessary to verify that the original arg[1] really is a JAR file.148* (These steps are unnecessary on Solaris because they are taken care of by149* the kernel.)150*/151int main(int argc, const char * argv[]) {152/* We need to exec the original arguments using java, instead of jexec.153* Also, for Linux, it is necessary to add the "-jar" argument between154* the new arg[0], and the old arg[1]. To do this we will create a new155* args array. */156char java[PATH_MAX + 1]; /* path to java binary */157const char ** nargv = NULL; /* new args array */158int nargc = 0; /* new args array count */159int argi = 0; /* index into old array */160size_t alen = 0; /* length of new array */161162/* Make sure we have something to work with */163if ((argc < 1) || (argv == NULL)) {164/* Shouldn't happen... */165errorExit(CRAZY_EXEC, CRAZY_EXEC_MSG);166}167168/* Get the path to the java binary, which is in a known position relative169* to our current position, which is in argv[0]. */170if (getJavaPath(argv[argi++], java, RELATIVE_DEPTH) != 0) {171errorExit(errno, MISSING_JAVA_MSG);172}173alen = (argc + 2) * (sizeof (const char *));174if (alen <= 0 || alen > INT_MAX / sizeof(char *)) {175errorExit(errno, BAD_ARG_MSG);176}177nargv = (const char **) malloc(alen);178if (nargv == NULL) {179errorExit(errno, MEM_FAILED_MSG);180}181nargv[nargc++] = java;182183#ifdef __linux__184/* The "-jar" flag is already in the original args list on Solaris,185* so it only needs to be added on Linux. */186nargv[nargc++] = JAR_FLAG;187#endif188189if (argc >= 2) {190const char * jarfile = argv[argi++];191const char * message = NULL;192193#ifdef __linux__194/* On Linux we also need to make sure argv[1] is really a JAR195* file (this will also resolve any symlinks, which helps). */196char jarPath[PATH_MAX + 1];197198if (realpath(jarfile, jarPath) == NULL) {199errorExit(errno, BAD_PATHNAME_MSG);200}201202message = isJar(jarPath);203if (message != NULL) {204errorExit(errno, message);205}206207jarfile = jarPath;208#endif209/* the next argument is the path to the JAR file */210nargv[nargc++] = jarfile;211}212213/* finally copy any remaining arguments */214while (argi < argc) {215nargv[nargc++] = argv[argi++];216}217218/* finally add one last terminating null */219nargv[nargc++] = NULL;220221/* It's time to exec the java binary with the new arguments. It222* is possible that we've reached this point without actually223* having a JAR file argument (i.e. if argc < 2), but we still224* want to exec the java binary, since that will take care of225* displaying the correct usage. */226execv(java, (char * const *) nargv);227228/* If the exec worked, this process would have been replaced229* by the new process. So any code reached beyond this point230* implies an error in the exec. */231free(nargv);232errorExit(errno, BAD_EXEC_MSG);233return 0; // keep the compiler happy234}235236237/*238* Exit the application by setting errno, and writing a message.239*240* Parameters:241* error - errno is set to this value, and it is used to exit.242* message - the message to write.243*/244void errorExit(int error, const char * message) {245if (error != 0) {246errno = error;247perror((message != NULL) ? message : UNKNOWN_ERROR);248}249250exit((error == 0) ? 0 : 1);251}252253254/*255* Get the path to the java binary that should be relative to the current path.256*257* Parameters:258* path - the input path that the java binary that should be relative to.259* buf - a buffer of size PATH_MAX or greater that the java path is260* copied to.261* depth - the number of names to trim off the current path, including the262* name of this program.263*264* Returns:265* This function returns 0 on success; otherwise it returns the value of266* errno.267*/268int getJavaPath(const char * path, char * buf, int depth) {269int result = 0;270271/* Get the full path to this program. Depending on whether this is Solaris272* or Linux, this will be something like,273*274* <FOO>/jre/lib/<ISA>/jexec (for Solaris)275* <FOO>/lib/jexec (for Linux)276*/277if (realpath(path, buf) != NULL) {278int count = 0;279280/* Pop off the filename, and then subdirectories for each level of281* depth */282for (count = 0; count < depth; count++) {283*(strrchr(buf, '/')) = '\0';284}285286/* Append the relative location of java, creating something like,287*288* <FOO>/jre/bin/java (for Solaris)289* <FOO>/bin/java (for Linux)290*/291strcat(buf, BIN_PATH);292}293else {294/* Failed to get the path */295result = errno;296}297298return (result);299}300301302#ifdef __linux__303/*304* Check if the given file is a JAR file.305*306* Parameters:307* path - the path to the file to check for JAR magic.308*309* Returns:310* This function return NULL on success. Otherwise, errno is set, and it311* returns a message that indicates what caused the failure.312*/313const char * isJar(const char * path) {314const char * result = BAD_FILE_MSG;315316int fd = open(path, O_RDONLY);317if (fd != -1) {318unsigned char buf[CHUNK_SIZE];319320ssize_t count = read(fd, buf, CHUNK_SIZE);321if (count >= MIN_SIZE) {322result = BAD_MAGIC_MSG;323324// be sure the file is at least a ZIP file325if (GETSIG(buf) == LOCSIG) {326327off_t flen = LOCNAM(buf);328off_t xlen = LOCEXT(buf);329off_t start = LOCHDR + flen;330off_t end = start + xlen;331332if (end <= count) {333end -= 4; // make sure there are 4 bytes to read at start334while (start < end) {335off_t xhid = SH(buf, start);336off_t xdlen = SH(buf, start + 2);337338start += 4 + xdlen;339if (xhid == 0xcafe) {340// found the JAR magic341result = NULL;342break;343}344}345}346}347}348349if (result != NULL) {350errno = BAD_MAGIC;351}352353close (fd);354}355356return (result);357}358#endif359360361