jdk/src/solaris/bin/jexec.c
author pchelko
Wed, 23 Apr 2014 17:56:05 +0400
changeset 24529 c580bcb3aabc
parent 17445 6c4e2d6b953b
permissions -rw-r--r--
8027148: SystemFlavorMap.getNativesForFlavor returns list of native formats in incorrect order Reviewed-by: anthony, serb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
17445
6c4e2d6b953b 8013736: [launcher] cleanup code for correctness
ksrini
parents: 13675
diff changeset
     2
 * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * jexec for J2SE
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * jexec is used by the system to allow execution of JAR files.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *    Essentially jexec needs to run java and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 *    needs to be a native ISA executable (not a shell script), although
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 *    this native ISA executable requirement was a mistake that will be fixed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 *    (<ISA> is sparc or i386 or amd64).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 *    When you execute a jar file, jexec is executed by the system as follows:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *      /usr/java/jre/lib/<ISA>/jexec -jar JARFILENAME
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *    so this just needs to be turned into:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *      /usr/java/jre/bin/java -jar JARFILENAME
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * Solaris systems (new 7's and all 8's) will be looking for jexec at:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *      /usr/java/jre/lib/<ISA>/jexec
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * Older systems may need to add this to their /etc/system file:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *      set javaexec:jexec="/usr/java/jre/lib/<ISA>/jexec"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *     and reboot the machine for this to work.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * This source should be compiled as:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *      cc -o jexec jexec.c
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * And jexec should be placed at the following location of the installation:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *      <INSTALLATIONDIR>/jre/lib/<ISA>/jexec  (for Solaris)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *      <INSTALLATIONDIR>/lib/jexec            (for Linux)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * NOTE: Unless <INSTALLATIONDIR> is the "default" JDK on the system
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *       (i.e. /usr/java -> <INSTALLATIONDIR>), this jexec will not be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *       found.  The 1.2 java is only the default on Solaris 8 and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *       on systems where the 1.2 packages were installed and no 1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *       java was found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * NOTE: You must use 1.2 jar to build your jar files. The system
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *       doesn't seem to pick up 1.1 jar files.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * NOTE: We don't need to set LD_LIBRARY_PATH here, even though we
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *       are running the actual java binary because the java binary will
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *       look for it's libraries through it's own runpath, which uses
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *       $ORIGIN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * NOTE: This jexec should NOT have any special .so library needs because
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *       it appears that this executable will NOT get the $ORIGIN of jexec
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *       but the $ORIGIN of the jar file being executed. Be careful to keep
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *       this program simple and with no .so dependencies.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
#include <stdlib.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
#include <stdio.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
#include <unistd.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
#include <string.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
#include <limits.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
#include <errno.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
#  include <sys/types.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
#  include <sys/stat.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
#  include <fcntl.h>
13675
c2999ee84468 7194005: (launcher) needs to be enhanced for 64-bit jar file handling
ksrini
parents: 7668
diff changeset
    83
#  include "jni.h"
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
#  include "manifest_info.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
static const int CRAZY_EXEC = ENOEXEC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
static const int BAD_MAGIC  = ENOEXEC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
static const char * BAD_EXEC_MSG     = "jexec failed";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
static const char * CRAZY_EXEC_MSG   = "missing args";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
static const char * MISSING_JAVA_MSG = "can't locate java";
17445
6c4e2d6b953b 8013736: [launcher] cleanup code for correctness
ksrini
parents: 13675
diff changeset
    93
static const char * BAD_ARG_MSG      = "incorrect number of arguments";
6c4e2d6b953b 8013736: [launcher] cleanup code for correctness
ksrini
parents: 13675
diff changeset
    94
