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