1 /**	$MirOS: src/sys/compat/openbsd/openbsd_file.c,v 1.1.7.1 2005/03/06 16:33:43 tg Exp $ */
2 /*	$OpenBSD: netbsd_file.c,v 1.5 2003/08/15 20:32:16 tedu Exp $	*/
3 /*	$NetBSD: freebsd_file.c,v 1.3 1996/05/03 17:03:09 christos Exp $	*/
4 
5 /*
6  * Copyright (c) 1995 Frank van der Linden
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *      This product includes software developed for the NetBSD Project
20  *      by Frank van der Linden
21  * 4. The name of the author may not be used to endorse or promote products
22  *    derived from this software without specific prior written permission
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
25  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  *
35  *	from: linux_file.c,v 1.3 1995/04/04 04:21:30 mycroft Exp
36  */
37 
38 #include <sys/param.h>
39 #include <sys/syscall.h>
40 #include <sys/systm.h>
41 #include <sys/namei.h>
42 #include <sys/proc.h>
43 #include <sys/file.h>
44 #include <sys/stat.h>
45 #include <sys/filedesc.h>
46 #include <sys/ioctl.h>
47 #include <sys/kernel.h>
48 #include <sys/vnode.h>
49 #include <sys/mount.h>
50 #include <sys/malloc.h>
51 #include <compat/common/compat_util.h>
52 #include <compat/openbsd/compat_openbsd.h>
53 #include <compat/openbsd/openbsd_syscallargs.h>
54 
55 #define	ARRAY_LENGTH(array)	(sizeof(array)/sizeof(array[0]))
56 
57 int sys_mount(struct proc *, void *, register_t *);
58 int sys_open(struct proc *, void *, register_t *);
59 int compat_43_sys_creat(struct proc *, void *, register_t *);
60 int sys_link(struct proc *, void *, register_t *);
61 int sys_unlink(struct proc *, void *, register_t *);
62 int sys_chdir(struct proc *, void *, register_t *);
63 int sys_mknod(struct proc *, void *, register_t *);
64 int sys_chmod(struct proc *, void *, register_t *);
65 int sys_chown(struct proc *, void *, register_t *);
66 int sys_unmount(struct proc *, void *, register_t *);
67 int sys_access(struct proc *, void *, register_t *);
68 int sys_chflags(struct proc *, void *, register_t *);
69 int sys_revoke(struct proc *, void *, register_t *);
70 int sys_symlink(struct proc *, void *, register_t *);
71 int sys_readlink(struct proc *, void *, register_t *);
72 int sys_execve(struct proc *, void *, register_t *);
73 int sys_chroot(struct proc *, void *, register_t *);
74 int sys_rename(struct proc *, void *, register_t *);
75 int compat_43_sys_truncate(struct proc *, void *, register_t *);
76 int sys_mkfifo(struct proc *, void *, register_t *);
77 int sys_mkdir(struct proc *, void *, register_t *);
78 int sys_rmdir(struct proc *, void *, register_t *);
79 int sys_getfh(struct proc *, void *, register_t *);
80 int sys_pathconf(struct proc *, void *, register_t *);
81 int sys_truncate(struct proc *, void *, register_t *);
82 int compat_43_sys_lstat(struct proc *, void *, register_t *);
83 int sys_ktrace(struct proc *, void *, register_t *);
84 int sys_quotactl(struct proc *, void *, register_t *);
85 int sys_lchown(struct proc *, void *, register_t *);
86 int sys_statfs(struct proc *, void *, register_t *);
87 
88 
89 int
openbsd_sys_mount(p,v,retval)90 openbsd_sys_mount(p, v, retval)
91 	struct proc *p;
92 	void *v;
93 	register_t *retval;
94 {
95 	struct openbsd_sys_mount_args /* {
96 		syscallarg(int) type;
97 		syscallarg(char *) path;
98 		syscallarg(int) flags;
99 		syscallarg(caddr_t) data;
100 	} */ *uap = v;
101 	caddr_t sg = stackgap_init(p->p_emul);
102 
103 	OPENBSD_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
104 	return sys_mount(p, uap, retval);
105 }
106 
107 /* XXX - UNIX domain: int openbsd_sys_bind(int s, caddr_t name, int namelen); */
108 /* XXX - UNIX domain: int openbsd_sys_connect(int s, caddr_t name, int namelen); */
109 
110 int
openbsd_sys_open(p,v,retval)111 openbsd_sys_open(p, v, retval)
112 	struct proc *p;
113 	void *v;
114 	register_t *retval;
115 {
116 	struct openbsd_sys_open_args /* {
117 		syscallarg(char *) path;
118 		syscallarg(int) flags;
119 		syscallarg(int) mode;
120 	} */ *uap = v;
121 	caddr_t sg = stackgap_init(p->p_emul);
122 
123 	if (SCARG(uap, flags) & O_CREAT)
124 		OPENBSD_CHECK_ALT_CREAT(p, &sg, SCARG(uap, path));
125 	else
126 		OPENBSD_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
127 	return sys_open(p, uap, retval);
128 }
129 
130 int
compat_43_openbsd_sys_creat(p,v,retval)131 compat_43_openbsd_sys_creat(p, v, retval)
132 	struct proc *p;
133 	void *v;
134 	register_t *retval;
135 {
136 	struct compat_43_openbsd_sys_creat_args /* {
137 		syscallarg(char *) path;
138 		syscallarg(int) mode;
139 	} */ *uap = v;
140 	caddr_t sg  = stackgap_init(p->p_emul);
141 
142 	OPENBSD_CHECK_ALT_CREAT(p, &sg, SCARG(uap, path));
143 	return compat_43_sys_creat(p, uap, retval);
144 }
145 
146 int
openbsd_sys_link(p,v,retval)147 openbsd_sys_link(p, v, retval)
148 	struct proc *p;
149 	void *v;
150 	register_t *retval;
151 {
152 	struct openbsd_sys_link_args /* {
153 		syscallarg(char *) path;
154 		syscallarg(char *) link;
155 	} */ *uap = v;
156 	caddr_t sg = stackgap_init(p->p_emul);
157 
158 	OPENBSD_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
159 	OPENBSD_CHECK_ALT_CREAT(p, &sg, SCARG(uap, link));
160 	return sys_link(p, uap, retval);
161 }
162 
163 int
openbsd_sys_unlink(p,v,retval)164 openbsd_sys_unlink(p, v, retval)
165 	struct proc *p;
166 	void *v;
167 	register_t *retval;
168 {
169 	struct openbsd_sys_unlink_args /* {
170 		syscallarg(char *) path;
171 	} */ *uap = v;
172 	caddr_t sg = stackgap_init(p->p_emul);
173 
174 	OPENBSD_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
175 	return sys_unlink(p, uap, retval);
176 }
177 
178 int
openbsd_sys_chdir(p,v,retval)179 openbsd_sys_chdir(p, v, retval)
180 	struct proc *p;
181 	void *v;
182 	register_t *retval;
183 {
184 	struct openbsd_sys_chdir_args /* {
185 		syscallarg(char *) path;
186 	} */ *uap = v;
187 	caddr_t sg = stackgap_init(p->p_emul);
188 
189 	OPENBSD_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
190 	return sys_chdir(p, uap, retval);
191 }
192 
193 int
openbsd_sys_mknod(p,v,retval)194 openbsd_sys_mknod(p, v, retval)
195 	struct proc *p;
196 	void *v;
197 	register_t *retval;
198 {
199 	struct openbsd_sys_mknod_args /* {
200 		syscallarg(char *) path;
201 		syscallarg(int) mode;
202 		syscallarg(int) dev;
203 	} */ *uap = v;
204 	caddr_t sg = stackgap_init(p->p_emul);
205 
206 	OPENBSD_CHECK_ALT_CREAT(p, &sg, SCARG(uap, path));
207 	return sys_mknod(p, uap, retval);
208 }
209 
210 int
openbsd_sys_chmod(p,v,retval)211 openbsd_sys_chmod(p, v, retval)
212 	struct proc *p;
213 	void *v;
214 	register_t *retval;
215 {
216 	struct openbsd_sys_chmod_args /* {
217 		syscallarg(char *) path;
218 		syscallarg(int) mode;
219 	} */ *uap = v;
220 	caddr_t sg = stackgap_init(p->p_emul);
221 
222 	OPENBSD_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
223 	return sys_chmod(p, uap, retval);
224 }
225 
226 int
openbsd_sys_chown(p,v,retval)227 openbsd_sys_chown(p, v, retval)
228 	struct proc *p;
229 	void *v;
230 	register_t *retval;
231 {
232 	struct openbsd_sys_chown_args /* {
233 		syscallarg(char *) path;
234 		syscallarg(int) uid;
235 		syscallarg(int) gid;
236 	} */ *uap = v;
237 	caddr_t sg = stackgap_init(p->p_emul);
238 
239 	OPENBSD_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
240 	return sys_chown(p, uap, retval);
241 }
242 
243 int
openbsd_sys_unmount(p,v,retval)244 openbsd_sys_unmount(p, v, retval)
245 	struct proc *p;
246 	void *v;
247 	register_t *retval;
248 {
249 	struct openbsd_sys_unmount_args /* {
250 		syscallarg(char *) path;
251 		syscallarg(int) flags;
252 	} */ *uap = v;
253 	caddr_t sg = stackgap_init(p->p_emul);
254 
255 	OPENBSD_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
256 	return sys_unmount(p, uap, retval);
257 }
258 
259 int
openbsd_sys_access(p,v,retval)260 openbsd_sys_access(p, v, retval)
261 	struct proc *p;
262 	void *v;
263 	register_t *retval;
264 {
265 	struct openbsd_sys_access_args /* {
266 		syscallarg(char *) path;
267 		syscallarg(int) flags;
268 	} */ *uap = v;
269 	caddr_t sg = stackgap_init(p->p_emul);
270 
271 	OPENBSD_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
272 	return sys_access(p, uap, retval);
273 }
274 
275 int
openbsd_sys_chflags(p,v,retval)276 openbsd_sys_chflags(p, v, retval)
277 	struct proc *p;
278 	void *v;
279 	register_t *retval;
280 {
281 	struct openbsd_sys_chflags_args /* {
282 		syscallarg(char *) path;
283 		syscallarg(int) flags;
284 	} */ *uap = v;
285 	caddr_t sg = stackgap_init(p->p_emul);
286 
287 	OPENBSD_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
288 	return sys_chflags(p, uap, retval);
289 }
290 
291 int
openbsd_sys_revoke(p,v,retval)292 openbsd_sys_revoke(p, v, retval)
293 	struct proc *p;
294 	void *v;
295 	register_t *retval;
296 {
297 	struct openbsd_sys_revoke_args /* {
298 		syscallarg(char *) path;
299 	} */ *uap = v;
300 	caddr_t sg = stackgap_init(p->p_emul);
301 
302 	OPENBSD_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
303 	return sys_revoke(p, uap, retval);
304 }
305 
306 int
openbsd_sys_symlink(p,v,retval)307 openbsd_sys_symlink(p, v, retval)
308 	struct proc *p;
309 	void *v;
310 	register_t *retval;
311 {
312 	struct openbsd_sys_symlink_args /* {
313 		syscallarg(char *) path;
314 		syscallarg(char *) link;
315 	} */ *uap = v;
316 	caddr_t sg = stackgap_init(p->p_emul);
317 
318 	OPENBSD_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
319 	OPENBSD_CHECK_ALT_CREAT(p, &sg, SCARG(uap, link));
320 	return sys_symlink(p, uap, retval);
321 }
322 
323 int
openbsd_sys_readlink(p,v,retval)324 openbsd_sys_readlink(p, v, retval)
325 	struct proc *p;
326 	void *v;
327 	register_t *retval;
328 {
329 	struct openbsd_sys_readlink_args /* {
330 		syscallarg(char *) path;
331 		syscallarg(char *) buf;
332 		syscallarg(int) count;
333 	} */ *uap = v;
334 	caddr_t sg = stackgap_init(p->p_emul);
335 
336 	OPENBSD_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
337 	return sys_readlink(p, uap, retval);
338 }
339 
340 int
openbsd_sys_execve(p,v,retval)341 openbsd_sys_execve(p, v, retval)
342 	struct proc *p;
343 	void *v;
344 	register_t *retval;
345 {
346 	struct openbsd_sys_execve_args /* {
347 		syscallarg(char *) path;
348 		syscallarg(char *const *) argp;
349 		syscallarg(char *const *) envp;
350 	} */ *uap = v;
351 	caddr_t sg = stackgap_init(p->p_emul);
352 
353 	OPENBSD_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
354 	return sys_execve(p, uap, retval);
355 }
356 
357 int
openbsd_sys_chroot(p,v,retval)358 openbsd_sys_chroot(p, v, retval)
359 	struct proc *p;
360 	void *v;
361 	register_t *retval;
362 {
363 	struct openbsd_sys_chroot_args /* {
364 		syscallarg(char *) path;
365 	} */ *uap = v;
366 	caddr_t sg = stackgap_init(p->p_emul);
367 
368 	OPENBSD_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
369 	return sys_chroot(p, uap, retval);
370 }
371 
372 int
openbsd_sys_rename(p,v,retval)373 openbsd_sys_rename(p, v, retval)
374 	struct proc *p;
375 	void *v;
376 	register_t *retval;
377 {
378 	struct openbsd_sys_rename_args /* {
379 		syscallarg(char *) from;
380 		syscallarg(char *) to;
381 	} */ *uap = v;
382 	caddr_t sg = stackgap_init(p->p_emul);
383 
384 	OPENBSD_CHECK_ALT_EXIST(p, &sg, SCARG(uap, from));
385 	OPENBSD_CHECK_ALT_CREAT(p, &sg, SCARG(uap, to));
386 	return sys_rename(p, uap, retval);
387 }
388 
389 int
compat_43_openbsd_sys_truncate(p,v,retval)390 compat_43_openbsd_sys_truncate(p, v, retval)
391 	struct proc *p;
392 	void *v;
393 	register_t *retval;
394 {
395 	struct compat_43_openbsd_sys_truncate_args /* {
396 		syscallarg(char *) path;
397 		syscallarg(long) length;
398 	} */ *uap = v;
399 	caddr_t sg = stackgap_init(p->p_emul);
400 
401 	OPENBSD_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
402 	return compat_43_sys_truncate(p, uap, retval);
403 }
404 
405 int
openbsd_sys_mkfifo(p,v,retval)406 openbsd_sys_mkfifo(p, v, retval)
407 	struct proc *p;
408 	void *v;
409 	register_t *retval;
410 {
411 	struct openbsd_sys_mkfifo_args /* {
412 		syscallarg(char *) path;
413 		syscallarg(int) mode;
414 	} */ *uap = v;
415 	caddr_t sg = stackgap_init(p->p_emul);
416 
417 	OPENBSD_CHECK_ALT_CREAT(p, &sg, SCARG(uap, path));
418 	return sys_mkfifo(p, uap, retval);
419 }
420 
421 int
openbsd_sys_mkdir(p,v,retval)422 openbsd_sys_mkdir(p, v, retval)
423 	struct proc *p;
424 	void *v;
425 	register_t *retval;
426 {
427 	struct openbsd_sys_mkdir_args /* {
428 		syscallarg(char *) path;
429 		syscallarg(int) mode;
430 	} */ *uap = v;
431 	caddr_t sg = stackgap_init(p->p_emul);
432 
433 	OPENBSD_CHECK_ALT_CREAT(p, &sg, SCARG(uap, path));
434 	return sys_mkdir(p, uap, retval);
435 }
436 
437 int
openbsd_sys_rmdir(p,v,retval)438 openbsd_sys_rmdir(p, v, retval)
439 	struct proc *p;
440 	void *v;
441 	register_t *retval;
442 {
443 	struct openbsd_sys_rmdir_args /* {
444 		syscallarg(char *) path;
445 	} */ *uap = v;
446 	caddr_t sg = stackgap_init(p->p_emul);
447 
448 	OPENBSD_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
449 	return sys_rmdir(p, uap, retval);
450 }
451 
452 int
openbsd_sys_getfh(p,v,retval)453 openbsd_sys_getfh(p, v, retval)
454 	struct proc *p;
455 	void *v;
456 	register_t *retval;
457 {
458 	struct openbsd_sys_getfh_args /* {
459 		syscallarg(char *) fname;
460 		syscallarg(fhandle_t *) fhp;
461 	} */ *uap = v;
462 	caddr_t sg = stackgap_init(p->p_emul);
463 
464 	OPENBSD_CHECK_ALT_EXIST(p, &sg, SCARG(uap, fname));
465 	return sys_getfh(p, uap, retval);
466 }
467 
468 int
openbsd_sys_pathconf(p,v,retval)469 openbsd_sys_pathconf(p, v, retval)
470 	struct proc *p;
471 	void *v;
472 	register_t *retval;
473 {
474 	struct openbsd_sys_pathconf_args /* {
475 		syscallarg(char *) path;
476 		syscallarg(int) name;
477 	} */ *uap = v;
478 	caddr_t sg = stackgap_init(p->p_emul);
479 
480 	OPENBSD_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
481 	return sys_pathconf(p, uap, retval);
482 }
483 
484 int
openbsd_sys_truncate(p,v,retval)485 openbsd_sys_truncate(p, v, retval)
486 	struct proc *p;
487 	void *v;
488 	register_t *retval;
489 {
490 	struct openbsd_sys_truncate_args /* {
491 		syscallarg(char *) path;
492 		syscallarg(int) pad;
493 		syscallarg(off_t) length;
494 	} */ *uap = v;
495 	caddr_t sg = stackgap_init(p->p_emul);
496 
497 	OPENBSD_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
498 	return sys_truncate(p, uap, retval);
499 }
500 
501 int
compat_43_openbsd_sys_lstat(struct proc * p,void * v,register_t * retval)502 compat_43_openbsd_sys_lstat(struct proc *p, void *v, register_t *retval)
503 {
504 	struct compat_43_openbsd_sys_lstat_args *uap = v;
505 	caddr_t sg = stackgap_init(p->p_emul);
506 
507 	OPENBSD_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
508 	return compat_43_sys_lstat(p, uap, retval);
509 }
510 
511 int
openbsd_sys_ktrace(struct proc * p,void * v,register_t * retval)512 openbsd_sys_ktrace(struct proc *p, void *v, register_t *retval)
513 {
514 	struct openbsd_sys_ktrace_args *uap = v;
515 	caddr_t sg = stackgap_init(p->p_emul);
516 
517 	OPENBSD_CHECK_ALT_EXIST(p, &sg, SCARG(uap, fname));
518 	return sys_ktrace(p, uap, retval);
519 }
520 
521 int
openbsd_sys_quotactl(struct proc * p,void * v,register_t * retval)522 openbsd_sys_quotactl(struct proc *p, void *v, register_t *retval)
523 {
524 	struct openbsd_sys_quotactl_args *uap = v;
525 	caddr_t sg = stackgap_init(p->p_emul);
526 
527 	OPENBSD_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
528 	return sys_quotactl(p, uap, retval);
529 }
530 
531 int
openbsd_sys_lchown(struct proc * p,void * v,register_t * retval)532 openbsd_sys_lchown(struct proc *p, void *v, register_t *retval)
533 {
534 	struct openbsd_sys_lchown_args *uap = v;
535 	caddr_t sg = stackgap_init(p->p_emul);
536 
537 	OPENBSD_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
538 	return sys_lchown(p, uap, retval);
539 }
540 
541 int
openbsd_sys_statfs(struct proc * p,void * v,register_t * retval)542 openbsd_sys_statfs(struct proc *p, void *v, register_t *retval)
543 {
544 	struct openbsd_sys_statfs_args *uap = v;
545 	caddr_t sg = stackgap_init(p->p_emul);
546 
547 	OPENBSD_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
548 	return sys_statfs(p, uap, retval);
549 }
550