xref: /netbsd-src/sys/compat/ultrix/ultrix_pathname.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: ultrix_pathname.c,v 1.30 2007/12/08 19:29:40 pooka 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. Neither the name of the University nor the names of its contributors
25  *    may be used to endorse or promote products derived from this software
26  *    without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38  * SUCH DAMAGE.
39  *
40  *
41  *	@(#)sun_misc.c	8.1 (Berkeley) 6/18/93
42  *
43  * from: Header: sun_misc.c,v 1.16 93/04/07 02:46:27 torek Exp
44  */
45 
46 
47 /*
48  * Ultrix emulation filesystem-namespace compatibility module.
49  *
50  * Ultrix system calls that examine the filesysten namespace
51  * are implemented here.  Each system call has a wrapper that
52  * first checks if the given file exists at a special `emulation'
53  * pathname: the given path, prefixex with '/emul/ultrix', and
54  * if that pathname exists, it is used instead of the providd pathname.
55  *
56  * Used to locate OS-specific files (shared libraries, config files,
57  * etc) used by emul processes at their `normal' pathnames, without
58  * polluting, or conflicting with, the native filesysten namespace.
59  */
60 
61 #include <sys/cdefs.h>
62 __KERNEL_RCSID(0, "$NetBSD: ultrix_pathname.c,v 1.30 2007/12/08 19:29:40 pooka Exp $");
63 
64 #include <sys/param.h>
65 #include <sys/systm.h>
66 #include <sys/namei.h>
67 #include <sys/file.h>
68 #include <sys/filedesc.h>
69 #include <sys/ioctl.h>
70 #include <sys/mount.h>
71 #include <sys/stat.h>
72 #include <sys/vnode.h>
73 #include <sys/syscallargs.h>
74 #include <sys/proc.h>
75 
76 #include <compat/ultrix/ultrix_syscallargs.h>
77 #include <compat/common/compat_util.h>
78 
79 static int ultrixstatfs(struct statvfs *, void *);
80 
81 int
82 ultrix_sys_creat(struct lwp *l, void *v, register_t *retval)
83 {
84 	struct ultrix_sys_creat_args *uap = v;
85 	struct sys_open_args ap;
86 
87 	SCARG(&ap, path) = SCARG(uap, path);
88 	SCARG(&ap, flags) = O_WRONLY | O_CREAT | O_TRUNC;
89 	SCARG(&ap, mode) = SCARG(uap, mode);
90 
91 	return (sys_open(l, &ap, retval));
92 }
93 
94 
95 int
96 ultrix_sys_access(struct lwp *l, void *v, register_t *retval)
97 {
98 	struct ultrix_sys_access_args *uap = v;
99 
100 	return (sys_access(l, uap, retval));
101 }
102 
103 int
104 ultrix_sys_stat(struct lwp *l, void *v, register_t *retval)
105 {
106 	struct ultrix_sys_stat_args *uap = v;
107 
108 	return (compat_43_sys_stat(l, uap, retval));
109 }
110 
111 int
112 ultrix_sys_lstat(struct lwp *l, void *v, register_t *retval)
113 {
114 	struct ultrix_sys_lstat_args *uap = v;
115 
116 	return (compat_43_sys_lstat(l, uap, retval));
117 }
118 
119 int
120 ultrix_sys_execv(struct lwp *l, void *v, register_t *retval)
121 {
122 	struct ultrix_sys_execv_args /* {
123 		syscallarg(const char *) path;
124 		syscallarg(char **) argv;
125 	} */ *uap = v;
126 	struct sys_execve_args ap;
127 
128 	SCARG(&ap, path) = SCARG(uap, path);
129 	SCARG(&ap, argp) = SCARG(uap, argp);
130 	SCARG(&ap, envp) = NULL;
131 
132 	return (sys_execve(l, &ap, retval));
133 }
134 
135 int
136 ultrix_sys_execve(struct lwp *l, void *v, register_t *retval)
137 {
138 	struct ultrix_sys_execve_args /* {
139 		syscallarg(const char *) path;
140 		syscallarg(char **) argv;
141 		syscallarg(char **) envp;
142 	} */ *uap = v;
143 	struct sys_execve_args ap;
144 
145 	SCARG(&ap, path) = SCARG(uap, path);
146 	SCARG(&ap, argp) = SCARG(uap, argp);
147 	SCARG(&ap, envp) = SCARG(uap, envp);
148 
149 	return (sys_execve(l, &ap, retval));
150 }
151 
152 int
153 ultrix_sys_open(struct lwp *l, void *v, register_t *retval)
154 {
155 	struct ultrix_sys_open_args *uap = v;
156 	struct proc *p = l->l_proc;
157 	int q, r;
158 	int noctty;
159 	int ret;
160 
161 	/* convert open flags into NetBSD flags */
162 	q = SCARG(uap, flags);
163 	noctty = q & 0x8000;
164 	r =	(q & (0x0001 | 0x0002 | 0x0008 | 0x0040 | 0x0200 | 0x0400 | 0x0800));
165 	r |=	((q & (0x0004 | 0x1000 | 0x4000)) ? O_NONBLOCK : 0);
166 	r |=	((q & 0x0080) ? O_SHLOCK : 0);
167 	r |=	((q & 0x0100) ? O_EXLOCK : 0);
168 	r |=	((q & 0x2000) ? O_FSYNC : 0);
169 
170 	SCARG(uap, flags) = r;
171 	ret = sys_open(l, (struct sys_open_args *)uap, retval);
172 
173 	/* XXXSMP */
174 	if (!ret && !noctty && SESS_LEADER(p) && !(p->p_lflag & PL_CONTROLT)) {
175 		struct filedesc *fdp = p->p_fd;
176 		struct file *fp;
177 
178 		fp = fd_getfile(fdp, *retval);
179 
180 		/* ignore any error, just give it a try */
181 		if (fp != NULL && fp->f_type == DTYPE_VNODE)
182 			(fp->f_ops->fo_ioctl)(fp, TIOCSCTTY, (void *)0, l);
183 	}
184 	return ret;
185 }
186 
187 
188 struct ultrix_statfs {
189 	long	f_type;		/* type of info, zero for now */
190 	long	f_bsize;	/* fundamental file system block size */
191 	long	f_blocks;	/* total blocks in file system */
192 	long	f_bfree;	/* free blocks */
193 	long	f_bavail;	/* free blocks available to non-super-user */
194 	long	f_files;	/* total file nodes in file system */
195 	long	f_ffree;	/* free file nodes in fs */
196 	fsid_t	f_fsid;		/* file system id */
197 	long	f_spare[7];	/* spare for later */
198 };
199 
200 /*
201  * Custruct ultrix statfs result from native.
202  * XXX should this be the same as returned by Ultrix getmnt(2)?
203  * XXX Ultrix predates DEV_BSIZE.  Is  conversion of disk space from 1k
204  *  block units to DEV_BSIZE necessary?
205  */
206 static int
207 ultrixstatfs(struct statvfs *sp, void *buf)
208 {
209 	struct ultrix_statfs ssfs;
210 
211 	memset(&ssfs, 0, sizeof ssfs);
212 	ssfs.f_type = 0;
213 	ssfs.f_bsize = sp->f_bsize;
214 	ssfs.f_blocks = sp->f_blocks;
215 	ssfs.f_bfree = sp->f_bfree;
216 	ssfs.f_bavail = sp->f_bavail;
217 	ssfs.f_files = sp->f_files;
218 	ssfs.f_ffree = sp->f_ffree;
219 	ssfs.f_fsid = sp->f_fsidx;
220 	return copyout((void *)&ssfs, buf, sizeof ssfs);
221 }
222 
223 
224 int
225 ultrix_sys_statfs(struct lwp *l, void *v, register_t *retval)
226 {
227 	struct ultrix_sys_statfs_args *uap = v;
228 	struct mount *mp;
229 	struct statvfs *sp;
230 	int error;
231 	struct nameidata nd;
232 
233 	NDINIT(&nd, LOOKUP, FOLLOW | TRYEMULROOT, UIO_USERSPACE,
234 	    SCARG(uap, path));
235 	if ((error = namei(&nd)) != 0)
236 		return (error);
237 
238 	mp = nd.ni_vp->v_mount;
239 	sp = &mp->mnt_stat;
240 	vrele(nd.ni_vp);
241 	if ((error = VFS_STATVFS(mp, sp)) != 0)
242 		return (error);
243 	sp->f_flag = mp->mnt_flag & MNT_VISFLAGMASK;
244 	return ultrixstatfs(sp, (void *)SCARG(uap, buf));
245 }
246 
247 /*
248  * sys_fstatfs() takes an fd, not a path, and so needs no emul
249  * pathname processing;  but it's similar enough to sys_statvfs() that
250  * it goes here anyway.
251  */
252 int
253 ultrix_sys_fstatfs(struct lwp *l, void *v, register_t *retval)
254 {
255 	struct ultrix_sys_fstatfs_args *uap = v;
256 	struct proc *p = l->l_proc;
257 	struct file *fp;
258 	struct mount *mp;
259 	struct statvfs *sp;
260 	int error;
261 
262 	/* getvnode() will use the descriptor for us */
263 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
264 		return (error);
265 	mp = ((struct vnode *)fp->f_data)->v_mount;
266 	sp = &mp->mnt_stat;
267 	if ((error = VFS_STATVFS(mp, sp)) != 0)
268 		goto out;
269 	sp->f_flag = mp->mnt_flag & MNT_VISFLAGMASK;
270 	error = ultrixstatfs(sp, (void *)SCARG(uap, buf));
271  out:
272 	FILE_UNUSE(fp, l);
273 	return (error);
274 }
275 
276 int
277 ultrix_sys_mknod(struct lwp *l, void *v, register_t *retval)
278 {
279 	struct ultrix_sys_mknod_args *uap = v;
280 
281 	if (S_ISFIFO(SCARG(uap, mode)))
282 		return sys_mkfifo(l, uap, retval);
283 
284 	return sys_mknod(l, (struct sys_mknod_args *)uap, retval);
285 }
286