4680N/A/*
4680N/A * Copyright (c) 1999, 2012, Oracle and/or its affiliates. All rights reserved.
4680N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4680N/A *
4680N/A * This code is free software; you can redistribute it and/or modify it
4680N/A * under the terms of the GNU General Public License version 2 only, as
4680N/A * published by the Free Software Foundation. Oracle designates this
4680N/A * particular file as subject to the "Classpath" exception as provided
4680N/A * by Oracle in the LICENSE file that accompanied this code.
4680N/A *
4680N/A * This code is distributed in the hope that it will be useful, but WITHOUT
4680N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4680N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
4680N/A * version 2 for more details (a copy is included in the LICENSE file that
4680N/A * accompanied this code).
4680N/A *
4680N/A * You should have received a copy of the GNU General Public License version
4680N/A * 2 along with this work; if not, write to the Free Software Foundation,
4680N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4680N/A *
4680N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4680N/A * or visit www.oracle.com if you need additional information or have any
4680N/A * questions.
4680N/A */
4680N/A
4680N/A/*
4680N/A * jexec for J2SE
4680N/A *
4680N/A * jexec is used by the system to allow execution of JAR files.
4680N/A * Essentially jexec needs to run java and
4680N/A * needs to be a native ISA executable (not a shell script), although
4680N/A * this native ISA executable requirement was a mistake that will be fixed.
4680N/A * (<ISA> is sparc or i386 or amd64).
4680N/A *
4680N/A * When you execute a jar file, jexec is executed by the system as follows:
4680N/A * /usr/java/jre/lib/<ISA>/jexec -jar JARFILENAME
4680N/A * so this just needs to be turned into:
4680N/A * /usr/java/jre/bin/java -jar JARFILENAME
4680N/A *
4680N/A * Solaris systems (new 7's and all 8's) will be looking for jexec at:
4680N/A * /usr/java/jre/lib/<ISA>/jexec
4680N/A * Older systems may need to add this to their /etc/system file:
4680N/A * set javaexec:jexec="/usr/java/jre/lib/<ISA>/jexec"
4680N/A * and reboot the machine for this to work.
4680N/A *
4680N/A * This source should be compiled as:
4680N/A * cc -o jexec jexec.c
4680N/A *
4680N/A * And jexec should be placed at the following location of the installation:
4680N/A * <INSTALLATIONDIR>/jre/lib/<ISA>/jexec (for Solaris)
4680N/A * <INSTALLATIONDIR>/lib/jexec (for Linux)
4680N/A *
4680N/A * NOTE: Unless <INSTALLATIONDIR> is the "default" JDK on the system
4680N/A * (i.e. /usr/java -> <INSTALLATIONDIR>), this jexec will not be
4680N/A * found. The 1.2 java is only the default on Solaris 8 and
4680N/A * on systems where the 1.2 packages were installed and no 1.1
4680N/A * java was found.
4680N/A *
4680N/A * NOTE: You must use 1.2 jar to build your jar files. The system
4680N/A * doesn't seem to pick up 1.1 jar files.
4680N/A *
4680N/A * NOTE: We don't need to set LD_LIBRARY_PATH here, even though we
4680N/A * are running the actual java binary because the java binary will
4680N/A * look for it's libraries through it's own runpath, which uses
4680N/A * $ORIGIN.
4680N/A *
4680N/A * NOTE: This jexec should NOT have any special .so library needs because
4680N/A * it appears that this executable will NOT get the $ORIGIN of jexec
4680N/A * but the $ORIGIN of the jar file being executed. Be careful to keep
4680N/A * this program simple and with no .so dependencies.
4680N/A */
4680N/A
4680N/A#include <stdlib.h>
4680N/A#include <stdio.h>
4680N/A#include <unistd.h>
4680N/A#include <string.h>
4680N/A#include <limits.h>
4680N/A#include <errno.h>
4680N/A
4680N/Astatic const int CRAZY_EXEC = ENOEXEC;
4680N/Astatic const int BAD_MAGIC = ENOEXEC;
4680N/A
4680N/Astatic const char * BAD_EXEC_MSG = "jexec failed";
4680N/Astatic const char * CRAZY_EXEC_MSG = "missing args";
4680N/Astatic const char * MISSING_JAVA_MSG = "can't locate java";
4680N/Astatic const char * UNKNOWN_ERROR = "unknown error";
4680N/A
4680N/A/* Define a constant that represents the number of directories to pop off the
4680N/A * current location to find the java binary */
4680N/Astatic const int RELATIVE_DEPTH = 3;
4680N/A
4680N/A/* path to java after popping */
4680N/Astatic const char * BIN_PATH = "/bin/java";
4680N/A
4680N/A/* flag used when running JAR files */
4680N/Astatic const char * JAR_FLAG = "-jar";
4680N/A
4680N/Aint main(int argc, const char * argv[]);
4680N/Avoid errorExit(int error, const char * message);
4680N/Aint getJavaPath(const char * path, char * buf, int depth);
4680N/A
4680N/A/*
4680N/A * This is the main entry point. This program (jexec) will attempt to execute
4680N/A * a JAR file by finding the Java program (java), relative to its own location.
4680N/A * The exact location of the Java program depends on the platform, i.e.
4680N/A *
4680N/A * <INSTALLATIONDIR>/jre/lib/<ISA>/jexec (for Solaris)
4680N/A * <INSTALLATIONDIR>/lib/jexec (for Linux JDK)
4680N/A *
4680N/A * Once the Java program is found, this program copies any remaining arguments
4680N/A * into another array, which is then used to exec the Java program.
4680N/A *
4680N/A * On Linux this program does some additional steps. When copying the array of
4680N/A * args, it is necessary to insert the "-jar" flag between arg[0], the program
4680N/A * name, and the original arg[1], which is presumed to be a path to a JAR file.
4680N/A * It is also necessary to verify that the original arg[1] really is a JAR file.
4680N/A * (These steps are unnecessary on Solaris because they are taken care of by
4680N/A * the kernel.)
4680N/A */
4680N/Aint main(int argc, const char * argv[]) {
4680N/A /* We need to exec the original arguments using java, instead of jexec.
4680N/A * Also, for Linux, it is necessary to add the "-jar" argument between
4680N/A * the new arg[0], and the old arg[1]. To do this we will create a new
4680N/A * args array. */
4680N/A char java[PATH_MAX + 1]; /* path to java binary */
4680N/A const char ** nargv = NULL; /* new args array */
4680N/A int nargc = 0; /* new args array count */
4680N/A int argi = 0; /* index into old array */
4680N/A
4680N/A /* Make sure we have something to work with */
4680N/A if ((argc < 1) || (argv == NULL)) {
4680N/A /* Shouldn't happen... */
4680N/A errorExit(CRAZY_EXEC, CRAZY_EXEC_MSG);
4680N/A }
4680N/A
4680N/A /* Get the path to the java binary, which is in a known position relative
4680N/A * to our current position, which is in argv[0]. */
4680N/A if (getJavaPath(argv[argi++], java, RELATIVE_DEPTH) != 0) {
4680N/A errorExit(errno, MISSING_JAVA_MSG);
4680N/A }
4680N/A
4680N/A nargv = (const char **) malloc((argc + 2) * (sizeof (const char *)));
4680N/A nargv[nargc++] = java;
4680N/A
4680N/A if (argc >= 2) {
4680N/A const char * jarfile = argv[argi++];
4680N/A const char * message = NULL;
4680N/A
4680N/A /* the next argument is the path to the JAR file */
4680N/A nargv[nargc++] = jarfile;
4680N/A }
4680N/A
4680N/A /* finally copy any remaining arguments */
4680N/A while (argi < argc) {
4680N/A nargv[nargc++] = argv[argi++];
4680N/A }
4680N/A
4680N/A /* finally add one last terminating null */
4680N/A nargv[nargc++] = NULL;
4680N/A
4680N/A /* It's time to exec the java binary with the new arguments. It
4680N/A * is possible that we've reached this point without actually
4680N/A * having a JAR file argument (i.e. if argc < 2), but we still
4680N/A * want to exec the java binary, since that will take care of
4680N/A * displaying the correct usage. */
4680N/A execv(java, (char * const *) nargv);
4680N/A
4680N/A /* If the exec worked, this process would have been replaced
4680N/A * by the new process. So any code reached beyond this point
4680N/A * implies an error in the exec. */
4680N/A free(nargv);
4680N/A errorExit(errno, BAD_EXEC_MSG);
4680N/A return 0; // keep the compiler happy
4680N/A}
4680N/A
4680N/A
4680N/A/*
4680N/A * Exit the application by setting errno, and writing a message.
4680N/A *
4680N/A * Parameters:
4680N/A * error - errno is set to this value, and it is used to exit.
4680N/A * message - the message to write.
4680N/A */
4680N/Avoid errorExit(int error, const char * message) {
4680N/A if (error != 0) {
4680N/A errno = error;
4680N/A perror((message != NULL) ? message : UNKNOWN_ERROR);
4680N/A }
4680N/A
4680N/A exit((error == 0) ? 0 : 1);
4680N/A}
4680N/A
4680N/A
4680N/A/*
4680N/A * Get the path to the java binary that should be relative to the current path.
4680N/A *
4680N/A * Parameters:
4680N/A * path - the input path that the java binary that should be relative to.
4680N/A * buf - a buffer of size PATH_MAX or greater that the java path is
4680N/A * copied to.
4680N/A * depth - the number of names to trim off the current path, including the
4680N/A * name of this program.
4680N/A *
4680N/A * Returns:
4680N/A * This function returns 0 on success; otherwise it returns the value of
4680N/A * errno.
4680N/A */
4680N/Aint getJavaPath(const char * path, char * buf, int depth) {
4680N/A int result = 0;
4680N/A
4680N/A /* Get the full path to this program. Depending on whether this is Solaris
4680N/A * or Linux, this will be something like,
4680N/A *
4680N/A * <FOO>/jre/lib/<ISA>/jexec (for Solaris)
4680N/A * <FOO>/lib/jexec (for Linux)
4680N/A */
4680N/A if (realpath(path, buf) != NULL) {
4680N/A int count = 0;
4680N/A
4680N/A /* Pop off the filename, and then subdirectories for each level of
4680N/A * depth */
4680N/A for (count = 0; count < depth; count++) {
4680N/A *(strrchr(buf, '/')) = '\0';
4680N/A }
4680N/A
4680N/A /* Append the relative location of java, creating something like,
4680N/A *
4680N/A * <FOO>/jre/bin/java (for Solaris)
4680N/A * <FOO>/bin/java (for Linux)
4680N/A */
4680N/A strcat(buf, BIN_PATH);
4680N/A }
4680N/A else {
4680N/A /* Failed to get the path */
4680N/A result = errno;
4680N/A }
4680N/A
4680N/A return (result);
4680N/A}