]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/compat/svr4/svr4_fcntl.c
This commit was generated by cvs2svn to compensate for changes in r152390,
[FreeBSD/FreeBSD.git] / sys / compat / svr4 / svr4_fcntl.c
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
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34
35 #include "opt_mac.h"
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/file.h>
40 #include <sys/filedesc.h>
41 /*#include <sys/ioctl.h>*/
42 #include <sys/lock.h>
43 #include <sys/mac.h>
44 #include <sys/malloc.h>
45 #include <sys/mount.h>
46 #include <sys/mutex.h>
47 #include <sys/namei.h>
48 #include <sys/proc.h>
49 #include <sys/stat.h>
50 #include <sys/syscallsubr.h>
51 #include <sys/unistd.h>
52 #include <sys/vnode.h>
53
54 #include <sys/sysproto.h>
55
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>
62
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 *);
72
73 static u_long
74 svr4_to_bsd_cmd(cmd)
75         u_long  cmd;
76 {
77         switch (cmd) {
78         case SVR4_F_DUPFD:
79                 return F_DUPFD;
80         case SVR4_F_GETFD:
81                 return F_GETFD;
82         case SVR4_F_SETFD:
83                 return F_SETFD;
84         case SVR4_F_GETFL:
85                 return F_GETFL;
86         case SVR4_F_SETFL:
87                 return F_SETFL;
88         case SVR4_F_GETLK:
89                 return F_GETLK;
90         case SVR4_F_SETLK:
91                 return F_SETLK;
92         case SVR4_F_SETLKW:
93                 return F_SETLKW;
94         default:
95                 return -1;
96         }
97 }
98
99 static int
100 svr4_to_bsd_flags(l)
101         int     l;
102 {
103         int     r = 0;
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;
116         return r;
117 }
118
119 static int
120 bsd_to_svr4_flags(l)
121         int     l;
122 {
123         int     r = 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;
136         return r;
137 }
138
139
140 static void
141 bsd_to_svr4_flock(iflp, oflp)
142         struct flock            *iflp;
143         struct svr4_flock       *oflp;
144 {
145         switch (iflp->l_type) {
146         case F_RDLCK:
147                 oflp->l_type = SVR4_F_RDLCK;
148                 break;
149         case F_WRLCK:
150                 oflp->l_type = SVR4_F_WRLCK;
151                 break;
152         case F_UNLCK:
153                 oflp->l_type = SVR4_F_UNLCK;
154                 break;
155         default:
156                 oflp->l_type = -1;
157                 break;
158         }
159
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;
163         oflp->l_sysid = 0;
164         oflp->l_pid = (svr4_pid_t) iflp->l_pid;
165 }
166
167
168 static void
169 svr4_to_bsd_flock(iflp, oflp)
170         struct svr4_flock       *iflp;
171         struct flock            *oflp;
172 {
173         switch (iflp->l_type) {
174         case SVR4_F_RDLCK:
175                 oflp->l_type = F_RDLCK;
176                 break;
177         case SVR4_F_WRLCK:
178                 oflp->l_type = F_WRLCK;
179                 break;
180         case SVR4_F_UNLCK:
181                 oflp->l_type = F_UNLCK;
182                 break;
183         default:
184                 oflp->l_type = -1;
185                 break;
186         }
187
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;
192
193 }
194
195 static void
196 bsd_to_svr4_flock64(iflp, oflp)
197         struct flock            *iflp;
198         struct svr4_flock64     *oflp;
199 {
200         switch (iflp->l_type) {
201         case F_RDLCK:
202                 oflp->l_type = SVR4_F_RDLCK;
203                 break;
204         case F_WRLCK:
205                 oflp->l_type = SVR4_F_WRLCK;
206                 break;
207         case F_UNLCK:
208                 oflp->l_type = SVR4_F_UNLCK;
209                 break;
210         default:
211                 oflp->l_type = -1;
212                 break;
213         }
214
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;
218         oflp->l_sysid = 0;
219         oflp->l_pid = (svr4_pid_t) iflp->l_pid;
220 }
221
222
223 static void
224 svr4_to_bsd_flock64(iflp, oflp)
225         struct svr4_flock64     *iflp;
226         struct flock            *oflp;
227 {
228         switch (iflp->l_type) {
229         case SVR4_F_RDLCK:
230                 oflp->l_type = F_RDLCK;
231                 break;
232         case SVR4_F_WRLCK:
233                 oflp->l_type = F_WRLCK;
234                 break;
235         case SVR4_F_UNLCK:
236                 oflp->l_type = F_UNLCK;
237                 break;
238         default:
239                 oflp->l_type = -1;
240                 break;
241         }
242
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;
247
248 }
249
250
251 static int
252 fd_revoke(td, fd)
253         struct thread *td;
254         int fd;
255 {
256         struct vnode *vp;
257         struct mount *mp;
258         struct vattr vattr;
259         int error, *retval;
260
261         retval = td->td_retval;
262         if ((error = fgetvp(td, fd, &vp)) != 0)
263                 return (error);
264
265         if (vp->v_type != VCHR && vp->v_type != VBLK) {
266                 error = EINVAL;
267                 goto out;
268         }
269
270 #ifdef MAC
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);
274         if (error)
275                 goto out;
276 #endif
277
278         if ((error = VOP_GETATTR(vp, &vattr, td->td_ucred, td)) != 0)
279                 goto out;
280
281         if (td->td_ucred->cr_uid != vattr.va_uid &&
282             (error = suser(td)) != 0)
283                 goto out;
284
285         if ((error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
286                 goto out;
287         if (vcount(vp) > 1)
288                 VOP_REVOKE(vp, REVOKEALL);
289         vn_finished_write(mp);
290 out:
291         vrele(vp);
292         return error;
293 }
294
295
296 static int
297 fd_truncate(td, fd, flp)
298         struct thread *td;
299         int fd;
300         struct flock *flp;
301 {
302         off_t start, length;
303         struct file *fp;
304         struct vnode *vp;
305         struct vattr vattr;
306         int error, *retval;
307         struct ftruncate_args ft;
308
309         retval = td->td_retval;
310
311         /*
312          * We only support truncating the file.
313          */
314         if ((error = fget(td, fd, &fp)) != 0)
315                 return (error);
316
317         vp = fp->f_vnode;
318
319         if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
320                 fdrop(fp, td);
321                 return ESPIPE;
322         }
323
324         if ((error = VOP_GETATTR(vp, &vattr, td->td_ucred, td)) != 0) {
325                 fdrop(fp, td);
326                 return error;
327         }
328
329         length = vattr.va_size;
330
331         switch (flp->l_whence) {
332         case SEEK_CUR:
333                 start = fp->f_offset + flp->l_start;
334                 break;
335
336         case SEEK_END:
337                 start = flp->l_start + length;
338                 break;
339
340         case SEEK_SET:
341                 start = flp->l_start;
342                 break;
343
344         default:
345                 fdrop(fp, td);
346                 return EINVAL;
347         }
348
349         if (start + flp->l_len < length) {
350                 /* We don't support free'ing in the middle of the file */
351                 fdrop(fp, td);
352                 return EINVAL;
353         }
354
355         ft.fd = fd;
356         ft.length = start;
357
358         error = ftruncate(td, &ft);
359
360         fdrop(fp, td);
361         return (error);
362 }
363
364 int
365 svr4_sys_open(td, uap)
366         register struct thread *td;
367         struct svr4_sys_open_args *uap;
368 {
369         struct proc *p = td->td_proc;
370         char *newpath;
371         int bsd_flags, error, retval;
372
373         CHECKALTEXIST(td, uap->path, &newpath);
374
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);
378
379         if (error) {
380           /*            uprintf("svr4_open(%s, 0x%0x, 0%o): %d\n", uap->path,
381                         uap->flags, uap->mode, error);*/
382                 return error;
383         }
384
385         retval = td->td_retval[0];
386
387         PROC_LOCK(p);
388         if (!(bsd_flags & O_NOCTTY) && SESS_LEADER(p) &&
389             !(p->p_flag & P_CONTROLT)) {
390 #if defined(NOTYET)
391                 struct file     *fp;
392
393                 error = fget(td, retval, &fp);
394                 PROC_UNLOCK(p);
395                 /*
396                  * we may have lost a race the above open() and
397                  * another thread issuing a close()
398                  */
399                 if (error) 
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,
404                             td);
405                 fdrop(fp, td);
406         } else {
407                 PROC_UNLOCK(p);
408         }
409 #else
410         }
411         PROC_UNLOCK(p);
412 #endif
413         return error;
414 }
415
416 int
417 svr4_sys_open64(td, uap)
418         register struct thread *td;
419         struct svr4_sys_open64_args *uap;
420 {
421         return svr4_sys_open(td, (struct svr4_sys_open_args *)uap);
422 }
423
424 int
425 svr4_sys_creat(td, uap)
426         register struct thread *td;
427         struct svr4_sys_creat_args *uap;
428 {
429         char *newpath;
430         int error;
431
432         CHECKALTEXIST(td, uap->path, &newpath);
433
434         error = kern_open(td, newpath, UIO_SYSSPACE, O_WRONLY | O_CREAT |
435             O_TRUNC, uap->mode);
436         free(newpath, M_TEMP);
437         return (error);
438 }
439
440 int
441 svr4_sys_creat64(td, uap)
442         register struct thread *td;
443         struct svr4_sys_creat64_args *uap;
444 {
445         return svr4_sys_creat(td, (struct svr4_sys_creat_args *)uap);
446 }
447
448 int
449 svr4_sys_llseek(td, uap)
450         register struct thread *td;
451         struct svr4_sys_llseek_args *uap;
452 {
453         struct lseek_args ap;
454
455         ap.fd = uap->fd;
456
457 #if BYTE_ORDER == BIG_ENDIAN
458         ap.offset = (((u_int64_t) uap->offset1) << 32) | 
459                 uap->offset2;
460 #else
461         ap.offset = (((u_int64_t) uap->offset2) << 32) | 
462                 uap->offset1;
463 #endif
464         ap.whence = uap->whence;
465
466         return lseek(td, &ap);
467 }
468
469 int
470 svr4_sys_access(td, uap)
471         register struct thread *td;
472         struct svr4_sys_access_args *uap;
473 {
474         char *newpath;
475         int error;
476
477         CHECKALTEXIST(td, uap->path, &newpath);
478         error = kern_access(td, newpath, UIO_SYSSPACE, uap->flags);
479         free(newpath, M_TEMP);
480         return (error);
481 }
482
483 #if defined(NOTYET)
484 int
485 svr4_sys_pread(td, uap)
486         register struct thread *td;
487         struct svr4_sys_pread_args *uap;
488 {
489         struct pread_args pra;
490
491         /*
492          * Just translate the args structure and call the NetBSD
493          * pread(2) system call (offset type is 64-bit in NetBSD).
494          */
495         pra.fd = uap->fd;
496         pra.buf = uap->buf;
497         pra.nbyte = uap->nbyte;
498         pra.offset = uap->off;
499
500         return pread(td, &pra);
501 }
502 #endif
503
504 #if defined(NOTYET)
505 int
506 svr4_sys_pread64(td, v, retval)
507         register struct thread *td;
508         void *v; 
509         register_t *retval;
510 {
511
512         struct svr4_sys_pread64_args *uap = v;
513         struct sys_pread_args pra;
514
515         /*
516          * Just translate the args structure and call the NetBSD
517          * pread(2) system call (offset type is 64-bit in NetBSD).
518          */
519         pra.fd = uap->fd;
520         pra.buf = uap->buf;
521         pra.nbyte = uap->nbyte;
522         pra.offset = uap->off;
523
524         return (sys_pread(td, &pra, retval));
525 }
526 #endif /* NOTYET */
527
528 #if defined(NOTYET)
529 int
530 svr4_sys_pwrite(td, uap)
531         register struct thread *td;
532         struct svr4_sys_pwrite_args *uap;
533 {
534         struct pwrite_args pwa;
535
536         /*
537          * Just translate the args structure and call the NetBSD
538          * pwrite(2) system call (offset type is 64-bit in NetBSD).
539          */
540         pwa.fd = uap->fd;
541         pwa.buf = uap->buf;
542         pwa.nbyte = uap->nbyte;
543         pwa.offset = uap->off;
544
545         return pwrite(td, &pwa);
546 }
547 #endif
548
549 #if defined(NOTYET)
550 int
551 svr4_sys_pwrite64(td, v, retval)
552         register struct thread *td;
553         void *v; 
554         register_t *retval;
555 {
556         struct svr4_sys_pwrite64_args *uap = v;
557         struct sys_pwrite_args pwa;
558
559         /*
560          * Just translate the args structure and call the NetBSD
561          * pwrite(2) system call (offset type is 64-bit in NetBSD).
562          */
563         pwa.fd = uap->fd;
564         pwa.buf = uap->buf;
565         pwa.nbyte = uap->nbyte;
566         pwa.offset = uap->off;
567
568         return (sys_pwrite(td, &pwa, retval));
569 }
570 #endif /* NOTYET */
571
572 int
573 svr4_sys_fcntl(td, uap)
574         register struct thread *td;
575         struct svr4_sys_fcntl_args *uap;
576 {
577         int cmd, error, *retval;
578
579         retval = td->td_retval;
580
581         cmd = svr4_to_bsd_cmd(uap->cmd);
582
583         switch (cmd) {
584         case F_DUPFD:
585         case F_GETFD:
586         case F_SETFD:
587                 return (kern_fcntl(td, uap->fd, cmd, (intptr_t)uap->arg));
588
589         case F_GETFL:
590                 error = kern_fcntl(td, uap->fd, cmd, (intptr_t)uap->arg);
591                 if (error)
592                         return (error);
593                 *retval = bsd_to_svr4_flags(*retval);
594                 return (error);
595
596         case F_SETFL:
597                 {
598                         /*
599                          * we must save the O_ASYNC flag, as that is
600                          * handled by ioctl(_, I_SETSIG, _) emulation.
601                          */
602                         int flags;
603
604                         DPRINTF(("Setting flags %p\n", uap->arg));
605
606                         error = kern_fcntl(td, uap->fd, F_GETFL, 0);
607                         if (error)
608                                 return (error);
609                         flags = *retval;
610                         flags &= O_ASYNC;
611                         flags |= svr4_to_bsd_flags((u_long) uap->arg);
612                         return (kern_fcntl(td, uap->fd, F_SETFL, flags));
613                 }
614
615         case F_GETLK:
616         case F_SETLK:
617         case F_SETLKW:
618                 {
619                         struct svr4_flock       ifl;
620                         struct flock            fl;
621
622                         error = copyin(uap->arg, &ifl, sizeof (ifl));
623                         if (error)
624                                 return (error);
625
626                         svr4_to_bsd_flock(&ifl, &fl);
627
628                         error = kern_fcntl(td, uap->fd, cmd, (intptr_t)&fl);
629                         if (error || cmd != F_GETLK)
630                                 return (error);
631
632                         bsd_to_svr4_flock(&fl, &ifl);
633
634                         return (copyout(&ifl, uap->arg, sizeof (ifl)));
635                 }
636         case -1:
637                 switch (uap->cmd) {
638                 case SVR4_F_DUP2FD:
639                         {
640                                 struct dup2_args du;
641
642                                 du.from = uap->fd;
643                                 du.to = (int)uap->arg;
644                                 error = dup2(td, &du);
645                                 if (error)
646                                         return error;
647                                 *retval = du.to;
648                                 return 0;
649                         }
650
651                 case SVR4_F_FREESP:
652                         {
653                                 struct svr4_flock        ifl;
654                                 struct flock             fl;
655
656                                 error = copyin(uap->arg, &ifl,
657                                     sizeof ifl);
658                                 if (error)
659                                         return error;
660                                 svr4_to_bsd_flock(&ifl, &fl);
661                                 return fd_truncate(td, uap->fd, &fl);
662                         }
663
664                 case SVR4_F_GETLK64:
665                 case SVR4_F_SETLK64:
666                 case SVR4_F_SETLKW64:
667                         {
668                                 struct svr4_flock64     ifl;
669                                 struct flock            fl;
670
671                                 switch (uap->cmd) {
672                                 case SVR4_F_GETLK64:
673                                         cmd = F_GETLK;
674                                         break;
675                                 case SVR4_F_SETLK64:
676                                         cmd = F_SETLK;
677                                         break;                                  
678                                 case SVR4_F_SETLKW64:
679                                         cmd = F_SETLKW;
680                                         break;
681                                 }
682                                 error = copyin(uap->arg, &ifl,
683                                     sizeof (ifl));
684                                 if (error)
685                                         return (error);
686
687                                 svr4_to_bsd_flock64(&ifl, &fl);
688
689                                 error = kern_fcntl(td, uap->fd, cmd,
690                                     (intptr_t)&fl);
691                                 if (error || cmd != F_GETLK)
692                                         return (error);
693
694                                 bsd_to_svr4_flock64(&fl, &ifl);
695
696                                 return (copyout(&ifl, uap->arg,
697                                     sizeof (ifl)));
698                         }
699
700                 case SVR4_F_FREESP64:
701                         {
702                                 struct svr4_flock64      ifl;
703                                 struct flock             fl;
704
705                                 error = copyin(uap->arg, &ifl,
706                                     sizeof ifl);
707                                 if (error)
708                                         return error;
709                                 svr4_to_bsd_flock64(&ifl, &fl);
710                                 return fd_truncate(td, uap->fd, &fl);
711                         }
712
713                 case SVR4_F_REVOKE:
714                         return fd_revoke(td, uap->fd);
715
716                 default:
717                         return ENOSYS;
718                 }
719
720         default:
721                 return ENOSYS;
722         }
723 }