ioctl.c 4.1 KB

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