inode.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /* -*- linux-c -*- --------------------------------------------------------- *
  2. *
  3. * linux/fs/devpts/inode.c
  4. *
  5. * Copyright 1998-2004 H. Peter Anvin -- All Rights Reserved
  6. *
  7. * This file is part of the Linux kernel and is made available under
  8. * the terms of the GNU General Public License, version 2, or at your
  9. * option, any later version, incorporated herein by reference.
  10. *
  11. * ------------------------------------------------------------------------- */
  12. #include <linux/module.h>
  13. #include <linux/init.h>
  14. #include <linux/fs.h>
  15. #include <linux/sched.h>
  16. #include <linux/namei.h>
  17. #include <linux/mount.h>
  18. #include <linux/tty.h>
  19. #include <linux/mutex.h>
  20. #include <linux/idr.h>
  21. #include <linux/devpts_fs.h>
  22. #include <linux/parser.h>
  23. #include <linux/fsnotify.h>
  24. #include <linux/seq_file.h>
  25. #define DEVPTS_SUPER_MAGIC 0x1cd1
  26. #define DEVPTS_DEFAULT_MODE 0600
  27. #define PTMX_MINOR 2
  28. extern int pty_limit; /* Config limit on Unix98 ptys */
  29. static DEFINE_IDA(allocated_ptys);
  30. static DEFINE_MUTEX(allocated_ptys_lock);
  31. static struct vfsmount *devpts_mnt;
  32. static struct dentry *devpts_root;
  33. static struct {
  34. int setuid;
  35. int setgid;
  36. uid_t uid;
  37. gid_t gid;
  38. umode_t mode;
  39. } config = {.mode = DEVPTS_DEFAULT_MODE};
  40. enum {
  41. Opt_uid, Opt_gid, Opt_mode,
  42. Opt_err
  43. };
  44. static const match_table_t tokens = {
  45. {Opt_uid, "uid=%u"},
  46. {Opt_gid, "gid=%u"},
  47. {Opt_mode, "mode=%o"},
  48. {Opt_err, NULL}
  49. };
  50. static int devpts_remount(struct super_block *sb, int *flags, char *data)
  51. {
  52. char *p;
  53. config.setuid = 0;
  54. config.setgid = 0;
  55. config.uid = 0;
  56. config.gid = 0;
  57. config.mode = DEVPTS_DEFAULT_MODE;
  58. while ((p = strsep(&data, ",")) != NULL) {
  59. substring_t args[MAX_OPT_ARGS];
  60. int token;
  61. int option;
  62. if (!*p)
  63. continue;
  64. token = match_token(p, tokens, args);
  65. switch (token) {
  66. case Opt_uid:
  67. if (match_int(&args[0], &option))
  68. return -EINVAL;
  69. config.uid = option;
  70. config.setuid = 1;
  71. break;
  72. case Opt_gid:
  73. if (match_int(&args[0], &option))
  74. return -EINVAL;
  75. config.gid = option;
  76. config.setgid = 1;
  77. break;
  78. case Opt_mode:
  79. if (match_octal(&args[0], &option))
  80. return -EINVAL;
  81. config.mode = option & S_IALLUGO;
  82. break;
  83. default:
  84. printk(KERN_ERR "devpts: called with bogus options\n");
  85. return -EINVAL;
  86. }
  87. }
  88. return 0;
  89. }
  90. static int devpts_show_options(struct seq_file *seq, struct vfsmount *vfs)
  91. {
  92. if (config.setuid)
  93. seq_printf(seq, ",uid=%u", config.uid);
  94. if (config.setgid)
  95. seq_printf(seq, ",gid=%u", config.gid);
  96. seq_printf(seq, ",mode=%03o", config.mode);
  97. return 0;
  98. }
  99. static const struct super_operations devpts_sops = {
  100. .statfs = simple_statfs,
  101. .remount_fs = devpts_remount,
  102. .show_options = devpts_show_options,
  103. };
  104. static int
  105. devpts_fill_super(struct super_block *s, void *data, int silent)
  106. {
  107. struct inode * inode;
  108. s->s_blocksize = 1024;
  109. s->s_blocksize_bits = 10;
  110. s->s_magic = DEVPTS_SUPER_MAGIC;
  111. s->s_op = &devpts_sops;
  112. s->s_time_gran = 1;
  113. inode = new_inode(s);
  114. if (!inode)
  115. goto fail;
  116. inode->i_ino = 1;
  117. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  118. inode->i_blocks = 0;
  119. inode->i_uid = inode->i_gid = 0;
  120. inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR;
  121. inode->i_op = &simple_dir_inode_operations;
  122. inode->i_fop = &simple_dir_operations;
  123. inode->i_nlink = 2;
  124. devpts_root = s->s_root = d_alloc_root(inode);
  125. if (s->s_root)
  126. return 0;
  127. printk("devpts: get root dentry failed\n");
  128. iput(inode);
  129. fail:
  130. return -ENOMEM;
  131. }
  132. static int devpts_get_sb(struct file_system_type *fs_type,
  133. int flags, const char *dev_name, void *data, struct vfsmount *mnt)
  134. {
  135. return get_sb_single(fs_type, flags, data, devpts_fill_super, mnt);
  136. }
  137. static struct file_system_type devpts_fs_type = {
  138. .owner = THIS_MODULE,
  139. .name = "devpts",
  140. .get_sb = devpts_get_sb,
  141. .kill_sb = kill_anon_super,
  142. };
  143. /*
  144. * The normal naming convention is simply /dev/pts/<number>; this conforms
  145. * to the System V naming convention
  146. */
  147. int devpts_new_index(struct inode *ptmx_inode)
  148. {
  149. int index;
  150. int ida_ret;
  151. retry:
  152. if (!ida_pre_get(&allocated_ptys, GFP_KERNEL)) {
  153. return -ENOMEM;
  154. }
  155. mutex_lock(&allocated_ptys_lock);
  156. ida_ret = ida_get_new(&allocated_ptys, &index);
  157. if (ida_ret < 0) {
  158. mutex_unlock(&allocated_ptys_lock);
  159. if (ida_ret == -EAGAIN)
  160. goto retry;
  161. return -EIO;
  162. }
  163. if (index >= pty_limit) {
  164. ida_remove(&allocated_ptys, index);
  165. mutex_unlock(&allocated_ptys_lock);
  166. return -EIO;
  167. }
  168. mutex_unlock(&allocated_ptys_lock);
  169. return index;
  170. }
  171. void devpts_kill_index(struct inode *ptmx_inode, int idx)
  172. {
  173. mutex_lock(&allocated_ptys_lock);
  174. ida_remove(&allocated_ptys, idx);
  175. mutex_unlock(&allocated_ptys_lock);
  176. }
  177. int devpts_pty_new(struct inode *ptmx_inode, struct tty_struct *tty)
  178. {
  179. int number = tty->index; /* tty layer puts index from devpts_new_index() in here */
  180. struct tty_driver *driver = tty->driver;
  181. dev_t device = MKDEV(driver->major, driver->minor_start+number);
  182. struct dentry *dentry;
  183. struct inode *inode = new_inode(devpts_mnt->mnt_sb);
  184. char s[12];
  185. /* We're supposed to be given the slave end of a pty */
  186. BUG_ON(driver->type != TTY_DRIVER_TYPE_PTY);
  187. BUG_ON(driver->subtype != PTY_TYPE_SLAVE);
  188. if (!inode)
  189. return -ENOMEM;
  190. inode->i_ino = number+2;
  191. inode->i_uid = config.setuid ? config.uid : current->fsuid;
  192. inode->i_gid = config.setgid ? config.gid : current->fsgid;
  193. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  194. init_special_inode(inode, S_IFCHR|config.mode, device);
  195. inode->i_private = tty;
  196. tty->driver_data = inode;
  197. sprintf(s, "%d", number);
  198. mutex_lock(&devpts_root->d_inode->i_mutex);
  199. dentry = d_alloc_name(devpts_root, s);
  200. if (!IS_ERR(dentry)) {
  201. d_add(dentry, inode);
  202. fsnotify_create(devpts_root->d_inode, dentry);
  203. }
  204. mutex_unlock(&devpts_root->d_inode->i_mutex);
  205. return 0;
  206. }
  207. struct tty_struct *devpts_get_tty(struct inode *pts_inode, int number)
  208. {
  209. BUG_ON(pts_inode->i_rdev == MKDEV(TTYAUX_MAJOR, PTMX_MINOR));
  210. if (pts_inode->i_sb->s_magic == DEVPTS_SUPER_MAGIC)
  211. return (struct tty_struct *)pts_inode->i_private;
  212. return NULL;
  213. }
  214. void devpts_pty_kill(struct tty_struct *tty)
  215. {
  216. struct inode *inode = tty->driver_data;
  217. struct dentry *dentry;
  218. BUG_ON(inode->i_rdev == MKDEV(TTYAUX_MAJOR, PTMX_MINOR));
  219. mutex_lock(&devpts_root->d_inode->i_mutex);
  220. dentry = d_find_alias(inode);
  221. if (dentry && !IS_ERR(dentry)) {
  222. inode->i_nlink--;
  223. d_delete(dentry);
  224. dput(dentry);
  225. }
  226. mutex_unlock(&devpts_root->d_inode->i_mutex);
  227. }
  228. static int __init init_devpts_fs(void)
  229. {
  230. int err = register_filesystem(&devpts_fs_type);
  231. if (!err) {
  232. devpts_mnt = kern_mount(&devpts_fs_type);
  233. if (IS_ERR(devpts_mnt))
  234. err = PTR_ERR(devpts_mnt);
  235. }
  236. return err;
  237. }
  238. static void __exit exit_devpts_fs(void)
  239. {
  240. unregister_filesystem(&devpts_fs_type);
  241. mntput(devpts_mnt);
  242. }
  243. module_init(init_devpts_fs)
  244. module_exit(exit_devpts_fs)
  245. MODULE_LICENSE("GPL");