static const char * MEM_FAILED_MSG   = "memory allocation failed";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
static const char * BAD_PATHNAME_MSG = "invalid path";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
static const char * BAD_FILE_MSG     = "invalid file";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
static const char * BAD_MAGIC_MSG    = "invalid file (bad magic number)";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
static const char * UNKNOWN_ERROR    = "unknown error";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
/* Define a constant that represents the number of directories to pop off the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * current location to find the java binary */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
static const int RELATIVE_DEPTH = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
#else /* Solaris */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
static const int RELATIVE_DEPTH = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
/* path to java after popping */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
static const char * BIN_PATH = "/bin/java";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
/* flag used when running JAR files */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
static const char * JAR_FLAG = "-jar";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
/* largest possible size for a local file header */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
static const size_t CHUNK_SIZE = 65535;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
/* smallest possible size for a local file header */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
static const ssize_t MIN_SIZE = LOCHDR + 1 + 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
int main(int argc, const char * argv[]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
void errorExit(int error, const char * message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
int getJavaPath(const char * path, char * buf, int depth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
const char * isJar(const char * path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 * This is the main entry point.  This program (jexec) will attempt to execute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
 * a JAR file by finding the Java program (java), relative to its own location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
 * The exact location of the Java program depends on the platform, i.e.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
 *      <INSTALLATIONDIR>/jre/lib/<ISA>/jexec  (for Solaris)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
 *      <INSTALLATIONDIR>/lib/jexec            (for Linux JDK)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
 * Once the Java program is found, this program copies any remaining arguments
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
 * into another array, which is then used to exec the Java program.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
 * On Linux this program does some additional steps.  When copying the array of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
 * args, it is necessary to insert the "-jar" flag between arg[0], the program
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
 * name, and the original arg[1], which is presumed to be a path to a JAR file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
 * It is also necessary to verify that the original arg[1] really is a JAR file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
 * (These steps are unnecessary on Solaris because they are taken care of by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
 * the kernel.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
int main(int argc, const char * argv[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    /* We need to exec the original arguments using java, instead of jexec.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * Also, for Linux, it is necessary to add the "-jar" argument between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * the new arg[0], and the old arg[1].  To do this we will create a new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * args array. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    char          java[PATH_MAX + 1];    /* path to java binary  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    const char ** nargv = NULL;          /* new args array       */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    int           nargc = 0;             /* new args array count */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    int           argi  = 0;             /* index into old array */
17445
6c4e2d6b953b 8013736: [launcher] cleanup code for correctness
ksrini
parents: 13675
diff changeset
   161
    size_t        alen  = 0;             /* length of new array */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    /* Make sure we have something to work with */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    if ((argc < 1) || (argv == NULL)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        /* Shouldn't happen... */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        errorExit(CRAZY_EXEC, CRAZY_EXEC_MSG);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    /* Get the path to the java binary, which is in a known position relative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * to our current position, which is in argv[0]. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    if (getJavaPath(argv[argi++], java, RELATIVE_DEPTH) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        errorExit(errno, MISSING_JAVA_MSG);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    }
17445
6c4e2d6b953b 8013736: [launcher] cleanup code for correctness
ksrini
parents: 13675
diff changeset
   174
    alen = (argc + 2) * (sizeof (const char *));
6c4e2d6b953b 8013736: [launcher] cleanup code for correctness
ksrini
parents: 13675
diff changeset
   175
    if (alen <= 0 || alen > INT_MAX / sizeof(char *)) {
6c4e2d6b953b 8013736: [launcher] cleanup code for correctness
ksrini
parents: 13675
diff changeset
   176
        errorExit(errno, BAD_ARG_MSG);
6c4e2d6b953b 8013736: [launcher] cleanup code for correctness
ksrini
parents: 13675
diff changeset
   177
    }
6c4e2d6b953b 8013736: [launcher] cleanup code for correctness
ksrini
parents: 13675
diff changeset
   178
    nargv = (const char **) malloc(alen);
6c4e2d6b953b 8013736: [launcher] cleanup code for correctness
ksrini
parents: 13675
diff changeset
   179
    if (nargv == NULL) {
6c4e2d6b953b 8013736: [launcher] cleanup code for correctness
ksrini
parents: 13675
diff changeset
   180
        errorExit(errno, MEM_FAILED_MSG);
6c4e2d6b953b 8013736: [launcher] cleanup code for correctness
ksrini
parents: 13675
diff changeset
   181
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    nargv[nargc++] = java;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    /* The "-jar" flag is already in the original args list on Solaris,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * so it only needs to be added on Linux. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    nargv[nargc++] = JAR_FLAG;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    if (argc >= 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        const char * jarfile = argv[argi++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        const char * message = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        /* On Linux we also need to make sure argv[1] is really a JAR
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
         * file (this will also resolve any symlinks, which helps). */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        char jarPath[PATH_MAX + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        if (realpath(jarfile, jarPath) == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            errorExit(errno, BAD_PATHNAME_MSG);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        message = isJar(jarPath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        if (message != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            errorExit(errno, message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        jarfile = jarPath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        /* the next argument is the path to the JAR file */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        nargv[nargc++] = jarfile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    /* finally copy any remaining arguments */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    while (argi < argc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        nargv[nargc++] = argv[argi++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    /* finally add one last terminating null */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    nargv[nargc++] = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    /* It's time to exec the java binary with the new arguments.  It
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * is possible that we've reached this point without actually
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * having a JAR file argument (i.e. if argc < 2), but we still
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * want to exec the java binary, since that will take care of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * displaying the correct usage. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    execv(java, (char * const *) nargv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    /* If the exec worked, this process would have been replaced
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * by the new process.  So any code reached beyond this point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * implies an error in the exec. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    free(nargv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    errorExit(errno, BAD_EXEC_MSG);
7028
adadd244f506 6989469: (launcher) compiler warnings in jli native code
ksrini
parents: 5506
diff changeset
   234
    return 0; // keep the compiler happy
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
 * Exit the application by setting errno, and writing a message.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
 * Parameters:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
 *     error   - errno is set to this value, and it is used to exit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
 *     message - the message to write.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
void errorExit(int error, const char * message) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    if (error != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        errno = error;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        perror((message != NULL) ? message : UNKNOWN_ERROR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    exit((error == 0) ? 0 : 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
 * Get the path to the java binary that should be relative to the current path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
 * Parameters:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
 *     path  - the input path that the java binary that should be relative to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
 *     buf   - a buffer of size PATH_MAX or greater that the java path is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
 *             copied to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
 *     depth - the number of names to trim off the current path, including the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
 *             name of this program.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
 * Returns:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
 *     This function returns 0 on success; otherwise it returns the value of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
 *     errno.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
int getJavaPath(const char * path, char * buf, int depth) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    int result = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    /* Get the full path to this program.  Depending on whether this is Solaris
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * or Linux, this will be something like,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     *     <FOO>/jre/lib/<ISA>/jexec  (for Solaris)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     *     <FOO>/lib/jexec            (for Linux)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    if (realpath(path, buf) != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        /* Pop off the filename, and then subdirectories for each level of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
         * depth */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        for (count = 0; count < depth; count++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            *(strrchr(buf, '/')) = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        /* Append the relative location of java, creating something like,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
         *     <FOO>/jre/bin/java  (for Solaris)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
         *     <FOO>/bin/java      (for Linux)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        strcat(buf, BIN_PATH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        /* Failed to get the path */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        result = errno;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    return (result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
#ifdef __linux__
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
 * Check if the given file is a JAR file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
 * Parameters:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
 *     path  - the path to the file to check for JAR magic.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
 * Returns:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
 *     This function return NULL on success.  Otherwise, errno is set, and it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
 *     returns a message that indicates what caused the failure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
const char * isJar(const char * path) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    const char * result = BAD_FILE_MSG;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    int fd = open(path, O_RDONLY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    if (fd != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        unsigned char buf[CHUNK_SIZE];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        ssize_t count = read(fd, buf, CHUNK_SIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        if (count >= MIN_SIZE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            result = BAD_MAGIC_MSG;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            // be sure the file is at least a ZIP file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            if (GETSIG(buf) == LOCSIG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                off_t flen  = LOCNAM(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                off_t xlen  = LOCEXT(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                off_t start = LOCHDR + flen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                off_t end   = start  + xlen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                if (end <= count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                    while (start < end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                        off_t xhid  = SH(buf, start);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                        off_t xdlen = SH(buf, start + 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                        start += 4 + xdlen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                        if (xhid == 0xcafe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                            // found the JAR magic
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                            result = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        if (result != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            errno = BAD_MAGIC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        close (fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    return (result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
#endif