compat_util.c revision 1.42 1 1.42 dholland /* $NetBSD: compat_util.c,v 1.42 2009/06/29 05:08:15 dholland Exp $ */
2 1.8 christos
3 1.8 christos /*-
4 1.8 christos * Copyright (c) 1994 The NetBSD Foundation, Inc.
5 1.8 christos * All rights reserved.
6 1.8 christos *
7 1.8 christos * This code is derived from software contributed to The NetBSD Foundation
8 1.9 fvdl * by Christos Zoulas and Frank van der Linden.
9 1.8 christos *
10 1.8 christos * Redistribution and use in source and binary forms, with or without
11 1.8 christos * modification, are permitted provided that the following conditions
12 1.8 christos * are met:
13 1.8 christos * 1. Redistributions of source code must retain the above copyright
14 1.8 christos * notice, this list of conditions and the following disclaimer.
15 1.8 christos * 2. Redistributions in binary form must reproduce the above copyright
16 1.8 christos * notice, this list of conditions and the following disclaimer in the
17 1.8 christos * documentation and/or other materials provided with the distribution.
18 1.8 christos *
19 1.8 christos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.8 christos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.8 christos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.8 christos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.8 christos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.8 christos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.8 christos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.8 christos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.8 christos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.8 christos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.8 christos * POSSIBILITY OF SUCH DAMAGE.
30 1.1 christos */
31 1.21 lukem
32 1.21 lukem #include <sys/cdefs.h>
33 1.42 dholland __KERNEL_RCSID(0, "$NetBSD: compat_util.c,v 1.42 2009/06/29 05:08:15 dholland Exp $");
34 1.1 christos
35 1.1 christos #include <sys/param.h>
36 1.1 christos #include <sys/systm.h>
37 1.1 christos #include <sys/namei.h>
38 1.1 christos #include <sys/proc.h>
39 1.1 christos #include <sys/file.h>
40 1.1 christos #include <sys/stat.h>
41 1.1 christos #include <sys/filedesc.h>
42 1.5 thorpej #include <sys/exec.h>
43 1.1 christos #include <sys/ioctl.h>
44 1.1 christos #include <sys/kernel.h>
45 1.1 christos #include <sys/malloc.h>
46 1.1 christos #include <sys/vnode.h>
47 1.7 fvdl #include <sys/syslog.h>
48 1.7 fvdl #include <sys/mount.h>
49 1.1 christos
50 1.1 christos #include <compat/common/compat_util.h>
51 1.1 christos
52 1.35 dsl void
53 1.35 dsl emul_find_root(struct lwp *l, struct exec_package *epp)
54 1.1 christos {
55 1.42 dholland struct vnode *vp;
56 1.35 dsl const char *emul_path;
57 1.13 christos
58 1.35 dsl if (epp->ep_emul_root != NULL)
59 1.35 dsl /* We've already found it */
60 1.35 dsl return;
61 1.35 dsl
62 1.35 dsl emul_path = epp->ep_esch->es_emul->e_path;
63 1.35 dsl if (emul_path == NULL)
64 1.35 dsl /* Emulation doesn't have a root */
65 1.35 dsl return;
66 1.35 dsl
67 1.42 dholland if (namei_simple_kernel(emul_path, NSM_FOLLOW_NOEMULROOT, &vp) != 0)
68 1.35 dsl /* emulation root doesn't exist */
69 1.35 dsl return;
70 1.3 mycroft
71 1.42 dholland epp->ep_emul_root = vp;
72 1.25 jdolecek }
73 1.25 jdolecek
74 1.25 jdolecek /*
75 1.25 jdolecek * Search the alternate path for dynamic binary interpreter. If not found
76 1.25 jdolecek * there, check if the interpreter exists in within 'proper' tree.
77 1.25 jdolecek */
78 1.25 jdolecek int
79 1.35 dsl emul_find_interp(struct lwp *l, struct exec_package *epp, const char *itp)
80 1.25 jdolecek {
81 1.25 jdolecek int error;
82 1.35 dsl struct nameidata nd;
83 1.35 dsl unsigned int flags;
84 1.25 jdolecek
85 1.35 dsl /* If we haven't found the emulation root already, do so now */
86 1.35 dsl /* Maybe we should remember failures somehow ? */
87 1.35 dsl if (epp->ep_esch->es_emul->e_path != 0 && epp->ep_emul_root == NULL)
88 1.35 dsl emul_find_root(l, epp);
89 1.35 dsl
90 1.35 dsl if (epp->ep_interp != NULL)
91 1.35 dsl vrele(epp->ep_interp);
92 1.35 dsl
93 1.35 dsl /* We need to use the emulation root for the new program,
94 1.35 dsl * not the one for the current process. */
95 1.36 dsl if (epp->ep_emul_root == NULL)
96 1.36 dsl flags = FOLLOW;
97 1.36 dsl else {
98 1.36 dsl nd.ni_erootdir = epp->ep_emul_root;
99 1.36 dsl /* hack: Pass in the emulation path for ktrace calls */
100 1.36 dsl nd.ni_next = epp->ep_esch->es_emul->e_path;
101 1.35 dsl flags = FOLLOW | TRYEMULROOT | EMULROOTSET;
102 1.36 dsl }
103 1.25 jdolecek
104 1.39 pooka NDINIT(&nd, LOOKUP, flags, UIO_SYSSPACE, itp);
105 1.35 dsl error = namei(&nd);
106 1.35 dsl if (error != 0) {
107 1.35 dsl epp->ep_interp = NULL;
108 1.35 dsl return error;
109 1.35 dsl }
110 1.28 perry
111 1.35 dsl /* Save interpreter in case we actually need to load it */
112 1.35 dsl epp->ep_interp = nd.ni_vp;
113 1.25 jdolecek
114 1.35 dsl return 0;
115 1.12 cgd }
116 1.12 cgd
117 1.12 cgd /*
118 1.12 cgd * Translate one set of flags to another, based on the entries in
119 1.12 cgd * the given table. If 'leftover' is specified, it is filled in
120 1.12 cgd * with any flags which could not be translated.
121 1.12 cgd */
122 1.12 cgd unsigned long
123 1.12 cgd emul_flags_translate(const struct emul_flags_xtab *tab,
124 1.12 cgd unsigned long in, unsigned long *leftover)
125 1.12 cgd {
126 1.12 cgd unsigned long out;
127 1.12 cgd
128 1.12 cgd for (out = 0; tab->omask != 0; tab++) {
129 1.12 cgd if ((in & tab->omask) == tab->oval) {
130 1.12 cgd in &= ~tab->omask;
131 1.12 cgd out |= tab->nval;
132 1.12 cgd }
133 1.12 cgd }
134 1.12 cgd if (leftover != NULL)
135 1.12 cgd *leftover = in;
136 1.12 cgd return (out);
137 1.5 thorpej }
138 1.5 thorpej
139 1.7 fvdl void
140 1.38 dsl compat_offseterr(struct vnode *vp, const char *msg)
141 1.7 fvdl {
142 1.7 fvdl struct mount *mp;
143 1.7 fvdl
144 1.7 fvdl mp = vp->v_mount;
145 1.7 fvdl
146 1.7 fvdl log(LOG_ERR, "%s: dir offset too large on fs %s (mounted from %s)\n",
147 1.7 fvdl msg, mp->mnt_stat.f_mntonname, mp->mnt_stat.f_mntfromname);
148 1.7 fvdl uprintf("%s: dir offset too large for emulated program\n", msg);
149 1.1 christos }
150