1 /* $NetBSD: osf1_file.c,v 1.43 2014/09/05 09:21:54 matt Exp $ */
2 
3 /*
4  * Copyright (c) 1999 Christopher G. Demetriou.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *      This product includes software developed by Christopher G. Demetriou
17  *	for the NetBSD Project.
18  * 4. The name of the author may not be used to endorse or promote products
19  *    derived from this software without specific prior written permission
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 /*
34  * Copyright (c) 1994, 1995 Carnegie-Mellon University.
35  * All rights reserved.
36  *
37  * Author: Chris G. Demetriou
38  *
39  * Permission to use, copy, modify and distribute this software and
40  * its documentation is hereby granted, provided that both the copyright
41  * notice and this permission notice appear in all copies of the
42  * software, derivative works or modified versions, and any portions
43  * thereof, and that both notices appear in supporting documentation.
44  *
45  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
46  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
47  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
48  *
49  * Carnegie Mellon requests users of this software to return to
50  *
51  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
52  *  School of Computer Science
53  *  Carnegie Mellon University
54  *  Pittsburgh PA 15213-3890
55  *
56  * any improvements or extensions that they make and grant Carnegie the
57  * rights to redistribute these changes.
58  */
59 
60 #include <sys/cdefs.h>
61 __KERNEL_RCSID(0, "$NetBSD: osf1_file.c,v 1.43 2014/09/05 09:21:54 matt Exp $");
62 
63 #if defined(_KERNEL_OPT)
64 #include "opt_syscall_debug.h"
65 #endif
66 
67 #include <sys/param.h>
68 #include <sys/systm.h>
69 #include <sys/namei.h>
70 #include <sys/proc.h>
71 #include <sys/file.h>
72 #include <sys/stat.h>
73 #include <sys/filedesc.h>
74 #include <sys/kernel.h>
75 #include <sys/malloc.h>
76 #include <sys/mman.h>
77 #include <sys/mount.h>
78 #include <sys/signal.h>
79 #include <sys/signalvar.h>
80 #include <sys/reboot.h>
81 #include <sys/syscallargs.h>
82 #include <sys/exec.h>
83 #include <sys/vnode.h>
84 #include <sys/socketvar.h>
85 #include <sys/resource.h>
86 #include <sys/resourcevar.h>
87 #include <sys/wait.h>
88 #include <sys/vfs_syscalls.h>
89 #include <sys/dirent.h>
90 
91 #include <compat/osf1/osf1.h>
92 #include <compat/osf1/osf1_syscallargs.h>
93 #include <compat/common/compat_util.h>
94 #include <compat/osf1/osf1_cvt.h>
95 #include <compat/osf1/osf1_dirent.h>
96 
97 #ifdef SYSCALL_DEBUG
98 extern int scdebug;
99 #endif
100 
101 int
osf1_sys_access(struct lwp * l,const struct osf1_sys_access_args * uap,register_t * retval)102 osf1_sys_access(struct lwp *l, const struct osf1_sys_access_args *uap, register_t *retval)
103 {
104 	struct sys_access_args a;
105 	unsigned long leftovers;
106 
107 	SCARG(&a, path) = SCARG(uap, path);
108 
109 	/* translate flags */
110 	SCARG(&a, flags) = emul_flags_translate(osf1_access_flags_xtab,
111 	    SCARG(uap, flags), &leftovers);
112 	if (leftovers != 0)
113 		return (EINVAL);
114 
115 	return sys_access(l, &a, retval);
116 }
117 
118 int
osf1_sys_execve(struct lwp * l,const struct osf1_sys_execve_args * uap,register_t * retval)119 osf1_sys_execve(struct lwp *l, const struct osf1_sys_execve_args *uap, register_t *retval)
120 {
121 	struct sys_execve_args ap;
122 
123 	SCARG(&ap, path) = SCARG(uap, path);
124 	SCARG(&ap, argp) = SCARG(uap, argp);
125 	SCARG(&ap, envp) = SCARG(uap, envp);
126 
127 	return sys_execve(l, &ap, retval);
128 }
129 
130 int
osf1_sys_getdirentries(struct lwp * l,const struct osf1_sys_getdirentries_args * uap,register_t * retval)131 osf1_sys_getdirentries(struct lwp *l, const struct osf1_sys_getdirentries_args *uap, register_t *retval)
132 {
133 	/* {
134 		syscallarg(int) fd;
135 		syscallarg(char *) buf;
136 		syscallarg(int) nbytes;
137 		syscallarg(long *) basep;
138 	} */
139 	struct dirent *bdp;
140 	struct vnode *vp;
141 	char *inp, *buf;        /* BSD-format */
142 	int len, reclen;        /* BSD-format */
143 	char *outp;             /* OSF1-format */
144 	int resid, osf1_reclen; /* OSF1-format */
145 	struct file *fp;
146 	struct uio auio;
147 	struct iovec aiov;
148 	struct osf1_dirent idb;
149 	off_t off, off1;        /* true file offset */
150 	int buflen, error, eofflag;
151 	off_t *cookiebuf = NULL, *cookie;
152 	int ncookies, fd;
153 
154 	if (SCARG(uap, nbytes) < 0)
155 		return EINVAL;
156 	if (SCARG(uap, nbytes) == 0)
157 		return 0;
158 
159 	fd = SCARG(uap, fd);
160 	if ((error = fd_getvnode(fd, &fp)) != 0)
161 		return (error);
162 	if ((fp->f_flag & FREAD) == 0) {
163 		error = EBADF;
164 		goto out1;
165 	}
166 
167 	vp = fp->f_vnode;
168 	if (vp->v_type != VDIR) {
169 		error = EINVAL;
170 		goto out1;
171 	}
172 
173 	buflen = min(MAXBSIZE, SCARG(uap, nbytes));
174 	buf = kmem_alloc(buflen, KM_SLEEP);
175 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
176 	off = off1 = fp->f_offset;
177 again:
178 	aiov.iov_base = buf;
179 	aiov.iov_len = buflen;
180 	auio.uio_iov = &aiov;
181 	auio.uio_iovcnt = 1;
182 	auio.uio_rw = UIO_READ;
183 	auio.uio_resid = buflen;
184 	auio.uio_offset = off;
185 	UIO_SETUP_SYSSPACE(&auio);
186 	/*
187 	 * First we read into the allocated buffer, then
188 	 * we massage it into user space, one record at a time.
189 	 */
190 	error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &cookiebuf,
191 	    &ncookies);
192 	if (error)
193 		goto out;
194 
195 	inp = buf;
196 	outp = (char *)SCARG(uap, buf);
197 	resid = SCARG(uap, nbytes);
198 	if ((len = buflen - auio.uio_resid) == 0)
199 		goto eof;
200 
201 	for (cookie = cookiebuf; len > 0; len -= reclen) {
202 		bdp = (struct dirent *)inp;
203 		reclen = bdp->d_reclen;
204 		if (reclen & 3)
205 			panic("osf1_sys_getdirentries: bad reclen");
206 		if (cookie)
207 			off = *cookie++; /* each entry points to the next */
208 		else
209 			off += reclen;
210 		if ((off >> 32) != 0) {
211 			compat_offseterr(vp, "osf1_sys_getdirentries");
212 			error = EINVAL;
213 			goto out;
214 		}
215 		if (bdp->d_fileno == 0) {
216 			inp += reclen;  /* it is a hole; squish it out */
217 			continue;
218 		}
219 		osf1_reclen = OSF1_RECLEN(&idb, bdp->d_namlen);
220 		if (reclen > len || resid < osf1_reclen) {
221 			/* entry too big for buffer, so just stop */
222 			outp++;
223 			break;
224 		}
225 		/*
226 		 * Massage in place to make a OSF1-shaped dirent (otherwise
227 		 * we have to worry about touching user memory outside of
228 		 * the copyout() call).
229 		 */
230 		idb.d_ino = (osf1_ino_t)bdp->d_fileno;
231 		idb.d_reclen = (u_short)osf1_reclen;
232 		idb.d_namlen = (u_short)bdp->d_namlen;
233 		strlcpy(idb.d_name, bdp->d_name, sizeof(idb.d_name));
234 		if ((error = copyout((void *)&idb, outp, osf1_reclen)))
235 			goto out;
236 		/* advance past this real entry */
237 		inp += reclen;
238 		/* advance output past OSF1-shaped entry */
239 		outp += osf1_reclen;
240 		resid -= osf1_reclen;
241 	}
242 
243 	/* if we squished out the whole block, try again */
244 	if (outp == (char *)SCARG(uap, buf)) {
245 		if (cookiebuf)
246 			free(cookiebuf, M_TEMP);
247 		cookiebuf = NULL;
248 		goto again;
249 	}
250 	fp->f_offset = off;     /* update the vnode offset */
251 
252 eof:
253 	*retval = SCARG(uap, nbytes) - resid;
254 out:
255 	VOP_UNLOCK(vp);
256 	if (cookiebuf)
257 		free(cookiebuf, M_TEMP);
258 	kmem_free(buf, buflen);
259 	if (SCARG(uap, basep) != NULL)
260 		error = copyout(&off1, SCARG(uap, basep), sizeof(long));
261 out1:
262 	fd_putfile(fd);
263 	return error;
264 }
265 
266 /*
267  * Get file status; this version does not follow links.
268  */
269 /* ARGSUSED */
270 int
osf1_sys_lstat(struct lwp * l,const struct osf1_sys_lstat_args * uap,register_t * retval)271 osf1_sys_lstat(struct lwp *l, const struct osf1_sys_lstat_args *uap, register_t *retval)
272 {
273 	struct stat sb;
274 	struct osf1_stat osb;
275 	int error;
276 
277 	error = do_sys_stat(SCARG(uap, path), NOFOLLOW, &sb);
278 	if (error)
279 		return (error);
280 	osf1_cvt_stat_from_native(&sb, &osb);
281 	error = copyout(&osb, SCARG(uap, ub), sizeof (osb));
282 	return (error);
283 }
284 
285 /*
286  * Get file status; this version does not follow links.
287  */
288 /* ARGSUSED */
289 int
osf1_sys_lstat2(struct lwp * l,const struct osf1_sys_lstat2_args * uap,register_t * retval)290 osf1_sys_lstat2(struct lwp *l, const struct osf1_sys_lstat2_args *uap, register_t *retval)
291 {
292 	struct stat sb;
293 	struct osf1_stat2 osb;
294 	int error;
295 
296 	error = do_sys_stat(SCARG(uap, path), NOFOLLOW, &sb);
297 	if (error)
298 		return (error);
299 	osf1_cvt_stat2_from_native(&sb, &osb);
300 	error = copyout((void *)&osb, (void *)SCARG(uap, ub), sizeof (osb));
301 	return (error);
302 }
303 
304 int
osf1_sys_mknod(struct lwp * l,const struct osf1_sys_mknod_args * uap,register_t * retval)305 osf1_sys_mknod(struct lwp *l, const struct osf1_sys_mknod_args *uap, register_t *retval)
306 {
307 
308 	return do_sys_mknod(l, SCARG(uap, path), SCARG(uap, mode),
309 	    osf1_cvt_dev_to_native(SCARG(uap, dev)), retval, UIO_USERSPACE);
310 }
311 
312 int
osf1_sys_open(struct lwp * l,const struct osf1_sys_open_args * uap,register_t * retval)313 osf1_sys_open(struct lwp *l, const struct osf1_sys_open_args *uap, register_t *retval)
314 {
315 	struct sys_open_args a;
316 	const char *path;
317 	unsigned long leftovers;
318 #ifdef SYSCALL_DEBUG
319 	char pnbuf[1024];
320 
321 	if (scdebug &&
322 	    copyinstr(SCARG(uap, path), pnbuf, sizeof pnbuf, NULL) == 0)
323 		printf("osf1_open: open: %s\n", pnbuf);
324 #endif
325 
326 	/* translate flags */
327 	SCARG(&a, flags) = emul_flags_translate(osf1_open_flags_xtab,
328 	    SCARG(uap, flags), &leftovers);
329 	if (leftovers != 0)
330 		return (EINVAL);
331 
332 	/* copy mode, no translation necessary */
333 	SCARG(&a, mode) = SCARG(uap, mode);
334 
335 	/* pick appropriate path */
336 	path = SCARG(uap, path);
337 	SCARG(&a, path) = path;
338 
339 	return sys_open(l, &a, retval);
340 }
341 
342 int
osf1_sys_pathconf(struct lwp * l,const struct osf1_sys_pathconf_args * uap,register_t * retval)343 osf1_sys_pathconf(struct lwp *l, const struct osf1_sys_pathconf_args *uap, register_t *retval)
344 {
345 	struct sys_pathconf_args a;
346 	int error;
347 
348 	SCARG(&a, path) = SCARG(uap, path);
349 
350 	error = osf1_cvt_pathconf_name_to_native(SCARG(uap, name),
351 	    &SCARG(&a, name));
352 
353 	if (error == 0)
354 		error = sys_pathconf(l, &a, retval);
355 
356 	return (error);
357 }
358 
359 /*
360  * Get file status; this version follows links.
361  */
362 /* ARGSUSED */
363 int
osf1_sys_stat(struct lwp * l,const struct osf1_sys_stat_args * uap,register_t * retval)364 osf1_sys_stat(struct lwp *l, const struct osf1_sys_stat_args *uap, register_t *retval)
365 {
366 	struct stat sb;
367 	struct osf1_stat osb;
368 	int error;
369 
370 	error = do_sys_stat(SCARG(uap, path), FOLLOW, &sb);
371 	if (error)
372 		return (error);
373 	osf1_cvt_stat_from_native(&sb, &osb);
374 	error = copyout((void *)&osb, (void *)SCARG(uap, ub), sizeof (osb));
375 	return (error);
376 }
377 
378 /*
379  * Get file status; this version follows links.
380  */
381 /* ARGSUSED */
382 int
osf1_sys_stat2(struct lwp * l,const struct osf1_sys_stat2_args * uap,register_t * retval)383 osf1_sys_stat2(struct lwp *l, const struct osf1_sys_stat2_args *uap, register_t *retval)
384 {
385 	struct stat sb;
386 	struct osf1_stat2 osb;
387 	int error;
388 
389 	error = do_sys_stat(SCARG(uap, path), FOLLOW, &sb);
390 	if (error)
391 		return (error);
392 	osf1_cvt_stat2_from_native(&sb, &osb);
393 	error = copyout((void *)&osb, (void *)SCARG(uap, ub), sizeof (osb));
394 	return (error);
395 }
396 
397 int
osf1_sys_truncate(struct lwp * l,const struct osf1_sys_truncate_args * uap,register_t * retval)398 osf1_sys_truncate(struct lwp *l, const struct osf1_sys_truncate_args *uap, register_t *retval)
399 {
400 	struct sys_truncate_args a;
401 
402 	SCARG(&a, path) = SCARG(uap, path);
403 	SCARG(&a, PAD) = 0;
404 	SCARG(&a, length) = SCARG(uap, length);
405 
406 	return sys_truncate(l, &a, retval);
407 }
408 
409 int
osf1_sys_utimes(struct lwp * l,const struct osf1_sys_utimes_args * uap,register_t * retval)410 osf1_sys_utimes(struct lwp *l, const struct osf1_sys_utimes_args *uap, register_t *retval)
411 {
412 	struct osf1_timeval otv;
413 	struct timeval tv[2], *tvp;
414 	int error;
415 
416 	if (SCARG(uap, tptr) == NULL)
417 		tvp = NULL;
418 	else {
419 		/* get the OSF/1 timeval argument */
420 		error = copyin(SCARG(uap, tptr), &otv, sizeof otv);
421 		if (error != 0)
422 			return error;
423 
424 		/* fill in and copy out the NetBSD timeval */
425 		tv[0].tv_sec = otv.tv_sec;
426 		tv[0].tv_usec = otv.tv_usec;
427 		/* Set access and modified to the same time */
428 		tv[1].tv_sec = otv.tv_sec;
429 		tv[1].tv_usec = otv.tv_usec;
430 		tvp = tv;
431 	}
432 
433 	return do_sys_utimes(l, NULL, SCARG(uap, path), FOLLOW,
434 			    tvp, UIO_SYSSPACE);
435 }
436