inode.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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/devpts_fs.h>
  20. #include <linux/parser.h>
  21. #include <linux/fsnotify.h>
  22. #define DEVPTS_SUPER_MAGIC 0x1cd1
  23. static struct vfsmount *devpts_mnt;
  24. static struct dentry *devpts_root;
  25. static struct {
  26. int setuid;
  27. int setgid;
  28. uid_t uid;
  29. gid_t gid;
  30. umode_t mode;
  31. } config = {.mode = 0600};
  32. enum {
  33. Opt_uid, Opt_gid, Opt_mode,
  34. Opt_err
  35. };
  36. static match_table_t tokens = {
  37. {Opt_uid, "uid=%u"},
  38. {Opt_gid, "gid=%u"},
  39. {Opt_mode, "mode=%o"},
  40. {Opt_err, NULL}
  41. };
  42. static int devpts_remount(struct super_block *sb, int *flags, char *data)
  43. {
  44. char *p;
  45. config.setuid = 0;
  46. config.setgid = 0;
  47. config.uid = 0;
  48. config.gid = 0;
  49. config.mode = 0600;
  50. while ((p = strsep(&data, ",")) != NULL) {
  51. substring_t args[MAX_OPT_ARGS];
  52. int token;
  53. int option;
  54. if (!*p)
  55. continue;
  56. token = match_token(p, tokens, args);
  57. switch (token) {
  58. case Opt_uid:
  59. if (match_int(&args[0], &option))
  60. return -EINVAL;
  61. config.uid = option;
  62. config.setuid = 1;
  63. break;
  64. case Opt_gid:
  65. if (match_int(&args[0], &option))
  66. return -EINVAL;
  67. config.gid = option;
  68. config.setgid = 1;
  69. break;
  70. case Opt_mode:
  71. if (match_octal(&args[0], &option))
  72. return -EINVAL;
  73. config.mode = option & ~S_IFMT;
  74. break;
  75. default:
  76. printk(KERN_ERR "devpts: called with bogus options\n");
  77. return -EINVAL;
  78. }
  79. }
  80. return 0;
  81. }
  82. static const struct super_operations devpts_sops = {
  83. .statfs = simple_statfs,
  84. .remount_fs = devpts_remount,
  85. };
  86. static int
  87. devpts_fill_super(struct super_block *s, void *data, int silent)
  88. {
  89. struct inode * inode;
  90. s->s_blocksize = 1024;
  91. s->s_blocksize_bits = 10;
  92. s->s_magic = DEVPTS_SUPER_MAGIC;
  93. s->s_op = &devpts_sops;
  94. s->s_time_gran = 1;
  95. inode = new_inode(s);
  96. if (!inode)
  97. goto fail;
  98. inode->i_ino = 1;
  99. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  100. inode->i_blocks = 0;
  101. inode->i_uid = inode->i_gid = 0;
  102. inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR;
  103. inode->i_op = &simple_dir_inode_operations;
  104. inode->i_fop = &simple_dir_operations;
  105. inode->i_nlink = 2;
  106. devpts_root = s->s_root = d_alloc_root(inode);
  107. if (s->s_root)
  108. return 0;
  109. printk("devpts: get root dentry failed\n");
  110. iput(inode);
  111. fail:
  112. return -ENOMEM;
  113. }
  114. static int devpts_get_sb(struct file_system_type *fs_type,
  115. int flags, const char *dev_name, void *data, struct vfsmount *mnt)
  116. {
  117. return get_sb_single(fs_type, flags, data, devpts_fill_super, mnt);
  118. }
  119. static struct file_system_type devpts_fs_type = {
  120. .owner = THIS_MODULE,
  121. .name = "devpts",
  122. .get_sb = devpts_get_sb,
  123. .kill_sb = kill_anon_super,
  124. };
  125. /*
  126. * The normal naming convention is simply /dev/pts/<number>; this conforms
  127. * to the System V naming convention
  128. */
  129. static struct dentry *get_node(int num)
  130. {
  131. char s[12];
  132. struct dentry *root = devpts_root;
  133. mutex_lock(&root->d_inode->i_mutex);
  134. return lookup_one_len(s, root, sprintf(s, "%d", num));
  135. }
  136. int devpts_pty_new(struct tty_struct *tty)
  137. {
  138. int number = tty->index;
  139. struct tty_driver *driver = tty->driver;
  140. dev_t device = MKDEV(driver->major, driver->minor_start+number);
  141. struct dentry *dentry;
  142. struct inode *inode = new_inode(devpts_mnt->mnt_sb);
  143. /* We're supposed to be given the slave end of a pty */
  144. BUG_ON(driver->type != TTY_DRIVER_TYPE_PTY);
  145. BUG_ON(driver->subtype != PTY_TYPE_SLAVE);
  146. if (!inode)
  147. return -ENOMEM;
  148. inode->i_ino = number+2;
  149. inode->i_uid = config.setuid ? config.uid : current->fsuid;
  150. inode->i_gid = config.setgid ? config.gid : current->fsgid;
  151. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  152. init_special_inode(inode, S_IFCHR|config.mode, device);
  153. inode->i_private = tty;
  154. dentry = get_node(number);
  155. if (!IS_ERR(dentry) && !dentry->d_inode) {
  156. d_instantiate(dentry, inode);
  157. fsnotify_create(devpts_root->d_inode, dentry);
  158. }
  159. mutex_unlock(&devpts_root->d_inode->i_mutex);
  160. return 0;
  161. }
  162. struct tty_struct *devpts_get_tty(int number)
  163. {
  164. struct dentry *dentry = get_node(number);
  165. struct tty_struct *tty;
  166. tty = NULL;
  167. if (!IS_ERR(dentry)) {
  168. if (dentry->d_inode)
  169. tty = dentry->d_inode->i_private;
  170. dput(dentry);
  171. }
  172. mutex_unlock(&devpts_root->d_inode->i_mutex);
  173. return tty;
  174. }
  175. void devpts_pty_kill(int number)
  176. {
  177. struct dentry *dentry = get_node(number);
  178. if (!IS_ERR(dentry)) {
  179. struct inode *inode = dentry->d_inode;
  180. if (inode) {
  181. inode->i_nlink--;
  182. d_delete(dentry);
  183. dput(dentry);
  184. }
  185. dput(dentry);
  186. }
  187. mutex_unlock(&devpts_root->d_inode->i_mutex);
  188. }
  189. static int __init init_devpts_fs(void)
  190. {
  191. int err = register_filesystem(&devpts_fs_type);
  192. if (!err) {
  193. devpts_mnt = kern_mount(&devpts_fs_type);
  194. if (IS_ERR(devpts_mnt))
  195. err = PTR_ERR(devpts_mnt);
  196. }
  197. return err;
  198. }
  199. static void __exit exit_devpts_fs(void)
  200. {
  201. unregister_filesystem(&devpts_fs_type);
  202. mntput(devpts_mnt);
  203. }
  204. module_init(init_devpts_fs)
  205. module_exit(exit_devpts_fs)
  206. MODULE_LICENSE("GPL");