2 * Copyright (c) 1998 Mark Newton
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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 Christos Zoulas.
16 * 4. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 * $FreeBSD: src/sys/svr4/svr4_sysvec.c,v 1.10.2.2 2002/07/09 14:12:43 robert Exp $
33 /* XXX we use functions that might not exist. */
34 #include "opt_compat.h"
37 #error "Unable to compile SVR4-emulator due to missing COMPAT_43 option!"
40 #include <sys/param.h>
41 #include <sys/systm.h>
44 #include <sys/sysent.h>
45 #include <sys/imgact.h>
46 #include <sys/imgact_elf.h>
47 #include <sys/socket.h>
48 #include <sys/malloc.h>
49 #include <sys/namei.h>
50 #include <sys/vnode.h>
51 #include <sys/module.h>
53 #include <vm/vm_zone.h>
55 #include <sys/kernel.h>
56 #include <machine/cpu.h>
57 #include <netinet/in.h>
59 #include <svr4/svr4.h>
60 #include <svr4/svr4_types.h>
61 #include <svr4/svr4_syscall.h>
62 #include <svr4/svr4_signal.h>
63 #include <svr4/svr4_sockio.h>
64 #include <svr4/svr4_socket.h>
65 #include <svr4/svr4_errno.h>
66 #include <svr4/svr4_proto.h>
67 #include <svr4/svr4_siginfo.h>
68 #include <svr4/svr4_util.h>
70 int bsd_to_svr4_errno[ELAST+1] = {
114 SVR4_EPROTONOSUPPORT,
115 SVR4_ESOCKTNOSUPPORT,
158 static int svr4_fixup(long **stack_base, struct image_params *imgp);
160 extern struct sysent svr4_sysent[];
164 extern int svr4_szsigcode;
165 extern char svr4_sigcode[];
167 struct sysentvec svr4_sysvec = {
187 Elf32_Brandinfo svr4_brand = {
195 const char svr4_emul_path[] = "/compat/svr4";
198 svr4_fixup(long **stack_base, struct image_params *imgp)
200 Elf32_Auxargs *args = (Elf32_Auxargs *)imgp->auxargs;
203 pos = *stack_base + (imgp->argc + imgp->envc + 2);
206 AUXARGS_ENTRY(pos, AT_DEBUG, 1);
208 if (args->execfd != -1) {
209 AUXARGS_ENTRY(pos, AT_EXECFD, args->execfd);
211 AUXARGS_ENTRY(pos, AT_PHDR, args->phdr);
212 AUXARGS_ENTRY(pos, AT_PHENT, args->phent);
213 AUXARGS_ENTRY(pos, AT_PHNUM, args->phnum);
214 AUXARGS_ENTRY(pos, AT_PAGESZ, args->pagesz);
215 AUXARGS_ENTRY(pos, AT_FLAGS, args->flags);
216 AUXARGS_ENTRY(pos, AT_ENTRY, args->entry);
217 AUXARGS_ENTRY(pos, AT_BASE, args->base);
218 AUXARGS_ENTRY(pos, AT_UID, imgp->proc->p_cred->p_ruid);
219 AUXARGS_ENTRY(pos, AT_EUID, imgp->proc->p_cred->p_svuid);
220 AUXARGS_ENTRY(pos, AT_GID, imgp->proc->p_cred->p_rgid);
221 AUXARGS_ENTRY(pos, AT_EGID, imgp->proc->p_cred->p_svgid);
222 AUXARGS_ENTRY(pos, AT_NULL, 0);
224 free(imgp->auxargs, M_TEMP);
225 imgp->auxargs = NULL;
228 **stack_base = (int)imgp->argc;
233 * Search an alternate path before passing pathname arguments on
234 * to system calls. Useful for keeping a seperate 'emulation tree'.
236 * If cflag is set, we check if an attempt can be made to create
237 * the named file, i.e. we check if the directory it should
240 * Code shamelessly stolen by Mark Newton from IBCS2 emulation code.
243 svr4_emul_find(p, sgp, prefix, path, pbuf, cflag)
245 caddr_t *sgp; /* Pointer to stackgap memory */
252 struct nameidata ndroot;
254 struct vattr vatroot;
256 char *ptr, *buf, *cp;
259 buf = (char *) malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
262 for (ptr = buf; (*ptr = *prefix) != '\0'; ptr++, prefix++)
265 sz = MAXPATHLEN - (ptr - buf);
268 * If sgp is not given then the path is already in kernel space
271 error = copystr(path, ptr, sz, &len);
273 error = copyinstr(path, ptr, sz, &len);
286 * We know that there is a / somewhere in this pathname.
287 * Search backwards for it, to find the file's parent dir
288 * to see if it exists in the alternate tree. If it does,
289 * and we want to create a file (cflag is set). We don't
290 * need to worry about the root comparison in this case.
294 for (cp = &ptr[len] - 1; *cp != '/'; cp--);
297 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, buf, p);
299 if ((error = namei(&nd)) != 0) {
303 NDFREE(&nd, NDF_ONLY_PNBUF);
308 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, buf, p);
310 if ((error = namei(&nd)) != 0) {
314 NDFREE(&nd, NDF_ONLY_PNBUF);
317 * We now compare the vnode of the svr4_root to the one
318 * vnode asked. If they resolve to be the same, then we
319 * ignore the match so that the real root gets used.
320 * This avoids the problem of traversing "../.." to find the
321 * root directory and never finding it, because "/" resolves
322 * to the emulation root directory. This is expensive :-(
324 NDINIT(&ndroot, LOOKUP, FOLLOW, UIO_SYSSPACE, svr4_emul_path,
327 if ((error = namei(&ndroot)) != 0) {
333 NDFREE(&ndroot, NDF_ONLY_PNBUF);
335 if ((error = VOP_GETATTR(nd.ni_vp, &vat, p->p_ucred, p)) != 0) {
339 if ((error = VOP_GETATTR(ndroot.ni_vp, &vatroot, p->p_ucred, p))
344 if (vat.va_fsid == vatroot.va_fsid &&
345 vat.va_fileid == vatroot.va_fileid) {
354 sz = &ptr[len] - buf;
355 *pbuf = stackgap_alloc(sgp, sz + 1);
356 error = copyout(buf, *pbuf, sz);
369 svr4_elf_modevent(module_t mod, int type, void *data)
377 if (elf_insert_brand_entry(&svr4_brand) < 0)
380 printf("cannot insert svr4 elf brand handler\n");
381 else if (bootverbose)
382 printf("svr4 ELF exec handler installed\n");
385 /* Only allow the emulator to be removed if it isn't in use. */
386 if (elf_brand_inuse(&svr4_brand) != 0) {
388 } else if (elf_remove_brand_entry(&svr4_brand) < 0) {
393 printf("Could not deinstall ELF interpreter entry (error %d)\n",
395 else if (bootverbose)
396 printf("svr4 ELF exec handler removed\n");
404 static moduledata_t svr4_elf_mod = {
409 DECLARE_MODULE(svr4elf, svr4_elf_mod, SI_SUB_EXEC, SI_ORDER_ANY);