2 * Copyright (c) 1998 Mark Newton
3 * Copyright (c) 1994, 1997 Christos Zoulas.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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 Christos Zoulas.
17 * 4. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 * $FreeBSD: src/sys/svr4/svr4_fcntl.c,v 1.7 1999/12/12 10:27:04 newton Exp $
32 * $DragonFly: src/sys/emulation/svr4/Attic/svr4_fcntl.c,v 1.17 2005/12/10 16:06:20 swildner Exp $
34 #include <sys/param.h>
35 #include <sys/systm.h>
37 #include <sys/namei.h>
40 #include <sys/filedesc.h>
41 /*#include <sys/ioctl.h>*/
42 #include <sys/kernel.h>
43 #include <sys/mount.h>
44 #include <sys/malloc.h>
45 #include <sys/vnode.h>
46 #include <sys/unistd.h>
48 #include <sys/sysproto.h>
51 #include "svr4_types.h"
52 #include "svr4_signal.h"
53 #include "svr4_proto.h"
54 #include "svr4_util.h"
55 #include "svr4_fcntl.h"
57 static int svr4_to_bsd_flags (int);
58 static u_long svr4_to_bsd_cmd (u_long);
59 static int fd_revoke (struct thread *, int);
60 static int fd_truncate (struct thread *, int, struct flock *, int *);
61 static int bsd_to_svr4_flags (int);
62 static void bsd_to_svr4_flock (struct flock *, struct svr4_flock *);
63 static void svr4_to_bsd_flock (struct svr4_flock *, struct flock *);
64 static void bsd_to_svr4_flock64 (struct flock *, struct svr4_flock64 *);
65 static void svr4_to_bsd_flock64 (struct svr4_flock64 *, struct flock *);
68 svr4_to_bsd_cmd(u_long cmd)
93 svr4_to_bsd_flags(int l)
96 r |= (l & SVR4_O_RDONLY) ? O_RDONLY : 0;
97 r |= (l & SVR4_O_WRONLY) ? O_WRONLY : 0;
98 r |= (l & SVR4_O_RDWR) ? O_RDWR : 0;
99 r |= (l & SVR4_O_NDELAY) ? O_NONBLOCK : 0;
100 r |= (l & SVR4_O_APPEND) ? O_APPEND : 0;
101 r |= (l & SVR4_O_SYNC) ? O_FSYNC : 0;
102 r |= (l & SVR4_O_NONBLOCK) ? O_NONBLOCK : 0;
103 r |= (l & SVR4_O_PRIV) ? O_EXLOCK : 0;
104 r |= (l & SVR4_O_CREAT) ? O_CREAT : 0;
105 r |= (l & SVR4_O_TRUNC) ? O_TRUNC : 0;
106 r |= (l & SVR4_O_EXCL) ? O_EXCL : 0;
107 r |= (l & SVR4_O_NOCTTY) ? O_NOCTTY : 0;
112 bsd_to_svr4_flags(int l)
115 r |= (l & O_RDONLY) ? SVR4_O_RDONLY : 0;
116 r |= (l & O_WRONLY) ? SVR4_O_WRONLY : 0;
117 r |= (l & O_RDWR) ? SVR4_O_RDWR : 0;
118 r |= (l & O_NDELAY) ? SVR4_O_NONBLOCK : 0;
119 r |= (l & O_APPEND) ? SVR4_O_APPEND : 0;
120 r |= (l & O_FSYNC) ? SVR4_O_SYNC : 0;
121 r |= (l & O_NONBLOCK) ? SVR4_O_NONBLOCK : 0;
122 r |= (l & O_EXLOCK) ? SVR4_O_PRIV : 0;
123 r |= (l & O_CREAT) ? SVR4_O_CREAT : 0;
124 r |= (l & O_TRUNC) ? SVR4_O_TRUNC : 0;
125 r |= (l & O_EXCL) ? SVR4_O_EXCL : 0;
126 r |= (l & O_NOCTTY) ? SVR4_O_NOCTTY : 0;
132 bsd_to_svr4_flock(struct flock *iflp, struct svr4_flock *oflp)
134 switch (iflp->l_type) {
136 oflp->l_type = SVR4_F_RDLCK;
139 oflp->l_type = SVR4_F_WRLCK;
142 oflp->l_type = SVR4_F_UNLCK;
149 oflp->l_whence = (short) iflp->l_whence;
150 oflp->l_start = (svr4_off_t) iflp->l_start;
151 oflp->l_len = (svr4_off_t) iflp->l_len;
153 oflp->l_pid = (svr4_pid_t) iflp->l_pid;
158 svr4_to_bsd_flock(struct svr4_flock *iflp, struct flock *oflp)
160 switch (iflp->l_type) {
162 oflp->l_type = F_RDLCK;
165 oflp->l_type = F_WRLCK;
168 oflp->l_type = F_UNLCK;
175 oflp->l_whence = iflp->l_whence;
176 oflp->l_start = (off_t) iflp->l_start;
177 oflp->l_len = (off_t) iflp->l_len;
178 oflp->l_pid = (pid_t) iflp->l_pid;
183 bsd_to_svr4_flock64(struct flock *iflp, struct svr4_flock64 *oflp)
185 switch (iflp->l_type) {
187 oflp->l_type = SVR4_F_RDLCK;
190 oflp->l_type = SVR4_F_WRLCK;
193 oflp->l_type = SVR4_F_UNLCK;
200 oflp->l_whence = (short) iflp->l_whence;
201 oflp->l_start = (svr4_off64_t) iflp->l_start;
202 oflp->l_len = (svr4_off64_t) iflp->l_len;
204 oflp->l_pid = (svr4_pid_t) iflp->l_pid;
209 svr4_to_bsd_flock64(struct svr4_flock64 *iflp, struct flock *oflp)
211 switch (iflp->l_type) {
213 oflp->l_type = F_RDLCK;
216 oflp->l_type = F_WRLCK;
219 oflp->l_type = F_UNLCK;
226 oflp->l_whence = iflp->l_whence;
227 oflp->l_start = (off_t) iflp->l_start;
228 oflp->l_len = (off_t) iflp->l_len;
229 oflp->l_pid = (pid_t) iflp->l_pid;
235 fd_revoke(struct thread *td, int fd)
237 struct proc *p = td->td_proc;
238 struct filedesc *fdp;
247 if ((u_int)fd >= fdp->fd_nfiles || (fp = fdp->fd_files[fd].fp) == NULL)
250 if (fp->f_type != DTYPE_VNODE)
253 vp = (struct vnode *) fp->f_data;
255 if ((error = vx_get(vp)) != 0)
258 if (vp->v_type != VCHR && vp->v_type != VBLK) {
263 if ((error = VOP_GETATTR(vp, &vattr, td)) != 0)
266 if (p->p_ucred->cr_uid != vattr.va_uid &&
267 (error = suser(p->p_thread)) != 0)
271 VOP_REVOKE(vp, REVOKEALL);
279 fd_truncate(struct thread *td, int fd, struct flock *flp, int *retval)
281 struct proc *p = td->td_proc;
282 struct filedesc *fdp;
288 struct ftruncate_args ft;
294 * We only support truncating the file.
296 if ((u_int)fd >= fdp->fd_nfiles || (fp = fdp->fd_files[fd].fp) == NULL)
299 vp = (struct vnode *)fp->f_data;
300 if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO)
303 if ((error = VOP_GETATTR(vp, &vattr, td)) != 0)
306 length = vattr.va_size;
308 switch (flp->l_whence) {
310 start = fp->f_offset + flp->l_start;
314 start = flp->l_start + length;
318 start = flp->l_start;
325 if (start + flp->l_len < length) {
326 /* We don't support free'ing in the middle of the file */
331 SCARG(&ft, length) = start;
332 error = ftruncate(&ft);
333 *retval = ft.sysmsg_result;
338 svr4_sys_open(struct svr4_sys_open_args *uap)
340 struct thread *td = curthread; /* XXX */
341 struct proc *p = td->td_proc;
342 struct open_args cup;
345 caddr_t sg = stackgap_init();
346 CHECKALTEXIST(&sg, SCARG(uap, path));
348 cup.sysmsg_result = 0;
349 cup.path = uap->path;
350 cup.flags = svr4_to_bsd_flags(uap->flags);
351 cup.mode = uap->mode;
355 /* uprintf("svr4_open(%s, 0x%0x, 0%o): %d\n", uap->path,
356 uap->flags, uap->mode, error);*/
361 retval = uap->sysmsg_result = cup.sysmsg_result;
363 if (!(SCARG(&cup, flags) & O_NOCTTY) && SESS_LEADER(p) &&
364 !(p->p_flag & P_CONTROLT)) {
366 struct filedesc *fdp = p->p_fd;
367 struct file *fp = fdp->fd_files[retval].fp;
369 /* ignore any error, just give it a try */
370 if (fp->f_type == DTYPE_VNODE)
371 fo_ioctl(fp, TIOCSCTTY, (caddr_t) 0, p);
378 svr4_sys_open64(struct svr4_sys_open64_args *uap)
380 return svr4_sys_open((struct svr4_sys_open_args *)uap);
384 svr4_sys_creat(struct svr4_sys_creat_args *uap)
386 struct open_args cup;
389 caddr_t sg = stackgap_init();
390 CHECKALTEXIST(&sg, SCARG(uap, path));
392 SCARG(&cup, path) = SCARG(uap, path);
393 SCARG(&cup, mode) = SCARG(uap, mode);
394 SCARG(&cup, flags) = O_WRONLY | O_CREAT | O_TRUNC;
396 cup.sysmsg_result = 0;
398 uap->sysmsg_result = cup.sysmsg_result;
403 svr4_sys_creat64(struct svr4_sys_creat64_args *uap)
405 return svr4_sys_creat((struct svr4_sys_creat_args *)uap);
409 svr4_sys_llseek(struct svr4_sys_llseek_args *v)
411 struct svr4_sys_llseek_args *uap = v;
412 struct lseek_args ap;
414 SCARG(&ap, fd) = SCARG(uap, fd);
416 #if BYTE_ORDER == BIG_ENDIAN
417 SCARG(&ap, offset) = (((long long) SCARG(uap, offset1)) << 32) |
420 SCARG(&ap, offset) = (((long long) SCARG(uap, offset2)) << 32) |
423 SCARG(&ap, whence) = SCARG(uap, whence);
429 svr4_sys_access(struct svr4_sys_access_args *uap)
431 struct access_args cup;
434 caddr_t sg = stackgap_init();
435 CHECKALTEXIST(&sg, SCARG(uap, path));
437 SCARG(&cup, path) = SCARG(uap, path);
438 SCARG(&cup, flags) = SCARG(uap, flags);
439 cup.sysmsg_result = 0;
440 error = access(&cup);
441 uap->sysmsg_result = cup.sysmsg_result;
447 svr4_sys_pread(struct svr4_sys_pread_args *uap)
449 struct pread_args pra;
453 * Just translate the args structure and call the NetBSD
454 * pread(2) system call (offset type is 64-bit in NetBSD).
456 SCARG(&pra, fd) = SCARG(uap, fd);
457 SCARG(&pra, buf) = SCARG(uap, buf);
458 SCARG(&pra, nbyte) = SCARG(uap, nbyte);
459 SCARG(&pra, offset) = SCARG(uap, off);
461 pra.sysmsg_result = 0;
463 uap->sysmsg_result = pra.sysmsg_result;
470 svr4_sys_pread64(struct thread *td, void *v, register_t *retval)
472 struct svr4_sys_pread64_args *uap = v;
473 struct sys_pread_args pra;
477 * Just translate the args structure and call the NetBSD
478 * pread(2) system call (offset type is 64-bit in NetBSD).
480 SCARG(&pra, fd) = SCARG(uap, fd);
481 SCARG(&pra, buf) = SCARG(uap, buf);
482 SCARG(&pra, nbyte) = SCARG(uap, nbyte);
483 SCARG(&pra, offset) = SCARG(uap, off);
485 pra.sysmsg_result = 0;
486 error = sys_pread(&pra, retval);
487 uap->sysmsg_result = pra.sysmsg_result;
494 svr4_sys_pwrite(struct svr4_sys_pwrite_args *uap)
496 struct pwrite_args pwa;
500 * Just translate the args structure and call the NetBSD
501 * pwrite(2) system call (offset type is 64-bit in NetBSD).
503 SCARG(&pwa, fd) = SCARG(uap, fd);
504 SCARG(&pwa, buf) = SCARG(uap, buf);
505 SCARG(&pwa, nbyte) = SCARG(uap, nbyte);
506 SCARG(&pwa, offset) = SCARG(uap, off);
508 pwa.sysmsg_result = 0;
509 error = pwrite(&pwa);
510 uap->sysmsg_result = pwa.sysmsg_result;
517 svr4_sys_pwrite64(struct thread *td, void *v, register_t *retval)
519 struct svr4_sys_pwrite64_args *uap = v;
520 struct sys_pwrite_args pwa;
523 * Just translate the args structure and call the NetBSD
524 * pwrite(2) system call (offset type is 64-bit in NetBSD).
526 SCARG(&pwa, fd) = SCARG(uap, fd);
527 SCARG(&pwa, buf) = SCARG(uap, buf);
528 SCARG(&pwa, nbyte) = SCARG(uap, nbyte);
529 SCARG(&pwa, offset) = SCARG(uap, off);
531 return (sys_pwrite(p, &pwa, retval));
536 svr4_sys_fcntl(struct svr4_sys_fcntl_args *uap)
538 struct thread *td = curthread; /* XXX */
540 struct fcntl_args fa;
543 retval = &uap->sysmsg_result;
545 SCARG(&fa, fd) = SCARG(uap, fd);
546 SCARG(&fa, cmd) = svr4_to_bsd_cmd(SCARG(uap, cmd));
548 switch (SCARG(&fa, cmd)) {
552 SCARG(&fa, arg) = (long) SCARG(uap, arg);
553 fa.sysmsg_result = 0;
555 *retval = fa.sysmsg_result;
559 SCARG(&fa, arg) = (long) SCARG(uap, arg);
563 *retval = bsd_to_svr4_flags(fa.sysmsg_result);
569 * we must save the O_ASYNC flag, as that is
570 * handled by ioctl(_, I_SETSIG, _) emulation.
575 DPRINTF(("Setting flags %p\n", SCARG(uap, arg)));
576 cmd = SCARG(&fa, cmd); /* save it for a while */
578 SCARG(&fa, cmd) = F_GETFL;
579 if ((error = fcntl(&fa)) != 0)
581 flags = fa.sysmsg_result;
583 flags |= svr4_to_bsd_flags((u_long) SCARG(uap, arg));
584 SCARG(&fa, cmd) = cmd;
585 SCARG(&fa, arg) = (long) flags;
587 *retval = fa.sysmsg_result;
595 struct svr4_flock ifl;
596 struct flock *flp, fl;
597 caddr_t sg = stackgap_init();
599 flp = stackgap_alloc(&sg, sizeof(struct flock));
600 SCARG(&fa, arg) = (long) flp;
602 error = copyin(SCARG(uap, arg), &ifl, sizeof ifl);
606 svr4_to_bsd_flock(&ifl, &fl);
608 error = copyout(&fl, flp, sizeof fl);
612 fa.sysmsg_result = 0;
614 *retval = fa.sysmsg_result;
615 if (error || SCARG(&fa, cmd) != F_GETLK)
618 error = copyin(flp, &fl, sizeof fl);
622 bsd_to_svr4_flock(&fl, &ifl);
624 return copyout(&ifl, SCARG(uap, arg), sizeof ifl);
627 switch (SCARG(uap, cmd)) {
632 SCARG(&du, from) = SCARG(uap, fd);
633 SCARG(&du, to) = (int)SCARG(uap, arg);
637 *retval = SCARG(&du, to);
643 struct svr4_flock ifl;
646 error = copyin(SCARG(uap, arg), &ifl,
650 svr4_to_bsd_flock(&ifl, &fl);
651 return fd_truncate(td, SCARG(uap, fd), &fl, retval);
656 case SVR4_F_SETLKW64:
658 struct svr4_flock64 ifl;
659 struct flock *flp, fl;
660 caddr_t sg = stackgap_init();
662 flp = stackgap_alloc(&sg, sizeof(struct flock));
663 SCARG(&fa, arg) = (long) flp;
665 error = copyin(SCARG(uap, arg), &ifl,
670 svr4_to_bsd_flock64(&ifl, &fl);
672 error = copyout(&fl, flp, sizeof fl);
676 fa.sysmsg_result = 0;
678 *retval = fa.sysmsg_result;
679 if (error || SCARG(&fa, cmd) != F_GETLK)
682 error = copyin(flp, &fl, sizeof fl);
686 bsd_to_svr4_flock64(&fl, &ifl);
688 return copyout(&ifl, SCARG(uap, arg),
692 case SVR4_F_FREESP64:
694 struct svr4_flock64 ifl;
697 error = copyin(SCARG(uap, arg), &ifl,
701 svr4_to_bsd_flock64(&ifl, &fl);
702 return fd_truncate(td, SCARG(uap, fd), &fl, retval);
706 return fd_revoke(td, SCARG(uap, fd));