pre-2.3.4..
[davej-history.git] / fs / binfmt_java.c
blobca1ad396c34b6fca6ba1b4ac71bf9ca1cac147ba
1 /*
2 * linux/fs/binfmt_java.c
4 * Copyright (C) 1996 Brian A. Lantz
5 * derived from binfmt_script.c
7 * Simplified and modified to support binary java interpreters
8 * by Tom May <ftom@netcom.com>.
9 */
11 #include <linux/module.h>
12 #include <linux/string.h>
13 #include <linux/stat.h>
14 #include <linux/malloc.h>
15 #include <linux/binfmts.h>
16 #include <linux/init.h>
18 #define _PATH_JAVA "/usr/bin/java"
19 #define _PATH_APPLET "/usr/bin/appletviewer"
21 /* These paths can be modified with sysctl(). */
23 char binfmt_java_interpreter[65] = _PATH_JAVA;
24 char binfmt_java_appletviewer[65] = _PATH_APPLET;
26 static int do_load_java(struct linux_binprm *bprm,struct pt_regs *regs)
28 char *i_name;
29 int len;
30 int retval;
31 struct dentry * dentry;
32 unsigned char *ucp = (unsigned char *) bprm->buf;
34 if ((ucp[0] != 0xca) || (ucp[1] != 0xfe) || (ucp[2] != 0xba) || (ucp[3] != 0xbe))
35 return -ENOEXEC;
38 * Fail if we're called recursively, e.g., the Java interpreter
39 * is a java binary.
42 if (bprm->java)
43 return -ENOEXEC;
45 bprm->java = 1;
47 dput(bprm->dentry);
48 bprm->dentry = NULL;
51 * Set args: [0] the name of the java interpreter
52 * [1] name of java class to execute, which is the
53 * filename without the path and without trailing
54 * ".class". Note that the interpreter will use
55 * its own way to found the class file (typically using
56 * environment variable CLASSPATH), and may in fact
57 * execute a different file from the one we want.
59 * This is done in reverse order, because of how the
60 * user environment and arguments are stored.
62 remove_arg_zero(bprm);
63 len = strlen (bprm->filename);
64 if (len >= 6 && !strcmp (bprm->filename + len - 6, ".class"))
65 bprm->filename[len - 6] = 0;
66 if ((i_name = strrchr (bprm->filename, '/')) != NULL)
67 i_name++;
68 else
69 i_name = bprm->filename;
70 bprm->p = copy_strings(1, &i_name, bprm->page, bprm->p, 2);
71 bprm->argc++;
73 i_name = binfmt_java_interpreter;
74 bprm->p = copy_strings(1, &i_name, bprm->page, bprm->p, 2);
75 bprm->argc++;
77 if (!bprm->p)
78 return -E2BIG;
80 * OK, now restart the process with the interpreter's dentry.
82 bprm->filename = binfmt_java_interpreter;
83 dentry = open_namei(binfmt_java_interpreter, 0, 0);
84 retval = PTR_ERR(dentry);
85 if (IS_ERR(dentry))
86 return retval;
88 bprm->dentry = dentry;
89 retval = prepare_binprm(bprm);
90 if (retval < 0)
91 return retval;
93 return search_binary_handler(bprm,regs);
96 static int do_load_applet(struct linux_binprm *bprm,struct pt_regs *regs)
98 char *i_name;
99 struct dentry * dentry;
100 int retval;
102 if (strncmp (bprm->buf, "<!--applet", 10))
103 return -ENOEXEC;
105 dput(bprm->dentry);
106 bprm->dentry = NULL;
109 * Set args: [0] the name of the appletviewer
110 * [1] filename of html file
112 * This is done in reverse order, because of how the
113 * user environment and arguments are stored.
115 remove_arg_zero(bprm);
116 i_name = bprm->filename;
117 bprm->p = copy_strings(1, &i_name, bprm->page, bprm->p, 2);
118 bprm->argc++;
120 i_name = binfmt_java_appletviewer;
121 bprm->p = copy_strings(1, &i_name, bprm->page, bprm->p, 2);
122 bprm->argc++;
124 if (!bprm->p)
125 return -E2BIG;
127 * OK, now restart the process with the interpreter's dentry.
129 bprm->filename = binfmt_java_appletviewer;
130 dentry = open_namei(binfmt_java_appletviewer, 0, 0);
131 retval = PTR_ERR(dentry);
132 if (IS_ERR(dentry))
133 return retval;
135 bprm->dentry = dentry;
136 retval = prepare_binprm(bprm);
137 if (retval < 0)
138 return retval;
140 return search_binary_handler(bprm,regs);
143 static int load_java(struct linux_binprm *bprm,struct pt_regs *regs)
145 int retval;
146 MOD_INC_USE_COUNT;
147 retval = do_load_java(bprm,regs);
148 MOD_DEC_USE_COUNT;
149 return retval;
152 static struct linux_binfmt java_format = {
153 #ifndef MODULE
154 NULL, 0, load_java, NULL, NULL
155 #else
156 NULL, &__this_module, load_java, NULL, NULL
157 #endif
160 static int load_applet(struct linux_binprm *bprm,struct pt_regs *regs)
162 int retval;
163 MOD_INC_USE_COUNT;
164 retval = do_load_applet(bprm,regs);
165 MOD_DEC_USE_COUNT;
166 return retval;
169 static struct linux_binfmt applet_format = {
170 #ifndef MODULE
171 NULL, 0, load_applet, NULL, NULL
172 #else
173 NULL, &__this_module, load_applet, NULL, NULL
174 #endif
177 int __init init_java_binfmt(void)
179 register_binfmt(&java_format);
180 return register_binfmt(&applet_format);
183 #ifdef MODULE
184 int init_module(void)
186 return init_java_binfmt();
189 void cleanup_module( void) {
190 unregister_binfmt(&java_format);
191 unregister_binfmt(&applet_format);
193 #endif