inode.c 5.7 KB

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