compat_util.c revision 1.46.18.1 1 1.46.18.1 pgoyette /* $NetBSD: compat_util.c,v 1.46.18.1 2018/03/06 10:37:41 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.1 pgoyette __KERNEL_RCSID(0, "$NetBSD: compat_util.c,v 1.46.18.1 2018/03/06 10:37:41 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.1 christos
77 1.1 christos #include <compat/common/compat_util.h>
78 1.1 christos
79 1.12 cgd /*
80 1.12 cgd * Translate one set of flags to another, based on the entries in
81 1.12 cgd * the given table. If 'leftover' is specified, it is filled in
82 1.12 cgd * with any flags which could not be translated.
83 1.12 cgd */
84 1.12 cgd unsigned long
85 1.12 cgd emul_flags_translate(const struct emul_flags_xtab *tab,
86 1.12 cgd unsigned long in, unsigned long *leftover)
87 1.12 cgd {
88 1.12 cgd unsigned long out;
89 1.12 cgd
90 1.12 cgd for (out = 0; tab->omask != 0; tab++) {
91 1.12 cgd if ((in & tab->omask) == tab->oval) {
92 1.12 cgd in &= ~tab->omask;
93 1.12 cgd out |= tab->nval;
94 1.12 cgd }
95 1.12 cgd }
96 1.12 cgd if (leftover != NULL)
97 1.12 cgd *leftover = in;
98 1.12 cgd return (out);
99 1.5 thorpej }
100 1.5 thorpej
101 1.7 fvdl void
102 1.38 dsl compat_offseterr(struct vnode *vp, const char *msg)
103 1.7 fvdl {
104 1.7 fvdl struct mount *mp;
105 1.7 fvdl
106 1.7 fvdl mp = vp->v_mount;
107 1.7 fvdl
108 1.7 fvdl log(LOG_ERR, "%s: dir offset too large on fs %s (mounted from %s)\n",
109 1.7 fvdl msg, mp->mnt_stat.f_mntonname, mp->mnt_stat.f_mntfromname);
110 1.7 fvdl uprintf("%s: dir offset too large for emulated program\n", msg);
111 1.1 christos }
112 1.43 mrg
113 1.43 mrg /*
114 1.43 mrg * Look for native NetBSD compatibility libraries, usually interp-ABI.
115 1.43 mrg * It returns 0 if it changed the interpreter, otherwise it returns
116 1.43 mrg * the error from namei(). Callers should not try any more processing
117 1.43 mrg * if this returns 0, and probably should just ignore the return value.
118 1.43 mrg */
119 1.43 mrg int
120 1.43 mrg compat_elf_check_interp(struct exec_package *epp,
121 1.43 mrg char *interp,
122 1.43 mrg const char *interp_suffix)
123 1.43 mrg {
124 1.43 mrg int error = 0;
125 1.43 mrg
126 1.43 mrg /*
127 1.43 mrg * Don't look for something else, if someone has already found and
128 1.43 mrg * setup the ep_interp already.
129 1.43 mrg */
130 1.43 mrg if (interp && epp->ep_interp == NULL) {
131 1.43 mrg /*
132 1.43 mrg * If the path is exactly "/usr/libexec/ld.elf_so", first
133 1.43 mrg * try to see if "/usr/libexec/ld.elf_so-<abi>" exists
134 1.43 mrg * and if so, use that instead.
135 1.43 mrg */
136 1.43 mrg if (strcmp(interp, "/usr/libexec/ld.elf_so") == 0 ||
137 1.43 mrg strcmp(interp, "/libexec/ld.elf_so") == 0) {
138 1.43 mrg struct vnode *vp;
139 1.43 mrg char *path;
140 1.43 mrg
141 1.43 mrg path = PNBUF_GET();
142 1.43 mrg snprintf(path, MAXPATHLEN, "%s-%s", interp, interp_suffix);
143 1.43 mrg error = namei_simple_kernel(path,
144 1.43 mrg NSM_FOLLOW_NOEMULROOT, &vp);
145 1.43 mrg /*
146 1.43 mrg * If that worked, replace interpreter in case we
147 1.43 mrg * actually need to load it.
148 1.43 mrg */
149 1.43 mrg if (error == 0) {
150 1.43 mrg epp->ep_interp = vp;
151 1.43 mrg snprintf(interp, MAXPATHLEN, "%s", path);
152 1.43 mrg }
153 1.43 mrg PNBUF_PUT(path);
154 1.43 mrg }
155 1.43 mrg }
156 1.43 mrg return error;
157 1.43 mrg }
158