inode.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 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_blksize = 1024;
  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 struct super_block *devpts_get_sb(struct file_system_type *fs_type,
  115. int flags, const char *dev_name, void *data)
  116. {
  117. return get_sb_single(fs_type, flags, data, devpts_fill_super);
  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_blksize = 1024;
  150. inode->i_uid = config.setuid ? config.uid : current->fsuid;
  151. inode->i_gid = config.setgid ? config.gid : current->fsgid;
  152. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  153. init_special_inode(inode, S_IFCHR|config.mode, device);
  154. inode->u.generic_ip = tty;
  155. dentry = get_node(number);
  156. if (!IS_ERR(dentry) && !dentry->d_inode)
  157. d_instantiate(dentry, inode);
  158. mutex_unlock(&devpts_root->d_inode->i_mutex);
  159. return 0;
  160. }
  161. struct tty_struct *devpts_get_tty(int number)
  162. {
  163. struct dentry *dentry = get_node(number);
  164. struct tty_struct *tty;
  165. tty = NULL;
  166. if (!IS_ERR(dentry)) {
  167. if (dentry->d_inode)
  168. tty = dentry->d_inode->u.generic_ip;
  169. dput(dentry);
  170. }
  171. mutex_unlock(&devpts_root->d_inode->i_mutex);
  172. return tty;
  173. }
  174. void devpts_pty_kill(int number)
  175. {
  176. struct dentry *dentry = get_node(number);
  177. if (!IS_ERR(dentry)) {
  178. struct inode *inode = dentry->d_inode;
  179. if (inode) {
  180. inode->i_nlink--;
  181. d_delete(dentry);
  182. dput(dentry);
  183. }
  184. dput(dentry);
  185. }
  186. mutex_unlock(&devpts_root->d_inode->i_mutex);
  187. }
  188. static int __init init_devpts_fs(void)
  189. {
  190. int err = register_filesystem(&devpts_fs_type);
  191. if (!err) {
  192. devpts_mnt = kern_mount(&devpts_fs_type);
  193. if (IS_ERR(devpts_mnt))
  194. err = PTR_ERR(devpts_mnt);
  195. }
  196. return err;
  197. }
  198. static void __exit exit_devpts_fs(void)
  199. {
  200. unregister_filesystem(&devpts_fs_type);
  201. mntput(devpts_mnt);
  202. }
  203. module_init(init_devpts_fs)
  204. module_exit(exit_devpts_fs)
  205. MODULE_LICENSE("GPL");