inode.c 5.0 KB

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