Import 2.3.7pre9
[davej-history.git] / fs / binfmt_java.c
blob2bd036d98e685a91fc819eff6eed5bc387f8a964
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;
71 retval = copy_strings_kernel(1, &i_name, bprm);
72 if (retval < 0)
73 return retval;
74 bprm->argc++;
76 i_name = binfmt_java_interpreter;
77 retval = copy_strings_kernel(1, &i_name, bprm);
78 if (retval < 0)
79 return retval;
80 bprm->argc++;
83 * OK, now restart the process with the interpreter's dentry.
85 bprm->filename = binfmt_java_interpreter;
86 dentry = open_namei(binfmt_java_interpreter, 0, 0);
87 retval = PTR_ERR(dentry);
88 if (IS_ERR(dentry))
89 return retval;
91 bprm->dentry = dentry;
92 retval = prepare_binprm(bprm);
93 if (retval < 0)
94 return retval;
96 return search_binary_handler(bprm,regs);
99 static int do_load_applet(struct linux_binprm *bprm,struct pt_regs *regs)
101 char *i_name;
102 struct dentry * dentry;
103 int retval;
105 if (strncmp (bprm->buf, "<!--applet", 10))
106 return -ENOEXEC;
108 dput(bprm->dentry);
109 bprm->dentry = NULL;
112 * Set args: [0] the name of the appletviewer
113 * [1] filename of html file
115 * This is done in reverse order, because of how the
116 * user environment and arguments are stored.
118 remove_arg_zero(bprm);
119 i_name = bprm->filename;
120 retval = copy_strings_kernel(1, &i_name, bprm);
121 if (retval < 0) return retval;
122 bprm->argc++;
124 i_name = binfmt_java_appletviewer;
125 retval = copy_strings_kernel(1, &i_name, bprm);
126 if (retval < 0) return retval;
127 bprm->argc++;
130 * OK, now restart the process with the interpreter's dentry.
132 bprm->filename = binfmt_java_appletviewer;
133 dentry = open_namei(binfmt_java_appletviewer, 0, 0);
134 retval = PTR_ERR(dentry);
135 if (IS_ERR(dentry))
136 return retval;
138 bprm->dentry = dentry;
139 retval = prepare_binprm(bprm);
140 if (retval < 0)
141 return retval;
143 return search_binary_handler(bprm,regs);
146 static int load_java(struct linux_binprm *bprm,struct pt_regs *regs)
148 int retval;
149 MOD_INC_USE_COUNT;
150 retval = do_load_java(bprm,regs);
151 MOD_DEC_USE_COUNT;
152 return retval;
155 static struct linux_binfmt java_format = {
156 #ifndef MODULE
157 NULL, 0, load_java, NULL, NULL
158 #else
159 NULL, &__this_module, load_java, NULL, NULL
160 #endif
163 static int load_applet(struct linux_binprm *bprm,struct pt_regs *regs)
165 int retval;
166 MOD_INC_USE_COUNT;
167 retval = do_load_applet(bprm,regs);
168 MOD_DEC_USE_COUNT;
169 return retval;
172 static struct linux_binfmt applet_format = {
173 #ifndef MODULE
174 NULL, 0, load_applet, NULL, NULL
175 #else
176 NULL, &__this_module, load_applet, NULL, NULL
177 #endif
180 int __init init_java_binfmt(void)
182 register_binfmt(&java_format);
183 return register_binfmt(&applet_format);
186 #ifdef MODULE
187 int init_module(void)
189 return init_java_binfmt();
192 void cleanup_module( void) {
193 unregister_binfmt(&java_format);
194 unregister_binfmt(&applet_format);
196 #endif