imgact_shell.c revision 3098
1/*
2 * Copyright (c) 1993, David Greenman
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *	This product includes software developed by David Greenman
16 * 4. The name of the developer may be used to endorse or promote products
17 *    derived from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
31 *	$Id: imgact_shell.c,v 1.5 1994/08/18 22:34:56 wollman Exp $
32 */
33
34#include <sys/param.h>
35#include <sys/systm.h>
36#include <sys/resourcevar.h>
37#include <sys/exec.h>
38#include <sys/imgact.h>
39#include <sys/kernel.h>
40#include <machine/endian.h>
41
42#if BYTE_ORDER == LITTLE_ENDIAN
43#define SHELLMAGIC	0x2123 /* #! */
44#else
45#define SHELLMAGIC	0x2321
46#endif
47
48#define MAXSHELLCMDLEN	64
49
50/*
51 * Shell interpreter image activator. A interpreter name beginning
52 *	at iparams->stringbase is the minimal successful exit requirement.
53 */
54int
55exec_shell_imgact(iparams)
56	struct image_params *iparams;
57{
58	const char *image_header = iparams->image_header;
59	const char *ihp, *line_endp;
60	char *interp;
61
62	/* a shell script? */
63	if (((short *) image_header)[0] != SHELLMAGIC)
64		return(-1);
65
66	/*
67	 * Don't allow a shell script to be the shell for a shell
68	 *	script. :-)
69	 */
70	if (iparams->interpreted)
71		return(ENOEXEC);
72
73	iparams->interpreted = 1;
74
75	/*
76	 * Copy shell name and arguments from image_header into string
77	 *	buffer.
78	 */
79
80	/*
81	 * Find end of line; return if the line > MAXSHELLCMDLEN long.
82	 */
83	for (ihp = &image_header[2]; *ihp != '\n'; ++ihp) {
84		if (ihp >= &image_header[MAXSHELLCMDLEN])
85			return(ENOEXEC);
86	}
87	line_endp = ihp;
88
89	/* reset for another pass */
90	ihp = &image_header[2];
91
92	/* Skip over leading spaces - until the interpreter name */
93	while ((*ihp == ' ') || (*ihp == '\t')) ihp++;
94
95	/* copy the interpreter name */
96	interp = iparams->interpreter_name;
97	while ((ihp < line_endp) && (*ihp != ' ') && (*ihp != '\t'))
98		*interp++ = *ihp++;
99	*interp = '\0';
100
101	/* Disallow a null interpreter filename */
102	if (*iparams->interpreter_name == '\0')
103		return(ENOEXEC);
104
105	/* reset for another pass */
106	ihp = &image_header[2];
107
108	/* copy the interpreter name and arguments */
109	while (ihp < line_endp) {
110		/* Skip over leading spaces */
111		while ((*ihp == ' ') || (*ihp == '\t')) ihp++;
112
113		if (ihp < line_endp) {
114			/*
115			 * Copy to end of token. No need to watch stringspace
116			 *	because this is at the front of the string buffer
117			 *	and the maximum shell command length is tiny.
118			 */
119			while ((ihp < line_endp) && (*ihp != ' ') && (*ihp != '\t')) {
120				*iparams->stringp++ = *ihp++;
121				iparams->stringspace--;
122			}
123
124			*iparams->stringp++ = 0;
125			iparams->stringspace--;
126
127			iparams->argc++;
128		}
129	}
130
131	/* set argv[0] to point to original file name */
132	suword(iparams->uap->argv, (int)iparams->uap->fname);
133
134	return(0);
135}
136
137/*
138 * Tell kern_execve.c about it, with a little help from the linker.
139 * Since `const' objects end up in the text segment, TEXT_SET is the
140 * correct directive to use.
141 */
142static const struct execsw shell_execsw = { exec_shell_imgact, "#!" };
143TEXT_SET(execsw_set, shell_execsw);
144