compat_util.c revision 1.46.18.2 1 1.46.18.2 pgoyette /* $NetBSD: compat_util.c,v 1.46.18.2 2018/03/30 23:57:59 pgoyette 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.43 mrg /*
33 1.43 mrg * Copyright (c) 2008, 2009 Matthew R. Green
34 1.43 mrg * All rights reserved.
35 1.43 mrg *
36 1.43 mrg * Redistribution and use in source and binary forms, with or without
37 1.43 mrg * modification, are permitted provided that the following conditions
38 1.43 mrg * are met:
39 1.43 mrg * 1. Redistributions of source code must retain the above copyright
40 1.43 mrg * notice, this list of conditions and the following disclaimer.
41 1.43 mrg * 2. Redistributions in binary form must reproduce the above copyright
42 1.43 mrg * notice, this list of conditions and the following disclaimer in the
43 1.43 mrg * documentation and/or other materials provided with the distribution.
44 1.43 mrg * 3. The name of the author may not be used to endorse or promote products
45 1.43 mrg * derived from this software without specific prior written permission.
46 1.43 mrg *
47 1.43 mrg * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
48 1.43 mrg * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
49 1.43 mrg * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
50 1.43 mrg * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
51 1.43 mrg * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
52 1.43 mrg * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
53 1.43 mrg * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
54 1.43 mrg * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
55 1.43 mrg * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56 1.43 mrg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57 1.43 mrg * SUCH DAMAGE.
58 1.43 mrg */
59 1.43 mrg
60 1.21 lukem #include <sys/cdefs.h>
61 1.46.18.2 pgoyette __KERNEL_RCSID(0, "$NetBSD: compat_util.c,v 1.46.18.2 2018/03/30 23:57:59 pgoyette Exp $");
62 1.1 christos
63 1.1 christos #include <sys/param.h>
64 1.1 christos #include <sys/systm.h>
65 1.1 christos #include <sys/namei.h>
66 1.1 christos #include <sys/proc.h>
67 1.1 christos #include <sys/file.h>
68 1.1 christos #include <sys/stat.h>
69 1.1 christos #include <sys/filedesc.h>
70 1.5 thorpej #include <sys/exec.h>
71 1.1 christos #include <sys/ioctl.h>
72 1.1 christos #include <sys/kernel.h>
73 1.1 christos #include <sys/vnode.h>
74 1.7 fvdl #include <sys/syslog.h>
75 1.7 fvdl #include <sys/mount.h>
76 1.46.18.2 pgoyette #include <sys/module.h>
77 1.1 christos
78 1.1 christos #include <compat/common/compat_util.h>
79 1.1 christos
80 1.12 cgd /*
81 1.12 cgd * Translate one set of flags to another, based on the entries in
82 1.12 cgd * the given table. If 'leftover' is specified, it is filled in
83 1.12 cgd * with any flags which could not be translated.
84 1.12 cgd */
85 1.12 cgd unsigned long
86 1.12 cgd emul_flags_translate(const struct emul_flags_xtab *tab,
87 1.12 cgd unsigned long in, unsigned long *leftover)
88 1.12 cgd {
89 1.12 cgd unsigned long out;
90 1.12 cgd
91 1.12 cgd for (out = 0; tab->omask != 0; tab++) {
92 1.12 cgd if ((in & tab->omask) == tab->oval) {
93 1.12 cgd in &= ~tab->omask;
94 1.12 cgd out |= tab->nval;
95 1.12 cgd }
96 1.12 cgd }
97 1.12 cgd if (leftover != NULL)
98 1.12 cgd *leftover = in;
99 1.12 cgd return (out);
100 1.5 thorpej }
101 1.5 thorpej
102 1.7 fvdl void
103 1.38 dsl compat_offseterr(struct vnode *vp, const char *msg)
104 1.7 fvdl {
105 1.7 fvdl struct mount *mp;
106 1.7 fvdl
107 1.7 fvdl mp = vp->v_mount;
108 1.7 fvdl
109 1.7 fvdl log(LOG_ERR, "%s: dir offset too large on fs %s (mounted from %s)\n",
110 1.7 fvdl msg, mp->mnt_stat.f_mntonname, mp->mnt_stat.f_mntfromname);
111 1.7 fvdl uprintf("%s: dir offset too large for emulated program\n", msg);
112 1.1 christos }
113 1.43 mrg
114 1.43 mrg /*
115 1.43 mrg * Look for native NetBSD compatibility libraries, usually interp-ABI.
116 1.43 mrg * It returns 0 if it changed the interpreter, otherwise it returns
117 1.43 mrg * the error from namei(). Callers should not try any more processing
118 1.43 mrg * if this returns 0, and probably should just ignore the return value.
119 1.43 mrg */
120 1.43 mrg int
121 1.43 mrg compat_elf_check_interp(struct exec_package *epp,
122 1.43 mrg char *interp,
123 1.43 mrg const char *interp_suffix)
124 1.43 mrg {
125 1.43 mrg int error = 0;
126 1.43 mrg
127 1.43 mrg /*
128 1.43 mrg * Don't look for something else, if someone has already found and
129 1.43 mrg * setup the ep_interp already.
130 1.43 mrg */
131 1.43 mrg if (interp && epp->ep_interp == NULL) {
132 1.43 mrg /*
133 1.43 mrg * If the path is exactly "/usr/libexec/ld.elf_so", first
134 1.43 mrg * try to see if "/usr/libexec/ld.elf_so-<abi>" exists
135 1.43 mrg * and if so, use that instead.
136 1.43 mrg */
137 1.43 mrg if (strcmp(interp, "/usr/libexec/ld.elf_so") == 0 ||
138 1.43 mrg strcmp(interp, "/libexec/ld.elf_so") == 0) {
139 1.43 mrg struct vnode *vp;
140 1.43 mrg char *path;
141 1.43 mrg
142 1.43 mrg path = PNBUF_GET();
143 1.43 mrg snprintf(path, MAXPATHLEN, "%s-%s", interp, interp_suffix);
144 1.43 mrg error = namei_simple_kernel(path,
145 1.43 mrg NSM_FOLLOW_NOEMULROOT, &vp);
146 1.43 mrg /*
147 1.43 mrg * If that worked, replace interpreter in case we
148 1.43 mrg * actually need to load it.
149 1.43 mrg */
150 1.43 mrg if (error == 0) {
151 1.43 mrg epp->ep_interp = vp;
152 1.43 mrg snprintf(interp, MAXPATHLEN, "%s", path);
153 1.43 mrg }
154 1.43 mrg PNBUF_PUT(path);
155 1.43 mrg }
156 1.43 mrg }
157 1.43 mrg return error;
158 1.43 mrg }
159 1.46.18.2 pgoyette
160 1.46.18.2 pgoyette #ifdef _MODULE
161 1.46.18.2 pgoyette
162 1.46.18.2 pgoyette MODULE(MODULE_CLASS_MISC, compat_util, NULL);
163 1.46.18.2 pgoyette
164 1.46.18.2 pgoyette int
165 1.46.18.2 pgoyette compat_util_modcmd(modcmd_t cmd, void *arg)
166 1.46.18.2 pgoyette {
167 1.46.18.2 pgoyette
168 1.46.18.2 pgoyette switch (cmd) {
169 1.46.18.2 pgoyette case MODULE_CMD_INIT:
170 1.46.18.2 pgoyette case MODULE_CMD_FINI:
171 1.46.18.2 pgoyette return 0;
172 1.46.18.2 pgoyette default:
173 1.46.18.2 pgoyette return ENOTTY;
174 1.46.18.2 pgoyette }
175 1.46.18.2 pgoyette }
176 1.46.18.2 pgoyette #endif /* _MODULE */
177