Home | History | Annotate | Line # | Download | only in ultrix
ultrix_pathname.c revision 1.1
      1 /*	$NetBSD: ultrix_pathname.c,v 1.1 1996/01/07 13:38:52 jonathan 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 
     76 #include <compat/ultrix/ultrix_syscallargs.h>
     77 #include <compat/ultrix/ultrix_util.h>
     78 
     79 const char ultrix_emul_path[] = "/emul/ultrix";
     80 
     81 int
     82 ultrix_sys_creat(p, v, retval)
     83 	struct proc *p;
     84 	void *v;
     85 	register_t *retval;
     86 {
     87 	struct ultrix_sys_creat_args *uap = v;
     88 	struct sys_open_args ouap;
     89 
     90 	caddr_t sg = stackgap_init(p->p_emul);
     91 	ULTRIX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
     92 
     93 	SCARG(&ouap, path) = SCARG(uap, path);
     94 	SCARG(&ouap, flags) = O_WRONLY | O_CREAT | O_TRUNC;
     95 	SCARG(&ouap, mode) = SCARG(uap, mode);
     96 
     97 	return (sys_open(p, &ouap, retval));
     98 }
     99 
    100 
    101 int
    102 ultrix_sys_access(p, v, retval)
    103 	struct proc *p;
    104 	void *v;
    105 	register_t *retval;
    106 {
    107 	struct ultrix_sys_access_args *uap = v;
    108 	caddr_t sg = stackgap_init(p->p_emul);
    109 	ULTRIX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    110 
    111 	return (sys_access(p, uap, retval));
    112 }
    113 
    114 int
    115 ultrix_sys_stat(p, v, retval)
    116 	struct proc *p;
    117 	void *v;
    118 	register_t *retval;
    119 {
    120 	struct ultrix_sys_stat_args *uap = v;
    121 	caddr_t sg = stackgap_init(p->p_emul);
    122 	ULTRIX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    123 
    124 	return (compat_43_sys_stat(p, uap, retval));
    125 }
    126 
    127 int
    128 ultrix_sys_lstat(p, v, retval)
    129 	struct proc *p;
    130 	void *v;
    131 	register_t *retval;
    132 {
    133 	struct ultrix_sys_lstat_args *uap = v;
    134 	caddr_t sg = stackgap_init(p->p_emul);
    135 	ULTRIX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    136 
    137 	return (compat_43_sys_lstat(p, uap, retval));
    138 }
    139 
    140 int
    141 ultrix_sys_execv(p, v, retval)
    142 	struct proc *p;
    143 	void *v;
    144 	register_t *retval;
    145 {
    146 	struct ultrix_sys_execv_args *uap = v;
    147 	struct sys_execve_args ouap;
    148 
    149 	caddr_t sg = stackgap_init(p->p_emul);
    150 	ULTRIX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    151 
    152 	SCARG(&ouap, path) = SCARG(uap, path);
    153 	SCARG(&ouap, argp) = SCARG(uap, argp);
    154 	SCARG(&ouap, envp) = NULL;
    155 
    156 	return (sys_execve(p, &ouap, retval));
    157 }
    158 
    159 int
    160 ultrix_sys_open(p, v, retval)
    161 	struct proc *p;
    162 	void *v;
    163 	register_t *retval;
    164 {
    165 	struct ultrix_sys_open_args *uap = v;
    166 	int l, r;
    167 	int noctty;
    168 	int ret;
    169 
    170 	caddr_t sg = stackgap_init(p->p_emul);
    171 	ULTRIX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    172 
    173 	/* convert open flags into NetBSD flags */
    174 	l = SCARG(uap, flags);
    175 	noctty = l & 0x8000;
    176 	r =	(l & (0x0001 | 0x0002 | 0x0008 | 0x0040 | 0x0200 | 0x0400 | 0x0800));
    177 	r |=	((l & (0x0004 | 0x1000 | 0x4000)) ? O_NONBLOCK : 0);
    178 	r |=	((l & 0x0080) ? O_SHLOCK : 0);
    179 	r |=	((l & 0x0100) ? O_EXLOCK : 0);
    180 	r |=	((l & 0x2000) ? O_FSYNC : 0);
    181 
    182 	SCARG(uap, flags) = r;
    183 	ret = sys_open(p, (struct sys_open_args *)uap, retval);
    184 
    185 	if (!ret && !noctty && SESS_LEADER(p) && !(p->p_flag & P_CONTROLT)) {
    186 		struct filedesc *fdp = p->p_fd;
    187 		struct file *fp = fdp->fd_ofiles[*retval];
    188 
    189 		/* ignore any error, just give it a try */
    190 		if (fp->f_type == DTYPE_VNODE)
    191 			(fp->f_ops->fo_ioctl)(fp, TIOCSCTTY, (caddr_t)0, p);
    192 	}
    193 	return ret;
    194 }
    195 
    196 
    197 struct ultrix_statfs {
    198 	long	f_type;		/* type of info, zero for now */
    199 	long	f_bsize;	/* fundamental file system block size */
    200 	long	f_blocks;	/* total blocks in file system */
    201 	long	f_bfree;	/* free blocks */
    202 	long	f_bavail;	/* free blocks available to non-super-user */
    203 	long	f_files;	/* total file nodes in file system */
    204 	long	f_ffree;	/* free file nodes in fs */
    205 	fsid_t	f_fsid;		/* file system id */
    206 	long	f_spare[7];	/* spare for later */
    207 };
    208 
    209 /*
    210  * Custruct ultrix statfs result from native.
    211  * XXX should this be the same as returned by Ultrix getmnt(2)?
    212  * XXX Ultrix predates DEV_BSIZE.  Is  conversion of disk space from 1k
    213  *  block units to DEV_BSIZE necessary?
    214  */
    215 static int
    216 ultrixstatfs(sp, buf)
    217 	struct statfs *sp;
    218 	caddr_t buf;
    219 {
    220 	struct ultrix_statfs ssfs;
    221 
    222 	bzero(&ssfs, sizeof ssfs);
    223 	ssfs.f_type = 0;
    224 	ssfs.f_bsize = sp->f_bsize;
    225 	ssfs.f_blocks = sp->f_blocks;
    226 	ssfs.f_bfree = sp->f_bfree;
    227 	ssfs.f_bavail = sp->f_bavail;
    228 	ssfs.f_files = sp->f_files;
    229 	ssfs.f_ffree = sp->f_ffree;
    230 	ssfs.f_fsid = sp->f_fsid;
    231 	return copyout((caddr_t)&ssfs, buf, sizeof ssfs);
    232 }
    233 
    234 
    235 int
    236 ultrix_sys_statfs(p, v, retval)
    237 	struct proc *p;
    238 	void *v;
    239 	register_t *retval;
    240 {
    241 	struct ultrix_sys_statfs_args *uap = v;
    242 	register struct mount *mp;
    243 	register struct statfs *sp;
    244 	int error;
    245 	struct nameidata nd;
    246 
    247 	caddr_t sg = stackgap_init(p->p_emul);
    248 	ULTRIX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    249 
    250 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
    251 	if (error = namei(&nd))
    252 		return (error);
    253 	mp = nd.ni_vp->v_mount;
    254 	sp = &mp->mnt_stat;
    255 	vrele(nd.ni_vp);
    256 	if (error = VFS_STATFS(mp, sp, p))
    257 		return (error);
    258 	sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
    259 	return ultrixstatfs(sp, (caddr_t)SCARG(uap, buf));
    260 }
    261 
    262 /*
    263  * sys_fstatfs() takes an fd, not a path, and so needs no emul
    264  * pathname processing;  but it's similar enough to sys_statfs() that
    265  * it goes here anyway.
    266  */
    267 int
    268 ultrix_sys_fstatfs(p, v, retval)
    269 	struct proc *p;
    270 	void *v;
    271 	register_t *retval;
    272 {
    273 	struct ultrix_sys_fstatfs_args *uap = v;
    274 	struct file *fp;
    275 	struct mount *mp;
    276 	register struct statfs *sp;
    277 	int error;
    278 
    279 	if (error = getvnode(p->p_fd, SCARG(uap, fd), &fp))
    280 		return (error);
    281 	mp = ((struct vnode *)fp->f_data)->v_mount;
    282 	sp = &mp->mnt_stat;
    283 	if (error = VFS_STATFS(mp, sp, p))
    284 		return (error);
    285 	sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
    286 	return ultrixstatfs(sp, (caddr_t)SCARG(uap, buf));
    287 }
    288 
    289 int
    290 ultrix_sys_mknod(p, v, retval)
    291 	struct proc *p;
    292 	void *v;
    293 	register_t *retval;
    294 {
    295 	struct ultrix_sys_mknod_args *uap = v;
    296 
    297 	caddr_t sg = stackgap_init(p->p_emul);
    298 	ULTRIX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    299 
    300 	if (S_ISFIFO(SCARG(uap, mode)))
    301 		return sys_mkfifo(p, uap, retval);
    302 
    303 	return sys_mknod(p, (struct sys_mknod_args *)uap, retval);
    304 }
    305