2 * linux/fs/binfmt_script.c
4 * Copyright (C) 1996 Martin von Löwis
5 * original #!-checking implemented by tytso.
8 #include <linux/module.h>
9 #include <linux/string.h>
10 #include <linux/stat.h>
11 #include <linux/slab.h>
12 #include <linux/binfmts.h>
13 #include <linux/init.h>
14 #include <linux/file.h>
15 #include <linux/err.h>
18 static int load_script(struct linux_binprm *bprm,struct pt_regs *regs)
20 char *cp, *i_name, *i_arg;
22 char interp[BINPRM_BUF_SIZE];
25 if ((bprm->buf[0] != '#') || (bprm->buf[1] != '!') || (bprm->sh_bang))
28 * This section does the #! interpretation.
29 * Sorta complicated, but hopefully it will work. -TYT
33 allow_write_access(bprm->file);
37 bprm->buf[BINPRM_BUF_SIZE - 1] = '\0';
38 if ((cp = strchr(bprm->buf, '\n')) == NULL)
39 cp = bprm->buf+BINPRM_BUF_SIZE-1;
41 while (cp > bprm->buf) {
43 if ((*cp == ' ') || (*cp == '\t'))
48 for (cp = bprm->buf+2; (*cp == ' ') || (*cp == '\t'); cp++);
50 return -ENOEXEC; /* No interpreter name found */
53 for ( ; *cp && (*cp != ' ') && (*cp != '\t'); cp++)
55 while ((*cp == ' ') || (*cp == '\t'))
59 strcpy (interp, i_name);
61 * OK, we've parsed out the interpreter name and
62 * (optional) argument.
63 * Splice in (1) the interpreter's name for argv[0]
64 * (2) (optional) argument to interpreter
65 * (3) filename of shell script (replace argv[0])
67 * This is done in reverse order, because of how the
68 * user environment and arguments are stored.
70 remove_arg_zero(bprm);
71 retval = copy_strings_kernel(1, &bprm->interp, bprm);
72 if (retval < 0) return retval;
75 retval = copy_strings_kernel(1, &i_arg, bprm);
76 if (retval < 0) return retval;
79 retval = copy_strings_kernel(1, &i_name, bprm);
80 if (retval) return retval;
82 bprm->interp = interp;
85 * OK, now restart the process with the interpreter's dentry.
87 file = open_exec(interp);
92 retval = prepare_binprm(bprm);
95 return search_binary_handler(bprm,regs);
98 static struct linux_binfmt script_format = {
99 .module = THIS_MODULE,
100 .load_binary = load_script,
103 static int __init init_script_binfmt(void)
105 return register_binfmt(&script_format);
108 static void __exit exit_script_binfmt(void)
110 unregister_binfmt(&script_format);
113 core_initcall(init_script_binfmt);
114 module_exit(exit_script_binfmt);
115 MODULE_LICENSE("GPL");