ioctl.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * linux/fs/ioctl.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. */
  6. #include <linux/syscalls.h>
  7. #include <linux/mm.h>
  8. #include <linux/smp_lock.h>
  9. #include <linux/capability.h>
  10. #include <linux/file.h>
  11. #include <linux/fs.h>
  12. #include <linux/security.h>
  13. #include <linux/module.h>
  14. #include <asm/uaccess.h>
  15. #include <asm/ioctls.h>
  16. static long do_ioctl(struct file *filp, unsigned int cmd,
  17. unsigned long arg)
  18. {
  19. int error = -ENOTTY;
  20. if (!filp->f_op)
  21. goto out;
  22. if (filp->f_op->unlocked_ioctl) {
  23. error = filp->f_op->unlocked_ioctl(filp, cmd, arg);
  24. if (error == -ENOIOCTLCMD)
  25. error = -EINVAL;
  26. goto out;
  27. } else if (filp->f_op->ioctl) {
  28. lock_kernel();
  29. error = filp->f_op->ioctl(filp->f_path.dentry->d_inode,
  30. filp, cmd, arg);
  31. unlock_kernel();
  32. }
  33. out:
  34. return error;
  35. }
  36. static int file_ioctl(struct file *filp, unsigned int cmd,
  37. unsigned long arg)
  38. {
  39. int error;
  40. int block;
  41. struct inode * inode = filp->f_path.dentry->d_inode;
  42. int __user *p = (int __user *)arg;
  43. switch (cmd) {
  44. case FIBMAP:
  45. {
  46. struct address_space *mapping = filp->f_mapping;
  47. int res;
  48. /* do we support this mess? */
  49. if (!mapping->a_ops->bmap)
  50. return -EINVAL;
  51. if (!capable(CAP_SYS_RAWIO))
  52. return -EPERM;
  53. if ((error = get_user(block, p)) != 0)
  54. return error;
  55. lock_kernel();
  56. res = mapping->a_ops->bmap(mapping, block);
  57. unlock_kernel();
  58. return put_user(res, p);
  59. }
  60. case FIGETBSZ:
  61. return put_user(inode->i_sb->s_blocksize, p);
  62. case FIONREAD:
  63. return put_user(i_size_read(inode) - filp->f_pos, p);
  64. }
  65. return do_ioctl(filp, cmd, arg);
  66. }
  67. /*
  68. * When you add any new common ioctls to the switches above and below
  69. * please update compat_sys_ioctl() too.
  70. *
  71. * vfs_ioctl() is not for drivers and not intended to be EXPORT_SYMBOL()'d.
  72. * It's just a simple helper for sys_ioctl and compat_sys_ioctl.
  73. */
  74. int vfs_ioctl(struct file *filp, unsigned int fd, unsigned int cmd, unsigned long arg)
  75. {
  76. unsigned int flag;
  77. int on, error = 0;
  78. switch (cmd) {
  79. case FIOCLEX:
  80. set_close_on_exec(fd, 1);
  81. break;
  82. case FIONCLEX:
  83. set_close_on_exec(fd, 0);
  84. break;
  85. case FIONBIO:
  86. if ((error = get_user(on, (int __user *)arg)) != 0)
  87. break;
  88. flag = O_NONBLOCK;
  89. #ifdef __sparc__
  90. /* SunOS compatibility item. */
  91. if(O_NONBLOCK != O_NDELAY)
  92. flag |= O_NDELAY;
  93. #endif
  94. if (on)
  95. filp->f_flags |= flag;
  96. else
  97. filp->f_flags &= ~flag;
  98. break;
  99. case FIOASYNC:
  100. if ((error = get_user(on, (int __user *)arg)) != 0)
  101. break;
  102. flag = on ? FASYNC : 0;
  103. /* Did FASYNC state change ? */
  104. if ((flag ^ filp->f_flags) & FASYNC) {
  105. if (filp->f_op && filp->f_op->fasync) {
  106. lock_kernel();
  107. error = filp->f_op->fasync(fd, filp, on);
  108. unlock_kernel();
  109. }
  110. else error = -ENOTTY;
  111. }
  112. if (error != 0)
  113. break;
  114. if (on)
  115. filp->f_flags |= FASYNC;
  116. else
  117. filp->f_flags &= ~FASYNC;
  118. break;
  119. case FIOQSIZE:
  120. if (S_ISDIR(filp->f_path.dentry->d_inode->i_mode) ||
  121. S_ISREG(filp->f_path.dentry->d_inode->i_mode) ||
  122. S_ISLNK(filp->f_path.dentry->d_inode->i_mode)) {
  123. loff_t res = inode_get_bytes(filp->f_path.dentry->d_inode);
  124. error = copy_to_user((loff_t __user *)arg, &res, sizeof(res)) ? -EFAULT : 0;
  125. }
  126. else
  127. error = -ENOTTY;
  128. break;
  129. default:
  130. if (S_ISREG(filp->f_path.dentry->d_inode->i_mode))
  131. error = file_ioctl(filp, cmd, arg);
  132. else
  133. error = do_ioctl(filp, cmd, arg);
  134. break;
  135. }
  136. return error;
  137. }
  138. asmlinkage long sys_ioctl(unsigned int fd, unsigned int cmd, unsigned long arg)
  139. {
  140. struct file * filp;
  141. int error = -EBADF;
  142. int fput_needed;
  143. filp = fget_light(fd, &fput_needed);
  144. if (!filp)
  145. goto out;
  146. error = security_file_ioctl(filp, cmd, arg);
  147. if (error)
  148. goto out_fput;
  149. error = vfs_ioctl(filp, fd, cmd, arg);
  150. out_fput:
  151. fput_light(filp, fput_needed);
  152. out:
  153. return error;
  154. }