1 /* 2 * Copyright (c) 1994 Christos Zoulas 3 * Copyright (c) 1995 Frank van der Linden 4 * Copyright (c) 1995 Scott Bartram 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. The name of the author may not be used to endorse or promote products 16 * derived from this software without specific prior written permission 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 * 29 * from: svr4_util.c,v 1.5 1995/01/22 23:44:50 christos Exp 30 * $FreeBSD$ 31 */ 32 33 #include <sys/param.h> 34 #include <sys/systm.h> 35 #include <sys/namei.h> 36 #include <sys/proc.h> 37 #include <sys/malloc.h> 38 #include <sys/vnode.h> 39 40 #include <i386/linux/linux_util.h> 41 42 43 const char linux_emul_path[] = "/compat/linux"; 44 45 /* 46 * Search an alternate path before passing pathname arguments on 47 * to system calls. Useful for keeping a seperate 'emulation tree'. 48 * 49 * If cflag is set, we check if an attempt can be made to create 50 * the named file, i.e. we check if the directory it should 51 * be in exists. 52 */ 53 int 54 linux_emul_find(p, sgp, prefix, path, pbuf, cflag) 55 struct proc *p; 56 caddr_t *sgp; /* Pointer to stackgap memory */ 57 const char *prefix; 58 char *path; 59 char **pbuf; 60 int cflag; 61 { 62 struct nameidata nd; 63 struct nameidata ndroot; 64 struct vattr vat; 65 struct vattr vatroot; 66 int error; 67 char *ptr, *buf, *cp; 68 size_t sz, len; 69 70 buf = (char *) malloc(MAXPATHLEN, M_TEMP, M_WAITOK); 71 *pbuf = path; 72 73 for (ptr = buf; (*ptr = *prefix) != '\0'; ptr++, prefix++) 74 continue; 75 76 sz = MAXPATHLEN - (ptr - buf); 77 78 /* 79 * If sgp is not given then the path is already in kernel space 80 */ 81 if (sgp == NULL) 82 error = copystr(path, ptr, sz, &len); 83 else 84 error = copyinstr(path, ptr, sz, &len); 85 86 if (error) { 87 free(buf, M_TEMP); 88 return error; 89 } 90 91 if (*ptr != '/') { 92 free(buf, M_TEMP); 93 return EINVAL; 94 } 95 96 /* 97 * We know that there is a / somewhere in this pathname. 98 * Search backwards for it, to find the file's parent dir 99 * to see if it exists in the alternate tree. If it does, 100 * and we want to create a file (cflag is set). We don't 101 * need to worry about the root comparison in this case. 102 */ 103 104 if (cflag) { 105 for (cp = &ptr[len] - 1; *cp != '/'; cp--); 106 *cp = '\0'; 107 108 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, buf, p); 109 110 if ((error = namei(&nd)) != 0) { 111 free(buf, M_TEMP); 112 return error; 113 } 114 115 *cp = '/'; 116 } 117 else { 118 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, buf, p); 119 120 if ((error = namei(&nd)) != 0) { 121 free(buf, M_TEMP); 122 return error; 123 } 124 125 /* 126 * We now compare the vnode of the linux_root to the one 127 * vnode asked. If they resolve to be the same, then we 128 * ignore the match so that the real root gets used. 129 * This avoids the problem of traversing "../.." to find the 130 * root directory and never finding it, because "/" resolves 131 * to the emulation root directory. This is expensive :-( 132 */ 133 NDINIT(&ndroot, LOOKUP, FOLLOW, UIO_SYSSPACE, linux_emul_path, 134 p); 135 136 if ((error = namei(&ndroot)) != 0) { 137 /* Cannot happen! */ 138 free(buf, M_TEMP); 139 NDFREE(&nd, NDF_ONLY_PNBUF); 140 vrele(nd.ni_vp); 141 return error; 142 } 143 144 if ((error = VOP_GETATTR(nd.ni_vp, &vat, p->p_ucred, p)) != 0) { 145 goto bad; 146 } 147 148 if ((error = VOP_GETATTR(ndroot.ni_vp, &vatroot, p->p_ucred, p)) 149 != 0) { 150 goto bad; 151 } 152 153 if (vat.va_fsid == vatroot.va_fsid && 154 vat.va_fileid == vatroot.va_fileid) { 155 error = ENOENT; 156 goto bad; 157 } 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 NDFREE(&nd, NDF_ONLY_PNBUF); 170 vrele(nd.ni_vp); 171 if (!cflag) { 172 NDFREE(&ndroot, NDF_ONLY_PNBUF); 173 vrele(ndroot.ni_vp); 174 } 175 return error; 176 177 bad: 178 NDFREE(&ndroot, NDF_ONLY_PNBUF); 179 vrele(ndroot.ni_vp); 180 NDFREE(&nd, NDF_ONLY_PNBUF); 181 vrele(nd.ni_vp); 182 free(buf, M_TEMP); 183 return error; 184 } 185