syscall messaging 3: Expand the 'header' that goes in front of the syscall
[dragonfly.git] / sys / emulation / svr4 / svr4_fcntl.c
CommitLineData
984263bc
MD
1/*
2 * Copyright (c) 1998 Mark Newton
3 * Copyright (c) 1994, 1997 Christos Zoulas.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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.
19 *
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.
30 *
31 * $FreeBSD: src/sys/svr4/svr4_fcntl.c,v 1.7 1999/12/12 10:27:04 newton Exp $
c7114eea 32 * $DragonFly: src/sys/emulation/svr4/Attic/svr4_fcntl.c,v 1.8 2003/07/30 00:19:15 dillon Exp $
984263bc
MD
33 */
34#include <sys/param.h>
35#include <sys/systm.h>
984263bc 36#include <sys/proc.h>
dadab5e9 37#include <sys/namei.h>
984263bc
MD
38#include <sys/file.h>
39#include <sys/stat.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>
47
48#include <sys/sysproto.h>
49
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>
56
57static int svr4_to_bsd_flags __P((int));
58static u_long svr4_to_bsd_cmd __P((u_long));
dadab5e9 59static int fd_revoke __P((struct thread *, int));
90b9818c 60static int fd_truncate __P((struct thread *, int, struct flock *, int *));
984263bc
MD
61static int bsd_to_svr4_flags __P((int));
62static void bsd_to_svr4_flock __P((struct flock *, struct svr4_flock *));
63static void svr4_to_bsd_flock __P((struct svr4_flock *, struct flock *));
64static void bsd_to_svr4_flock64 __P((struct flock *, struct svr4_flock64 *));
65static void svr4_to_bsd_flock64 __P((struct svr4_flock64 *, struct flock *));
66
67static u_long
68svr4_to_bsd_cmd(cmd)
69 u_long cmd;
70{
71 switch (cmd) {
72 case SVR4_F_DUPFD:
73 return F_DUPFD;
74 case SVR4_F_GETFD:
75 return F_GETFD;
76 case SVR4_F_SETFD:
77 return F_SETFD;
78 case SVR4_F_GETFL:
79 return F_GETFL;
80 case SVR4_F_SETFL:
81 return F_SETFL;
82 case SVR4_F_GETLK:
83 return F_GETLK;
84 case SVR4_F_SETLK:
85 return F_SETLK;
86 case SVR4_F_SETLKW:
87 return F_SETLKW;
88 default:
89 return -1;
90 }
91}
92
93static int
94svr4_to_bsd_flags(l)
95 int l;
96{
97 int r = 0;
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;
110 return r;
111}
112
113static int
114bsd_to_svr4_flags(l)
115 int l;
116{
117 int r = 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;
130 return r;
131}
132
133
134static void
135bsd_to_svr4_flock(iflp, oflp)
136 struct flock *iflp;
137 struct svr4_flock *oflp;
138{
139 switch (iflp->l_type) {
140 case F_RDLCK:
141 oflp->l_type = SVR4_F_RDLCK;
142 break;
143 case F_WRLCK:
144 oflp->l_type = SVR4_F_WRLCK;
145 break;
146 case F_UNLCK:
147 oflp->l_type = SVR4_F_UNLCK;
148 break;
149 default:
150 oflp->l_type = -1;
151 break;
152 }
153
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;
157 oflp->l_sysid = 0;
158 oflp->l_pid = (svr4_pid_t) iflp->l_pid;
159}
160
161
162static void
163svr4_to_bsd_flock(iflp, oflp)
164 struct svr4_flock *iflp;
165 struct flock *oflp;
166{
167 switch (iflp->l_type) {
168 case SVR4_F_RDLCK:
169 oflp->l_type = F_RDLCK;
170 break;
171 case SVR4_F_WRLCK:
172 oflp->l_type = F_WRLCK;
173 break;
174 case SVR4_F_UNLCK:
175 oflp->l_type = F_UNLCK;
176 break;
177 default:
178 oflp->l_type = -1;
179 break;
180 }
181
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;
186
187}
188
189static void
190bsd_to_svr4_flock64(iflp, oflp)
191 struct flock *iflp;
192 struct svr4_flock64 *oflp;
193{
194 switch (iflp->l_type) {
195 case F_RDLCK:
196 oflp->l_type = SVR4_F_RDLCK;
197 break;
198 case F_WRLCK:
199 oflp->l_type = SVR4_F_WRLCK;
200 break;
201 case F_UNLCK:
202 oflp->l_type = SVR4_F_UNLCK;
203 break;
204 default:
205 oflp->l_type = -1;
206 break;
207 }
208
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;
212 oflp->l_sysid = 0;
213 oflp->l_pid = (svr4_pid_t) iflp->l_pid;
214}
215
216
217static void
218svr4_to_bsd_flock64(iflp, oflp)
219 struct svr4_flock64 *iflp;
220 struct flock *oflp;
221{
222 switch (iflp->l_type) {
223 case SVR4_F_RDLCK:
224 oflp->l_type = F_RDLCK;
225 break;
226 case SVR4_F_WRLCK:
227 oflp->l_type = F_WRLCK;
228 break;
229 case SVR4_F_UNLCK:
230 oflp->l_type = F_UNLCK;
231 break;
232 default:
233 oflp->l_type = -1;
234 break;
235 }
236
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;
241
242}
243
244
245static int
dadab5e9 246fd_revoke(struct thread *td, int fd)
984263bc 247{
dadab5e9
MD
248 struct proc *p = td->td_proc;
249 struct filedesc *fdp;
984263bc
MD
250 struct file *fp;
251 struct vnode *vp;
252 struct vattr vattr;
90b9818c 253 int error;
984263bc 254
dadab5e9
MD
255 KKASSERT(p);
256
257 fdp = p->p_fd;
984263bc
MD
258 if ((u_int)fd >= fdp->fd_nfiles || (fp = fdp->fd_ofiles[fd]) == NULL)
259 return EBADF;
260
261 if (fp->f_type != DTYPE_VNODE)
262 return EINVAL;
263
264 vp = (struct vnode *) fp->f_data;
265
266 if (vp->v_type != VCHR && vp->v_type != VBLK) {
267 error = EINVAL;
268 goto out;
269 }
270
3b568787 271 if ((error = VOP_GETATTR(vp, &vattr, td)) != 0)
984263bc
MD
272 goto out;
273
274 if (p->p_ucred->cr_uid != vattr.va_uid &&
dadab5e9 275 (error = suser(p->p_thread)) != 0)
984263bc
MD
276 goto out;
277
278 if (vcount(vp) > 1)
279 VOP_REVOKE(vp, REVOKEALL);
280out:
281 vrele(vp);
282 return error;
283}
284
285
286static int
90b9818c 287fd_truncate(struct thread *td, int fd, struct flock *flp, int *retval)
984263bc 288{
dadab5e9
MD
289 struct proc *p = td->td_proc;
290 struct filedesc *fdp;
984263bc
MD
291 struct file *fp;
292 off_t start, length;
293 struct vnode *vp;
294 struct vattr vattr;
90b9818c 295 int error;
984263bc
MD
296 struct ftruncate_args ft;
297
dadab5e9
MD
298 KKASSERT(p);
299 fdp = p->p_fd;
984263bc
MD
300
301 /*
302 * We only support truncating the file.
303 */
304 if ((u_int)fd >= fdp->fd_nfiles || (fp = fdp->fd_ofiles[fd]) == NULL)
305 return EBADF;
306
307 vp = (struct vnode *)fp->f_data;
308 if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO)
309 return ESPIPE;
310
3b568787 311 if ((error = VOP_GETATTR(vp, &vattr, td)) != 0)
984263bc
MD
312 return error;
313
314 length = vattr.va_size;
315
316 switch (flp->l_whence) {
317 case SEEK_CUR:
318 start = fp->f_offset + flp->l_start;
319 break;
320
321 case SEEK_END:
322 start = flp->l_start + length;
323 break;
324
325 case SEEK_SET:
326 start = flp->l_start;
327 break;
328
329 default:
330 return EINVAL;
331 }
332
333 if (start + flp->l_len < length) {
334 /* We don't support free'ing in the middle of the file */
335 return EINVAL;
336 }
337
338 SCARG(&ft, fd) = fd;
339 SCARG(&ft, length) = start;
90b9818c 340 error = ftruncate(&ft);
c7114eea 341 *retval = ft.sysmsg_result;
90b9818c 342 return(error);
984263bc
MD
343}
344
345int
41c20dac 346svr4_sys_open(struct svr4_sys_open_args *uap)
984263bc 347{
dadab5e9
MD
348 struct thread *td = curthread; /* XXX */
349 struct proc *p = td->td_proc;
350 struct open_args cup;
351 int error, retval;
984263bc
MD
352
353 caddr_t sg = stackgap_init();
41c20dac 354 CHECKALTEXIST(&sg, SCARG(uap, path));
984263bc 355
c7114eea 356 cup.sysmsg_result = 0;
90b9818c
MD
357 cup.path = uap->path;
358 cup.flags = svr4_to_bsd_flags(uap->flags);
359 cup.mode = uap->mode;
41c20dac 360 error = open(&cup);
984263bc
MD
361
362 if (error) {
363 /* uprintf("svr4_open(%s, 0x%0x, 0%o): %d\n", uap->path,
364 uap->flags, uap->mode, error);*/
365 return error;
366 }
367
dadab5e9 368 KKASSERT(p);
c7114eea 369 retval = uap->sysmsg_result = cup.sysmsg_result;
984263bc
MD
370
371 if (!(SCARG(&cup, flags) & O_NOCTTY) && SESS_LEADER(p) &&
372 !(p->p_flag & P_CONTROLT)) {
373#if defined(NOTYET)
374 struct filedesc *fdp = p->p_fd;
375 struct file *fp = fdp->fd_ofiles[retval];
376
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);
380#endif
381 }
382 return error;
383}
384
385int
41c20dac 386svr4_sys_open64(struct svr4_sys_open64_args *uap)
984263bc 387{
41c20dac 388 return svr4_sys_open((struct svr4_sys_open_args *)uap);
984263bc
MD
389}
390
391int
41c20dac 392svr4_sys_creat(struct svr4_sys_creat_args *uap)
984263bc
MD
393{
394 struct open_args cup;
90b9818c 395 int error;
984263bc
MD
396
397 caddr_t sg = stackgap_init();
41c20dac 398 CHECKALTEXIST(&sg, SCARG(uap, path));
984263bc
MD
399
400 SCARG(&cup, path) = SCARG(uap, path);
401 SCARG(&cup, mode) = SCARG(uap, mode);
402 SCARG(&cup, flags) = O_WRONLY | O_CREAT | O_TRUNC;
403
c7114eea 404 cup.sysmsg_result = 0;
90b9818c 405 error = open(&cup);
c7114eea 406 uap->sysmsg_result = cup.sysmsg_result;
90b9818c 407 return(error);
984263bc
MD
408}
409
410int
41c20dac 411svr4_sys_creat64(struct svr4_sys_creat64_args *uap)
984263bc 412{
41c20dac 413 return svr4_sys_creat((struct svr4_sys_creat_args *)uap);
984263bc
MD
414}
415
416int
41c20dac 417svr4_sys_llseek(struct svr4_sys_llseek_args *v)
984263bc
MD
418{
419 struct svr4_sys_llseek_args *uap = v;
420 struct lseek_args ap;
421
422 SCARG(&ap, fd) = SCARG(uap, fd);
423
424#if BYTE_ORDER == BIG_ENDIAN
425 SCARG(&ap, offset) = (((long long) SCARG(uap, offset1)) << 32) |
426 SCARG(uap, offset2);
427#else
428 SCARG(&ap, offset) = (((long long) SCARG(uap, offset2)) << 32) |
429 SCARG(uap, offset1);
430#endif
431 SCARG(&ap, whence) = SCARG(uap, whence);
432
41c20dac 433 return lseek(&ap);
984263bc
MD
434}
435
436int
41c20dac 437svr4_sys_access(struct svr4_sys_access_args *uap)
984263bc 438{
dadab5e9
MD
439 struct thread *td = curthread; /* XXX */
440 struct proc *p = td->td_proc;
984263bc 441 struct access_args cup;
90b9818c 442 int error;
984263bc
MD
443
444 caddr_t sg = stackgap_init();
41c20dac 445 CHECKALTEXIST(&sg, SCARG(uap, path));
984263bc 446
dadab5e9 447 KKASSERT(p);
984263bc
MD
448
449 SCARG(&cup, path) = SCARG(uap, path);
450 SCARG(&cup, flags) = SCARG(uap, flags);
c7114eea 451 cup.sysmsg_result = 0;
90b9818c 452 error = access(&cup);
c7114eea 453 uap->sysmsg_result = cup.sysmsg_result;
90b9818c 454 return(error);
984263bc
MD
455}
456
457#if defined(NOTYET)
458int
41c20dac 459svr4_sys_pread(struct svr4_sys_pread_args *uap)
984263bc
MD
460{
461 struct pread_args pra;
90b9818c 462 int error;
984263bc
MD
463
464 /*
465 * Just translate the args structure and call the NetBSD
466 * pread(2) system call (offset type is 64-bit in NetBSD).
467 */
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);
472
c7114eea 473 pra.sysmsg_result = 0;
90b9818c 474 error = pread(&pra);
c7114eea 475 uap->sysmsg_result = pra.sysmsg_result;
90b9818c 476 return(error);
984263bc
MD
477}
478#endif
479
480#if defined(NOTYET)
481int
dadab5e9 482svr4_sys_pread64(struct thread *td, void *v, register_t *retval)
984263bc 483{
984263bc
MD
484 struct svr4_sys_pread64_args *uap = v;
485 struct sys_pread_args pra;
90b9818c 486 int error;
984263bc
MD
487
488 /*
489 * Just translate the args structure and call the NetBSD
490 * pread(2) system call (offset type is 64-bit in NetBSD).
491 */
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);
496
c7114eea 497 pra.sysmsg_result = 0;
90b9818c 498 error = sys_pread(&pra, retval);
c7114eea 499 uap->sysmsg_result = pra.sysmsg_result;
90b9818c 500 return(error);
984263bc
MD
501}
502#endif /* NOTYET */
503
504#if defined(NOTYET)
505int
41c20dac 506svr4_sys_pwrite(struct svr4_sys_pwrite_args *uap)
984263bc
MD
507{
508 struct pwrite_args pwa;
90b9818c 509 int error;
984263bc
MD
510
511 /*
512 * Just translate the args structure and call the NetBSD
513 * pwrite(2) system call (offset type is 64-bit in NetBSD).
514 */
515 SCARG(&pwa, fd) = SCARG(uap, fd);
516 SCARG(&pwa, buf) = SCARG(uap, buf);
517 SCARG(&pwa, nbyte) = SCARG(uap, nbyte);
518 SCARG(&pwa, offset) = SCARG(uap, off);
519
c7114eea 520 pwa.sysmsg_result = 0;
90b9818c 521 error = pwrite(&pwa);
c7114eea 522 uap->sysmsg_result = pwa.sysmsg_result;
90b9818c 523 return(error);
984263bc
MD
524}
525#endif
526
527#if defined(NOTYET)
528int
dadab5e9 529svr4_sys_pwrite64(struct thread *td, void *v, register_t *retval)
984263bc
MD
530{
531 struct svr4_sys_pwrite64_args *uap = v;
532 struct sys_pwrite_args pwa;
533
534 /*
535 * Just translate the args structure and call the NetBSD
536 * pwrite(2) system call (offset type is 64-bit in NetBSD).
537 */
538 SCARG(&pwa, fd) = SCARG(uap, fd);
539 SCARG(&pwa, buf) = SCARG(uap, buf);
540 SCARG(&pwa, nbyte) = SCARG(uap, nbyte);
541 SCARG(&pwa, offset) = SCARG(uap, off);
542
543 return (sys_pwrite(p, &pwa, retval));
544}
545#endif /* NOTYET */
546
547int
41c20dac 548svr4_sys_fcntl(struct svr4_sys_fcntl_args *uap)
984263bc 549{
dadab5e9
MD
550 struct thread *td = curthread; /* XXX */
551 struct proc *p = td->td_proc;
984263bc
MD
552 int error;
553 struct fcntl_args fa;
554 int *retval;
555
dadab5e9 556 KKASSERT(p);
c7114eea 557 retval = &uap->sysmsg_result;
984263bc
MD
558
559 SCARG(&fa, fd) = SCARG(uap, fd);
560 SCARG(&fa, cmd) = svr4_to_bsd_cmd(SCARG(uap, cmd));
561
562 switch (SCARG(&fa, cmd)) {
563 case F_DUPFD:
564 case F_GETFD:
565 case F_SETFD:
566 SCARG(&fa, arg) = (long) SCARG(uap, arg);
c7114eea 567 fa.sysmsg_result = 0;
90b9818c 568 error = fcntl(&fa);
c7114eea 569 *retval = fa.sysmsg_result;
90b9818c 570 return error;
984263bc
MD
571
572 case F_GETFL:
573 SCARG(&fa, arg) = (long) SCARG(uap, arg);
41c20dac 574 error = fcntl(&fa);
984263bc
MD
575 if (error)
576 return error;
c7114eea 577 *retval = bsd_to_svr4_flags(fa.sysmsg_result);
984263bc
MD
578 return error;
579
580 case F_SETFL:
581 {
582 /*
583 * we must save the O_ASYNC flag, as that is
584 * handled by ioctl(_, I_SETSIG, _) emulation.
585 */
586 long cmd;
587 int flags;
588
a99c2fff 589 DPRINTF(("Setting flags %p\n", SCARG(uap, arg)));
984263bc
MD
590 cmd = SCARG(&fa, cmd); /* save it for a while */
591
592 SCARG(&fa, cmd) = F_GETFL;
41c20dac 593 if ((error = fcntl(&fa)) != 0)
984263bc 594 return error;
c7114eea 595 flags = fa.sysmsg_result;
984263bc
MD
596 flags &= O_ASYNC;
597 flags |= svr4_to_bsd_flags((u_long) SCARG(uap, arg));
598 SCARG(&fa, cmd) = cmd;
599 SCARG(&fa, arg) = (long) flags;
90b9818c 600 error = fcntl(&fa);
c7114eea 601 *retval = fa.sysmsg_result;
90b9818c 602 return error;
984263bc
MD
603 }
604
605 case F_GETLK:
606 case F_SETLK:
607 case F_SETLKW:
608 {
609 struct svr4_flock ifl;
610 struct flock *flp, fl;
611 caddr_t sg = stackgap_init();
612
613 flp = stackgap_alloc(&sg, sizeof(struct flock));
614 SCARG(&fa, arg) = (long) flp;
615
616 error = copyin(SCARG(uap, arg), &ifl, sizeof ifl);
617 if (error)
618 return error;
619
620 svr4_to_bsd_flock(&ifl, &fl);
621
622 error = copyout(&fl, flp, sizeof fl);
623 if (error)
624 return error;
625
c7114eea 626 fa.sysmsg_result = 0;
41c20dac 627 error = fcntl(&fa);
c7114eea 628 *retval = fa.sysmsg_result;
984263bc
MD
629 if (error || SCARG(&fa, cmd) != F_GETLK)
630 return error;
631
632 error = copyin(flp, &fl, sizeof fl);
633 if (error)
634 return error;
635
636 bsd_to_svr4_flock(&fl, &ifl);
637
638 return copyout(&ifl, SCARG(uap, arg), sizeof ifl);
639 }
640 case -1:
641 switch (SCARG(uap, cmd)) {
642 case SVR4_F_DUP2FD:
643 {
644 struct dup2_args du;
645
646 SCARG(&du, from) = SCARG(uap, fd);
647 SCARG(&du, to) = (int)SCARG(uap, arg);
41c20dac 648 error = dup2(&du);
984263bc
MD
649 if (error)
650 return error;
651 *retval = SCARG(&du, to);
652 return 0;
653 }
654
655 case SVR4_F_FREESP:
656 {
657 struct svr4_flock ifl;
658 struct flock fl;
659
660 error = copyin(SCARG(uap, arg), &ifl,
661 sizeof ifl);
662 if (error)
663 return error;
664 svr4_to_bsd_flock(&ifl, &fl);
90b9818c 665 return fd_truncate(td, SCARG(uap, fd), &fl, retval);
984263bc
MD
666 }
667
668 case SVR4_F_GETLK64:
669 case SVR4_F_SETLK64:
670 case SVR4_F_SETLKW64:
671 {
672 struct svr4_flock64 ifl;
673 struct flock *flp, fl;
674 caddr_t sg = stackgap_init();
675
676 flp = stackgap_alloc(&sg, sizeof(struct flock));
677 SCARG(&fa, arg) = (long) flp;
678
679 error = copyin(SCARG(uap, arg), &ifl,
680 sizeof ifl);
681 if (error)
682 return error;
683
684 svr4_to_bsd_flock64(&ifl, &fl);
685
686 error = copyout(&fl, flp, sizeof fl);
687 if (error)
688 return error;
689
c7114eea 690 fa.sysmsg_result = 0;
41c20dac 691 error = fcntl(&fa);
c7114eea 692 *retval = fa.sysmsg_result;
984263bc
MD
693 if (error || SCARG(&fa, cmd) != F_GETLK)
694 return error;
695
696 error = copyin(flp, &fl, sizeof fl);
697 if (error)
698 return error;
699
700 bsd_to_svr4_flock64(&fl, &ifl);
701
702 return copyout(&ifl, SCARG(uap, arg),
703 sizeof ifl);
704 }
705
706 case SVR4_F_FREESP64:
707 {
708 struct svr4_flock64 ifl;
709 struct flock fl;
710
711 error = copyin(SCARG(uap, arg), &ifl,
712 sizeof ifl);
713 if (error)
714 return error;
715 svr4_to_bsd_flock64(&ifl, &fl);
90b9818c 716 return fd_truncate(td, SCARG(uap, fd), &fl, retval);
984263bc
MD
717 }
718
719 case SVR4_F_REVOKE:
dadab5e9 720 return fd_revoke(td, SCARG(uap, fd));
984263bc
MD
721
722 default:
723 return ENOSYS;
724 }
725
726 default:
727 return ENOSYS;
728 }
729}