1 1.47 pgoyette /* $NetBSD: compat_util.c,v 1.47 2019/01/27 02:08:39 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.47 pgoyette __KERNEL_RCSID(0, "$NetBSD: compat_util.c,v 1.47 2019/01/27 02:08:39 pgoyette Exp $"); 62 1.47 pgoyette 63 1.47 pgoyette #if defined(_KERNEL_OPT) 64 1.47 pgoyette #include "opt_compat_netbsd.h" 65 1.47 pgoyette #endif 66 1.1 christos 67 1.1 christos #include <sys/param.h> 68 1.1 christos #include <sys/systm.h> 69 1.1 christos #include <sys/namei.h> 70 1.1 christos #include <sys/proc.h> 71 1.1 christos #include <sys/file.h> 72 1.1 christos #include <sys/stat.h> 73 1.1 christos #include <sys/filedesc.h> 74 1.5 thorpej #include <sys/exec.h> 75 1.1 christos #include <sys/ioctl.h> 76 1.1 christos #include <sys/kernel.h> 77 1.1 christos #include <sys/vnode.h> 78 1.7 fvdl #include <sys/syslog.h> 79 1.7 fvdl #include <sys/mount.h> 80 1.47 pgoyette #include <sys/module.h> 81 1.1 christos 82 1.1 christos #include <compat/common/compat_util.h> 83 1.1 christos 84 1.12 cgd /* 85 1.12 cgd * Translate one set of flags to another, based on the entries in 86 1.12 cgd * the given table. If 'leftover' is specified, it is filled in 87 1.12 cgd * with any flags which could not be translated. 88 1.12 cgd */ 89 1.12 cgd unsigned long 90 1.12 cgd emul_flags_translate(const struct emul_flags_xtab *tab, 91 1.12 cgd unsigned long in, unsigned long *leftover) 92 1.12 cgd { 93 1.12 cgd unsigned long out; 94 1.12 cgd 95 1.12 cgd for (out = 0; tab->omask != 0; tab++) { 96 1.12 cgd if ((in & tab->omask) == tab->oval) { 97 1.12 cgd in &= ~tab->omask; 98 1.12 cgd out |= tab->nval; 99 1.12 cgd } 100 1.12 cgd } 101 1.12 cgd if (leftover != NULL) 102 1.12 cgd *leftover = in; 103 1.12 cgd return (out); 104 1.5 thorpej } 105 1.5 thorpej 106 1.7 fvdl void 107 1.38 dsl compat_offseterr(struct vnode *vp, const char *msg) 108 1.7 fvdl { 109 1.7 fvdl struct mount *mp; 110 1.7 fvdl 111 1.7 fvdl mp = vp->v_mount; 112 1.7 fvdl 113 1.7 fvdl log(LOG_ERR, "%s: dir offset too large on fs %s (mounted from %s)\n", 114 1.7 fvdl msg, mp->mnt_stat.f_mntonname, mp->mnt_stat.f_mntfromname); 115 1.7 fvdl uprintf("%s: dir offset too large for emulated program\n", msg); 116 1.1 christos } 117 1.43 mrg 118 1.43 mrg /* 119 1.43 mrg * Look for native NetBSD compatibility libraries, usually interp-ABI. 120 1.43 mrg * It returns 0 if it changed the interpreter, otherwise it returns 121 1.43 mrg * the error from namei(). Callers should not try any more processing 122 1.43 mrg * if this returns 0, and probably should just ignore the return value. 123 1.43 mrg */ 124 1.43 mrg int 125 1.43 mrg compat_elf_check_interp(struct exec_package *epp, 126 1.43 mrg char *interp, 127 1.43 mrg const char *interp_suffix) 128 1.43 mrg { 129 1.43 mrg int error = 0; 130 1.43 mrg 131 1.43 mrg /* 132 1.43 mrg * Don't look for something else, if someone has already found and 133 1.43 mrg * setup the ep_interp already. 134 1.43 mrg */ 135 1.43 mrg if (interp && epp->ep_interp == NULL) { 136 1.43 mrg /* 137 1.43 mrg * If the path is exactly "/usr/libexec/ld.elf_so", first 138 1.43 mrg * try to see if "/usr/libexec/ld.elf_so-<abi>" exists 139 1.43 mrg * and if so, use that instead. 140 1.43 mrg */ 141 1.43 mrg if (strcmp(interp, "/usr/libexec/ld.elf_so") == 0 || 142 1.43 mrg strcmp(interp, "/libexec/ld.elf_so") == 0) { 143 1.43 mrg struct vnode *vp; 144 1.43 mrg char *path; 145 1.43 mrg 146 1.43 mrg path = PNBUF_GET(); 147 1.43 mrg snprintf(path, MAXPATHLEN, "%s-%s", interp, interp_suffix); 148 1.43 mrg error = namei_simple_kernel(path, 149 1.43 mrg NSM_FOLLOW_NOEMULROOT, &vp); 150 1.43 mrg /* 151 1.43 mrg * If that worked, replace interpreter in case we 152 1.43 mrg * actually need to load it. 153 1.43 mrg */ 154 1.43 mrg if (error == 0) { 155 1.43 mrg epp->ep_interp = vp; 156 1.43 mrg snprintf(interp, MAXPATHLEN, "%s", path); 157 1.43 mrg } 158 1.43 mrg PNBUF_PUT(path); 159 1.43 mrg } 160 1.43 mrg } 161 1.43 mrg return error; 162 1.43 mrg } 163 1.47 pgoyette 164 1.47 pgoyette MODULE(MODULE_CLASS_MISC, compat_util, NULL); 165 1.47 pgoyette 166 1.47 pgoyette int 167 1.47 pgoyette compat_util_modcmd(modcmd_t cmd, void *arg) 168 1.47 pgoyette { 169 1.47 pgoyette 170 1.47 pgoyette switch (cmd) { 171 1.47 pgoyette case MODULE_CMD_INIT: 172 1.47 pgoyette case MODULE_CMD_FINI: 173 1.47 pgoyette return 0; 174 1.47 pgoyette default: 175 1.47 pgoyette return ENOTTY; 176 1.47 pgoyette } 177 1.47 pgoyette } 178