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