root/fs/fcntl.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. dupfd
  2. sys_dup2
  3. sys_dup
  4. sys_fcntl

   1 /*
   2  *  linux/fs/fcntl.c
   3  *
   4  *  Copyright (C) 1991, 1992  Linus Torvalds
   5  */
   6 
   7 #include <asm/segment.h>
   8 
   9 #include <linux/sched.h>
  10 #include <linux/kernel.h>
  11 #include <linux/errno.h>
  12 #include <linux/stat.h>
  13 #include <linux/fcntl.h>
  14 #include <linux/string.h>
  15 
  16 extern int sys_close(int fd);
  17 extern int fcntl_getlk(unsigned int, struct flock *);
  18 extern int fcntl_setlk(unsigned int, unsigned int, struct flock *);
  19 
  20 static int dupfd(unsigned int fd, unsigned int arg)
     /* [previous][next][first][last][top][bottom][index][help] */
  21 {
  22         if (fd >= NR_OPEN || !current->filp[fd])
  23                 return -EBADF;
  24         if (arg >= NR_OPEN)
  25                 return -EINVAL;
  26         while (arg < NR_OPEN)
  27                 if (current->filp[arg])
  28                         arg++;
  29                 else
  30                         break;
  31         if (arg >= NR_OPEN)
  32                 return -EMFILE;
  33         FD_CLR(arg, &current->close_on_exec);
  34         (current->filp[arg] = current->filp[fd])->f_count++;
  35         return arg;
  36 }
  37 
  38 int sys_dup2(unsigned int oldfd, unsigned int newfd)
     /* [previous][next][first][last][top][bottom][index][help] */
  39 {
  40         if (oldfd >= NR_OPEN || !current->filp[oldfd])
  41                 return -EBADF;
  42         if (newfd == oldfd)
  43                 return newfd;
  44         /*
  45          * errno's for dup2() are slightly different than for fcntl(F_DUPFD)
  46          * for historical reasons.
  47          */
  48         if (newfd > NR_OPEN)    /* historical botch - should have been >= */
  49                 return -EBADF;  /* dupfd() would return -EINVAL */
  50 #if 1
  51         if (newfd == NR_OPEN)
  52                 return -EBADF;  /* dupfd() does return -EINVAL and that may
  53                                  * even be the standard!  But that is too
  54                                  * weird for now.
  55                                  */
  56 #endif
  57         sys_close(newfd);
  58         return dupfd(oldfd,newfd);
  59 }
  60 
  61 int sys_dup(unsigned int fildes)
     /* [previous][next][first][last][top][bottom][index][help] */
  62 {
  63         return dupfd(fildes,0);
  64 }
  65 
  66 int sys_fcntl(unsigned int fd, unsigned int cmd, unsigned long arg)
     /* [previous][next][first][last][top][bottom][index][help] */
  67 {       
  68         struct file * filp;
  69         extern int sock_fcntl (struct file *, unsigned int cmd,
  70                                unsigned long arg);
  71         if (fd >= NR_OPEN || !(filp = current->filp[fd]))
  72                 return -EBADF;
  73         switch (cmd) {
  74                 case F_DUPFD:
  75                         return dupfd(fd,arg);
  76                 case F_GETFD:
  77                         return FD_ISSET(fd, &current->close_on_exec);
  78                 case F_SETFD:
  79                         if (arg&1)
  80                                 FD_SET(fd, &current->close_on_exec);
  81                         else
  82                                 FD_CLR(fd, &current->close_on_exec);
  83                         return 0;
  84                 case F_GETFL:
  85                         return filp->f_flags;
  86                 case F_SETFL:
  87                         filp->f_flags &= ~(O_APPEND | O_NONBLOCK);
  88                         filp->f_flags |= arg & (O_APPEND | O_NONBLOCK);
  89                         return 0;
  90                 case F_GETLK:
  91                         return fcntl_getlk(fd, (struct flock *) arg);
  92                 case F_SETLK:
  93                         return fcntl_setlk(fd, cmd, (struct flock *) arg);
  94                 case F_SETLKW:
  95                         return fcntl_setlk(fd, cmd, (struct flock *) arg);
  96                 default:
  97                         /* sockets need a few special fcntls. */
  98                         if (S_ISSOCK (filp->f_inode->i_mode))
  99                           {
 100                              return (sock_fcntl (filp, cmd, arg));
 101                           }
 102                         return -EINVAL;
 103         }
 104 }

/* [previous][next][first][last][top][bottom][index][help] */