compat_util.c revision 1.29.2.4 1 1.29.2.4 yamt /* $NetBSD: compat_util.c,v 1.29.2.4 2008/01/21 09:40:42 yamt 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 * 3. All advertising materials mentioning features or use of this software
19 1.8 christos * must display the following acknowledgement:
20 1.8 christos * This product includes software developed by the NetBSD
21 1.8 christos * Foundation, Inc. and its contributors.
22 1.8 christos * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.8 christos * contributors may be used to endorse or promote products derived
24 1.8 christos * from this software without specific prior written permission.
25 1.8 christos *
26 1.8 christos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.8 christos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.8 christos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.8 christos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.8 christos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.8 christos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.8 christos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.8 christos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.8 christos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.8 christos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.8 christos * POSSIBILITY OF SUCH DAMAGE.
37 1.1 christos */
38 1.21 lukem
39 1.21 lukem #include <sys/cdefs.h>
40 1.29.2.4 yamt __KERNEL_RCSID(0, "$NetBSD: compat_util.c,v 1.29.2.4 2008/01/21 09:40:42 yamt Exp $");
41 1.1 christos
42 1.1 christos #include <sys/param.h>
43 1.1 christos #include <sys/systm.h>
44 1.1 christos #include <sys/namei.h>
45 1.1 christos #include <sys/proc.h>
46 1.1 christos #include <sys/file.h>
47 1.1 christos #include <sys/stat.h>
48 1.1 christos #include <sys/filedesc.h>
49 1.5 thorpej #include <sys/exec.h>
50 1.1 christos #include <sys/ioctl.h>
51 1.1 christos #include <sys/kernel.h>
52 1.1 christos #include <sys/malloc.h>
53 1.1 christos #include <sys/vnode.h>
54 1.7 fvdl #include <sys/syslog.h>
55 1.7 fvdl #include <sys/mount.h>
56 1.1 christos
57 1.1 christos #include <compat/common/compat_util.h>
58 1.1 christos
59 1.29.2.3 yamt void
60 1.29.2.3 yamt emul_find_root(struct lwp *l, struct exec_package *epp)
61 1.1 christos {
62 1.29.2.3 yamt struct nameidata nd;
63 1.29.2.3 yamt const char *emul_path;
64 1.1 christos
65 1.29.2.3 yamt if (epp->ep_emul_root != NULL)
66 1.29.2.3 yamt /* We've already found it */
67 1.29.2.3 yamt return;
68 1.29.2.3 yamt
69 1.29.2.3 yamt emul_path = epp->ep_esch->es_emul->e_path;
70 1.29.2.3 yamt if (emul_path == NULL)
71 1.29.2.3 yamt /* Emulation doesn't have a root */
72 1.29.2.3 yamt return;
73 1.29.2.3 yamt
74 1.29.2.4 yamt NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, emul_path);
75 1.29.2.3 yamt if (namei(&nd) != 0)
76 1.29.2.3 yamt /* emulation root doesn't exist */
77 1.29.2.3 yamt return;
78 1.19 jdolecek
79 1.29.2.3 yamt epp->ep_emul_root = nd.ni_vp;
80 1.25 jdolecek }
81 1.25 jdolecek
82 1.25 jdolecek /*
83 1.25 jdolecek * Search the alternate path for dynamic binary interpreter. If not found
84 1.25 jdolecek * there, check if the interpreter exists in within 'proper' tree.
85 1.25 jdolecek */
86 1.25 jdolecek int
87 1.29.2.3 yamt emul_find_interp(struct lwp *l, struct exec_package *epp, const char *itp)
88 1.25 jdolecek {
89 1.25 jdolecek int error;
90 1.29.2.3 yamt struct nameidata nd;
91 1.29.2.3 yamt unsigned int flags;
92 1.25 jdolecek
93 1.29.2.3 yamt /* If we haven't found the emulation root already, do so now */
94 1.29.2.3 yamt /* Maybe we should remember failures somehow ? */
95 1.29.2.3 yamt if (epp->ep_esch->es_emul->e_path != 0 && epp->ep_emul_root == NULL)
96 1.29.2.3 yamt emul_find_root(l, epp);
97 1.29.2.3 yamt
98 1.29.2.3 yamt if (epp->ep_interp != NULL)
99 1.29.2.3 yamt vrele(epp->ep_interp);
100 1.29.2.3 yamt
101 1.29.2.3 yamt /* We need to use the emulation root for the new program,
102 1.29.2.3 yamt * not the one for the current process. */
103 1.29.2.3 yamt if (epp->ep_emul_root == NULL)
104 1.29.2.3 yamt flags = FOLLOW;
105 1.29.2.3 yamt else {
106 1.29.2.3 yamt nd.ni_erootdir = epp->ep_emul_root;
107 1.29.2.3 yamt /* hack: Pass in the emulation path for ktrace calls */
108 1.29.2.3 yamt nd.ni_next = epp->ep_esch->es_emul->e_path;
109 1.29.2.3 yamt flags = FOLLOW | TRYEMULROOT | EMULROOTSET;
110 1.29.2.3 yamt }
111 1.25 jdolecek
112 1.29.2.4 yamt NDINIT(&nd, LOOKUP, flags, UIO_SYSSPACE, itp);
113 1.29.2.3 yamt error = namei(&nd);
114 1.29.2.3 yamt if (error != 0) {
115 1.29.2.3 yamt epp->ep_interp = NULL;
116 1.29.2.3 yamt return error;
117 1.25 jdolecek }
118 1.25 jdolecek
119 1.29.2.3 yamt /* Save interpreter in case we actually need to load it */
120 1.29.2.3 yamt epp->ep_interp = nd.ni_vp;
121 1.29.2.3 yamt
122 1.29.2.3 yamt return 0;
123 1.12 cgd }
124 1.12 cgd
125 1.12 cgd /*
126 1.12 cgd * Translate one set of flags to another, based on the entries in
127 1.12 cgd * the given table. If 'leftover' is specified, it is filled in
128 1.12 cgd * with any flags which could not be translated.
129 1.12 cgd */
130 1.12 cgd unsigned long
131 1.12 cgd emul_flags_translate(const struct emul_flags_xtab *tab,
132 1.12 cgd unsigned long in, unsigned long *leftover)
133 1.12 cgd {
134 1.12 cgd unsigned long out;
135 1.12 cgd
136 1.12 cgd for (out = 0; tab->omask != 0; tab++) {
137 1.12 cgd if ((in & tab->omask) == tab->oval) {
138 1.12 cgd in &= ~tab->omask;
139 1.12 cgd out |= tab->nval;
140 1.12 cgd }
141 1.12 cgd }
142 1.12 cgd if (leftover != NULL)
143 1.12 cgd *leftover = in;
144 1.12 cgd return (out);
145 1.5 thorpej }
146 1.5 thorpej
147 1.7 fvdl void
148 1.29.2.4 yamt compat_offseterr(struct vnode *vp, const char *msg)
149 1.7 fvdl {
150 1.7 fvdl struct mount *mp;
151 1.7 fvdl
152 1.7 fvdl mp = vp->v_mount;
153 1.7 fvdl
154 1.7 fvdl log(LOG_ERR, "%s: dir offset too large on fs %s (mounted from %s)\n",
155 1.7 fvdl msg, mp->mnt_stat.f_mntonname, mp->mnt_stat.f_mntfromname);
156 1.7 fvdl uprintf("%s: dir offset too large for emulated program\n", msg);
157 1.1 christos }
158