linux-old/fs/binfmt_java.c
<<
>>
Prefs
   1/*
   2 *  linux/fs/binfmt_java.c
   3 *
   4 *  Copyright (C) 1996  Brian A. Lantz
   5 *  derived from binfmt_script.c
   6 *
   7 *  Simplified and modified to support binary java interpreters
   8 *  by Tom May <ftom@netcom.com>.
   9 */
  10
  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>
  17
  18#define _PATH_JAVA      "/usr/bin/java"
  19#define _PATH_APPLET    "/usr/bin/appletviewer"
  20
  21/*  These paths can be modified with sysctl().  */
  22
  23char binfmt_java_interpreter[65] = _PATH_JAVA;
  24char binfmt_java_appletviewer[65] = _PATH_APPLET;
  25
  26static int load_java(struct linux_binprm *bprm,struct pt_regs *regs)
  27{
  28        char *i_name;
  29        int len;
  30        int retval;
  31        struct dentry * dentry;
  32        unsigned char *ucp = (unsigned char *) bprm->buf;
  33
  34        if ((ucp[0] != 0xca) || (ucp[1] != 0xfe) || (ucp[2] != 0xba) || (ucp[3] != 0xbe)) 
  35                return -ENOEXEC;
  36
  37        /*
  38         * Fail if we're called recursively, e.g., the Java interpreter
  39         * is a java binary.
  40         */
  41
  42        if (bprm->java)
  43                return -ENOEXEC;
  44
  45        bprm->java = 1;
  46
  47        dput(bprm->dentry);
  48        bprm->dentry = NULL;
  49
  50        /*
  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.
  58         *
  59         * This is done in reverse order, because of how the
  60         * user environment and arguments are stored.
  61         */
  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++;
  72
  73        i_name = binfmt_java_interpreter;
  74        bprm->p = copy_strings(1, &i_name, bprm->page, bprm->p, 2);
  75        bprm->argc++;
  76
  77        if ((long)bprm->p < 0)
  78                return (long)bprm->p;
  79        /*
  80         * OK, now restart the process with the interpreter's dentry.
  81         */
  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;
  87
  88        bprm->dentry = dentry;
  89        retval = prepare_binprm(bprm);
  90        if (retval < 0)
  91                return retval;
  92
  93        return search_binary_handler(bprm,regs);
  94}
  95
  96static int load_applet(struct linux_binprm *bprm,struct pt_regs *regs)
  97{
  98        char *i_name;
  99        struct dentry * dentry;
 100        int retval;
 101
 102        if (strncmp (bprm->buf, "<!--applet", 10))
 103                return -ENOEXEC;
 104
 105        dput(bprm->dentry);
 106        bprm->dentry = NULL;
 107
 108        /*
 109         * Set args: [0] the name of the appletviewer
 110         *           [1] filename of html file
 111         *
 112         * This is done in reverse order, because of how the
 113         * user environment and arguments are stored.
 114         */
 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++;
 119
 120        i_name = binfmt_java_appletviewer;
 121        bprm->p = copy_strings(1, &i_name, bprm->page, bprm->p, 2);
 122        bprm->argc++;
 123
 124        if ((long)bprm->p < 0)
 125                return (long)bprm->p;
 126        /*
 127         * OK, now restart the process with the interpreter's dentry.
 128         */
 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;
 134
 135        bprm->dentry = dentry;
 136        retval = prepare_binprm(bprm);
 137        if (retval < 0)
 138                return retval;
 139
 140        return search_binary_handler(bprm,regs);
 141}
 142
 143static struct linux_binfmt java_format = {
 144        module:         THIS_MODULE,
 145        load_binary:    load_java,
 146};
 147
 148static struct linux_binfmt applet_format = {
 149        module:         THIS_MODULE,
 150        load_binary:    load_applet,
 151};
 152
 153int __init init_java_binfmt(void)
 154{
 155        register_binfmt(&java_format);
 156        return register_binfmt(&applet_format);
 157}
 158
 159#ifdef MODULE
 160int init_module(void)
 161{
 162        return init_java_binfmt();
 163}
 164
 165void cleanup_module( void) {
 166        unregister_binfmt(&java_format);
 167        unregister_binfmt(&applet_format);
 168}
 169#endif
 170
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.