remove unnecessary rlim64_t casts of 0
[unleashed.git] / usr / src / uts / common / exec / java / java.c
blob58a99d91cb896f5bef43d4ea8539dbe2bbb2a92e
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
27 * Launch Java executables via exec(2).
29 * Java executables are platform-independent executable files
30 * based on the JAR file format. Executable JAR files contain a
31 * special 'extra field' header in the first file of the archive
32 * that marks the file as a true executable. The data in that field
33 * is used to pass additional run-time information to the Java VM.
35 * This handler looks for the appropriate magic number on the
36 * front of the file, checks that the JAR file is executable, then
37 * invokes the Java runtime environment to do the rest of the work.
40 #include <sys/types.h>
41 #include <sys/proc.h>
42 #include <sys/vnode.h>
43 #include <sys/exec.h>
44 #include <sys/modctl.h>
45 #include <sys/cmn_err.h>
46 #include <sys/pathname.h>
49 * These variables can be tweaked via /etc/system to allow prototyping
50 * and debugging. See PSARC/1997/123.
52 * Modified by PSARC/1999/012 to be Contract Private between Solaris and
53 * the Java Technology Group. It is expected that any future change to
54 * these variables be coordinated between the consolidations.
56 #if defined(__sparc)
57 char *jexec = "/usr/java/jre/lib/sparc/jexec";
58 #elif defined(__i386) || defined(__i386_COMPAT)
59 char *jexec = "/usr/java/jre/lib/i386/jexec";
60 #else
61 #error "Unknown ISA"
62 #endif
63 char *jexec_arg = "-jar";
66 * ZIP/JAR file header information
68 #define SIGSIZ 4
69 #define LOCSIG "PK\003\004"
70 #define LOCHDRSIZ 30
72 #define CH(b, n) (((unsigned char *)(b))[n])
73 #define SH(b, n) (CH(b, n) | (CH(b, n+1) << 8))
74 #define LG(b, n) (SH(b, n) | (SH(b, n+2) << 16))
76 #define LOCNAM(b) (SH(b, 26)) /* filename size */
77 #define LOCEXT(b) (SH(b, 28)) /* extra field size */
79 #define XFHSIZ 4 /* header id, data size */
80 #define XFHID(b) (SH(b, 0)) /* extract field header id */
81 #define XFDATASIZ(b) (SH(b, 2)) /* extract field data size */
82 #define XFJAVASIG 0xcafe /* java executables */
84 /*ARGSUSED3*/
85 static int
86 javaexec(vnode_t *vp, struct execa *uap, struct uarg *args,
87 struct intpdata *idatap, int level, long *execsz, int setid,
88 caddr_t execfile, cred_t *cred, int brand_action)
90 struct intpdata idata;
91 int error;
92 ssize_t resid;
93 vnode_t *nvp;
94 off_t xoff, xoff_end;
95 char lochdr[LOCHDRSIZ];
96 struct pathname lookpn;
97 struct pathname resolvepn;
98 char *opath;
100 if (level)
101 return (ENOEXEC); /* no recursion */
104 * Read in the full local file header, and validate
105 * the initial signature.
107 if ((error = vn_rdwr(UIO_READ, vp, lochdr, sizeof (lochdr),
108 0, UIO_SYSSPACE, 0, 0, cred, &resid)) != 0)
109 return (error);
110 if (resid != 0 || strncmp(lochdr, LOCSIG, SIGSIZ) != 0)
111 return (ENOEXEC);
114 * Ok, so this -is- a ZIP file, and might even be a JAR file.
115 * Is it a Java executable?
117 xoff = sizeof (lochdr) + LOCNAM(lochdr);
118 xoff_end = xoff + LOCEXT(lochdr);
120 while (xoff < xoff_end) {
121 char xfhdr[XFHSIZ];
123 if ((error = vn_rdwr(UIO_READ, vp, xfhdr, sizeof (xfhdr),
124 xoff, UIO_SYSSPACE, 0, 0, cred, &resid)) != 0)
125 return (error);
126 if (resid != 0)
127 return (ENOEXEC);
128 if (XFHID(xfhdr) == XFJAVASIG)
129 break;
130 xoff += sizeof (xfhdr) + XFDATASIZ(xfhdr);
133 if (xoff >= xoff_end)
134 return (ENOEXEC);
137 * Note: If we ever make setid execution work, we need to ensure
138 * that we use /dev/fd to avoid the classic setuid shell script
139 * security hole.
141 if (setid)
142 return (EACCES);
145 * Find and invoke the Java runtime environment on the file
147 idata.intp = NULL;
148 idata.intp_name[0] = jexec;
149 idata.intp_arg[0] = jexec_arg;
150 if (error = pn_get(idata.intp_name[0], UIO_SYSSPACE, &lookpn))
151 return (error);
152 pn_alloc(&resolvepn);
153 if (error = lookuppn(&lookpn, &resolvepn, FOLLOW, NULLVPP, &nvp)) {
154 pn_free(&resolvepn);
155 pn_free(&lookpn);
156 return (ENOEXEC);
158 opath = args->pathname;
159 args->pathname = resolvepn.pn_path;
160 /* don't free resolvepn until we are done with args */
161 pn_free(&lookpn);
162 error = gexec(&nvp, uap, args, &idata, level + 1, execsz, execfile,
163 cred, EBA_NONE);
165 if (!error) {
167 * Close this Java executable as the interpreter
168 * will open and close it later on.
170 (void) fop_close(vp, FREAD, 1, 0, cred, NULL);
173 VN_RELE(nvp);
174 args->pathname = opath;
175 pn_free(&resolvepn);
176 return (error);
179 static struct execsw jexecsw = {
180 javamagicstr,
183 javaexec,
184 NULL
187 static struct modlexec jmodlexec = {
188 &mod_execops, "exec for Java", &jexecsw
191 static struct modlinkage jmodlinkage = {
192 MODREV_1, &jmodlexec, NULL
196 _init(void)
198 return (mod_install(&jmodlinkage));
202 _fini(void)
204 return (mod_remove(&jmodlinkage));
208 _info(struct modinfo *modinfop)
210 return (mod_info(&jmodlinkage, modinfop));