linux-bk/fs/ioctl.c
<<
>>
Prefs
   1/*
   2 *  linux/fs/ioctl.c
   3 *
   4 *  Copyright (C) 1991, 1992  Linus Torvalds
   5 */
   6
   7#include <linux/mm.h>
   8#include <linux/smp_lock.h>
   9#include <linux/file.h>
  10#include <linux/fs.h>
  11#include <linux/security.h>
  12
  13#include <asm/uaccess.h>
  14#include <asm/ioctls.h>
  15
  16static int file_ioctl(struct file *filp,unsigned int cmd,unsigned long arg)
  17{
  18        int error;
  19        int block;
  20        struct inode * inode = filp->f_dentry->d_inode;
  21
  22        switch (cmd) {
  23                case FIBMAP:
  24                {
  25                        struct address_space *mapping = inode->i_mapping;
  26                        int res;
  27                        /* do we support this mess? */
  28                        if (!mapping->a_ops->bmap)
  29                                return -EINVAL;
  30                        if (!capable(CAP_SYS_RAWIO))
  31                                return -EPERM;
  32                        if ((error = get_user(block, (int *) arg)) != 0)
  33                                return error;
  34
  35                        res = mapping->a_ops->bmap(mapping, block);
  36                        return put_user(res, (int *) arg);
  37                }
  38                case FIGETBSZ:
  39                        if (inode->i_sb == NULL)
  40                                return -EBADF;
  41                        return put_user(inode->i_sb->s_blocksize, (int *) arg);
  42                case FIONREAD:
  43                        return put_user(i_size_read(inode) - filp->f_pos, (int *) arg);
  44        }
  45        if (filp->f_op && filp->f_op->ioctl)
  46                return filp->f_op->ioctl(inode, filp, cmd, arg);
  47        return -ENOTTY;
  48}
  49
  50
  51asmlinkage long sys_ioctl(unsigned int fd, unsigned int cmd, unsigned long arg)
  52{       
  53        struct file * filp;
  54        unsigned int flag;
  55        int on, error = -EBADF;
  56
  57        filp = fget(fd);
  58        if (!filp)
  59                goto out;
  60
  61        error = security_file_ioctl(filp, cmd, arg);
  62        if (error) {
  63                fput(filp);
  64                goto out;
  65        }
  66
  67        lock_kernel();
  68        switch (cmd) {
  69                case FIOCLEX:
  70                        set_close_on_exec(fd, 1);
  71                        break;
  72
  73                case FIONCLEX:
  74                        set_close_on_exec(fd, 0);
  75                        break;
  76
  77                case FIONBIO:
  78                        if ((error = get_user(on, (int __user *)arg)) != 0)
  79                                break;
  80                        flag = O_NONBLOCK;
  81#ifdef __sparc__
  82                        /* SunOS compatibility item. */
  83                        if(O_NONBLOCK != O_NDELAY)
  84                                flag |= O_NDELAY;
  85#endif
  86                        if (on)
  87                                filp->f_flags |= flag;
  88                        else
  89                                filp->f_flags &= ~flag;
  90                        break;
  91
  92                case FIOASYNC:
  93                        if ((error = get_user(on, (int __user *)arg)) != 0)
  94                                break;
  95                        flag = on ? FASYNC : 0;
  96
  97                        /* Did FASYNC state change ? */
  98                        if ((flag ^ filp->f_flags) & FASYNC) {
  99                                if (filp->f_op && filp->f_op->fasync)
 100                                        error = filp->f_op->fasync(fd, filp, on);
 101                                else error = -ENOTTY;
 102                        }
 103                        if (error != 0)
 104                                break;
 105
 106                        if (on)
 107                                filp->f_flags |= FASYNC;
 108                        else
 109                                filp->f_flags &= ~FASYNC;
 110                        break;
 111
 112                case FIOQSIZE:
 113                        if (S_ISDIR(filp->f_dentry->d_inode->i_mode) ||
 114                            S_ISREG(filp->f_dentry->d_inode->i_mode) ||
 115                            S_ISLNK(filp->f_dentry->d_inode->i_mode)) {
 116                                loff_t res = inode_get_bytes(filp->f_dentry->d_inode);
 117                                error = copy_to_user((loff_t __user *)arg, &res, sizeof(res)) ? -EFAULT : 0;
 118                        }
 119                        else
 120                                error = -ENOTTY;
 121                        break;
 122                default:
 123                        error = -ENOTTY;
 124                        if (S_ISREG(filp->f_dentry->d_inode->i_mode))
 125                                error = file_ioctl(filp, cmd, arg);
 126                        else if (filp->f_op && filp->f_op->ioctl)
 127                                error = filp->f_op->ioctl(filp->f_dentry->d_inode, filp, cmd, arg);
 128        }
 129        unlock_kernel();
 130        fput(filp);
 131
 132out:
 133        return error;
 134}
 135
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.