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 $
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/namei.h>
39 #include <sys/filedesc.h>
40 /*#include <sys/ioctl.h>*/
41 #include <sys/kernel.h>
42 #include <sys/mount.h>
43 #include <sys/malloc.h>
44 #include <sys/vnode.h>
45 #include <sys/unistd.h>
47 #include <sys/sysproto.h>
49 #include <svr4/svr4.h>
50 #include <svr4/svr4_types.h>
51 #include <svr4/svr4_signal.h>
52 #include <svr4/svr4_proto.h>
53 #include <svr4/svr4_util.h>
54 #include <svr4/svr4_fcntl.h>
56 static int svr4_to_bsd_flags __P((int));
57 static u_long svr4_to_bsd_cmd __P((u_long));
58 static int fd_revoke __P((struct proc *, int));
59 static int fd_truncate __P((struct proc *, int, struct flock *));
60 static int bsd_to_svr4_flags __P((int));
61 static void bsd_to_svr4_flock __P((struct flock *, struct svr4_flock *));
62 static void svr4_to_bsd_flock __P((struct svr4_flock *, struct flock *));
63 static void bsd_to_svr4_flock64 __P((struct flock *, struct svr4_flock64 *));
64 static void svr4_to_bsd_flock64 __P((struct svr4_flock64 *, struct flock *));
97 r |= (l & SVR4_O_RDONLY) ? O_RDONLY : 0;
98 r |= (l & SVR4_O_WRONLY) ? O_WRONLY : 0;
99 r |= (l & SVR4_O_RDWR) ? O_RDWR : 0;
100 r |= (l & SVR4_O_NDELAY) ? O_NONBLOCK : 0;
101 r |= (l & SVR4_O_APPEND) ? O_APPEND : 0;
102 r |= (l & SVR4_O_SYNC) ? O_FSYNC : 0;
103 r |= (l & SVR4_O_NONBLOCK) ? O_NONBLOCK : 0;
104 r |= (l & SVR4_O_PRIV) ? O_EXLOCK : 0;
105 r |= (l & SVR4_O_CREAT) ? O_CREAT : 0;
106 r |= (l & SVR4_O_TRUNC) ? O_TRUNC : 0;
107 r |= (l & SVR4_O_EXCL) ? O_EXCL : 0;
108 r |= (l & SVR4_O_NOCTTY) ? O_NOCTTY : 0;
117 r |= (l & O_RDONLY) ? SVR4_O_RDONLY : 0;
118 r |= (l & O_WRONLY) ? SVR4_O_WRONLY : 0;
119 r |= (l & O_RDWR) ? SVR4_O_RDWR : 0;
120 r |= (l & O_NDELAY) ? SVR4_O_NONBLOCK : 0;
121 r |= (l & O_APPEND) ? SVR4_O_APPEND : 0;
122 r |= (l & O_FSYNC) ? SVR4_O_SYNC : 0;
123 r |= (l & O_NONBLOCK) ? SVR4_O_NONBLOCK : 0;
124 r |= (l & O_EXLOCK) ? SVR4_O_PRIV : 0;
125 r |= (l & O_CREAT) ? SVR4_O_CREAT : 0;
126 r |= (l & O_TRUNC) ? SVR4_O_TRUNC : 0;
127 r |= (l & O_EXCL) ? SVR4_O_EXCL : 0;
128 r |= (l & O_NOCTTY) ? SVR4_O_NOCTTY : 0;
134 bsd_to_svr4_flock(iflp, oflp)
136 struct svr4_flock *oflp;
138 switch (iflp->l_type) {
140 oflp->l_type = SVR4_F_RDLCK;
143 oflp->l_type = SVR4_F_WRLCK;
146 oflp->l_type = SVR4_F_UNLCK;
153 oflp->l_whence = (short) iflp->l_whence;
154 oflp->l_start = (svr4_off_t) iflp->l_start;
155 oflp->l_len = (svr4_off_t) iflp->l_len;
157 oflp->l_pid = (svr4_pid_t) iflp->l_pid;
162 svr4_to_bsd_flock(iflp, oflp)
163 struct svr4_flock *iflp;
166 switch (iflp->l_type) {
168 oflp->l_type = F_RDLCK;
171 oflp->l_type = F_WRLCK;
174 oflp->l_type = F_UNLCK;
181 oflp->l_whence = iflp->l_whence;
182 oflp->l_start = (off_t) iflp->l_start;
183 oflp->l_len = (off_t) iflp->l_len;
184 oflp->l_pid = (pid_t) iflp->l_pid;
189 bsd_to_svr4_flock64(iflp, oflp)
191 struct svr4_flock64 *oflp;
193 switch (iflp->l_type) {
195 oflp->l_type = SVR4_F_RDLCK;
198 oflp->l_type = SVR4_F_WRLCK;
201 oflp->l_type = SVR4_F_UNLCK;
208 oflp->l_whence = (short) iflp->l_whence;
209 oflp->l_start = (svr4_off64_t) iflp->l_start;
210 oflp->l_len = (svr4_off64_t) iflp->l_len;
212 oflp->l_pid = (svr4_pid_t) iflp->l_pid;
217 svr4_to_bsd_flock64(iflp, oflp)
218 struct svr4_flock64 *iflp;
221 switch (iflp->l_type) {
223 oflp->l_type = F_RDLCK;
226 oflp->l_type = F_WRLCK;
229 oflp->l_type = F_UNLCK;
236 oflp->l_whence = iflp->l_whence;
237 oflp->l_start = (off_t) iflp->l_start;
238 oflp->l_len = (off_t) iflp->l_len;
239 oflp->l_pid = (pid_t) iflp->l_pid;
249 struct filedesc *fdp = p->p_fd;
255 retval = p->p_retval;
256 if ((u_int)fd >= fdp->fd_nfiles || (fp = fdp->fd_ofiles[fd]) == NULL)
259 if (fp->f_type != DTYPE_VNODE)
262 vp = (struct vnode *) fp->f_data;
264 if (vp->v_type != VCHR && vp->v_type != VBLK) {
269 if ((error = VOP_GETATTR(vp, &vattr, p->p_ucred, p)) != 0)
272 if (p->p_ucred->cr_uid != vattr.va_uid &&
273 (error = suser(p)) != 0)
277 VOP_REVOKE(vp, REVOKEALL);
285 fd_truncate(p, fd, flp)
290 struct filedesc *fdp = p->p_fd;
296 struct ftruncate_args ft;
298 retval = p->p_retval;
301 * We only support truncating the file.
303 if ((u_int)fd >= fdp->fd_nfiles || (fp = fdp->fd_ofiles[fd]) == NULL)
306 vp = (struct vnode *)fp->f_data;
307 if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO)
310 if ((error = VOP_GETATTR(vp, &vattr, p->p_ucred, p)) != 0)
313 length = vattr.va_size;
315 switch (flp->l_whence) {
317 start = fp->f_offset + flp->l_start;
321 start = flp->l_start + length;
325 start = flp->l_start;
332 if (start + flp->l_len < length) {
333 /* We don't support free'ing in the middle of the file */
338 SCARG(&ft, length) = start;
340 return ftruncate(p, &ft);
344 svr4_sys_open(p, uap)
345 register struct proc *p;
346 struct svr4_sys_open_args *uap;
349 struct open_args cup;
351 caddr_t sg = stackgap_init();
352 CHECKALTEXIST(p, &sg, SCARG(uap, path));
354 (&cup)->path = uap->path;
355 (&cup)->flags = svr4_to_bsd_flags(uap->flags);
356 (&cup)->mode = uap->mode;
357 error = open(p, &cup);
360 /* uprintf("svr4_open(%s, 0x%0x, 0%o): %d\n", uap->path,
361 uap->flags, uap->mode, error);*/
365 retval = p->p_retval[0];
367 if (!(SCARG(&cup, flags) & O_NOCTTY) && SESS_LEADER(p) &&
368 !(p->p_flag & P_CONTROLT)) {
370 struct filedesc *fdp = p->p_fd;
371 struct file *fp = fdp->fd_ofiles[retval];
373 /* ignore any error, just give it a try */
374 if (fp->f_type == DTYPE_VNODE)
375 fo_ioctl(fp, TIOCSCTTY, (caddr_t) 0, p);
382 svr4_sys_open64(p, uap)
383 register struct proc *p;
384 struct svr4_sys_open64_args *uap;
386 return svr4_sys_open(p, (struct svr4_sys_open_args *)uap);
390 svr4_sys_creat(p, uap)
391 register struct proc *p;
392 struct svr4_sys_creat_args *uap;
394 struct open_args cup;
396 caddr_t sg = stackgap_init();
397 CHECKALTEXIST(p, &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;
403 return open(p, &cup);
407 svr4_sys_creat64(p, uap)
408 register struct proc *p;
409 struct svr4_sys_creat64_args *uap;
411 return svr4_sys_creat(p, (struct svr4_sys_creat_args *)uap);
415 svr4_sys_llseek(p, v)
416 register struct proc *p;
417 struct svr4_sys_llseek_args *v;
419 struct svr4_sys_llseek_args *uap = v;
420 struct lseek_args ap;
422 SCARG(&ap, fd) = SCARG(uap, fd);
424 #if BYTE_ORDER == BIG_ENDIAN
425 SCARG(&ap, offset) = (((long long) SCARG(uap, offset1)) << 32) |
428 SCARG(&ap, offset) = (((long long) SCARG(uap, offset2)) << 32) |
431 SCARG(&ap, whence) = SCARG(uap, whence);
433 return lseek(p, &ap);
437 svr4_sys_access(p, uap)
438 register struct proc *p;
439 struct svr4_sys_access_args *uap;
441 struct access_args cup;
444 caddr_t sg = stackgap_init();
445 CHECKALTEXIST(p, &sg, SCARG(uap, path));
447 retval = p->p_retval;
449 SCARG(&cup, path) = SCARG(uap, path);
450 SCARG(&cup, flags) = SCARG(uap, flags);
452 return access(p, &cup);
457 svr4_sys_pread(p, uap)
458 register struct proc *p;
459 struct svr4_sys_pread_args *uap;
461 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 return pread(p, &pra);
478 svr4_sys_pread64(p, v, retval)
479 register struct proc *p;
484 struct svr4_sys_pread64_args *uap = v;
485 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 return (sys_pread(p, &pra, retval));
502 svr4_sys_pwrite(p, uap)
503 register struct proc *p;
504 struct svr4_sys_pwrite_args *uap;
506 struct pwrite_args pwa;
509 * Just translate the args structure and call the NetBSD
510 * pwrite(2) system call (offset type is 64-bit in NetBSD).
512 SCARG(&pwa, fd) = SCARG(uap, fd);
513 SCARG(&pwa, buf) = SCARG(uap, buf);
514 SCARG(&pwa, nbyte) = SCARG(uap, nbyte);
515 SCARG(&pwa, offset) = SCARG(uap, off);
517 return pwrite(p, &pwa);
523 svr4_sys_pwrite64(p, v, retval)
524 register struct proc *p;
528 struct svr4_sys_pwrite64_args *uap = v;
529 struct sys_pwrite_args pwa;
532 * Just translate the args structure and call the NetBSD
533 * pwrite(2) system call (offset type is 64-bit in NetBSD).
535 SCARG(&pwa, fd) = SCARG(uap, fd);
536 SCARG(&pwa, buf) = SCARG(uap, buf);
537 SCARG(&pwa, nbyte) = SCARG(uap, nbyte);
538 SCARG(&pwa, offset) = SCARG(uap, off);
540 return (sys_pwrite(p, &pwa, retval));
545 svr4_sys_fcntl(p, uap)
546 register struct proc *p;
547 struct svr4_sys_fcntl_args *uap;
550 struct fcntl_args fa;
553 retval = p->p_retval;
555 SCARG(&fa, fd) = SCARG(uap, fd);
556 SCARG(&fa, cmd) = svr4_to_bsd_cmd(SCARG(uap, cmd));
558 switch (SCARG(&fa, cmd)) {
562 SCARG(&fa, arg) = (long) SCARG(uap, arg);
563 return fcntl(p, &fa);
566 SCARG(&fa, arg) = (long) SCARG(uap, arg);
567 error = fcntl(p, &fa);
570 *retval = bsd_to_svr4_flags(*retval);
576 * we must save the O_ASYNC flag, as that is
577 * handled by ioctl(_, I_SETSIG, _) emulation.
582 DPRINTF(("Setting flags 0x%x\n", SCARG(uap, arg)));
583 cmd = SCARG(&fa, cmd); /* save it for a while */
585 SCARG(&fa, cmd) = F_GETFL;
586 if ((error = fcntl(p, &fa)) != 0)
590 flags |= svr4_to_bsd_flags((u_long) SCARG(uap, arg));
591 SCARG(&fa, cmd) = cmd;
592 SCARG(&fa, arg) = (long) flags;
593 return fcntl(p, &fa);
600 struct svr4_flock ifl;
601 struct flock *flp, fl;
602 caddr_t sg = stackgap_init();
604 flp = stackgap_alloc(&sg, sizeof(struct flock));
605 SCARG(&fa, arg) = (long) flp;
607 error = copyin(SCARG(uap, arg), &ifl, sizeof ifl);
611 svr4_to_bsd_flock(&ifl, &fl);
613 error = copyout(&fl, flp, sizeof fl);
617 error = fcntl(p, &fa);
618 if (error || SCARG(&fa, cmd) != F_GETLK)
621 error = copyin(flp, &fl, sizeof fl);
625 bsd_to_svr4_flock(&fl, &ifl);
627 return copyout(&ifl, SCARG(uap, arg), sizeof ifl);
630 switch (SCARG(uap, cmd)) {
635 SCARG(&du, from) = SCARG(uap, fd);
636 SCARG(&du, to) = (int)SCARG(uap, arg);
637 error = dup2(p, &du);
640 *retval = SCARG(&du, to);
646 struct svr4_flock ifl;
649 error = copyin(SCARG(uap, arg), &ifl,
653 svr4_to_bsd_flock(&ifl, &fl);
654 return fd_truncate(p, SCARG(uap, fd), &fl);
659 case SVR4_F_SETLKW64:
661 struct svr4_flock64 ifl;
662 struct flock *flp, fl;
663 caddr_t sg = stackgap_init();
665 flp = stackgap_alloc(&sg, sizeof(struct flock));
666 SCARG(&fa, arg) = (long) flp;
668 error = copyin(SCARG(uap, arg), &ifl,
673 svr4_to_bsd_flock64(&ifl, &fl);
675 error = copyout(&fl, flp, sizeof fl);
679 error = fcntl(p, &fa);
680 if (error || SCARG(&fa, cmd) != F_GETLK)
683 error = copyin(flp, &fl, sizeof fl);
687 bsd_to_svr4_flock64(&fl, &ifl);
689 return copyout(&ifl, SCARG(uap, arg),
693 case SVR4_F_FREESP64:
695 struct svr4_flock64 ifl;
698 error = copyin(SCARG(uap, arg), &ifl,
702 svr4_to_bsd_flock64(&ifl, &fl);
703 return fd_truncate(p, SCARG(uap, fd), &fl);
707 return fd_revoke(p, SCARG(uap, fd));