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