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