1 /* $NetBSD: compat_util.c,v 1.11 1999/02/14 14:32:02 christos 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/param.h> 40 #include <sys/systm.h> 41 #include <sys/namei.h> 42 #include <sys/proc.h> 43 #include <sys/file.h> 44 #include <sys/stat.h> 45 #include <sys/filedesc.h> 46 #include <sys/exec.h> 47 #include <sys/ioctl.h> 48 #include <sys/kernel.h> 49 #include <sys/malloc.h> 50 #include <sys/vnode.h> 51 #include <sys/syslog.h> 52 #include <sys/mount.h> 53 54 #include <vm/vm_param.h> 55 56 #include <compat/common/compat_util.h> 57 58 /* 59 * Search an alternate path before passing pathname arguments on 60 * to system calls. Useful for keeping a separate 'emulation tree'. 61 * 62 * If cflag is set, we check if an attempt can be made to create 63 * the named file, i.e. we check if the directory it should 64 * be in exists. 65 */ 66 int 67 emul_find(p, sgp, prefix, path, pbuf, cflag) 68 struct proc *p; 69 caddr_t *sgp; /* Pointer to stackgap memory */ 70 const char *prefix; 71 const char *path; 72 const char **pbuf; 73 int cflag; 74 { 75 struct nameidata nd; 76 struct nameidata ndroot; 77 struct vattr vat; 78 struct vattr vatroot; 79 int error; 80 char *ptr, *buf, *cp; 81 const char *pr; 82 size_t sz, len; 83 84 buf = (char *) malloc(MAXPATHLEN, M_TEMP, M_WAITOK); 85 *pbuf = path; 86 87 for (ptr = buf, pr = prefix; (*ptr = *pr) != '\0'; ptr++, pr++) 88 continue; 89 90 sz = MAXPATHLEN - (ptr - buf); 91 92 /* 93 * If sgp is not given then the path is already in kernel space 94 */ 95 if (sgp == NULL) 96 error = copystr(path, ptr, sz, &len); 97 else 98 error = copyinstr(path, ptr, sz, &len); 99 100 if (error) 101 goto bad; 102 103 if (*ptr != '/') { 104 error = EINVAL; 105 goto bad; 106 } 107 108 /* 109 * We provide an escape method, so that the user can 110 * always specify the real root. If the path is prefixed 111 * by /../ we kill the alternate search 112 */ 113 if (ptr[1] == '.' && ptr[2] == '.' && ptr[3] == '/') { 114 *pbuf = &path[3]; 115 goto bad; 116 } 117 118 /* 119 * We know that there is a / somewhere in this pathname. 120 * Search backwards for it, to find the file's parent dir 121 * to see if it exists in the alternate tree. If it does, 122 * and we want to create a file (cflag is set). We don't 123 * need to worry about the root comparison in this case. 124 */ 125 126 if (cflag) { 127 for (cp = &ptr[len] - 1; *cp != '/'; cp--) 128 ; 129 *cp = '\0'; 130 131 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, buf, p); 132 133 if ((error = namei(&nd)) != 0) 134 goto bad; 135 136 *cp = '/'; 137 } 138 else { 139 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, buf, p); 140 141 if ((error = namei(&nd)) != 0) 142 goto bad; 143 144 /* 145 * We now compare the vnode of the emulation root to the one 146 * vnode asked. If they resolve to be the same, then we 147 * ignore the match so that the real root gets used. 148 * This avoids the problem of traversing "../.." to find the 149 * root directory and never finding it, because "/" resolves 150 * to the emulation root directory. This is expensive :-( 151 */ 152 NDINIT(&ndroot, LOOKUP, FOLLOW, UIO_SYSSPACE, prefix, p); 153 154 if ((error = namei(&ndroot)) != 0) 155 goto bad2; 156 157 if ((error = VOP_GETATTR(nd.ni_vp, &vat, p->p_ucred, p)) != 0) 158 goto bad3; 159 160 if ((error = VOP_GETATTR(ndroot.ni_vp, &vatroot, p->p_ucred, p)) 161 != 0) 162 goto bad3; 163 164 if (vat.va_fsid == vatroot.va_fsid && 165 vat.va_fileid == vatroot.va_fileid) { 166 error = ENOENT; 167 goto bad3; 168 } 169 } 170 if (sgp == NULL) 171 *pbuf = buf; 172 else { 173 sz = &ptr[len] - buf; 174 *pbuf = stackgap_alloc(sgp, sz + 1); 175 error = copyout(buf, (void *)*pbuf, sz); 176 free(buf, M_TEMP); 177 } 178 179 vrele(nd.ni_vp); 180 if (!cflag) 181 vrele(ndroot.ni_vp); 182 return error; 183 184 bad3: 185 vrele(ndroot.ni_vp); 186 bad2: 187 vrele(nd.ni_vp); 188 bad: 189 free(buf, M_TEMP); 190 return error; 191 } 192 193 caddr_t 194 stackgap_init(e) 195 struct emul *e; 196 { 197 198 #define szsigcode ((caddr_t)(e->e_esigcode - e->e_sigcode)) 199 return STACKGAPBASE; 200 #undef szsigcode 201 } 202 203 204 void * 205 stackgap_alloc(sgp, sz) 206 caddr_t *sgp; 207 size_t sz; 208 { 209 void *p = (void *) *sgp; 210 211 *sgp += ALIGN(sz); 212 return p; 213 } 214 215 void 216 compat_offseterr(vp, msg) 217 struct vnode *vp; 218 char *msg; 219 { 220 struct mount *mp; 221 222 mp = vp->v_mount; 223 224 log(LOG_ERR, "%s: dir offset too large on fs %s (mounted from %s)\n", 225 msg, mp->mnt_stat.f_mntonname, mp->mnt_stat.f_mntfromname); 226 uprintf("%s: dir offset too large for emulated program\n", msg); 227 } 228