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 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. static struct dentry *get_node(int num)
  148. {
  149. char s[12];
  150. struct dentry *root = devpts_root;
  151. mutex_lock(&root->d_inode->i_mutex);
  152. return lookup_one_len(s, root, sprintf(s, "%d", num));
  153. }
  154. int devpts_new_index(struct inode *ptmx_inode)
  155. {
  156. int index;
  157. int ida_ret;
  158. retry:
  159. if (!ida_pre_get(&allocated_ptys, GFP_KERNEL)) {
  160. return -ENOMEM;
  161. }
  162. mutex_lock(&allocated_ptys_lock);
  163. ida_ret = ida_get_new(&allocated_ptys, &index);
  164. if (ida_ret < 0) {
  165. mutex_unlock(&allocated_ptys_lock);
  166. if (ida_ret == -EAGAIN)
  167. goto retry;
  168. return -EIO;
  169. }
  170. if (index >= pty_limit) {
  171. ida_remove(&allocated_ptys, index);
  172. mutex_unlock(&allocated_ptys_lock);
  173. return -EIO;
  174. }
  175. mutex_unlock(&allocated_ptys_lock);
  176. return index;
  177. }
  178. void devpts_kill_index(struct inode *ptmx_inode, int idx)
  179. {
  180. mutex_lock(&allocated_ptys_lock);
  181. ida_remove(&allocated_ptys, idx);
  182. mutex_unlock(&allocated_ptys_lock);
  183. }
  184. int devpts_pty_new(struct inode *ptmx_inode, struct tty_struct *tty)
  185. {
  186. int number = tty->index; /* tty layer puts index from devpts_new_index() in here */
  187. struct tty_driver *driver = tty->driver;
  188. dev_t device = MKDEV(driver->major, driver->minor_start+number);
  189. struct dentry *dentry;
  190. struct inode *inode = new_inode(devpts_mnt->mnt_sb);
  191. /* We're supposed to be given the slave end of a pty */
  192. BUG_ON(driver->type != TTY_DRIVER_TYPE_PTY);
  193. BUG_ON(driver->subtype != PTY_TYPE_SLAVE);
  194. if (!inode)
  195. return -ENOMEM;
  196. inode->i_ino = number+2;
  197. inode->i_uid = config.setuid ? config.uid : current->fsuid;
  198. inode->i_gid = config.setgid ? config.gid : current->fsgid;
  199. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  200. init_special_inode(inode, S_IFCHR|config.mode, device);
  201. inode->i_private = tty;
  202. dentry = get_node(number);
  203. if (!IS_ERR(dentry) && !dentry->d_inode) {
  204. d_instantiate(dentry, inode);
  205. fsnotify_create(devpts_root->d_inode, dentry);
  206. }
  207. mutex_unlock(&devpts_root->d_inode->i_mutex);
  208. return 0;
  209. }
  210. struct tty_struct *devpts_get_tty(struct inode *pts_inode, int number)
  211. {
  212. BUG_ON(pts_inode->i_rdev == MKDEV(TTYAUX_MAJOR, PTMX_MINOR));
  213. if (pts_inode->i_sb->s_magic == DEVPTS_SUPER_MAGIC)
  214. return (struct tty_struct *)pts_inode->i_private;
  215. return NULL;
  216. }
  217. void devpts_pty_kill(struct tty_struct *tty)
  218. {
  219. int number = tty->index;
  220. struct dentry *dentry = get_node(number);
  221. if (!IS_ERR(dentry)) {
  222. struct inode *inode = dentry->d_inode;
  223. if (inode) {
  224. inode->i_nlink--;
  225. d_delete(dentry);
  226. dput(dentry);
  227. }
  228. dput(dentry);
  229. }
  230. mutex_unlock(&devpts_root->d_inode->i_mutex);
  231. }
  232. static int __init init_devpts_fs(void)
  233. {
  234. int err = register_filesystem(&devpts_fs_type);
  235. if (!err) {
  236. devpts_mnt = kern_mount(&devpts_fs_type);
  237. if (IS_ERR(devpts_mnt))
  238. err = PTR_ERR(devpts_mnt);
  239. }
  240. return err;
  241. }
  242. static void __exit exit_devpts_fs(void)
  243. {
  244. unregister_filesystem(&devpts_fs_type);
  245. mntput(devpts_mnt);
  246. }
  247. module_init(init_devpts_fs)
  248. module_exit(exit_devpts_fs)
  249. MODULE_LICENSE("GPL");