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