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.6 2003/06/26 05:55:19 dillon 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>
50 #include <svr4/svr4.h>
51 #include <svr4/svr4_types.h>
52 #include <svr4/svr4_signal.h>
53 #include <svr4/svr4_proto.h>
54 #include <svr4/svr4_util.h>
55 #include <svr4/svr4_fcntl.h>
57 static int svr4_to_bsd_flags __P((int));
58 static u_long svr4_to_bsd_cmd __P((u_long));
59 static int fd_revoke __P((struct thread *, int));
60 static int fd_truncate __P((struct thread *, int, struct flock *));
61 static int bsd_to_svr4_flags __P((int));
62 static void bsd_to_svr4_flock __P((struct flock *, struct svr4_flock *));
63 static void svr4_to_bsd_flock __P((struct svr4_flock *, struct flock *));
64 static void bsd_to_svr4_flock64 __P((struct flock *, struct svr4_flock64 *));
65 static void svr4_to_bsd_flock64 __P((struct svr4_flock64 *, struct flock *));
98 r |= (l & SVR4_O_RDONLY) ? O_RDONLY : 0;
99 r |= (l & SVR4_O_WRONLY) ? O_WRONLY : 0;
100 r |= (l & SVR4_O_RDWR) ? O_RDWR : 0;
101 r |= (l & SVR4_O_NDELAY) ? O_NONBLOCK : 0;
102 r |= (l & SVR4_O_APPEND) ? O_APPEND : 0;
103 r |= (l & SVR4_O_SYNC) ? O_FSYNC : 0;
104 r |= (l & SVR4_O_NONBLOCK) ? O_NONBLOCK : 0;
105 r |= (l & SVR4_O_PRIV) ? O_EXLOCK : 0;
106 r |= (l & SVR4_O_CREAT) ? O_CREAT : 0;
107 r |= (l & SVR4_O_TRUNC) ? O_TRUNC : 0;
108 r |= (l & SVR4_O_EXCL) ? O_EXCL : 0;
109 r |= (l & SVR4_O_NOCTTY) ? O_NOCTTY : 0;
118 r |= (l & O_RDONLY) ? SVR4_O_RDONLY : 0;
119 r |= (l & O_WRONLY) ? SVR4_O_WRONLY : 0;
120 r |= (l & O_RDWR) ? SVR4_O_RDWR : 0;
121 r |= (l & O_NDELAY) ? SVR4_O_NONBLOCK : 0;
122 r |= (l & O_APPEND) ? SVR4_O_APPEND : 0;
123 r |= (l & O_FSYNC) ? SVR4_O_SYNC : 0;
124 r |= (l & O_NONBLOCK) ? SVR4_O_NONBLOCK : 0;
125 r |= (l & O_EXLOCK) ? SVR4_O_PRIV : 0;
126 r |= (l & O_CREAT) ? SVR4_O_CREAT : 0;
127 r |= (l & O_TRUNC) ? SVR4_O_TRUNC : 0;
128 r |= (l & O_EXCL) ? SVR4_O_EXCL : 0;
129 r |= (l & O_NOCTTY) ? SVR4_O_NOCTTY : 0;
135 bsd_to_svr4_flock(iflp, oflp)
137 struct svr4_flock *oflp;
139 switch (iflp->l_type) {
141 oflp->l_type = SVR4_F_RDLCK;
144 oflp->l_type = SVR4_F_WRLCK;
147 oflp->l_type = SVR4_F_UNLCK;
154 oflp->l_whence = (short) iflp->l_whence;
155 oflp->l_start = (svr4_off_t) iflp->l_start;
156 oflp->l_len = (svr4_off_t) iflp->l_len;
158 oflp->l_pid = (svr4_pid_t) iflp->l_pid;
163 svr4_to_bsd_flock(iflp, oflp)
164 struct svr4_flock *iflp;
167 switch (iflp->l_type) {
169 oflp->l_type = F_RDLCK;
172 oflp->l_type = F_WRLCK;
175 oflp->l_type = F_UNLCK;
182 oflp->l_whence = iflp->l_whence;
183 oflp->l_start = (off_t) iflp->l_start;
184 oflp->l_len = (off_t) iflp->l_len;
185 oflp->l_pid = (pid_t) iflp->l_pid;
190 bsd_to_svr4_flock64(iflp, oflp)
192 struct svr4_flock64 *oflp;
194 switch (iflp->l_type) {
196 oflp->l_type = SVR4_F_RDLCK;
199 oflp->l_type = SVR4_F_WRLCK;
202 oflp->l_type = SVR4_F_UNLCK;
209 oflp->l_whence = (short) iflp->l_whence;
210 oflp->l_start = (svr4_off64_t) iflp->l_start;
211 oflp->l_len = (svr4_off64_t) iflp->l_len;
213 oflp->l_pid = (svr4_pid_t) iflp->l_pid;
218 svr4_to_bsd_flock64(iflp, oflp)
219 struct svr4_flock64 *iflp;
222 switch (iflp->l_type) {
224 oflp->l_type = F_RDLCK;
227 oflp->l_type = F_WRLCK;
230 oflp->l_type = F_UNLCK;
237 oflp->l_whence = iflp->l_whence;
238 oflp->l_start = (off_t) iflp->l_start;
239 oflp->l_len = (off_t) iflp->l_len;
240 oflp->l_pid = (pid_t) iflp->l_pid;
246 fd_revoke(struct thread *td, int fd)
248 struct proc *p = td->td_proc;
249 struct filedesc *fdp;
258 retval = p->p_retval;
259 if ((u_int)fd >= fdp->fd_nfiles || (fp = fdp->fd_ofiles[fd]) == NULL)
262 if (fp->f_type != DTYPE_VNODE)
265 vp = (struct vnode *) fp->f_data;
267 if (vp->v_type != VCHR && vp->v_type != VBLK) {
272 if ((error = VOP_GETATTR(vp, &vattr, td)) != 0)
275 if (p->p_ucred->cr_uid != vattr.va_uid &&
276 (error = suser(p->p_thread)) != 0)
280 VOP_REVOKE(vp, REVOKEALL);
288 fd_truncate(struct thread *td, int fd, struct flock *flp)
290 struct proc *p = td->td_proc;
291 struct filedesc *fdp;
297 struct ftruncate_args ft;
301 retval = p->p_retval;
304 * We only support truncating the file.
306 if ((u_int)fd >= fdp->fd_nfiles || (fp = fdp->fd_ofiles[fd]) == NULL)
309 vp = (struct vnode *)fp->f_data;
310 if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO)
313 if ((error = VOP_GETATTR(vp, &vattr, td)) != 0)
316 length = vattr.va_size;
318 switch (flp->l_whence) {
320 start = fp->f_offset + flp->l_start;
324 start = flp->l_start + length;
328 start = flp->l_start;
335 if (start + flp->l_len < length) {
336 /* We don't support free'ing in the middle of the file */
341 SCARG(&ft, length) = start;
343 return ftruncate(&ft);
347 svr4_sys_open(struct svr4_sys_open_args *uap)
349 struct thread *td = curthread; /* XXX */
350 struct proc *p = td->td_proc;
351 struct open_args cup;
354 caddr_t sg = stackgap_init();
355 CHECKALTEXIST(&sg, SCARG(uap, path));
357 (&cup)->path = uap->path;
358 (&cup)->flags = svr4_to_bsd_flags(uap->flags);
359 (&cup)->mode = uap->mode;
363 /* uprintf("svr4_open(%s, 0x%0x, 0%o): %d\n", uap->path,
364 uap->flags, uap->mode, error);*/
369 retval = p->p_retval[0];
371 if (!(SCARG(&cup, flags) & O_NOCTTY) && SESS_LEADER(p) &&
372 !(p->p_flag & P_CONTROLT)) {
374 struct filedesc *fdp = p->p_fd;
375 struct file *fp = fdp->fd_ofiles[retval];
377 /* ignore any error, just give it a try */
378 if (fp->f_type == DTYPE_VNODE)
379 fo_ioctl(fp, TIOCSCTTY, (caddr_t) 0, p);
386 svr4_sys_open64(struct svr4_sys_open64_args *uap)
388 return svr4_sys_open((struct svr4_sys_open_args *)uap);
392 svr4_sys_creat(struct svr4_sys_creat_args *uap)
394 struct open_args cup;
396 caddr_t sg = stackgap_init();
397 CHECKALTEXIST(&sg, SCARG(uap, path));
399 SCARG(&cup, path) = SCARG(uap, path);
400 SCARG(&cup, mode) = SCARG(uap, mode);
401 SCARG(&cup, flags) = O_WRONLY | O_CREAT | O_TRUNC;
407 svr4_sys_creat64(struct svr4_sys_creat64_args *uap)
409 return svr4_sys_creat((struct svr4_sys_creat_args *)uap);
413 svr4_sys_llseek(struct svr4_sys_llseek_args *v)
415 struct svr4_sys_llseek_args *uap = v;
416 struct lseek_args ap;
418 SCARG(&ap, fd) = SCARG(uap, fd);
420 #if BYTE_ORDER == BIG_ENDIAN
421 SCARG(&ap, offset) = (((long long) SCARG(uap, offset1)) << 32) |
424 SCARG(&ap, offset) = (((long long) SCARG(uap, offset2)) << 32) |
427 SCARG(&ap, whence) = SCARG(uap, whence);
433 svr4_sys_access(struct svr4_sys_access_args *uap)
435 struct thread *td = curthread; /* XXX */
436 struct proc *p = td->td_proc;
437 struct access_args cup;
440 caddr_t sg = stackgap_init();
441 CHECKALTEXIST(&sg, SCARG(uap, path));
444 retval = p->p_retval;
446 SCARG(&cup, path) = SCARG(uap, path);
447 SCARG(&cup, flags) = SCARG(uap, flags);
454 svr4_sys_pread(struct svr4_sys_pread_args *uap)
456 struct pread_args pra;
459 * Just translate the args structure and call the NetBSD
460 * pread(2) system call (offset type is 64-bit in NetBSD).
462 SCARG(&pra, fd) = SCARG(uap, fd);
463 SCARG(&pra, buf) = SCARG(uap, buf);
464 SCARG(&pra, nbyte) = SCARG(uap, nbyte);
465 SCARG(&pra, offset) = SCARG(uap, off);
473 svr4_sys_pread64(struct thread *td, void *v, register_t *retval)
475 struct svr4_sys_pread64_args *uap = v;
476 struct sys_pread_args pra;
479 * Just translate the args structure and call the NetBSD
480 * pread(2) system call (offset type is 64-bit in NetBSD).
482 SCARG(&pra, fd) = SCARG(uap, fd);
483 SCARG(&pra, buf) = SCARG(uap, buf);
484 SCARG(&pra, nbyte) = SCARG(uap, nbyte);
485 SCARG(&pra, offset) = SCARG(uap, off);
487 return (sys_pread(&pra, retval));
493 svr4_sys_pwrite(struct svr4_sys_pwrite_args *uap)
495 struct pwrite_args pwa;
498 * Just translate the args structure and call the NetBSD
499 * pwrite(2) system call (offset type is 64-bit in NetBSD).
501 SCARG(&pwa, fd) = SCARG(uap, fd);
502 SCARG(&pwa, buf) = SCARG(uap, buf);
503 SCARG(&pwa, nbyte) = SCARG(uap, nbyte);
504 SCARG(&pwa, offset) = SCARG(uap, off);
512 svr4_sys_pwrite64(struct thread *td, void *v, register_t *retval)
514 struct svr4_sys_pwrite64_args *uap = v;
515 struct sys_pwrite_args pwa;
518 * Just translate the args structure and call the NetBSD
519 * pwrite(2) system call (offset type is 64-bit in NetBSD).
521 SCARG(&pwa, fd) = SCARG(uap, fd);
522 SCARG(&pwa, buf) = SCARG(uap, buf);
523 SCARG(&pwa, nbyte) = SCARG(uap, nbyte);
524 SCARG(&pwa, offset) = SCARG(uap, off);
526 return (sys_pwrite(p, &pwa, retval));
531 svr4_sys_fcntl(struct svr4_sys_fcntl_args *uap)
533 struct thread *td = curthread; /* XXX */
534 struct proc *p = td->td_proc;
536 struct fcntl_args fa;
540 retval = p->p_retval;
542 SCARG(&fa, fd) = SCARG(uap, fd);
543 SCARG(&fa, cmd) = svr4_to_bsd_cmd(SCARG(uap, cmd));
545 switch (SCARG(&fa, cmd)) {
549 SCARG(&fa, arg) = (long) SCARG(uap, arg);
553 SCARG(&fa, arg) = (long) SCARG(uap, arg);
557 *retval = bsd_to_svr4_flags(*retval);
563 * we must save the O_ASYNC flag, as that is
564 * handled by ioctl(_, I_SETSIG, _) emulation.
569 DPRINTF(("Setting flags %p\n", SCARG(uap, arg)));
570 cmd = SCARG(&fa, cmd); /* save it for a while */
572 SCARG(&fa, cmd) = F_GETFL;
573 if ((error = fcntl(&fa)) != 0)
577 flags |= svr4_to_bsd_flags((u_long) SCARG(uap, arg));
578 SCARG(&fa, cmd) = cmd;
579 SCARG(&fa, arg) = (long) flags;
587 struct svr4_flock ifl;
588 struct flock *flp, fl;
589 caddr_t sg = stackgap_init();
591 flp = stackgap_alloc(&sg, sizeof(struct flock));
592 SCARG(&fa, arg) = (long) flp;
594 error = copyin(SCARG(uap, arg), &ifl, sizeof ifl);
598 svr4_to_bsd_flock(&ifl, &fl);
600 error = copyout(&fl, flp, sizeof fl);
605 if (error || SCARG(&fa, cmd) != F_GETLK)
608 error = copyin(flp, &fl, sizeof fl);
612 bsd_to_svr4_flock(&fl, &ifl);
614 return copyout(&ifl, SCARG(uap, arg), sizeof ifl);
617 switch (SCARG(uap, cmd)) {
622 SCARG(&du, from) = SCARG(uap, fd);
623 SCARG(&du, to) = (int)SCARG(uap, arg);
627 *retval = SCARG(&du, to);
633 struct svr4_flock ifl;
636 error = copyin(SCARG(uap, arg), &ifl,
640 svr4_to_bsd_flock(&ifl, &fl);
641 return fd_truncate(td, SCARG(uap, fd), &fl);
646 case SVR4_F_SETLKW64:
648 struct svr4_flock64 ifl;
649 struct flock *flp, fl;
650 caddr_t sg = stackgap_init();
652 flp = stackgap_alloc(&sg, sizeof(struct flock));
653 SCARG(&fa, arg) = (long) flp;
655 error = copyin(SCARG(uap, arg), &ifl,
660 svr4_to_bsd_flock64(&ifl, &fl);
662 error = copyout(&fl, flp, sizeof fl);
667 if (error || SCARG(&fa, cmd) != F_GETLK)
670 error = copyin(flp, &fl, sizeof fl);
674 bsd_to_svr4_flock64(&fl, &ifl);
676 return copyout(&ifl, SCARG(uap, arg),
680 case SVR4_F_FREESP64:
682 struct svr4_flock64 ifl;
685 error = copyin(SCARG(uap, arg), &ifl,
689 svr4_to_bsd_flock64(&ifl, &fl);
690 return fd_truncate(td, SCARG(uap, fd), &fl);
694 return fd_revoke(td, SCARG(uap, fd));