1 /* $NetBSD: compat_util.c,v 1.9 1998/10/03 15:48:38 fvdl 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 char *path; 72 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 know that there is a / somewhere in this pathname. 110 * Search backwards for it, to find the file's parent dir 111 * to see if it exists in the alternate tree. If it does, 112 * and we want to create a file (cflag is set). We don't 113 * need to worry about the root comparison in this case. 114 */ 115 116 if (cflag) { 117 for (cp = &ptr[len] - 1; *cp != '/'; cp--) 118 ; 119 *cp = '\0'; 120 121 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, buf, p); 122 123 if ((error = namei(&nd)) != 0) 124 goto bad; 125 126 *cp = '/'; 127 } 128 else { 129 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, buf, p); 130 131 if ((error = namei(&nd)) != 0) 132 goto bad; 133 134 /* 135 * We now compare the vnode of the emulation root to the one 136 * vnode asked. If they resolve to be the same, then we 137 * ignore the match so that the real root gets used. 138 * This avoids the problem of traversing "../.." to find the 139 * root directory and never finding it, because "/" resolves 140 * to the emulation root directory. This is expensive :-( 141 */ 142 NDINIT(&ndroot, LOOKUP, FOLLOW, UIO_SYSSPACE, prefix, p); 143 144 if ((error = namei(&ndroot)) != 0) 145 goto bad2; 146 147 if ((error = VOP_GETATTR(nd.ni_vp, &vat, p->p_ucred, p)) != 0) 148 goto bad3; 149 150 if ((error = VOP_GETATTR(ndroot.ni_vp, &vatroot, p->p_ucred, p)) 151 != 0) 152 goto bad3; 153 154 if (vat.va_fsid == vatroot.va_fsid && 155 vat.va_fileid == vatroot.va_fileid) { 156 error = ENOENT; 157 goto bad3; 158 } 159 } 160 if (sgp == NULL) 161 *pbuf = buf; 162 else { 163 sz = &ptr[len] - buf; 164 *pbuf = stackgap_alloc(sgp, sz + 1); 165 error = copyout(buf, *pbuf, sz); 166 free(buf, M_TEMP); 167 } 168 169 vrele(nd.ni_vp); 170 if (!cflag) 171 vrele(ndroot.ni_vp); 172 return error; 173 174 bad3: 175 vrele(ndroot.ni_vp); 176 bad2: 177 vrele(nd.ni_vp); 178 bad: 179 free(buf, M_TEMP); 180 return error; 181 } 182 183 caddr_t 184 stackgap_init(e) 185 struct emul *e; 186 { 187 188 #define szsigcode ((caddr_t)(e->e_esigcode - e->e_sigcode)) 189 return STACKGAPBASE; 190 #undef szsigcode 191 } 192 193 194 void * 195 stackgap_alloc(sgp, sz) 196 caddr_t *sgp; 197 size_t sz; 198 { 199 void *p = (void *) *sgp; 200 201 *sgp += ALIGN(sz); 202 return p; 203 } 204 205 void 206 compat_offseterr(vp, msg) 207 struct vnode *vp; 208 char *msg; 209 { 210 struct mount *mp; 211 212 mp = vp->v_mount; 213 214 log(LOG_ERR, "%s: dir offset too large on fs %s (mounted from %s)\n", 215 msg, mp->mnt_stat.f_mntonname, mp->mnt_stat.f_mntfromname); 216 uprintf("%s: dir offset too large for emulated program\n", msg); 217 } 218