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.15 2004/10/12 19:20:42 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>
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 *);
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 if ((u_int)fd >= fdp->fd_nfiles || (fp = fdp->fd_ofiles[fd]) == NULL)
261 if (fp->f_type != DTYPE_VNODE)
264 vp = (struct vnode *) fp->f_data;
266 if ((error = vx_get(vp)) != 0)
269 if (vp->v_type != VCHR && vp->v_type != VBLK) {
274 if ((error = VOP_GETATTR(vp, &vattr, td)) != 0)
277 if (p->p_ucred->cr_uid != vattr.va_uid &&
278 (error = suser(p->p_thread)) != 0)
282 VOP_REVOKE(vp, REVOKEALL);
290 fd_truncate(struct thread *td, int fd, struct flock *flp, int *retval)
292 struct proc *p = td->td_proc;
293 struct filedesc *fdp;
299 struct ftruncate_args ft;
305 * We only support truncating the file.
307 if ((u_int)fd >= fdp->fd_nfiles || (fp = fdp->fd_ofiles[fd]) == NULL)
310 vp = (struct vnode *)fp->f_data;
311 if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO)
314 if ((error = VOP_GETATTR(vp, &vattr, td)) != 0)
317 length = vattr.va_size;
319 switch (flp->l_whence) {
321 start = fp->f_offset + flp->l_start;
325 start = flp->l_start + length;
329 start = flp->l_start;
336 if (start + flp->l_len < length) {
337 /* We don't support free'ing in the middle of the file */
342 SCARG(&ft, length) = start;
343 error = ftruncate(&ft);
344 *retval = ft.sysmsg_result;
349 svr4_sys_open(struct svr4_sys_open_args *uap)
351 struct thread *td = curthread; /* XXX */
352 struct proc *p = td->td_proc;
353 struct open_args cup;
356 caddr_t sg = stackgap_init();
357 CHECKALTEXIST(&sg, SCARG(uap, path));
359 cup.sysmsg_result = 0;
360 cup.path = uap->path;
361 cup.flags = svr4_to_bsd_flags(uap->flags);
362 cup.mode = uap->mode;
366 /* uprintf("svr4_open(%s, 0x%0x, 0%o): %d\n", uap->path,
367 uap->flags, uap->mode, error);*/
372 retval = uap->sysmsg_result = cup.sysmsg_result;
374 if (!(SCARG(&cup, flags) & O_NOCTTY) && SESS_LEADER(p) &&
375 !(p->p_flag & P_CONTROLT)) {
377 struct filedesc *fdp = p->p_fd;
378 struct file *fp = fdp->fd_ofiles[retval];
380 /* ignore any error, just give it a try */
381 if (fp->f_type == DTYPE_VNODE)
382 fo_ioctl(fp, TIOCSCTTY, (caddr_t) 0, p);
389 svr4_sys_open64(struct svr4_sys_open64_args *uap)
391 return svr4_sys_open((struct svr4_sys_open_args *)uap);
395 svr4_sys_creat(struct svr4_sys_creat_args *uap)
397 struct open_args cup;
400 caddr_t sg = stackgap_init();
401 CHECKALTEXIST(&sg, SCARG(uap, path));
403 SCARG(&cup, path) = SCARG(uap, path);
404 SCARG(&cup, mode) = SCARG(uap, mode);
405 SCARG(&cup, flags) = O_WRONLY | O_CREAT | O_TRUNC;
407 cup.sysmsg_result = 0;
409 uap->sysmsg_result = cup.sysmsg_result;
414 svr4_sys_creat64(struct svr4_sys_creat64_args *uap)
416 return svr4_sys_creat((struct svr4_sys_creat_args *)uap);
420 svr4_sys_llseek(struct svr4_sys_llseek_args *v)
422 struct svr4_sys_llseek_args *uap = v;
423 struct lseek_args ap;
425 SCARG(&ap, fd) = SCARG(uap, fd);
427 #if BYTE_ORDER == BIG_ENDIAN
428 SCARG(&ap, offset) = (((long long) SCARG(uap, offset1)) << 32) |
431 SCARG(&ap, offset) = (((long long) SCARG(uap, offset2)) << 32) |
434 SCARG(&ap, whence) = SCARG(uap, whence);
440 svr4_sys_access(struct svr4_sys_access_args *uap)
442 struct access_args cup;
445 caddr_t sg = stackgap_init();
446 CHECKALTEXIST(&sg, SCARG(uap, path));
448 SCARG(&cup, path) = SCARG(uap, path);
449 SCARG(&cup, flags) = SCARG(uap, flags);
450 cup.sysmsg_result = 0;
451 error = access(&cup);
452 uap->sysmsg_result = cup.sysmsg_result;
458 svr4_sys_pread(struct svr4_sys_pread_args *uap)
460 struct pread_args pra;
464 * Just translate the args structure and call the NetBSD
465 * pread(2) system call (offset type is 64-bit in NetBSD).
467 SCARG(&pra, fd) = SCARG(uap, fd);
468 SCARG(&pra, buf) = SCARG(uap, buf);
469 SCARG(&pra, nbyte) = SCARG(uap, nbyte);
470 SCARG(&pra, offset) = SCARG(uap, off);
472 pra.sysmsg_result = 0;
474 uap->sysmsg_result = pra.sysmsg_result;
481 svr4_sys_pread64(struct thread *td, void *v, register_t *retval)
483 struct svr4_sys_pread64_args *uap = v;
484 struct sys_pread_args pra;
488 * Just translate the args structure and call the NetBSD
489 * pread(2) system call (offset type is 64-bit in NetBSD).
491 SCARG(&pra, fd) = SCARG(uap, fd);
492 SCARG(&pra, buf) = SCARG(uap, buf);
493 SCARG(&pra, nbyte) = SCARG(uap, nbyte);
494 SCARG(&pra, offset) = SCARG(uap, off);
496 pra.sysmsg_result = 0;
497 error = sys_pread(&pra, retval);
498 uap->sysmsg_result = pra.sysmsg_result;
505 svr4_sys_pwrite(struct svr4_sys_pwrite_args *uap)
507 struct pwrite_args pwa;
511 * Just translate the args structure and call the NetBSD
512 * pwrite(2) system call (offset type is 64-bit in NetBSD).
514 SCARG(&pwa, fd) = SCARG(uap, fd);
515 SCARG(&pwa, buf) = SCARG(uap, buf);
516 SCARG(&pwa, nbyte) = SCARG(uap, nbyte);
517 SCARG(&pwa, offset) = SCARG(uap, off);
519 pwa.sysmsg_result = 0;
520 error = pwrite(&pwa);
521 uap->sysmsg_result = pwa.sysmsg_result;
528 svr4_sys_pwrite64(struct thread *td, void *v, register_t *retval)
530 struct svr4_sys_pwrite64_args *uap = v;
531 struct sys_pwrite_args pwa;
534 * Just translate the args structure and call the NetBSD
535 * pwrite(2) system call (offset type is 64-bit in NetBSD).
537 SCARG(&pwa, fd) = SCARG(uap, fd);
538 SCARG(&pwa, buf) = SCARG(uap, buf);
539 SCARG(&pwa, nbyte) = SCARG(uap, nbyte);
540 SCARG(&pwa, offset) = SCARG(uap, off);
542 return (sys_pwrite(p, &pwa, retval));
547 svr4_sys_fcntl(struct svr4_sys_fcntl_args *uap)
549 struct thread *td = curthread; /* XXX */
551 struct fcntl_args fa;
554 retval = &uap->sysmsg_result;
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 fa.sysmsg_result = 0;
566 *retval = fa.sysmsg_result;
570 SCARG(&fa, arg) = (long) SCARG(uap, arg);
574 *retval = bsd_to_svr4_flags(fa.sysmsg_result);
580 * we must save the O_ASYNC flag, as that is
581 * handled by ioctl(_, I_SETSIG, _) emulation.
586 DPRINTF(("Setting flags %p\n", SCARG(uap, arg)));
587 cmd = SCARG(&fa, cmd); /* save it for a while */
589 SCARG(&fa, cmd) = F_GETFL;
590 if ((error = fcntl(&fa)) != 0)
592 flags = fa.sysmsg_result;
594 flags |= svr4_to_bsd_flags((u_long) SCARG(uap, arg));
595 SCARG(&fa, cmd) = cmd;
596 SCARG(&fa, arg) = (long) flags;
598 *retval = fa.sysmsg_result;
606 struct svr4_flock ifl;
607 struct flock *flp, fl;
608 caddr_t sg = stackgap_init();
610 flp = stackgap_alloc(&sg, sizeof(struct flock));
611 SCARG(&fa, arg) = (long) flp;
613 error = copyin(SCARG(uap, arg), &ifl, sizeof ifl);
617 svr4_to_bsd_flock(&ifl, &fl);
619 error = copyout(&fl, flp, sizeof fl);
623 fa.sysmsg_result = 0;
625 *retval = fa.sysmsg_result;
626 if (error || SCARG(&fa, cmd) != F_GETLK)
629 error = copyin(flp, &fl, sizeof fl);
633 bsd_to_svr4_flock(&fl, &ifl);
635 return copyout(&ifl, SCARG(uap, arg), sizeof ifl);
638 switch (SCARG(uap, cmd)) {
643 SCARG(&du, from) = SCARG(uap, fd);
644 SCARG(&du, to) = (int)SCARG(uap, arg);
648 *retval = SCARG(&du, to);
654 struct svr4_flock ifl;
657 error = copyin(SCARG(uap, arg), &ifl,
661 svr4_to_bsd_flock(&ifl, &fl);
662 return fd_truncate(td, SCARG(uap, fd), &fl, retval);
667 case SVR4_F_SETLKW64:
669 struct svr4_flock64 ifl;
670 struct flock *flp, fl;
671 caddr_t sg = stackgap_init();
673 flp = stackgap_alloc(&sg, sizeof(struct flock));
674 SCARG(&fa, arg) = (long) flp;
676 error = copyin(SCARG(uap, arg), &ifl,
681 svr4_to_bsd_flock64(&ifl, &fl);
683 error = copyout(&fl, flp, sizeof fl);
687 fa.sysmsg_result = 0;
689 *retval = fa.sysmsg_result;
690 if (error || SCARG(&fa, cmd) != F_GETLK)
693 error = copyin(flp, &fl, sizeof fl);
697 bsd_to_svr4_flock64(&fl, &ifl);
699 return copyout(&ifl, SCARG(uap, arg),
703 case SVR4_F_FREESP64:
705 struct svr4_flock64 ifl;
708 error = copyin(SCARG(uap, arg), &ifl,
712 svr4_to_bsd_flock64(&ifl, &fl);
713 return fd_truncate(td, SCARG(uap, fd), &fl, retval);
717 return fd_revoke(td, SCARG(uap, fd));