Home | History | Annotate | Line # | Download | only in ultrix
ultrix_pathname.c revision 1.5
      1 /*	$NetBSD: ultrix_pathname.c,v 1.5 1996/10/14 05:15:39 mhitch Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1992, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This software was developed by the Computer Systems Engineering group
      8  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
      9  * contributed to Berkeley.
     10  *
     11  * All advertising materials mentioning features or use of this software
     12  * must display the following acknowledgement:
     13  *	This product includes software developed by the University of
     14  *	California, Lawrence Berkeley Laboratory.
     15  *
     16  * Redistribution and use in source and binary forms, with or without
     17  * modification, are permitted provided that the following conditions
     18  * are met:
     19  * 1. Redistributions of source code must retain the above copyright
     20  *    notice, this list of conditions and the following disclaimer.
     21  * 2. Redistributions in binary form must reproduce the above copyright
     22  *    notice, this list of conditions and the following disclaimer in the
     23  *    documentation and/or other materials provided with the distribution.
     24  * 3. All advertising materials mentioning features or use of this software
     25  *    must display the following acknowledgement:
     26  *	This product includes software developed by the University of
     27  *	California, Berkeley and its contributors.
     28  * 4. Neither the name of the University nor the names of its contributors
     29  *    may be used to endorse or promote products derived from this software
     30  *    without specific prior written permission.
     31  *
     32  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     33  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     34  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     35  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     36  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     37  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     38  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     39  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     40  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     41  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     42  * SUCH DAMAGE.
     43  *
     44  *
     45  *	@(#)sun_misc.c	8.1 (Berkeley) 6/18/93
     46  *
     47  * from: Header: sun_misc.c,v 1.16 93/04/07 02:46:27 torek Exp
     48  */
     49 
     50 
     51 /*
     52  * Ultrix emulation filesystem-namespace compatibility module.
     53  *
     54  * Ultrix system calls that examine the filesysten namespace
     55  * are implemented here.  Each system call has a wrapper that
     56  * first checks if the given file exists at a special `emulation'
     57  * pathname: the given path, prefixex with '/emul/ultrix', and
     58  * if that pathname exists, it is used instead of the providd pathname.
     59  *
     60  * Used to locate OS-specific files (shared libraries, config files,
     61  * etc) used by emul processes at their `normal' pathnames, without
     62  * polluting, or conflicting with, the native filesysten namespace.
     63  */
     64 
     65 #include <sys/param.h>
     66 #include <sys/systm.h>
     67 #include <sys/namei.h>
     68 #include <sys/file.h>
     69 #include <sys/filedesc.h>
     70 #include <sys/ioctl.h>
     71 #include <sys/mount.h>
     72 #include <sys/stat.h>
     73 #include <sys/vnode.h>
     74 #include <sys/syscallargs.h>
     75 #include <sys/proc.h>
     76 
     77 #include <compat/ultrix/ultrix_syscallargs.h>
     78 #include <compat/ultrix/ultrix_util.h>
     79 
     80 const char ultrix_emul_path[] = "/emul/ultrix";
     81 
     82 int
     83 ultrix_sys_creat(p, v, retval)
     84 	struct proc *p;
     85 	void *v;
     86 	register_t *retval;
     87 {
     88 	struct ultrix_sys_creat_args *uap = v;
     89 	struct sys_open_args ap;
     90 
     91 	caddr_t sg = stackgap_init(p->p_emul);
     92 	ULTRIX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
     93 
     94 	SCARG(&ap, path) = SCARG(uap, path);
     95 	SCARG(&ap, flags) = O_WRONLY | O_CREAT | O_TRUNC;
     96 	SCARG(&ap, mode) = SCARG(uap, mode);
     97 
     98 	return (sys_open(p, &ap, retval));
     99 }
    100 
    101 
    102 int
    103 ultrix_sys_access(p, v, retval)
    104 	struct proc *p;
    105 	void *v;
    106 	register_t *retval;
    107 {
    108 	struct ultrix_sys_access_args *uap = v;
    109 	caddr_t sg = stackgap_init(p->p_emul);
    110 	ULTRIX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    111 
    112 	return (sys_access(p, uap, retval));
    113 }
    114 
    115 int
    116 ultrix_sys_stat(p, v, retval)
    117 	struct proc *p;
    118 	void *v;
    119 	register_t *retval;
    120 {
    121 	struct ultrix_sys_stat_args *uap = v;
    122 	caddr_t sg = stackgap_init(p->p_emul);
    123 	ULTRIX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    124 
    125 	return (compat_43_sys_stat(p, uap, retval));
    126 }
    127 
    128 int
    129 ultrix_sys_lstat(p, v, retval)
    130 	struct proc *p;
    131 	void *v;
    132 	register_t *retval;
    133 {
    134 	struct ultrix_sys_lstat_args *uap = v;
    135 	caddr_t sg = stackgap_init(p->p_emul);
    136 	ULTRIX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    137 
    138 	return (compat_43_sys_lstat(p, uap, retval));
    139 }
    140 
    141 int
    142 ultrix_sys_execv(p, v, retval)
    143 	struct proc *p;
    144 	void *v;
    145 	register_t *retval;
    146 {
    147 	struct ultrix_sys_execv_args /* {
    148 		syscallarg(char *) path;
    149 		syscallarg(char **) argv;
    150 	} */ *uap = v;
    151 	struct sys_execve_args ap;
    152 	caddr_t sg;
    153 
    154 	sg = stackgap_init(p->p_emul);
    155 	ULTRIX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    156 
    157 	SCARG(&ap, path) = SCARG(uap, path);
    158 	SCARG(&ap, argp) = SCARG(uap, argp);
    159 	SCARG(&ap, envp) = NULL;
    160 
    161 	return (sys_execve(p, &ap, retval));
    162 }
    163 
    164 int
    165 ultrix_sys_execve(p, v, retval)
    166 	struct proc *p;
    167 	void *v;
    168 	register_t *retval;
    169 {
    170 	struct ultrix_sys_execve_args /* {
    171 		syscallarg(char *) path;
    172 		syscallarg(char **) argv;
    173 		syscallarg(char **) envp;
    174 	} */ *uap = v;
    175 	struct sys_execve_args ap;
    176 	caddr_t sg;
    177 
    178 	sg = stackgap_init(p->p_emul);
    179 	ULTRIX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    180 
    181 	SCARG(&ap, path) = SCARG(uap, path);
    182 	SCARG(&ap, argp) = SCARG(uap, argp);
    183 	SCARG(&ap, envp) = SCARG(uap, envp);
    184 
    185 	return (sys_execve(p, &ap, retval));
    186 }
    187 
    188 int
    189 ultrix_sys_open(p, v, retval)
    190 	struct proc *p;
    191 	void *v;
    192 	register_t *retval;
    193 {
    194 	struct ultrix_sys_open_args *uap = v;
    195 	int l, r;
    196 	int noctty;
    197 	int ret;
    198 
    199 	caddr_t sg = stackgap_init(p->p_emul);
    200 	ULTRIX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    201 
    202 	/* convert open flags into NetBSD flags */
    203 	l = SCARG(uap, flags);
    204 	noctty = l & 0x8000;
    205 	r =	(l & (0x0001 | 0x0002 | 0x0008 | 0x0040 | 0x0200 | 0x0400 | 0x0800));
    206 	r |=	((l & (0x0004 | 0x1000 | 0x4000)) ? O_NONBLOCK : 0);
    207 	r |=	((l & 0x0080) ? O_SHLOCK : 0);
    208 	r |=	((l & 0x0100) ? O_EXLOCK : 0);
    209 	r |=	((l & 0x2000) ? O_FSYNC : 0);
    210 
    211 	SCARG(uap, flags) = r;
    212 	ret = sys_open(p, (struct sys_open_args *)uap, retval);
    213 
    214 	if (!ret && !noctty && SESS_LEADER(p) && !(p->p_flag & P_CONTROLT)) {
    215 		struct filedesc *fdp = p->p_fd;
    216 		struct file *fp = fdp->fd_ofiles[*retval];
    217 
    218 		/* ignore any error, just give it a try */
    219 		if (fp->f_type == DTYPE_VNODE)
    220 			(fp->f_ops->fo_ioctl)(fp, TIOCSCTTY, (caddr_t)0, p);
    221 	}
    222 	return ret;
    223 }
    224 
    225 
    226 struct ultrix_statfs {
    227 	long	f_type;		/* type of info, zero for now */
    228 	long	f_bsize;	/* fundamental file system block size */
    229 	long	f_blocks;	/* total blocks in file system */
    230 	long	f_bfree;	/* free blocks */
    231 	long	f_bavail;	/* free blocks available to non-super-user */
    232 	long	f_files;	/* total file nodes in file system */
    233 	long	f_ffree;	/* free file nodes in fs */
    234 	fsid_t	f_fsid;		/* file system id */
    235 	long	f_spare[7];	/* spare for later */
    236 };
    237 
    238 /*
    239  * Custruct ultrix statfs result from native.
    240  * XXX should this be the same as returned by Ultrix getmnt(2)?
    241  * XXX Ultrix predates DEV_BSIZE.  Is  conversion of disk space from 1k
    242  *  block units to DEV_BSIZE necessary?
    243  */
    244 static int
    245 ultrixstatfs(sp, buf)
    246 	struct statfs *sp;
    247 	caddr_t buf;
    248 {
    249 	struct ultrix_statfs ssfs;
    250 
    251 	bzero(&ssfs, sizeof ssfs);
    252 	ssfs.f_type = 0;
    253 	ssfs.f_bsize = sp->f_bsize;
    254 	ssfs.f_blocks = sp->f_blocks;
    255 	ssfs.f_bfree = sp->f_bfree;
    256 	ssfs.f_bavail = sp->f_bavail;
    257 	ssfs.f_files = sp->f_files;
    258 	ssfs.f_ffree = sp->f_ffree;
    259 	ssfs.f_fsid = sp->f_fsid;
    260 	return copyout((caddr_t)&ssfs, buf, sizeof ssfs);
    261 }
    262 
    263 
    264 int
    265 ultrix_sys_statfs(p, v, retval)
    266 	struct proc *p;
    267 	void *v;
    268 	register_t *retval;
    269 {
    270 	struct ultrix_sys_statfs_args *uap = v;
    271 	register struct mount *mp;
    272 	register struct statfs *sp;
    273 	int error;
    274 	struct nameidata nd;
    275 
    276 	caddr_t sg = stackgap_init(p->p_emul);
    277 	ULTRIX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    278 
    279 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
    280 	if ((error = namei(&nd)) != 0)
    281 		return (error);
    282 
    283 	mp = nd.ni_vp->v_mount;
    284 	sp = &mp->mnt_stat;
    285 	vrele(nd.ni_vp);
    286 	if ((error = VFS_STATFS(mp, sp, p)) != 0)
    287 		return (error);
    288 	sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
    289 	return ultrixstatfs(sp, (caddr_t)SCARG(uap, buf));
    290 }
    291 
    292 /*
    293  * sys_fstatfs() takes an fd, not a path, and so needs no emul
    294  * pathname processing;  but it's similar enough to sys_statfs() that
    295  * it goes here anyway.
    296  */
    297 int
    298 ultrix_sys_fstatfs(p, v, retval)
    299 	struct proc *p;
    300 	void *v;
    301 	register_t *retval;
    302 {
    303 	struct ultrix_sys_fstatfs_args *uap = v;
    304 	struct file *fp;
    305 	struct mount *mp;
    306 	register struct statfs *sp;
    307 	int error;
    308 
    309 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
    310 		return (error);
    311 	mp = ((struct vnode *)fp->f_data)->v_mount;
    312 	sp = &mp->mnt_stat;
    313 	if ((error = VFS_STATFS(mp, sp, p)) != 0)
    314 		return (error);
    315 	sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
    316 	return ultrixstatfs(sp, (caddr_t)SCARG(uap, buf));
    317 }
    318 
    319 int
    320 ultrix_sys_mknod(p, v, retval)
    321 	struct proc *p;
    322 	void *v;
    323 	register_t *retval;
    324 {
    325 	struct ultrix_sys_mknod_args *uap = v;
    326 
    327 	caddr_t sg = stackgap_init(p->p_emul);
    328 	ULTRIX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    329 
    330 	if (S_ISFIFO(SCARG(uap, mode)))
    331 		return sys_mkfifo(p, uap, retval);
    332 
    333 	return sys_mknod(p, (struct sys_mknod_args *)uap, retval);
    334 }
    335