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.2 2003/06/17 04:28:57 dillon Exp $
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #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 proc *, int));
60 static int fd_truncate __P((struct proc *, 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;
250 struct filedesc *fdp = p->p_fd;
256 retval = p->p_retval;
257 if ((u_int)fd >= fdp->fd_nfiles || (fp = fdp->fd_ofiles[fd]) == NULL)
260 if (fp->f_type != DTYPE_VNODE)
263 vp = (struct vnode *) fp->f_data;
265 if (vp->v_type != VCHR && vp->v_type != VBLK) {
270 if ((error = VOP_GETATTR(vp, &vattr, p->p_ucred, p)) != 0)
273 if (p->p_ucred->cr_uid != vattr.va_uid &&
274 (error = suser(p)) != 0)
278 VOP_REVOKE(vp, REVOKEALL);
286 fd_truncate(p, fd, flp)
291 struct filedesc *fdp = p->p_fd;
297 struct ftruncate_args ft;
299 retval = p->p_retval;
302 * We only support truncating the file.
304 if ((u_int)fd >= fdp->fd_nfiles || (fp = fdp->fd_ofiles[fd]) == NULL)
307 vp = (struct vnode *)fp->f_data;
308 if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO)
311 if ((error = VOP_GETATTR(vp, &vattr, p->p_ucred, p)) != 0)
314 length = vattr.va_size;
316 switch (flp->l_whence) {
318 start = fp->f_offset + flp->l_start;
322 start = flp->l_start + length;
326 start = flp->l_start;
333 if (start + flp->l_len < length) {
334 /* We don't support free'ing in the middle of the file */
339 SCARG(&ft, length) = start;
341 return ftruncate(p, &ft);
345 svr4_sys_open(p, uap)
346 register struct proc *p;
347 struct svr4_sys_open_args *uap;
350 struct open_args cup;
352 caddr_t sg = stackgap_init();
353 CHECKALTEXIST(p, &sg, SCARG(uap, path));
355 (&cup)->path = uap->path;
356 (&cup)->flags = svr4_to_bsd_flags(uap->flags);
357 (&cup)->mode = uap->mode;
358 error = open(p, &cup);
361 /* uprintf("svr4_open(%s, 0x%0x, 0%o): %d\n", uap->path,
362 uap->flags, uap->mode, error);*/
366 retval = p->p_retval[0];
368 if (!(SCARG(&cup, flags) & O_NOCTTY) && SESS_LEADER(p) &&
369 !(p->p_flag & P_CONTROLT)) {
371 struct filedesc *fdp = p->p_fd;
372 struct file *fp = fdp->fd_ofiles[retval];
374 /* ignore any error, just give it a try */
375 if (fp->f_type == DTYPE_VNODE)
376 fo_ioctl(fp, TIOCSCTTY, (caddr_t) 0, p);
383 svr4_sys_open64(p, uap)
384 register struct proc *p;
385 struct svr4_sys_open64_args *uap;
387 return svr4_sys_open(p, (struct svr4_sys_open_args *)uap);
391 svr4_sys_creat(p, uap)
392 register struct proc *p;
393 struct svr4_sys_creat_args *uap;
395 struct open_args cup;
397 caddr_t sg = stackgap_init();
398 CHECKALTEXIST(p, &sg, SCARG(uap, path));
400 SCARG(&cup, path) = SCARG(uap, path);
401 SCARG(&cup, mode) = SCARG(uap, mode);
402 SCARG(&cup, flags) = O_WRONLY | O_CREAT | O_TRUNC;
404 return open(p, &cup);
408 svr4_sys_creat64(p, uap)
409 register struct proc *p;
410 struct svr4_sys_creat64_args *uap;
412 return svr4_sys_creat(p, (struct svr4_sys_creat_args *)uap);
416 svr4_sys_llseek(p, v)
417 register struct proc *p;
418 struct svr4_sys_llseek_args *v;
420 struct svr4_sys_llseek_args *uap = v;
421 struct lseek_args ap;
423 SCARG(&ap, fd) = SCARG(uap, fd);
425 #if BYTE_ORDER == BIG_ENDIAN
426 SCARG(&ap, offset) = (((long long) SCARG(uap, offset1)) << 32) |
429 SCARG(&ap, offset) = (((long long) SCARG(uap, offset2)) << 32) |
432 SCARG(&ap, whence) = SCARG(uap, whence);
434 return lseek(p, &ap);
438 svr4_sys_access(p, uap)
439 register struct proc *p;
440 struct svr4_sys_access_args *uap;
442 struct access_args cup;
445 caddr_t sg = stackgap_init();
446 CHECKALTEXIST(p, &sg, SCARG(uap, path));
448 retval = p->p_retval;
450 SCARG(&cup, path) = SCARG(uap, path);
451 SCARG(&cup, flags) = SCARG(uap, flags);
453 return access(p, &cup);
458 svr4_sys_pread(p, uap)
459 register struct proc *p;
460 struct svr4_sys_pread_args *uap;
462 struct pread_args pra;
465 * Just translate the args structure and call the NetBSD
466 * pread(2) system call (offset type is 64-bit in NetBSD).
468 SCARG(&pra, fd) = SCARG(uap, fd);
469 SCARG(&pra, buf) = SCARG(uap, buf);
470 SCARG(&pra, nbyte) = SCARG(uap, nbyte);
471 SCARG(&pra, offset) = SCARG(uap, off);
473 return pread(p, &pra);
479 svr4_sys_pread64(p, v, retval)
480 register struct proc *p;
485 struct svr4_sys_pread64_args *uap = v;
486 struct sys_pread_args pra;
489 * Just translate the args structure and call the NetBSD
490 * pread(2) system call (offset type is 64-bit in NetBSD).
492 SCARG(&pra, fd) = SCARG(uap, fd);
493 SCARG(&pra, buf) = SCARG(uap, buf);
494 SCARG(&pra, nbyte) = SCARG(uap, nbyte);
495 SCARG(&pra, offset) = SCARG(uap, off);
497 return (sys_pread(p, &pra, retval));
503 svr4_sys_pwrite(p, uap)
504 register struct proc *p;
505 struct svr4_sys_pwrite_args *uap;
507 struct pwrite_args pwa;
510 * Just translate the args structure and call the NetBSD
511 * pwrite(2) system call (offset type is 64-bit in NetBSD).
513 SCARG(&pwa, fd) = SCARG(uap, fd);
514 SCARG(&pwa, buf) = SCARG(uap, buf);
515 SCARG(&pwa, nbyte) = SCARG(uap, nbyte);
516 SCARG(&pwa, offset) = SCARG(uap, off);
518 return pwrite(p, &pwa);
524 svr4_sys_pwrite64(p, v, retval)
525 register struct proc *p;
529 struct svr4_sys_pwrite64_args *uap = v;
530 struct sys_pwrite_args pwa;
533 * Just translate the args structure and call the NetBSD
534 * pwrite(2) system call (offset type is 64-bit in NetBSD).
536 SCARG(&pwa, fd) = SCARG(uap, fd);
537 SCARG(&pwa, buf) = SCARG(uap, buf);
538 SCARG(&pwa, nbyte) = SCARG(uap, nbyte);
539 SCARG(&pwa, offset) = SCARG(uap, off);
541 return (sys_pwrite(p, &pwa, retval));
546 svr4_sys_fcntl(p, uap)
547 register struct proc *p;
548 struct svr4_sys_fcntl_args *uap;
551 struct fcntl_args fa;
554 retval = p->p_retval;
556 SCARG(&fa, fd) = SCARG(uap, fd);
557 SCARG(&fa, cmd) = svr4_to_bsd_cmd(SCARG(uap, cmd));
559 switch (SCARG(&fa, cmd)) {
563 SCARG(&fa, arg) = (long) SCARG(uap, arg);
564 return fcntl(p, &fa);
567 SCARG(&fa, arg) = (long) SCARG(uap, arg);
568 error = fcntl(p, &fa);
571 *retval = bsd_to_svr4_flags(*retval);
577 * we must save the O_ASYNC flag, as that is
578 * handled by ioctl(_, I_SETSIG, _) emulation.
583 DPRINTF(("Setting flags 0x%x\n", SCARG(uap, arg)));
584 cmd = SCARG(&fa, cmd); /* save it for a while */
586 SCARG(&fa, cmd) = F_GETFL;
587 if ((error = fcntl(p, &fa)) != 0)
591 flags |= svr4_to_bsd_flags((u_long) SCARG(uap, arg));
592 SCARG(&fa, cmd) = cmd;
593 SCARG(&fa, arg) = (long) flags;
594 return fcntl(p, &fa);
601 struct svr4_flock ifl;
602 struct flock *flp, fl;
603 caddr_t sg = stackgap_init();
605 flp = stackgap_alloc(&sg, sizeof(struct flock));
606 SCARG(&fa, arg) = (long) flp;
608 error = copyin(SCARG(uap, arg), &ifl, sizeof ifl);
612 svr4_to_bsd_flock(&ifl, &fl);
614 error = copyout(&fl, flp, sizeof fl);
618 error = fcntl(p, &fa);
619 if (error || SCARG(&fa, cmd) != F_GETLK)
622 error = copyin(flp, &fl, sizeof fl);
626 bsd_to_svr4_flock(&fl, &ifl);
628 return copyout(&ifl, SCARG(uap, arg), sizeof ifl);
631 switch (SCARG(uap, cmd)) {
636 SCARG(&du, from) = SCARG(uap, fd);
637 SCARG(&du, to) = (int)SCARG(uap, arg);
638 error = dup2(p, &du);
641 *retval = SCARG(&du, to);
647 struct svr4_flock ifl;
650 error = copyin(SCARG(uap, arg), &ifl,
654 svr4_to_bsd_flock(&ifl, &fl);
655 return fd_truncate(p, SCARG(uap, fd), &fl);
660 case SVR4_F_SETLKW64:
662 struct svr4_flock64 ifl;
663 struct flock *flp, fl;
664 caddr_t sg = stackgap_init();
666 flp = stackgap_alloc(&sg, sizeof(struct flock));
667 SCARG(&fa, arg) = (long) flp;
669 error = copyin(SCARG(uap, arg), &ifl,
674 svr4_to_bsd_flock64(&ifl, &fl);
676 error = copyout(&fl, flp, sizeof fl);
680 error = fcntl(p, &fa);
681 if (error || SCARG(&fa, cmd) != F_GETLK)
684 error = copyin(flp, &fl, sizeof fl);
688 bsd_to_svr4_flock64(&fl, &ifl);
690 return copyout(&ifl, SCARG(uap, arg),
694 case SVR4_F_FREESP64:
696 struct svr4_flock64 ifl;
699 error = copyin(SCARG(uap, arg), &ifl,
703 svr4_to_bsd_flock64(&ifl, &fl);
704 return fd_truncate(p, SCARG(uap, fd), &fl);
708 return fd_revoke(p, SCARG(uap, fd));