linux_util.c revision 54655
1218885Sdim/* 2218885Sdim * Copyright (c) 1994 Christos Zoulas 3218885Sdim * Copyright (c) 1995 Frank van der Linden 4218885Sdim * Copyright (c) 1995 Scott Bartram 5218885Sdim * All rights reserved. 6218885Sdim * 7218885Sdim * Redistribution and use in source and binary forms, with or without 8218885Sdim * modification, are permitted provided that the following conditions 9218885Sdim * are met: 10218885Sdim * 1. Redistributions of source code must retain the above copyright 11218885Sdim * notice, this list of conditions and the following disclaimer. 12218885Sdim * 2. Redistributions in binary form must reproduce the above copyright 13218885Sdim * notice, this list of conditions and the following disclaimer in the 14218885Sdim * documentation and/or other materials provided with the distribution. 15218885Sdim * 3. The name of the author may not be used to endorse or promote products 16218885Sdim * derived from this software without specific prior written permission 17218885Sdim * 18218885Sdim * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 19218885Sdim * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20218885Sdim * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21218885Sdim * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 22218885Sdim * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 23218885Sdim * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24218885Sdim * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25218885Sdim * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26218885Sdim * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 27249423Sdim * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28249423Sdim * 29218885Sdim * from: svr4_util.c,v 1.5 1995/01/22 23:44:50 christos Exp 30234353Sdim * $FreeBSD: head/sys/compat/linux/linux_util.c 54655 1999-12-15 23:02:35Z eivind $ 31239462Sdim */ 32218885Sdim 33218885Sdim#include <sys/param.h> 34218885Sdim#include <sys/systm.h> 35234353Sdim#include <sys/namei.h> 36263508Sdim#include <sys/proc.h> 37218885Sdim#include <sys/malloc.h> 38218885Sdim#include <sys/vnode.h> 39218885Sdim 40234353Sdim#include <i386/linux/linux_util.h> 41218885Sdim 42234353Sdim#include <vm/vm_zone.h> 43218885Sdim 44243830Sdimconst char linux_emul_path[] = "/compat/linux"; 45234353Sdim 46234353Sdim/* 47234353Sdim * Search an alternate path before passing pathname arguments on 48218885Sdim * to system calls. Useful for keeping a seperate 'emulation tree'. 49218885Sdim * 50218885Sdim * If cflag is set, we check if an attempt can be made to create 51218885Sdim * the named file, i.e. we check if the directory it should 52218885Sdim * be in exists. 53218885Sdim */ 54218885Sdimint 55218885Sdimlinux_emul_find(p, sgp, prefix, path, pbuf, cflag) 56218885Sdim struct proc *p; 57218885Sdim caddr_t *sgp; /* Pointer to stackgap memory */ 58218885Sdim const char *prefix; 59218885Sdim char *path; 60218885Sdim char **pbuf; 61218885Sdim int cflag; 62218885Sdim{ 63218885Sdim struct nameidata nd; 64218885Sdim struct nameidata ndroot; 65218885Sdim struct vattr vat; 66218885Sdim struct vattr vatroot; 67218885Sdim int error; 68218885Sdim char *ptr, *buf, *cp; 69218885Sdim size_t sz, len; 70218885Sdim 71218885Sdim buf = (char *) malloc(MAXPATHLEN, M_TEMP, M_WAITOK); 72218885Sdim *pbuf = path; 73218885Sdim 74218885Sdim for (ptr = buf; (*ptr = *prefix) != '\0'; ptr++, prefix++) 75218885Sdim continue; 76218885Sdim 77218885Sdim sz = MAXPATHLEN - (ptr - buf); 78218885Sdim 79218885Sdim /* 80218885Sdim * If sgp is not given then the path is already in kernel space 81218885Sdim */ 82218885Sdim if (sgp == NULL) 83239462Sdim error = copystr(path, ptr, sz, &len); 84263508Sdim else 85263508Sdim error = copyinstr(path, ptr, sz, &len); 86263508Sdim 87263508Sdim if (error) { 88263508Sdim free(buf, M_TEMP); 89263508Sdim return error; 90263508Sdim } 91263508Sdim 92263508Sdim if (*ptr != '/') { 93263508Sdim free(buf, M_TEMP); 94263508Sdim return EINVAL; 95263508Sdim } 96263508Sdim 97263508Sdim /* 98263508Sdim * We know that there is a / somewhere in this pathname. 99263508Sdim * Search backwards for it, to find the file's parent dir 100263508Sdim * to see if it exists in the alternate tree. If it does, 101263508Sdim * and we want to create a file (cflag is set). We don't 102263508Sdim * need to worry about the root comparison in this case. 103263508Sdim */ 104263508Sdim 105239462Sdim if (cflag) { 106239462Sdim for (cp = &ptr[len] - 1; *cp != '/'; cp--); 107239462Sdim *cp = '\0'; 108239462Sdim 109239462Sdim NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, buf, p); 110239462Sdim 111239462Sdim if ((error = namei(&nd)) != 0) { 112239462Sdim free(buf, M_TEMP); 113239462Sdim return error; 114239462Sdim } 115239462Sdim 116239462Sdim *cp = '/'; 117239462Sdim } 118239462Sdim else { 119239462Sdim NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, buf, p); 120239462Sdim 121239462Sdim if ((error = namei(&nd)) != 0) { 122239462Sdim free(buf, M_TEMP); 123239462Sdim return error; 124239462Sdim } 125239462Sdim 126239462Sdim /* 127239462Sdim * We now compare the vnode of the linux_root to the one 128263508Sdim * vnode asked. If they resolve to be the same, then we 129263508Sdim * ignore the match so that the real root gets used. 130263508Sdim * This avoids the problem of traversing "../.." to find the 131239462Sdim * root directory and never finding it, because "/" resolves 132263508Sdim * to the emulation root directory. This is expensive :-( 133263508Sdim */ 134263508Sdim NDINIT(&ndroot, LOOKUP, FOLLOW, UIO_SYSSPACE, linux_emul_path, 135263508Sdim p); 136263508Sdim 137263508Sdim if ((error = namei(&ndroot)) != 0) { 138263508Sdim /* Cannot happen! */ 139263508Sdim free(buf, M_TEMP); 140263508Sdim NDFREE(&nd, NDF_ONLY_PNBUF); 141263508Sdim vrele(nd.ni_vp); 142263508Sdim return error; 143263508Sdim } 144263508Sdim 145263508Sdim if ((error = VOP_GETATTR(nd.ni_vp, &vat, p->p_ucred, p)) != 0) { 146263508Sdim goto bad; 147218885Sdim } 148218885Sdim 149218885Sdim if ((error = VOP_GETATTR(ndroot.ni_vp, &vatroot, p->p_ucred, p)) 150218885Sdim != 0) { 151234353Sdim goto bad; 152239462Sdim } 153239462Sdim 154263508Sdim if (vat.va_fsid == vatroot.va_fsid && 155263508Sdim vat.va_fileid == vatroot.va_fileid) { 156263508Sdim error = ENOENT; 157263508Sdim goto bad; 158234353Sdim } 159234353Sdim 160234353Sdim } 161234353Sdim if (sgp == NULL) 162234353Sdim *pbuf = buf; 163234353Sdim else { 164234353Sdim sz = &ptr[len] - buf; 165234353Sdim *pbuf = stackgap_alloc(sgp, sz + 1); 166234353Sdim error = copyout(buf, *pbuf, sz); 167234353Sdim free(buf, M_TEMP); 168218885Sdim } 169239462Sdim 170218885Sdim NDFREE(&nd, NDF_ONLY_PNBUF); 171263508Sdim vrele(nd.ni_vp); 172263508Sdim if (!cflag) { 173218885Sdim NDFREE(&ndroot, NDF_ONLY_PNBUF); 174263508Sdim vrele(ndroot.ni_vp); 175263508Sdim } 176263508Sdim return error; 177263508Sdim 178263508Sdimbad: 179263508Sdim NDFREE(&ndroot, NDF_ONLY_PNBUF); 180263508Sdim vrele(ndroot.ni_vp); 181263508Sdim NDFREE(&nd, NDF_ONLY_PNBUF); 182263508Sdim vrele(nd.ni_vp); 183263508Sdim free(buf, M_TEMP); 184263508Sdim return error; 185263508Sdim} 186263508Sdim