Home | History | Annotate | Line # | Download | only in ultrix
ultrix_pathname.c revision 1.4
      1 /*	$NetBSD: ultrix_pathname.c,v 1.4 1996/09/29 10:52:02 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 ap;
     89 
     90 	caddr_t sg = stackgap_init(p->p_emul);
     91 	ULTRIX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
     92 
     93 	SCARG(&ap, path) = SCARG(uap, path);
     94 	SCARG(&ap, flags) = O_WRONLY | O_CREAT | O_TRUNC;
     95 	SCARG(&ap, mode) = SCARG(uap, mode);
     96 
     97 	return (sys_open(p, &ap, 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 /* {
    147 		syscallarg(char *) path;
    148 		syscallarg(char **) argv;
    149 	} */ *uap = v;
    150 	struct sys_execve_args ap;
    151 	caddr_t sg;
    152 
    153 	sg = stackgap_init(p->p_emul);
    154 	ULTRIX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    155 
    156 	SCARG(&ap, path) = SCARG(uap, path);
    157 	SCARG(&ap, argp) = SCARG(uap, argp);
    158 	SCARG(&ap, envp) = NULL;
    159 
    160 	return (sys_execve(p, &ap, retval));
    161 }
    162 
    163 int
    164 ultrix_sys_execve(p, v, retval)
    165 	struct proc *p;
    166 	void *v;
    167 	register_t *retval;
    168 {
    169 	struct ultrix_sys_execve_args /* {
    170 		syscallarg(char *) path;
    171 		syscallarg(char **) argv;
    172 		syscallarg(char **) envp;
    173 	} */ *uap = v;
    174 	struct sys_execve_args ap;
    175 	caddr_t sg;
    176 
    177 	sg = stackgap_init(p->p_emul);
    178 	ULTRIX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    179 
    180 	SCARG(&ap, path) = SCARG(uap, path);
    181 	SCARG(&ap, argp) = SCARG(uap, argp);
    182 	SCARG(&ap, envp) = SCARG(uap, envp);
    183 
    184 	return (sys_execve(p, &ap, retval));
    185 }
    186 
    187 int
    188 ultrix_sys_open(p, v, retval)
    189 	struct proc *p;
    190 	void *v;
    191 	register_t *retval;
    192 {
    193 	struct ultrix_sys_open_args *uap = v;
    194 	int l, r;
    195 	int noctty;
    196 	int ret;
    197 
    198 	caddr_t sg = stackgap_init(p->p_emul);
    199 	ULTRIX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    200 
    201 	/* convert open flags into NetBSD flags */
    202 	l = SCARG(uap, flags);
    203 	noctty = l & 0x8000;
    204 	r =	(l & (0x0001 | 0x0002 | 0x0008 | 0x0040 | 0x0200 | 0x0400 | 0x0800));
    205 	r |=	((l & (0x0004 | 0x1000 | 0x4000)) ? O_NONBLOCK : 0);
    206 	r |=	((l & 0x0080) ? O_SHLOCK : 0);
    207 	r |=	((l & 0x0100) ? O_EXLOCK : 0);
    208 	r |=	((l & 0x2000) ? O_FSYNC : 0);
    209 
    210 	SCARG(uap, flags) = r;
    211 	ret = sys_open(p, (struct sys_open_args *)uap, retval);
    212 
    213 	if (!ret && !noctty && SESS_LEADER(p) && !(p->p_flag & P_CONTROLT)) {
    214 		struct filedesc *fdp = p->p_fd;
    215 		struct file *fp = fdp->fd_ofiles[*retval];
    216 
    217 		/* ignore any error, just give it a try */
    218 		if (fp->f_type == DTYPE_VNODE)
    219 			(fp->f_ops->fo_ioctl)(fp, TIOCSCTTY, (caddr_t)0, p);
    220 	}
    221 	return ret;
    222 }
    223 
    224 
    225 struct ultrix_statfs {
    226 	long	f_type;		/* type of info, zero for now */
    227 	long	f_bsize;	/* fundamental file system block size */
    228 	long	f_blocks;	/* total blocks in file system */
    229 	long	f_bfree;	/* free blocks */
    230 	long	f_bavail;	/* free blocks available to non-super-user */
    231 	long	f_files;	/* total file nodes in file system */
    232 	long	f_ffree;	/* free file nodes in fs */
    233 	fsid_t	f_fsid;		/* file system id */
    234 	long	f_spare[7];	/* spare for later */
    235 };
    236 
    237 /*
    238  * Custruct ultrix statfs result from native.
    239  * XXX should this be the same as returned by Ultrix getmnt(2)?
    240  * XXX Ultrix predates DEV_BSIZE.  Is  conversion of disk space from 1k
    241  *  block units to DEV_BSIZE necessary?
    242  */
    243 static int
    244 ultrixstatfs(sp, buf)
    245 	struct statfs *sp;
    246 	caddr_t buf;
    247 {
    248 	struct ultrix_statfs ssfs;
    249 
    250 	bzero(&ssfs, sizeof ssfs);
    251 	ssfs.f_type = 0;
    252 	ssfs.f_bsize = sp->f_bsize;
    253 	ssfs.f_blocks = sp->f_blocks;
    254 	ssfs.f_bfree = sp->f_bfree;
    255 	ssfs.f_bavail = sp->f_bavail;
    256 	ssfs.f_files = sp->f_files;
    257 	ssfs.f_ffree = sp->f_ffree;
    258 	ssfs.f_fsid = sp->f_fsid;
    259 	return copyout((caddr_t)&ssfs, buf, sizeof ssfs);
    260 }
    261 
    262 
    263 int
    264 ultrix_sys_statfs(p, v, retval)
    265 	struct proc *p;
    266 	void *v;
    267 	register_t *retval;
    268 {
    269 	struct ultrix_sys_statfs_args *uap = v;
    270 	register struct mount *mp;
    271 	register struct statfs *sp;
    272 	int error;
    273 	struct nameidata nd;
    274 
    275 	caddr_t sg = stackgap_init(p->p_emul);
    276 	ULTRIX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    277 
    278 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
    279 	if ((error = namei(&nd)) != 0)
    280 		return (error);
    281 
    282 	mp = nd.ni_vp->v_mount;
    283 	sp = &mp->mnt_stat;
    284 	vrele(nd.ni_vp);
    285 	if ((error = VFS_STATFS(mp, sp, p)) != 0)
    286 		return (error);
    287 	sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
    288 	return ultrixstatfs(sp, (caddr_t)SCARG(uap, buf));
    289 }
    290 
    291 /*
    292  * sys_fstatfs() takes an fd, not a path, and so needs no emul
    293  * pathname processing;  but it's similar enough to sys_statfs() that
    294  * it goes here anyway.
    295  */
    296 int
    297 ultrix_sys_fstatfs(p, v, retval)
    298 	struct proc *p;
    299 	void *v;
    300 	register_t *retval;
    301 {
    302 	struct ultrix_sys_fstatfs_args *uap = v;
    303 	struct file *fp;
    304 	struct mount *mp;
    305 	register struct statfs *sp;
    306 	int error;
    307 
    308 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
    309 		return (error);
    310 	mp = ((struct vnode *)fp->f_data)->v_mount;
    311 	sp = &mp->mnt_stat;
    312 	if ((error = VFS_STATFS(mp, sp, p)) != 0)
    313 		return (error);
    314 	sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
    315 	return ultrixstatfs(sp, (caddr_t)SCARG(uap, buf));
    316 }
    317 
    318 int
    319 ultrix_sys_mknod(p, v, retval)
    320 	struct proc *p;
    321 	void *v;
    322 	register_t *retval;
    323 {
    324 	struct ultrix_sys_mknod_args *uap = v;
    325 
    326 	caddr_t sg = stackgap_init(p->p_emul);
    327 	ULTRIX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    328 
    329 	if (S_ISFIFO(SCARG(uap, mode)))
    330 		return sys_mkfifo(p, uap, retval);
    331 
    332 	return sys_mknod(p, (struct sys_mknod_args *)uap, retval);
    333 }
    334