74 #include <stdio.h>
75 #include <unistd.h>
76 #include <string.h>
77 #include <limits.h>
78 #include <errno.h>
79 #ifdef __linux__
80 # include <sys/types.h>
81 # include <sys/stat.h>
82 # include <fcntl.h>
83 # include "jni.h"
84 # include "manifest_info.h"
85 #endif
86
87 static const int CRAZY_EXEC = ENOEXEC;
88 static const int BAD_MAGIC = ENOEXEC;
89
90 static const char * BAD_EXEC_MSG = "jexec failed";
91 static const char * CRAZY_EXEC_MSG = "missing args";
92 static const char * MISSING_JAVA_MSG = "can't locate java";
93 static const char * BAD_ARG_MSG = "incorrect number of arguments";
94 #ifdef __linux__
95 static const char * BAD_PATHNAME_MSG = "invalid path";
96 static const char * BAD_FILE_MSG = "invalid file";
97 static const char * BAD_MAGIC_MSG = "invalid file (bad magic number)";
98 #endif
99 static const char * UNKNOWN_ERROR = "unknown error";
100
101 /* Define a constant that represents the number of directories to pop off the
102 * current location to find the java binary */
103 #ifdef __linux__
104 static const int RELATIVE_DEPTH = 2;
105 #else /* Solaris */
106 static const int RELATIVE_DEPTH = 3;
107 #endif
108
109 /* path to java after popping */
110 static const char * BIN_PATH = "/bin/java";
111
112 /* flag used when running JAR files */
113 static const char * JAR_FLAG = "-jar";
155 * args array. */
156 char java[PATH_MAX + 1]; /* path to java binary */
157 const char ** nargv = NULL; /* new args array */
158 int nargc = 0; /* new args array count */
159 int argi = 0; /* index into old array */
160 size_t alen = 0; /* length of new array */
161
162 /* Make sure we have something to work with */
163 if ((argc < 1) || (argv == NULL)) {
164 /* Shouldn't happen... */
165 errorExit(CRAZY_EXEC, CRAZY_EXEC_MSG);
166 }
167
168 /* Get the path to the java binary, which is in a known position relative
169 * to our current position, which is in argv[0]. */
170 if (getJavaPath(argv[argi++], java, RELATIVE_DEPTH) != 0) {
171 errorExit(errno, MISSING_JAVA_MSG);
172 }
173 alen = (argc + 2) * (sizeof (const char *));
174 if (alen <= 0 || alen > INT_MAX / sizeof(char *)) {
175 errorExit(3, BAD_ARG_MSG);
176 }
177 nargv = (const char **) malloc(alen);
178 nargv[nargc++] = java;
179
180 #ifdef __linux__
181 /* The "-jar" flag is already in the original args list on Solaris,
182 * so it only needs to be added on Linux. */
183 nargv[nargc++] = JAR_FLAG;
184 #endif
185
186 if (argc >= 2) {
187 const char * jarfile = argv[argi++];
188 const char * message = NULL;
189
190 #ifdef __linux__
191 /* On Linux we also need to make sure argv[1] is really a JAR
192 * file (this will also resolve any symlinks, which helps). */
193 char jarPath[PATH_MAX + 1];
194
195 if (realpath(jarfile, jarPath) == NULL) {
196 errorExit(errno, BAD_PATHNAME_MSG);
197 }
|
74 #include <stdio.h>
75 #include <unistd.h>
76 #include <string.h>
77 #include <limits.h>
78 #include <errno.h>
79 #ifdef __linux__
80 # include <sys/types.h>
81 # include <sys/stat.h>
82 # include <fcntl.h>
83 # include "jni.h"
84 # include "manifest_info.h"
85 #endif
86
87 static const int CRAZY_EXEC = ENOEXEC;
88 static const int BAD_MAGIC = ENOEXEC;
89
90 static const char * BAD_EXEC_MSG = "jexec failed";
91 static const char * CRAZY_EXEC_MSG = "missing args";
92 static const char * MISSING_JAVA_MSG = "can't locate java";
93 static const char * BAD_ARG_MSG = "incorrect number of arguments";
94 static const char * MEM_FAILED_MSG = "memory allocation failed";
95 #ifdef __linux__
96 static const char * BAD_PATHNAME_MSG = "invalid path";
97 static const char * BAD_FILE_MSG = "invalid file";
98 static const char * BAD_MAGIC_MSG = "invalid file (bad magic number)";
99 #endif
100 static const char * UNKNOWN_ERROR = "unknown error";
101
102 /* Define a constant that represents the number of directories to pop off the
103 * current location to find the java binary */
104 #ifdef __linux__
105 static const int RELATIVE_DEPTH = 2;
106 #else /* Solaris */
107 static const int RELATIVE_DEPTH = 3;
108 #endif
109
110 /* path to java after popping */
111 static const char * BIN_PATH = "/bin/java";
112
113 /* flag used when running JAR files */
114 static const char * JAR_FLAG = "-jar";
156 * args array. */
157 char java[PATH_MAX + 1]; /* path to java binary */
158 const char ** nargv = NULL; /* new args array */
159 int nargc = 0; /* new args array count */
160 int argi = 0; /* index into old array */
161 size_t alen = 0; /* length of new array */
162
163 /* Make sure we have something to work with */
164 if ((argc < 1) || (argv == NULL)) {
165 /* Shouldn't happen... */
166 errorExit(CRAZY_EXEC, CRAZY_EXEC_MSG);
167 }
168
169 /* Get the path to the java binary, which is in a known position relative
170 * to our current position, which is in argv[0]. */
171 if (getJavaPath(argv[argi++], java, RELATIVE_DEPTH) != 0) {
172 errorExit(errno, MISSING_JAVA_MSG);
173 }
174 alen = (argc + 2) * (sizeof (const char *));
175 if (alen <= 0 || alen > INT_MAX / sizeof(char *)) {
176 errorExit(errno, BAD_ARG_MSG);
177 }
178 nargv = (const char **) malloc(alen);
179 if (nargv == NULL) {
180 errorExit(errno, MEM_FAILED_MSG);
181 }
182 nargv[nargc++] = java;
183
184 #ifdef __linux__
185 /* The "-jar" flag is already in the original args list on Solaris,
186 * so it only needs to be added on Linux. */
187 nargv[nargc++] = JAR_FLAG;
188 #endif
189
190 if (argc >= 2) {
191 const char * jarfile = argv[argi++];
192 const char * message = NULL;
193
194 #ifdef __linux__
195 /* On Linux we also need to make sure argv[1] is really a JAR
196 * file (this will also resolve any symlinks, which helps). */
197 char jarPath[PATH_MAX + 1];
198
199 if (realpath(jarfile, jarPath) == NULL) {
200 errorExit(errno, BAD_PATHNAME_MSG);
201 }
|