27 #include <sys/cdefs.h>
30 #include <sys/param.h>
31 #include <sys/vnode.h>
34 #include <sys/systm.h>
35 #include <sys/sysproto.h>
37 #include <sys/imgact.h>
38 #include <sys/kernel.h>
40 #if BYTE_ORDER == LITTLE_ENDIAN
41 #define SHELLMAGIC 0x2123
43 #define SHELLMAGIC 0x2321
51 #if MAXSHELLCMDLEN > PAGE_SIZE
52 #error "MAXSHELLCMDLEN is larger than a single page!"
59 CTASSERT(MAXSHELLCMDLEN >= MAXINTERP + 3);
101 struct image_params *imgp;
103 const char *image_header = imgp->image_header;
104 const char *ihp, *interpb, *interpe, *maxp, *optb, *opte, *fname;
111 if (((
const short *)image_header)[0] !=
SHELLMAGIC)
118 if (imgp->interpreted)
121 imgp->interpreted = 1;
129 error = VOP_GETATTR(imgp->vp, &vattr, imgp->proc->p_ucred);
137 maxp = &image_header[MIN(vattr.va_size, MAXSHELLCMDLEN)];
138 ihp = &image_header[2];
145 while (ihp < maxp && ((*ihp ==
' ') || (*ihp ==
'\t')))
148 while (ihp < maxp && ((*ihp !=
' ') && (*ihp !=
'\t') && (*ihp !=
'\n')
152 if (interpb == interpe)
154 if (interpe - interpb >= MAXINTERP)
155 return (ENAMETOOLONG);
162 while (ihp < maxp && ((*ihp ==
' ') || (*ihp ==
'\t')))
165 while (ihp < maxp && ((*ihp !=
'\n') && (*ihp !=
'\0')))
170 while (--ihp > optb && ((*ihp ==
' ') || (*ihp ==
'\t')))
173 if (imgp->args->fname != NULL) {
174 fname = imgp->args->fname;
177 sname = sbuf_new_auto();
191 offset = interpe - interpb + 1;
193 offset += opte - optb + 1;
194 offset += strlen(fname) + 1;
195 length = (imgp->args->argc == 0) ? 0 :
196 strlen(imgp->args->begin_argv) + 1;
198 if (offset > imgp->args->stringspace + length) {
204 bcopy(imgp->args->begin_argv + length, imgp->args->begin_argv + offset,
205 imgp->args->endp - (imgp->args->begin_argv + length));
208 imgp->args->begin_envv += offset;
209 imgp->args->endp += offset;
210 imgp->args->stringspace -= offset;
218 if (imgp->args->argc == 0)
219 imgp->args->argc = 1;
226 length = interpe - interpb;
227 bcopy(interpb, imgp->args->begin_argv, length);
228 *(imgp->args->begin_argv + length) =
'\0';
231 length = opte - optb;
232 bcopy(optb, imgp->args->begin_argv + offset, length);
233 *(imgp->args->begin_argv + offset + length) =
'\0';
234 offset += length + 1;
243 error = copystr(fname, imgp->args->begin_argv + offset,
244 imgp->args->stringspace, NULL);
247 imgp->interpreter_name = imgp->args->begin_argv;
EXEC_SET(shell, shell_execsw)
CTASSERT(MAXSHELLCMDLEN >=MAXINTERP+3)
int exec_shell_imgact(struct image_params *imgp)
static struct execsw shell_execsw
int sbuf_printf(struct sbuf *s, const char *fmt,...)
static const struct execsw ** execsw
void sbuf_delete(struct sbuf *s)
char * sbuf_data(struct sbuf *s)
int sbuf_finish(struct sbuf *s)