The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/i386/ibcs2/ibcs2_fcntl.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * Copyright (c) 1995 Scott Bartram
    3  * All rights reserved.
    4  *
    5  * Redistribution and use in source and binary forms, with or without
    6  * modification, are permitted provided that the following conditions
    7  * are met:
    8  * 1. Redistributions of source code must retain the above copyright
    9  *    notice, this list of conditions and the following disclaimer.
   10  * 2. Redistributions in binary form must reproduce the above copyright
   11  *    notice, this list of conditions and the following disclaimer in the
   12  *    documentation and/or other materials provided with the distribution.
   13  * 3. The name of the author may not be used to endorse or promote products
   14  *    derived from this software without specific prior written permission
   15  *
   16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
   17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
   19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
   20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
   21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
   22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
   23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
   25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   26  */
   27 
   28 #include <sys/cdefs.h>
   29 __FBSDID("$FreeBSD: releng/6.2/sys/i386/ibcs2/ibcs2_fcntl.c 141488 2005-02-07 22:02:18Z jhb $");
   30 
   31 #include "opt_spx_hack.h"
   32 
   33 #include <sys/param.h>
   34 #include <sys/systm.h>
   35 #include <sys/fcntl.h>
   36 #include <sys/file.h>
   37 #include <sys/filedesc.h>
   38 #include <sys/lock.h>
   39 #include <sys/malloc.h>
   40 #include <sys/mutex.h>
   41 #include <sys/syscallsubr.h>
   42 #include <sys/sysproto.h>
   43 #include <sys/ttycom.h>
   44 
   45 #include <i386/ibcs2/ibcs2_fcntl.h>
   46 #include <i386/ibcs2/ibcs2_signal.h>
   47 #include <i386/ibcs2/ibcs2_proto.h>
   48 #include <i386/ibcs2/ibcs2_util.h>
   49 
   50 static void cvt_iflock2flock(struct ibcs2_flock *, struct flock *);
   51 static void cvt_flock2iflock(struct flock *, struct ibcs2_flock *);
   52 static int  cvt_o_flags(int);
   53 static int  oflags2ioflags(int);
   54 static int  ioflags2oflags(int);
   55 
   56 static int
   57 cvt_o_flags(flags)
   58         int flags;
   59 {
   60         int r = 0;
   61 
   62         /* convert mode into NetBSD mode */
   63         if (flags & IBCS2_O_WRONLY) r |= O_WRONLY;
   64         if (flags & IBCS2_O_RDWR)   r |= O_RDWR;
   65         if (flags & (IBCS2_O_NDELAY | IBCS2_O_NONBLOCK)) r |= O_NONBLOCK;
   66         if (flags & IBCS2_O_APPEND) r |= O_APPEND;
   67         if (flags & IBCS2_O_SYNC)   r |= O_FSYNC;
   68         if (flags & IBCS2_O_CREAT)  r |= O_CREAT;
   69         if (flags & IBCS2_O_TRUNC)  r |= O_TRUNC /* | O_CREAT ??? */;
   70         if (flags & IBCS2_O_EXCL)   r |= O_EXCL;
   71         if (flags & IBCS2_O_RDONLY) r |= O_RDONLY;
   72         if (flags & IBCS2_O_PRIV)   r |= O_EXLOCK;
   73         if (flags & IBCS2_O_NOCTTY) r |= O_NOCTTY;
   74         return r;
   75 }
   76 
   77 static void
   78 cvt_flock2iflock(flp, iflp)
   79         struct flock *flp;
   80         struct ibcs2_flock *iflp;
   81 {
   82         switch (flp->l_type) {
   83         case F_RDLCK:
   84                 iflp->l_type = IBCS2_F_RDLCK;
   85                 break;
   86         case F_WRLCK:
   87                 iflp->l_type = IBCS2_F_WRLCK;
   88                 break;
   89         case F_UNLCK:
   90                 iflp->l_type = IBCS2_F_UNLCK;
   91                 break;
   92         }
   93         iflp->l_whence = (short)flp->l_whence;
   94         iflp->l_start = (ibcs2_off_t)flp->l_start;
   95         iflp->l_len = (ibcs2_off_t)flp->l_len;
   96         iflp->l_sysid = 0;
   97         iflp->l_pid = (ibcs2_pid_t)flp->l_pid;
   98 }
   99 
  100 #ifdef DEBUG_IBCS2
  101 static void
  102 print_flock(struct flock *flp)
  103 {
  104   printf("flock: start=%x len=%x pid=%d type=%d whence=%d\n",
  105          (int)flp->l_start, (int)flp->l_len, (int)flp->l_pid,
  106          flp->l_type, flp->l_whence);
  107 }
  108 #endif
  109 
  110 static void
  111 cvt_iflock2flock(iflp, flp)
  112         struct ibcs2_flock *iflp;
  113         struct flock *flp;
  114 {
  115         flp->l_start = (off_t)iflp->l_start;
  116         flp->l_len = (off_t)iflp->l_len;
  117         flp->l_pid = (pid_t)iflp->l_pid;
  118         switch (iflp->l_type) {
  119         case IBCS2_F_RDLCK:
  120                 flp->l_type = F_RDLCK;
  121                 break;
  122         case IBCS2_F_WRLCK:
  123                 flp->l_type = F_WRLCK;
  124                 break;
  125         case IBCS2_F_UNLCK:
  126                 flp->l_type = F_UNLCK;
  127                 break;
  128         }
  129         flp->l_whence = iflp->l_whence;
  130 }
  131 
  132 /* convert iBCS2 mode into NetBSD mode */
  133 static int
  134 ioflags2oflags(flags)
  135         int flags;
  136 {
  137         int r = 0;
  138         
  139         if (flags & IBCS2_O_RDONLY) r |= O_RDONLY;
  140         if (flags & IBCS2_O_WRONLY) r |= O_WRONLY;
  141         if (flags & IBCS2_O_RDWR) r |= O_RDWR;
  142         if (flags & IBCS2_O_NDELAY) r |= O_NONBLOCK;
  143         if (flags & IBCS2_O_APPEND) r |= O_APPEND;
  144         if (flags & IBCS2_O_SYNC) r |= O_FSYNC;
  145         if (flags & IBCS2_O_NONBLOCK) r |= O_NONBLOCK;
  146         if (flags & IBCS2_O_CREAT) r |= O_CREAT;
  147         if (flags & IBCS2_O_TRUNC) r |= O_TRUNC;
  148         if (flags & IBCS2_O_EXCL) r |= O_EXCL;
  149         if (flags & IBCS2_O_NOCTTY) r |= O_NOCTTY;
  150         return r;
  151 }
  152 
  153 /* convert NetBSD mode into iBCS2 mode */
  154 static int
  155 oflags2ioflags(flags)
  156         int flags;
  157 {
  158         int r = 0;
  159         
  160         if (flags & O_RDONLY) r |= IBCS2_O_RDONLY;
  161         if (flags & O_WRONLY) r |= IBCS2_O_WRONLY;
  162         if (flags & O_RDWR) r |= IBCS2_O_RDWR;
  163         if (flags & O_NDELAY) r |= IBCS2_O_NONBLOCK;
  164         if (flags & O_APPEND) r |= IBCS2_O_APPEND;
  165         if (flags & O_FSYNC) r |= IBCS2_O_SYNC;
  166         if (flags & O_NONBLOCK) r |= IBCS2_O_NONBLOCK;
  167         if (flags & O_CREAT) r |= IBCS2_O_CREAT;
  168         if (flags & O_TRUNC) r |= IBCS2_O_TRUNC;
  169         if (flags & O_EXCL) r |= IBCS2_O_EXCL;
  170         if (flags & O_NOCTTY) r |= IBCS2_O_NOCTTY;
  171         return r;
  172 }
  173 
  174 int
  175 ibcs2_open(td, uap)
  176         struct thread *td;
  177         struct ibcs2_open_args *uap;
  178 {
  179         struct proc *p;
  180         char *path;
  181         int flags, noctty, ret;
  182 
  183         p = td->td_proc;
  184         noctty = uap->flags & IBCS2_O_NOCTTY;
  185         flags = cvt_o_flags(uap->flags);
  186         if (uap->flags & O_CREAT)
  187                 CHECKALTCREAT(td, uap->path, &path);
  188         else
  189                 CHECKALTEXIST(td, uap->path, &path);
  190         ret = kern_open(td, path, UIO_SYSSPACE, flags, uap->mode);
  191 
  192 #ifdef SPX_HACK
  193         if (ret == ENXIO) {
  194                 if (!strcmp(path, "/compat/ibcs2/dev/spx"))
  195                         ret = spx_open(td);
  196                 free(path, M_TEMP);
  197         } else
  198 #endif /* SPX_HACK */
  199         free(path, M_TEMP);
  200         PROC_LOCK(p);
  201         if (!ret && !noctty && SESS_LEADER(p) && !(p->p_flag & P_CONTROLT)) {
  202                 struct file *fp;
  203                 int error;
  204 
  205                 error = fget(td, td->td_retval[0], &fp);
  206                 PROC_UNLOCK(p);
  207                 if (error)
  208                         return (EBADF);
  209 
  210                 /* ignore any error, just give it a try */
  211                 if (fp->f_type == DTYPE_VNODE)
  212                         fo_ioctl(fp, TIOCSCTTY, (caddr_t) 0, td->td_ucred,
  213                             td);
  214                 fdrop(fp, td);
  215         } else
  216                 PROC_UNLOCK(p);
  217         return ret;
  218 }
  219 
  220 int
  221 ibcs2_creat(td, uap)
  222         struct thread *td;  
  223         struct ibcs2_creat_args *uap;
  224 {
  225         char *path;
  226         int error;
  227 
  228         CHECKALTCREAT(td, uap->path, &path);
  229         error = kern_open(td, path, UIO_SYSSPACE, O_WRONLY | O_CREAT | O_TRUNC,
  230             uap->mode);
  231         free(path, M_TEMP);
  232         return (error);
  233 }       
  234 
  235 int
  236 ibcs2_access(td, uap)
  237         struct thread *td;
  238         struct ibcs2_access_args *uap;
  239 {
  240         char *path;
  241         int error;
  242 
  243         CHECKALTEXIST(td, uap->path, &path);
  244         error = kern_access(td, path, UIO_SYSSPACE, uap->flags);
  245         free(path, M_TEMP);
  246         return (error);
  247 }
  248 
  249 int
  250 ibcs2_fcntl(td, uap)
  251         struct thread *td;
  252         struct ibcs2_fcntl_args *uap;
  253 {
  254         intptr_t arg;
  255         int error;
  256         struct flock fl;
  257         struct ibcs2_flock ifl;
  258 
  259         arg = (intptr_t)uap->arg;
  260         switch(uap->cmd) {
  261         case IBCS2_F_DUPFD:
  262                 return (kern_fcntl(td, uap->fd, F_DUPFD, arg));
  263         case IBCS2_F_GETFD:
  264                 return (kern_fcntl(td, uap->fd, F_GETFD, arg));
  265         case IBCS2_F_SETFD:
  266                 return (kern_fcntl(td, uap->fd, F_SETFD, arg));
  267         case IBCS2_F_GETFL:
  268                 error = kern_fcntl(td, uap->fd, F_GETFL, arg);
  269                 if (error)
  270                         return error;
  271                 td->td_retval[0] = oflags2ioflags(td->td_retval[0]);
  272                 return error;
  273         case IBCS2_F_SETFL:
  274                 return (kern_fcntl(td, uap->fd, F_SETFL,
  275                     ioflags2oflags(arg)));
  276 
  277         case IBCS2_F_GETLK:
  278             {
  279                 error = copyin((caddr_t)uap->arg, (caddr_t)&ifl,
  280                                ibcs2_flock_len);
  281                 if (error)
  282                         return error;
  283                 cvt_iflock2flock(&ifl, &fl);
  284                 error = kern_fcntl(td, uap->fd, F_GETLK, (intptr_t)&fl);
  285                 if (error)
  286                         return error;
  287                 cvt_flock2iflock(&fl, &ifl);
  288                 return copyout((caddr_t)&ifl, (caddr_t)uap->arg,
  289                                ibcs2_flock_len);
  290             }
  291 
  292         case IBCS2_F_SETLK:
  293             {
  294                 error = copyin((caddr_t)uap->arg, (caddr_t)&ifl,
  295                                ibcs2_flock_len);
  296                 if (error)
  297                         return error;
  298                 cvt_iflock2flock(&ifl, &fl);
  299                 return (kern_fcntl(td, uap->fd, F_SETLK, (intptr_t)&fl));
  300             }
  301 
  302         case IBCS2_F_SETLKW:
  303             {
  304                 error = copyin((caddr_t)uap->arg, (caddr_t)&ifl,
  305                                ibcs2_flock_len);
  306                 if (error)
  307                         return error;
  308                 cvt_iflock2flock(&ifl, &fl);
  309                 return (kern_fcntl(td, uap->fd, F_SETLKW, (intptr_t)&fl));
  310             }
  311         }
  312         return ENOSYS;
  313 }

Cache object: cb6618d45ff1a159aaf7b7c72e14b50d


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.