inode.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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/xattr.h>
  21. #define DEVPTS_SUPER_MAGIC 0x1cd1
  22. extern struct xattr_handler devpts_xattr_security_handler;
  23. static struct xattr_handler *devpts_xattr_handlers[] = {
  24. #ifdef CONFIG_DEVPTS_FS_SECURITY
  25. &devpts_xattr_security_handler,
  26. #endif
  27. NULL
  28. };
  29. static struct inode_operations devpts_file_inode_operations = {
  30. #ifdef CONFIG_DEVPTS_FS_XATTR
  31. .setxattr = generic_setxattr,
  32. .getxattr = generic_getxattr,
  33. .listxattr = generic_listxattr,
  34. .removexattr = generic_removexattr,
  35. #endif
  36. };
  37. static struct vfsmount *devpts_mnt;
  38. static struct dentry *devpts_root;
  39. static struct {
  40. int setuid;
  41. int setgid;
  42. uid_t uid;
  43. gid_t gid;
  44. umode_t mode;
  45. } config = {.mode = 0600};
  46. static int devpts_remount(struct super_block *sb, int *flags, char *data)
  47. {
  48. int setuid = 0;
  49. int setgid = 0;
  50. uid_t uid = 0;
  51. gid_t gid = 0;
  52. umode_t mode = 0600;
  53. char *this_char;
  54. this_char = NULL;
  55. while ((this_char = strsep(&data, ",")) != NULL) {
  56. int n;
  57. char dummy;
  58. if (!*this_char)
  59. continue;
  60. if (sscanf(this_char, "uid=%i%c", &n, &dummy) == 1) {
  61. setuid = 1;
  62. uid = n;
  63. } else if (sscanf(this_char, "gid=%i%c", &n, &dummy) == 1) {
  64. setgid = 1;
  65. gid = n;
  66. } else if (sscanf(this_char, "mode=%o%c", &n, &dummy) == 1)
  67. mode = n & ~S_IFMT;
  68. else {
  69. printk("devpts: called with bogus options\n");
  70. return -EINVAL;
  71. }
  72. }
  73. config.setuid = setuid;
  74. config.setgid = setgid;
  75. config.uid = uid;
  76. config.gid = gid;
  77. config.mode = mode;
  78. return 0;
  79. }
  80. static struct super_operations devpts_sops = {
  81. .statfs = simple_statfs,
  82. .remount_fs = devpts_remount,
  83. };
  84. static int
  85. devpts_fill_super(struct super_block *s, void *data, int silent)
  86. {
  87. struct inode * inode;
  88. s->s_blocksize = 1024;
  89. s->s_blocksize_bits = 10;
  90. s->s_magic = DEVPTS_SUPER_MAGIC;
  91. s->s_op = &devpts_sops;
  92. s->s_xattr = devpts_xattr_handlers;
  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. down(&root->d_inode->i_sem);
  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->i_op = &devpts_file_inode_operations;
  155. inode->u.generic_ip = tty;
  156. dentry = get_node(number);
  157. if (!IS_ERR(dentry) && !dentry->d_inode)
  158. d_instantiate(dentry, inode);
  159. up(&devpts_root->d_inode->i_sem);
  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->u.generic_ip;
  170. dput(dentry);
  171. }
  172. up(&devpts_root->d_inode->i_sem);
  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. up(&devpts_root->d_inode->i_sem);
  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");