inode.c 6.6 KB

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