/*
|
/*
|
* linux/fs/binfmt_script.c
|
* linux/fs/binfmt_script.c
|
*
|
*
|
* Copyright (C) 1996 Martin von Löwis
|
* Copyright (C) 1996 Martin von Löwis
|
* original #!-checking implemented by tytso.
|
* original #!-checking implemented by tytso.
|
*
|
*
|
*/
|
*/
|
|
|
/*
|
/*
|
* uClinux revisions for NO_MM
|
* uClinux revisions for NO_MM
|
* Copyright (C) 1998 Kenneth Albanowski <kjahds@kjahds.com>,
|
* Copyright (C) 1998 Kenneth Albanowski <kjahds@kjahds.com>,
|
* The Silver Hammer Group, Ltd.
|
* The Silver Hammer Group, Ltd.
|
*
|
*
|
* FIXME: Modifications are untested!
|
* FIXME: Modifications are untested!
|
*/
|
*/
|
|
|
#include <linux/module.h>
|
#include <linux/module.h>
|
#include <linux/string.h>
|
#include <linux/string.h>
|
#include <linux/stat.h>
|
#include <linux/stat.h>
|
#include <linux/malloc.h>
|
#include <linux/malloc.h>
|
#include <linux/binfmts.h>
|
#include <linux/binfmts.h>
|
|
|
static int do_load_script(struct linux_binprm *bprm,struct pt_regs *regs)
|
static int do_load_script(struct linux_binprm *bprm,struct pt_regs *regs)
|
{
|
{
|
char *cp, *interp, *i_name, *i_arg;
|
char *cp, *interp, *i_name, *i_arg;
|
int retval;
|
int retval;
|
if ((bprm->buf[0] != '#') || (bprm->buf[1] != '!') || (bprm->sh_bang))
|
if ((bprm->buf[0] != '#') || (bprm->buf[1] != '!') || (bprm->sh_bang))
|
return -ENOEXEC;
|
return -ENOEXEC;
|
/*
|
/*
|
* This section does the #! interpretation.
|
* This section does the #! interpretation.
|
* Sorta complicated, but hopefully it will work. -TYT
|
* Sorta complicated, but hopefully it will work. -TYT
|
*/
|
*/
|
|
|
bprm->sh_bang++;
|
bprm->sh_bang++;
|
iput(bprm->inode);
|
iput(bprm->inode);
|
bprm->dont_iput=1;
|
bprm->dont_iput=1;
|
|
|
bprm->buf[127] = '\0';
|
bprm->buf[127] = '\0';
|
if ((cp = strchr(bprm->buf, '\n')) == NULL)
|
if ((cp = strchr(bprm->buf, '\n')) == NULL)
|
cp = bprm->buf+127;
|
cp = bprm->buf+127;
|
*cp = '\0';
|
*cp = '\0';
|
while (cp > bprm->buf) {
|
while (cp > bprm->buf) {
|
cp--;
|
cp--;
|
if ((*cp == ' ') || (*cp == '\t'))
|
if ((*cp == ' ') || (*cp == '\t'))
|
*cp = '\0';
|
*cp = '\0';
|
else
|
else
|
break;
|
break;
|
}
|
}
|
for (cp = bprm->buf+2; (*cp == ' ') || (*cp == '\t'); cp++);
|
for (cp = bprm->buf+2; (*cp == ' ') || (*cp == '\t'); cp++);
|
if (!cp || *cp == '\0')
|
if (!cp || *cp == '\0')
|
return -ENOEXEC; /* No interpreter name found */
|
return -ENOEXEC; /* No interpreter name found */
|
interp = i_name = cp;
|
interp = i_name = cp;
|
i_arg = 0;
|
i_arg = 0;
|
for ( ; *cp && (*cp != ' ') && (*cp != '\t'); cp++) {
|
for ( ; *cp && (*cp != ' ') && (*cp != '\t'); cp++) {
|
if (*cp == '/')
|
if (*cp == '/')
|
i_name = cp+1;
|
i_name = cp+1;
|
}
|
}
|
while ((*cp == ' ') || (*cp == '\t'))
|
while ((*cp == ' ') || (*cp == '\t'))
|
*cp++ = '\0';
|
*cp++ = '\0';
|
if (*cp)
|
if (*cp)
|
i_arg = cp;
|
i_arg = cp;
|
/*
|
/*
|
* OK, we've parsed out the interpreter name and
|
* OK, we've parsed out the interpreter name and
|
* (optional) argument.
|
* (optional) argument.
|
* Splice in (1) the interpreter's name for argv[0]
|
* Splice in (1) the interpreter's name for argv[0]
|
* (2) (optional) argument to interpreter
|
* (2) (optional) argument to interpreter
|
* (3) filename of shell script (replace argv[0])
|
* (3) filename of shell script (replace argv[0])
|
*
|
*
|
* This is done in reverse order, because of how the
|
* This is done in reverse order, because of how the
|
* user environment and arguments are stored.
|
* user environment and arguments are stored.
|
*/
|
*/
|
#ifndef NO_MM
|
#ifndef NO_MM
|
remove_arg_zero(bprm);
|
remove_arg_zero(bprm);
|
bprm->p = copy_strings(1, &bprm->filename, bprm->page, bprm->p, 2);
|
bprm->p = copy_strings(1, &bprm->filename, bprm->page, bprm->p, 2);
|
bprm->argc++;
|
bprm->argc++;
|
if (i_arg) {
|
if (i_arg) {
|
bprm->p = copy_strings(1, &i_arg, bprm->page, bprm->p, 2);
|
bprm->p = copy_strings(1, &i_arg, bprm->page, bprm->p, 2);
|
bprm->argc++;
|
bprm->argc++;
|
}
|
}
|
bprm->p = copy_strings(1, &i_name, bprm->page, bprm->p, 2);
|
bprm->p = copy_strings(1, &i_name, bprm->page, bprm->p, 2);
|
bprm->argc++;
|
bprm->argc++;
|
if (!bprm->p)
|
if (!bprm->p)
|
return -E2BIG;
|
return -E2BIG;
|
/*
|
/*
|
* OK, now restart the process with the interpreter's inode.
|
* OK, now restart the process with the interpreter's inode.
|
* Note that we use open_namei() as the name is now in kernel
|
* Note that we use open_namei() as the name is now in kernel
|
* space, and we don't need to copy it.
|
* space, and we don't need to copy it.
|
*/
|
*/
|
retval = open_namei(interp, 0, 0, &bprm->inode, NULL);
|
retval = open_namei(interp, 0, 0, &bprm->inode, NULL);
|
if (retval)
|
if (retval)
|
return retval;
|
return retval;
|
bprm->dont_iput=0;
|
bprm->dont_iput=0;
|
retval=prepare_binprm(bprm);
|
retval=prepare_binprm(bprm);
|
if(retval<0)
|
if(retval<0)
|
return retval;
|
return retval;
|
return search_binary_handler(bprm,regs);
|
return search_binary_handler(bprm,regs);
|
#else /* !NO_MM */
|
#else /* !NO_MM */
|
{
|
{
|
char ** oldargv = bprm->argv;
|
char ** oldargv = bprm->argv;
|
int oldargc = bprm->argc;
|
int oldargc = bprm->argc;
|
int i;
|
int i;
|
|
|
bprm->argc = oldargc + (i_arg ? 2 : 1);
|
bprm->argc = oldargc + (i_arg ? 2 : 1);
|
bprm->argv = kmalloc(sizeof(char*) * bprm->argc, GFP_KERNEL);
|
bprm->argv = kmalloc(sizeof(char*) * bprm->argc, GFP_KERNEL);
|
|
|
if (!bprm->argv) {
|
if (!bprm->argv) {
|
bprm->argc = oldargc;
|
bprm->argc = oldargc;
|
bprm->argv = oldargv;
|
bprm->argv = oldargv;
|
return -E2BIG;
|
return -E2BIG;
|
}
|
}
|
|
|
for(i=1;i<=oldargc;i++)
|
for(i=1;i<=oldargc;i++)
|
bprm->argv[bprm->argc-i] = oldargv[oldargc-i];
|
bprm->argv[bprm->argc-i] = oldargv[oldargc-i];
|
|
|
bprm->argv[0] = i_name;
|
bprm->argv[0] = i_name;
|
i=1;
|
i=1;
|
if (i_arg)
|
if (i_arg)
|
bprm->argv[i++] = i_arg;
|
bprm->argv[i++] = i_arg;
|
bprm->argv[i++] = bprm->filename;
|
bprm->argv[i++] = bprm->filename;
|
|
|
|
|
/*
|
/*
|
* OK, now restart the process with the interpreter's inode.
|
* OK, now restart the process with the interpreter's inode.
|
* Note that we use open_namei() as the name is now in kernel
|
* Note that we use open_namei() as the name is now in kernel
|
* space, and we don't need to copy it.
|
* space, and we don't need to copy it.
|
*/
|
*/
|
retval = open_namei(interp, 0, 0, &bprm->inode, NULL);
|
retval = open_namei(interp, 0, 0, &bprm->inode, NULL);
|
if (!retval) {
|
if (!retval) {
|
bprm->dont_iput=0;
|
bprm->dont_iput=0;
|
retval=prepare_binprm(bprm);
|
retval=prepare_binprm(bprm);
|
}
|
}
|
if (!retval) {
|
if (!retval) {
|
retval = search_binary_handler(bprm,regs);
|
retval = search_binary_handler(bprm,regs);
|
}
|
}
|
|
|
kfree(bprm->argv);
|
kfree(bprm->argv);
|
bprm->argv = oldargv;
|
bprm->argv = oldargv;
|
bprm->argc = oldargc;
|
bprm->argc = oldargc;
|
|
|
return retval;
|
return retval;
|
}
|
}
|
#endif /* !NO_MM */
|
#endif /* !NO_MM */
|
}
|
}
|
|
|
static int load_script(struct linux_binprm *bprm,struct pt_regs *regs)
|
static int load_script(struct linux_binprm *bprm,struct pt_regs *regs)
|
{
|
{
|
int retval;
|
int retval;
|
MOD_INC_USE_COUNT;
|
MOD_INC_USE_COUNT;
|
retval = do_load_script(bprm,regs);
|
retval = do_load_script(bprm,regs);
|
MOD_DEC_USE_COUNT;
|
MOD_DEC_USE_COUNT;
|
return retval;
|
return retval;
|
}
|
}
|
|
|
struct linux_binfmt script_format = {
|
struct linux_binfmt script_format = {
|
#ifndef MODULE
|
#ifndef MODULE
|
NULL, 0, load_script, NULL, NULL
|
NULL, 0, load_script, NULL, NULL
|
#else
|
#else
|
NULL, &mod_use_count_, load_script, NULL, NULL
|
NULL, &mod_use_count_, load_script, NULL, NULL
|
#endif
|
#endif
|
};
|
};
|
|
|
int init_script_binfmt(void) {
|
int init_script_binfmt(void) {
|
return register_binfmt(&script_format);
|
return register_binfmt(&script_format);
|
}
|
}
|
|
|
#ifdef MODULE
|
#ifdef MODULE
|
int init_module(void)
|
int init_module(void)
|
{
|
{
|
return init_script_binfmt();
|
return init_script_binfmt();
|
}
|
}
|
|
|
void cleanup_module( void) {
|
void cleanup_module( void) {
|
unregister_binfmt(&script_format);
|
unregister_binfmt(&script_format);
|
}
|
}
|
#endif
|
#endif
|
|
|