imgact_shell.c revision 1549
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.1 1993/12/20 16:16:46 wollman Exp $
32 */
33
34#include "param.h"
35#include "systm.h"
36#include "resourcevar.h"
37#include <sys/exec.h>
38#include "imgact.h"
39#include "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	int length;
61	char *interp;
62	char **argv;
63
64	/* a shell script? */
65	if (((short *) image_header)[0] != SHELLMAGIC)
66		return(-1);
67
68	/*
69	 * Don't allow a shell script to be the shell for a shell
70	 *	script. :-)
71	 */
72	if (iparams->interpreted)
73		return(ENOEXEC);
74
75	iparams->interpreted = 1;
76
77	/*
78	 * Copy shell name and arguments from image_header into string
79	 *	buffer.
80	 */
81
82	/*
83	 * Find end of line; return if the line > MAXSHELLCMDLEN long.
84	 */
85	for (ihp = &image_header[2]; *ihp != '\n'; ++ihp) {
86		if (ihp >= &image_header[MAXSHELLCMDLEN])
87			return(ENOEXEC);
88	}
89	line_endp = ihp;
90
91	/* reset for another pass */
92	ihp = &image_header[2];
93
94	/* Skip over leading spaces - until the interpreter name */
95	while ((*ihp == ' ') || (*ihp == '\t')) ihp++;
96
97	/* copy the interpreter name */
98	interp = iparams->interpreter_name;
99	while ((ihp < line_endp) && (*ihp != ' ') && (*ihp != '\t'))
100		*interp++ = *ihp++;
101	*interp = '\0';
102
103	/* Disallow a null interpreter filename */
104	if (*iparams->interpreter_name == '\0')
105		return(ENOEXEC);
106
107	/* reset for another pass */
108	ihp = &image_header[2];
109
110	/* copy the interpreter name and arguments */
111	while (ihp < line_endp) {
112		/* Skip over leading spaces */
113		while ((*ihp == ' ') || (*ihp == '\t')) ihp++;
114
115		if (ihp < line_endp) {
116			/*
117			 * Copy to end of token. No need to watch stringspace
118			 *	because this is at the front of the string buffer
119			 *	and the maximum shell command length is tiny.
120			 */
121			while ((ihp < line_endp) && (*ihp != ' ') && (*ihp != '\t')) {
122				*iparams->stringp++ = *ihp++;
123				iparams->stringspace--;
124			}
125
126			*iparams->stringp++ = 0;
127			iparams->stringspace--;
128
129			iparams->argc++;
130		}
131	}
132
133	/* set argv[0] to point to original file name */
134	suword(iparams->uap->argv, (int)iparams->uap->fname);
135
136	return(0);
137}
138
139/*
140 * Tell kern_execve.c about it, with a little help from the linker.
141 * Since `const' objects end up in the text segment, TEXT_SET is the
142 * correct directive to use.
143 */
144static const struct execsw shell_execsw = { exec_shell_imgact };
145TEXT_SET(execsw_set, shell_execsw);
146