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 */ 31 32 #include <sys/cdefs.h> 33 __FBSDID("$FreeBSD$"); 34 35 #include <sys/param.h> 36 #include <sys/systm.h> 37 #include <sys/namei.h> 38 #include <sys/proc.h> 39 #include <sys/malloc.h> 40 #include <sys/vnode.h> 41 42 #include <machine/stdarg.h> 43 44 #include <compat/linux/linux_util.h> 45 46 const char linux_emul_path[] = "/compat/linux"; 47 48 /* 49 * Search an alternate path before passing pathname arguments on 50 * to system calls. Useful for keeping a separate 'emulation tree'. 51 * 52 * If cflag is set, we check if an attempt can be made to create 53 * the named file, i.e. we check if the directory it should 54 * be in exists. 55 */ 56 int 57 linux_emul_find(td, sgp, path, pbuf, cflag) 58 struct thread *td; 59 caddr_t *sgp; /* Pointer to stackgap memory */ 60 char *path; 61 char **pbuf; 62 int cflag; 63 { 64 char *newpath; 65 size_t sz; 66 int error; 67 68 error = linux_emul_convpath(td, path, (sgp == NULL) ? UIO_SYSSPACE : 69 UIO_USERSPACE, &newpath, cflag); 70 if (newpath == NULL) 71 return (error); 72 73 if (sgp == NULL) { 74 *pbuf = newpath; 75 return (error); 76 } 77 78 sz = strlen(newpath); 79 *pbuf = stackgap_alloc(sgp, sz + 1); 80 if (*pbuf != NULL) 81 error = copyout(newpath, *pbuf, sz + 1); 82 else 83 error = ENAMETOOLONG; 84 free(newpath, M_TEMP); 85 86 return (error); 87 } 88 89 int 90 linux_emul_convpath(td, path, pathseg, pbuf, cflag) 91 struct thread *td; 92 char *path; 93 enum uio_seg pathseg; 94 char **pbuf; 95 int cflag; 96 { 97 struct nameidata nd; 98 struct nameidata ndroot; 99 struct vattr vat; 100 struct vattr vatroot; 101 int error; 102 const char *prefix; 103 char *ptr, *buf, *cp; 104 size_t len, sz; 105 106 buf = (char *) malloc(MAXPATHLEN, M_TEMP, M_WAITOK); 107 *pbuf = buf; 108 109 prefix = linux_emul_path; 110 for (ptr = buf; (*ptr = *prefix) != '\0'; ptr++, prefix++) 111 continue; 112 sz = MAXPATHLEN - (ptr - buf); 113 114 if (pathseg == UIO_SYSSPACE) 115 error = copystr(path, ptr, sz, &len); 116 else 117 error = copyinstr(path, ptr, sz, &len); 118 119 if (error) { 120 *pbuf = NULL; 121 free(buf, M_TEMP); 122 return error; 123 } 124 125 if (*ptr != '/') { 126 error = EINVAL; 127 goto keeporig; 128 } 129 130 /* 131 * We know that there is a / somewhere in this pathname. 132 * Search backwards for it, to find the file's parent dir 133 * to see if it exists in the alternate tree. If it does, 134 * and we want to create a file (cflag is set). We don't 135 * need to worry about the root comparison in this case. 136 */ 137 138 if (cflag) { 139 for (cp = &ptr[len] - 1; *cp != '/'; cp--); 140 *cp = '\0'; 141 142 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, buf, td); 143 error = namei(&nd); 144 *cp = '/'; 145 if (error != 0) 146 goto keeporig; 147 } 148 else { 149 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, buf, td); 150 151 if ((error = namei(&nd)) != 0) 152 goto keeporig; 153 154 /* 155 * We now compare the vnode of the linux_root to the one 156 * vnode asked. If they resolve to be the same, then we 157 * ignore the match so that the real root gets used. 158 * This avoids the problem of traversing "../.." to find the 159 * root directory and never finding it, because "/" resolves 160 * to the emulation root directory. This is expensive :-( 161 */ 162 NDINIT(&ndroot, LOOKUP, FOLLOW, UIO_SYSSPACE, linux_emul_path, 163 td); 164 165 if ((error = namei(&ndroot)) != 0) { 166 /* Cannot happen! */ 167 NDFREE(&nd, NDF_ONLY_PNBUF); 168 vrele(nd.ni_vp); 169 goto keeporig; 170 } 171 172 if ((error = VOP_GETATTR(nd.ni_vp, &vat, td->td_ucred, td)) != 0) { 173 goto bad; 174 } 175 176 if ((error = VOP_GETATTR(ndroot.ni_vp, &vatroot, td->td_ucred, td)) 177 != 0) { 178 goto bad; 179 } 180 181 if (vat.va_fsid == vatroot.va_fsid && 182 vat.va_fileid == vatroot.va_fileid) { 183 error = ENOENT; 184 goto bad; 185 } 186 187 } 188 189 NDFREE(&nd, NDF_ONLY_PNBUF); 190 vrele(nd.ni_vp); 191 if (!cflag) { 192 NDFREE(&ndroot, NDF_ONLY_PNBUF); 193 vrele(ndroot.ni_vp); 194 } 195 return error; 196 197 bad: 198 NDFREE(&ndroot, NDF_ONLY_PNBUF); 199 vrele(ndroot.ni_vp); 200 NDFREE(&nd, NDF_ONLY_PNBUF); 201 vrele(nd.ni_vp); 202 keeporig: 203 /* Keep the original path; copy it back to the start of the buffer. */ 204 bcopy(ptr, buf, len); 205 return error; 206 } 207 208 void 209 linux_msg(const struct thread *td, const char *fmt, ...) 210 { 211 va_list ap; 212 struct proc *p; 213 214 p = td->td_proc; 215 printf("linux: pid %d (%s): ", (int)p->p_pid, p->p_comm); 216 va_start(ap, fmt); 217 vprintf(fmt, ap); 218 va_end(ap); 219 printf("\n"); 220 } 221