inode.c 5.3 KB

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