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 $
31 * $DragonFly: src/sys/emulation/svr4/Attic/svr4_sysvec.c,v 1.13 2005/06/01 16:51:46 joerg Exp $
34 /* XXX we use functions that might not exist. */
35 #include "opt_compat.h"
38 #error "Unable to compile SVR4-emulator due to missing COMPAT_43 option!"
41 #include <sys/param.h>
42 #include <sys/systm.h>
45 #include <sys/sysent.h>
46 #include <sys/imgact.h>
47 #include <sys/imgact_elf.h>
48 #include <sys/socket.h>
49 #include <sys/malloc.h>
50 #include <sys/nlookup.h>
51 #include <sys/vnode.h>
52 #include <sys/module.h>
54 #include <vm/vm_zone.h>
56 #include <sys/kernel.h>
57 #include <machine/cpu.h>
58 #include <netinet/in.h>
60 #include <emulation/43bsd/43bsd_socket.h>
63 #include "svr4_types.h"
64 #include "svr4_syscall.h"
65 #include "svr4_signal.h"
66 #include "svr4_sockio.h"
67 #include "svr4_socket.h"
68 #include "svr4_errno.h"
69 #include "svr4_proto.h"
70 #include "svr4_siginfo.h"
71 #include "svr4_util.h"
73 int bsd_to_svr4_errno[ELAST+1] = {
117 SVR4_EPROTONOSUPPORT,
118 SVR4_ESOCKTNOSUPPORT,
161 static int svr4_fixup(register_t **stack_base, struct image_params *imgp);
163 extern struct sysent svr4_sysent[];
167 extern int svr4_szsigcode;
168 extern char svr4_sigcode[];
170 struct sysentvec svr4_sysvec = {
190 Elf32_Brandinfo svr4_brand = {
193 NULL, /* Keep this, otherwise SYSV binaries are undetectable! */
199 const char svr4_emul_path[] = "/compat/svr4";
202 svr4_fixup(register_t **stack_base, struct image_params *imgp)
204 Elf32_Auxargs *args = (Elf32_Auxargs *)imgp->auxargs;
207 pos = *stack_base + (imgp->args->argc + imgp->args->envc + 2);
210 AUXARGS_ENTRY(pos, AT_DEBUG, 1);
212 if (args->execfd != -1) {
213 AUXARGS_ENTRY(pos, AT_EXECFD, args->execfd);
215 AUXARGS_ENTRY(pos, AT_PHDR, args->phdr);
216 AUXARGS_ENTRY(pos, AT_PHENT, args->phent);
217 AUXARGS_ENTRY(pos, AT_PHNUM, args->phnum);
218 AUXARGS_ENTRY(pos, AT_PAGESZ, args->pagesz);
219 AUXARGS_ENTRY(pos, AT_FLAGS, args->flags);
220 AUXARGS_ENTRY(pos, AT_ENTRY, args->entry);
221 AUXARGS_ENTRY(pos, AT_BASE, args->base);
222 AUXARGS_ENTRY(pos, AT_UID, imgp->proc->p_ucred->cr_ruid);
223 AUXARGS_ENTRY(pos, AT_EUID, imgp->proc->p_ucred->cr_svuid);
224 AUXARGS_ENTRY(pos, AT_GID, imgp->proc->p_ucred->cr_rgid);
225 AUXARGS_ENTRY(pos, AT_EGID, imgp->proc->p_ucred->cr_svgid);
226 AUXARGS_ENTRY(pos, AT_NULL, 0);
228 free(imgp->auxargs, M_TEMP);
229 imgp->auxargs = NULL;
232 **stack_base = (int)imgp->args->argc;
237 * Search an alternate path before passing pathname arguments on
238 * to system calls. Useful for keeping a separate 'emulation tree'.
240 * If cflag is set, we check if an attempt can be made to create
241 * the named file, i.e. we check if the directory it should
244 * Code shamelessly stolen by Mark Newton from IBCS2 emulation code.
247 svr4_emul_find(sgp, prefix, path, pbuf, cflag)
248 caddr_t *sgp; /* Pointer to stackgap memory */
254 struct thread *td = curthread; /* XXX */
255 struct nlookupdata nd;
256 struct nlookupdata ndroot;
258 struct vattr vatroot;
261 char *ptr, *buf, *cp;
265 KKASSERT(td->td_proc);
266 cred = td->td_proc->p_ucred;
268 buf = (char *) malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
271 for (ptr = buf; (*ptr = *prefix) != '\0'; ptr++, prefix++)
274 sz = MAXPATHLEN - (ptr - buf);
277 * If sgp is not given then the path is already in kernel space
280 error = copystr(path, ptr, sz, &len);
282 error = copyinstr(path, ptr, sz, &len);
295 * We know that there is a / somewhere in this pathname.
296 * Search backwards for it, to find the file's parent dir
297 * to see if it exists in the alternate tree. If it does,
298 * and we want to create a file (cflag is set). We don't
299 * need to worry about the root comparison in this case.
303 for (cp = &ptr[len] - 1; *cp != '/'; cp--);
306 error = nlookup_init(&nd, buf, UIO_SYSSPACE, NLC_FOLLOW);
308 error = nlookup(&nd);
316 error = nlookup_init(&nd, buf, UIO_SYSSPACE, NLC_FOLLOW);
318 error = nlookup(&nd);
321 error = cache_vref(nd.nl_ncp, nd.nl_cred, &vp);
327 error = VOP_GETATTR(vp, &vat, td);
333 * We now compare the vnode of the svr4_root to the one
334 * vnode asked. If they resolve to be the same, then we
335 * ignore the match so that the real root gets used.
336 * This avoids the problem of traversing "../.." to find the
337 * root directory and never finding it, because "/" resolves
338 * to the emulation root directory. This is expensive :-(
340 error = nlookup_init(&ndroot, svr4_emul_path, UIO_SYSSPACE,
343 error = nlookup(&ndroot);
346 error = cache_vref(ndroot.nl_ncp, ndroot.nl_cred, &vp);
347 nlookup_done(&ndroot);
352 error = VOP_GETATTR(vp, &vatroot, td);
357 if (vat.va_fsid == vatroot.va_fsid &&
358 vat.va_fileid == vatroot.va_fileid) {
366 sz = &ptr[len] - buf;
367 *pbuf = stackgap_alloc(sgp, sz + 1);
368 error = copyout(buf, *pbuf, sz);
376 svr4_elf_modevent(module_t mod, int type, void *data)
384 if (elf_insert_brand_entry(&svr4_brand) < 0)
387 printf("cannot insert svr4 elf brand handler\n");
388 else if (bootverbose)
389 printf("svr4 ELF exec handler installed\n");
392 /* Only allow the emulator to be removed if it isn't in use. */
393 if (elf_brand_inuse(&svr4_brand) != 0) {
395 } else if (elf_remove_brand_entry(&svr4_brand) < 0) {
400 printf("Could not deinstall ELF interpreter entry (error %d)\n",
402 else if (bootverbose)
403 printf("svr4 ELF exec handler removed\n");
411 static moduledata_t svr4_elf_mod = {
416 DECLARE_MODULE(svr4elf, svr4_elf_mod, SI_SUB_EXEC, SI_ORDER_ANY);