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.
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
37 #include <sys/param.h>
38 #include <sys/systm.h>
40 #include <sys/filedesc.h>
41 /*#include <sys/ioctl.h>*/
44 #include <sys/malloc.h>
45 #include <sys/mount.h>
46 #include <sys/mutex.h>
47 #include <sys/namei.h>
50 #include <sys/syscallsubr.h>
51 #include <sys/unistd.h>
52 #include <sys/vnode.h>
54 #include <sys/sysproto.h>
56 #include <compat/svr4/svr4.h>
57 #include <compat/svr4/svr4_types.h>
58 #include <compat/svr4/svr4_signal.h>
59 #include <compat/svr4/svr4_proto.h>
60 #include <compat/svr4/svr4_util.h>
61 #include <compat/svr4/svr4_fcntl.h>
63 static int svr4_to_bsd_flags(int);
64 static u_long svr4_to_bsd_cmd(u_long);
65 static int fd_revoke(struct thread *, int);
66 static int fd_truncate(struct thread *, int, struct flock *);
67 static int bsd_to_svr4_flags(int);
68 static void bsd_to_svr4_flock(struct flock *, struct svr4_flock *);
69 static void svr4_to_bsd_flock(struct svr4_flock *, struct flock *);
70 static void bsd_to_svr4_flock64(struct flock *, struct svr4_flock64 *);
71 static void svr4_to_bsd_flock64(struct svr4_flock64 *, struct flock *);
104 r |= (l & SVR4_O_RDONLY) ? O_RDONLY : 0;
105 r |= (l & SVR4_O_WRONLY) ? O_WRONLY : 0;
106 r |= (l & SVR4_O_RDWR) ? O_RDWR : 0;
107 r |= (l & SVR4_O_NDELAY) ? O_NONBLOCK : 0;
108 r |= (l & SVR4_O_APPEND) ? O_APPEND : 0;
109 r |= (l & SVR4_O_SYNC) ? O_FSYNC : 0;
110 r |= (l & SVR4_O_NONBLOCK) ? O_NONBLOCK : 0;
111 r |= (l & SVR4_O_PRIV) ? O_EXLOCK : 0;
112 r |= (l & SVR4_O_CREAT) ? O_CREAT : 0;
113 r |= (l & SVR4_O_TRUNC) ? O_TRUNC : 0;
114 r |= (l & SVR4_O_EXCL) ? O_EXCL : 0;
115 r |= (l & SVR4_O_NOCTTY) ? O_NOCTTY : 0;
124 r |= (l & O_RDONLY) ? SVR4_O_RDONLY : 0;
125 r |= (l & O_WRONLY) ? SVR4_O_WRONLY : 0;
126 r |= (l & O_RDWR) ? SVR4_O_RDWR : 0;
127 r |= (l & O_NDELAY) ? SVR4_O_NONBLOCK : 0;
128 r |= (l & O_APPEND) ? SVR4_O_APPEND : 0;
129 r |= (l & O_FSYNC) ? SVR4_O_SYNC : 0;
130 r |= (l & O_NONBLOCK) ? SVR4_O_NONBLOCK : 0;
131 r |= (l & O_EXLOCK) ? SVR4_O_PRIV : 0;
132 r |= (l & O_CREAT) ? SVR4_O_CREAT : 0;
133 r |= (l & O_TRUNC) ? SVR4_O_TRUNC : 0;
134 r |= (l & O_EXCL) ? SVR4_O_EXCL : 0;
135 r |= (l & O_NOCTTY) ? SVR4_O_NOCTTY : 0;
141 bsd_to_svr4_flock(iflp, oflp)
143 struct svr4_flock *oflp;
145 switch (iflp->l_type) {
147 oflp->l_type = SVR4_F_RDLCK;
150 oflp->l_type = SVR4_F_WRLCK;
153 oflp->l_type = SVR4_F_UNLCK;
160 oflp->l_whence = (short) iflp->l_whence;
161 oflp->l_start = (svr4_off_t) iflp->l_start;
162 oflp->l_len = (svr4_off_t) iflp->l_len;
164 oflp->l_pid = (svr4_pid_t) iflp->l_pid;
169 svr4_to_bsd_flock(iflp, oflp)
170 struct svr4_flock *iflp;
173 switch (iflp->l_type) {
175 oflp->l_type = F_RDLCK;
178 oflp->l_type = F_WRLCK;
181 oflp->l_type = F_UNLCK;
188 oflp->l_whence = iflp->l_whence;
189 oflp->l_start = (off_t) iflp->l_start;
190 oflp->l_len = (off_t) iflp->l_len;
191 oflp->l_pid = (pid_t) iflp->l_pid;
196 bsd_to_svr4_flock64(iflp, oflp)
198 struct svr4_flock64 *oflp;
200 switch (iflp->l_type) {
202 oflp->l_type = SVR4_F_RDLCK;
205 oflp->l_type = SVR4_F_WRLCK;
208 oflp->l_type = SVR4_F_UNLCK;
215 oflp->l_whence = (short) iflp->l_whence;
216 oflp->l_start = (svr4_off64_t) iflp->l_start;
217 oflp->l_len = (svr4_off64_t) iflp->l_len;
219 oflp->l_pid = (svr4_pid_t) iflp->l_pid;
224 svr4_to_bsd_flock64(iflp, oflp)
225 struct svr4_flock64 *iflp;
228 switch (iflp->l_type) {
230 oflp->l_type = F_RDLCK;
233 oflp->l_type = F_WRLCK;
236 oflp->l_type = F_UNLCK;
243 oflp->l_whence = iflp->l_whence;
244 oflp->l_start = (off_t) iflp->l_start;
245 oflp->l_len = (off_t) iflp->l_len;
246 oflp->l_pid = (pid_t) iflp->l_pid;
261 retval = td->td_retval;
262 if ((error = fgetvp(td, fd, &vp)) != 0)
265 if (vp->v_type != VCHR && vp->v_type != VBLK) {
271 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
272 error = mac_check_vnode_revoke(td->td_ucred, vp);
273 VOP_UNLOCK(vp, 0, td);
278 if ((error = VOP_GETATTR(vp, &vattr, td->td_ucred, td)) != 0)
281 if (td->td_ucred->cr_uid != vattr.va_uid &&
282 (error = suser(td)) != 0)
285 if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
288 VOP_REVOKE(vp, REVOKEALL);
289 vn_finished_write(mp);
297 fd_truncate(td, fd, flp)
307 struct ftruncate_args ft;
309 retval = td->td_retval;
312 * We only support truncating the file.
314 if ((error = fget(td, fd, &fp)) != 0)
319 if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
324 if ((error = VOP_GETATTR(vp, &vattr, td->td_ucred, td)) != 0) {
329 length = vattr.va_size;
331 switch (flp->l_whence) {
333 start = fp->f_offset + flp->l_start;
337 start = flp->l_start + length;
341 start = flp->l_start;
349 if (start + flp->l_len < length) {
350 /* We don't support free'ing in the middle of the file */
358 error = ftruncate(td, &ft);
365 svr4_sys_open(td, uap)
366 register struct thread *td;
367 struct svr4_sys_open_args *uap;
369 struct proc *p = td->td_proc;
371 int bsd_flags, error, retval;
373 CHECKALTEXIST(td, uap->path, &newpath);
375 bsd_flags = svr4_to_bsd_flags(uap->flags);
376 error = kern_open(td, newpath, UIO_SYSSPACE, bsd_flags, uap->mode);
377 free(newpath, M_TEMP);
380 /* uprintf("svr4_open(%s, 0x%0x, 0%o): %d\n", uap->path,
381 uap->flags, uap->mode, error);*/
385 retval = td->td_retval[0];
388 if (!(bsd_flags & O_NOCTTY) && SESS_LEADER(p) &&
389 !(p->p_flag & P_CONTROLT)) {
393 error = fget(td, retval, &fp);
396 * we may have lost a race the above open() and
397 * another thread issuing a close()
400 return (EBADF); /* XXX: correct errno? */
401 /* ignore any error, just give it a try */
402 if (fp->f_type == DTYPE_VNODE)
403 fo_ioctl(fp, TIOCSCTTY, (caddr_t) 0, td->td_ucred,
417 svr4_sys_open64(td, uap)
418 register struct thread *td;
419 struct svr4_sys_open64_args *uap;
421 return svr4_sys_open(td, (struct svr4_sys_open_args *)uap);
425 svr4_sys_creat(td, uap)
426 register struct thread *td;
427 struct svr4_sys_creat_args *uap;
432 CHECKALTEXIST(td, uap->path, &newpath);
434 error = kern_open(td, newpath, UIO_SYSSPACE, O_WRONLY | O_CREAT |
436 free(newpath, M_TEMP);
441 svr4_sys_creat64(td, uap)
442 register struct thread *td;
443 struct svr4_sys_creat64_args *uap;
445 return svr4_sys_creat(td, (struct svr4_sys_creat_args *)uap);
449 svr4_sys_llseek(td, uap)
450 register struct thread *td;
451 struct svr4_sys_llseek_args *uap;
453 struct lseek_args ap;
457 #if BYTE_ORDER == BIG_ENDIAN
458 ap.offset = (((u_int64_t) uap->offset1) << 32) |
461 ap.offset = (((u_int64_t) uap->offset2) << 32) |
464 ap.whence = uap->whence;
466 return lseek(td, &ap);
470 svr4_sys_access(td, uap)
471 register struct thread *td;
472 struct svr4_sys_access_args *uap;
477 CHECKALTEXIST(td, uap->path, &newpath);
478 error = kern_access(td, newpath, UIO_SYSSPACE, uap->flags);
479 free(newpath, M_TEMP);
485 svr4_sys_pread(td, uap)
486 register struct thread *td;
487 struct svr4_sys_pread_args *uap;
489 struct pread_args pra;
492 * Just translate the args structure and call the NetBSD
493 * pread(2) system call (offset type is 64-bit in NetBSD).
497 pra.nbyte = uap->nbyte;
498 pra.offset = uap->off;
500 return pread(td, &pra);
506 svr4_sys_pread64(td, v, retval)
507 register struct thread *td;
512 struct svr4_sys_pread64_args *uap = v;
513 struct sys_pread_args pra;
516 * Just translate the args structure and call the NetBSD
517 * pread(2) system call (offset type is 64-bit in NetBSD).
521 pra.nbyte = uap->nbyte;
522 pra.offset = uap->off;
524 return (sys_pread(td, &pra, retval));
530 svr4_sys_pwrite(td, uap)
531 register struct thread *td;
532 struct svr4_sys_pwrite_args *uap;
534 struct pwrite_args pwa;
537 * Just translate the args structure and call the NetBSD
538 * pwrite(2) system call (offset type is 64-bit in NetBSD).
542 pwa.nbyte = uap->nbyte;
543 pwa.offset = uap->off;
545 return pwrite(td, &pwa);
551 svr4_sys_pwrite64(td, v, retval)
552 register struct thread *td;
556 struct svr4_sys_pwrite64_args *uap = v;
557 struct sys_pwrite_args pwa;
560 * Just translate the args structure and call the NetBSD
561 * pwrite(2) system call (offset type is 64-bit in NetBSD).
565 pwa.nbyte = uap->nbyte;
566 pwa.offset = uap->off;
568 return (sys_pwrite(td, &pwa, retval));
573 svr4_sys_fcntl(td, uap)
574 register struct thread *td;
575 struct svr4_sys_fcntl_args *uap;
577 int cmd, error, *retval;
579 retval = td->td_retval;
581 cmd = svr4_to_bsd_cmd(uap->cmd);
587 return (kern_fcntl(td, uap->fd, cmd, (intptr_t)uap->arg));
590 error = kern_fcntl(td, uap->fd, cmd, (intptr_t)uap->arg);
593 *retval = bsd_to_svr4_flags(*retval);
599 * we must save the O_ASYNC flag, as that is
600 * handled by ioctl(_, I_SETSIG, _) emulation.
604 DPRINTF(("Setting flags %p\n", uap->arg));
606 error = kern_fcntl(td, uap->fd, F_GETFL, 0);
611 flags |= svr4_to_bsd_flags((u_long) uap->arg);
612 return (kern_fcntl(td, uap->fd, F_SETFL, flags));
619 struct svr4_flock ifl;
622 error = copyin(uap->arg, &ifl, sizeof (ifl));
626 svr4_to_bsd_flock(&ifl, &fl);
628 error = kern_fcntl(td, uap->fd, cmd, (intptr_t)&fl);
629 if (error || cmd != F_GETLK)
632 bsd_to_svr4_flock(&fl, &ifl);
634 return (copyout(&ifl, uap->arg, sizeof (ifl)));
643 du.to = (int)uap->arg;
644 error = dup2(td, &du);
653 struct svr4_flock ifl;
656 error = copyin(uap->arg, &ifl,
660 svr4_to_bsd_flock(&ifl, &fl);
661 return fd_truncate(td, uap->fd, &fl);
666 case SVR4_F_SETLKW64:
668 struct svr4_flock64 ifl;
678 case SVR4_F_SETLKW64:
682 error = copyin(uap->arg, &ifl,
687 svr4_to_bsd_flock64(&ifl, &fl);
689 error = kern_fcntl(td, uap->fd, cmd,
691 if (error || cmd != F_GETLK)
694 bsd_to_svr4_flock64(&fl, &ifl);
696 return (copyout(&ifl, uap->arg,
700 case SVR4_F_FREESP64:
702 struct svr4_flock64 ifl;
705 error = copyin(uap->arg, &ifl,
709 svr4_to_bsd_flock64(&ifl, &fl);
710 return fd_truncate(td, uap->fd, &fl);
714 return fd_revoke(td, uap->fd);