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> |
Sukadev Bhattiprolu | 718a916 | 2008-04-30 00:54:21 -0700 | [diff] [blame] | 20 | #include <linux/mutex.h> |
| 21 | #include <linux/idr.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <linux/devpts_fs.h> |
Domen Puncer | 7a673c6 | 2006-03-23 03:00:59 -0800 | [diff] [blame] | 23 | #include <linux/parser.h> |
Florin Malita | 3972b7f | 2007-05-08 00:24:18 -0700 | [diff] [blame] | 24 | #include <linux/fsnotify.h> |
Miklos Szeredi | b87a267 | 2008-02-08 04:21:41 -0800 | [diff] [blame] | 25 | #include <linux/seq_file.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | |
| 27 | #define DEVPTS_SUPER_MAGIC 0x1cd1 |
| 28 | |
Miklos Szeredi | b87a267 | 2008-02-08 04:21:41 -0800 | [diff] [blame] | 29 | #define DEVPTS_DEFAULT_MODE 0600 |
Sukadev Bhattiprolu | 527b3e4 | 2008-10-13 10:43:08 +0100 | [diff] [blame] | 30 | #define PTMX_MINOR 2 |
Miklos Szeredi | b87a267 | 2008-02-08 04:21:41 -0800 | [diff] [blame] | 31 | |
Sukadev Bhattiprolu | 718a916 | 2008-04-30 00:54:21 -0700 | [diff] [blame] | 32 | extern int pty_limit; /* Config limit on Unix98 ptys */ |
Alexey Dobriyan | 7ee7c12 | 2008-07-26 11:42:16 +0400 | [diff] [blame] | 33 | static DEFINE_IDA(allocated_ptys); |
Sukadev Bhattiprolu | 718a916 | 2008-04-30 00:54:21 -0700 | [diff] [blame] | 34 | static DEFINE_MUTEX(allocated_ptys_lock); |
| 35 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | static struct vfsmount *devpts_mnt; |
| 37 | static struct dentry *devpts_root; |
| 38 | |
| 39 | static struct { |
| 40 | int setuid; |
| 41 | int setgid; |
| 42 | uid_t uid; |
| 43 | gid_t gid; |
| 44 | umode_t mode; |
Miklos Szeredi | b87a267 | 2008-02-08 04:21:41 -0800 | [diff] [blame] | 45 | } config = {.mode = DEVPTS_DEFAULT_MODE}; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | |
Domen Puncer | 7a673c6 | 2006-03-23 03:00:59 -0800 | [diff] [blame] | 47 | enum { |
| 48 | Opt_uid, Opt_gid, Opt_mode, |
| 49 | Opt_err |
| 50 | }; |
| 51 | |
Steven Whitehouse | a447c09 | 2008-10-13 10:46:57 +0100 | [diff] [blame] | 52 | static const match_table_t tokens = { |
Domen Puncer | 7a673c6 | 2006-03-23 03:00:59 -0800 | [diff] [blame] | 53 | {Opt_uid, "uid=%u"}, |
| 54 | {Opt_gid, "gid=%u"}, |
| 55 | {Opt_mode, "mode=%o"}, |
| 56 | {Opt_err, NULL} |
| 57 | }; |
| 58 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | static int devpts_remount(struct super_block *sb, int *flags, char *data) |
| 60 | { |
Domen Puncer | 7a673c6 | 2006-03-23 03:00:59 -0800 | [diff] [blame] | 61 | char *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | |
Domen Puncer | 7a673c6 | 2006-03-23 03:00:59 -0800 | [diff] [blame] | 63 | config.setuid = 0; |
| 64 | config.setgid = 0; |
| 65 | config.uid = 0; |
| 66 | config.gid = 0; |
Miklos Szeredi | b87a267 | 2008-02-08 04:21:41 -0800 | [diff] [blame] | 67 | config.mode = DEVPTS_DEFAULT_MODE; |
Domen Puncer | 7a673c6 | 2006-03-23 03:00:59 -0800 | [diff] [blame] | 68 | |
| 69 | while ((p = strsep(&data, ",")) != NULL) { |
| 70 | substring_t args[MAX_OPT_ARGS]; |
| 71 | int token; |
| 72 | int option; |
| 73 | |
| 74 | if (!*p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | continue; |
Domen Puncer | 7a673c6 | 2006-03-23 03:00:59 -0800 | [diff] [blame] | 76 | |
| 77 | token = match_token(p, tokens, args); |
| 78 | switch (token) { |
| 79 | case Opt_uid: |
| 80 | if (match_int(&args[0], &option)) |
| 81 | return -EINVAL; |
| 82 | config.uid = option; |
| 83 | config.setuid = 1; |
| 84 | break; |
| 85 | case Opt_gid: |
| 86 | if (match_int(&args[0], &option)) |
| 87 | return -EINVAL; |
| 88 | config.gid = option; |
| 89 | config.setgid = 1; |
| 90 | break; |
| 91 | case Opt_mode: |
| 92 | if (match_octal(&args[0], &option)) |
| 93 | return -EINVAL; |
Miklos Szeredi | b87a267 | 2008-02-08 04:21:41 -0800 | [diff] [blame] | 94 | config.mode = option & S_IALLUGO; |
Domen Puncer | 7a673c6 | 2006-03-23 03:00:59 -0800 | [diff] [blame] | 95 | break; |
| 96 | default: |
| 97 | printk(KERN_ERR "devpts: called with bogus options\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | return -EINVAL; |
| 99 | } |
| 100 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | |
| 102 | return 0; |
| 103 | } |
| 104 | |
Miklos Szeredi | b87a267 | 2008-02-08 04:21:41 -0800 | [diff] [blame] | 105 | static int devpts_show_options(struct seq_file *seq, struct vfsmount *vfs) |
| 106 | { |
| 107 | if (config.setuid) |
| 108 | seq_printf(seq, ",uid=%u", config.uid); |
| 109 | if (config.setgid) |
| 110 | seq_printf(seq, ",gid=%u", config.gid); |
| 111 | seq_printf(seq, ",mode=%03o", config.mode); |
| 112 | |
| 113 | return 0; |
| 114 | } |
| 115 | |
Josef 'Jeff' Sipek | ee9b6d6 | 2007-02-12 00:55:41 -0800 | [diff] [blame] | 116 | static const struct super_operations devpts_sops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | .statfs = simple_statfs, |
| 118 | .remount_fs = devpts_remount, |
Miklos Szeredi | b87a267 | 2008-02-08 04:21:41 -0800 | [diff] [blame] | 119 | .show_options = devpts_show_options, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | }; |
| 121 | |
| 122 | static int |
| 123 | devpts_fill_super(struct super_block *s, void *data, int silent) |
| 124 | { |
| 125 | struct inode * inode; |
| 126 | |
| 127 | s->s_blocksize = 1024; |
| 128 | s->s_blocksize_bits = 10; |
| 129 | s->s_magic = DEVPTS_SUPER_MAGIC; |
| 130 | s->s_op = &devpts_sops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | s->s_time_gran = 1; |
| 132 | |
| 133 | inode = new_inode(s); |
| 134 | if (!inode) |
| 135 | goto fail; |
| 136 | inode->i_ino = 1; |
| 137 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; |
| 138 | inode->i_blocks = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | inode->i_uid = inode->i_gid = 0; |
| 140 | inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR; |
| 141 | inode->i_op = &simple_dir_inode_operations; |
| 142 | inode->i_fop = &simple_dir_operations; |
| 143 | inode->i_nlink = 2; |
| 144 | |
| 145 | devpts_root = s->s_root = d_alloc_root(inode); |
| 146 | if (s->s_root) |
| 147 | return 0; |
| 148 | |
| 149 | printk("devpts: get root dentry failed\n"); |
| 150 | iput(inode); |
| 151 | fail: |
| 152 | return -ENOMEM; |
| 153 | } |
| 154 | |
David Howells | 454e239 | 2006-06-23 02:02:57 -0700 | [diff] [blame] | 155 | static int devpts_get_sb(struct file_system_type *fs_type, |
| 156 | int flags, const char *dev_name, void *data, struct vfsmount *mnt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | { |
David Howells | 454e239 | 2006-06-23 02:02:57 -0700 | [diff] [blame] | 158 | return get_sb_single(fs_type, flags, data, devpts_fill_super, mnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | static struct file_system_type devpts_fs_type = { |
| 162 | .owner = THIS_MODULE, |
| 163 | .name = "devpts", |
| 164 | .get_sb = devpts_get_sb, |
| 165 | .kill_sb = kill_anon_super, |
| 166 | }; |
| 167 | |
| 168 | /* |
| 169 | * The normal naming convention is simply /dev/pts/<number>; this conforms |
| 170 | * to the System V naming convention |
| 171 | */ |
| 172 | |
Sukadev Bhattiprolu | 15f1a63 | 2008-10-13 10:42:59 +0100 | [diff] [blame] | 173 | int devpts_new_index(struct inode *ptmx_inode) |
Sukadev Bhattiprolu | 718a916 | 2008-04-30 00:54:21 -0700 | [diff] [blame] | 174 | { |
| 175 | int index; |
Alexey Dobriyan | 7ee7c12 | 2008-07-26 11:42:16 +0400 | [diff] [blame] | 176 | int ida_ret; |
Sukadev Bhattiprolu | 718a916 | 2008-04-30 00:54:21 -0700 | [diff] [blame] | 177 | |
| 178 | retry: |
Alexey Dobriyan | 7ee7c12 | 2008-07-26 11:42:16 +0400 | [diff] [blame] | 179 | if (!ida_pre_get(&allocated_ptys, GFP_KERNEL)) { |
Sukadev Bhattiprolu | 718a916 | 2008-04-30 00:54:21 -0700 | [diff] [blame] | 180 | return -ENOMEM; |
| 181 | } |
| 182 | |
| 183 | mutex_lock(&allocated_ptys_lock); |
Alexey Dobriyan | 7ee7c12 | 2008-07-26 11:42:16 +0400 | [diff] [blame] | 184 | ida_ret = ida_get_new(&allocated_ptys, &index); |
| 185 | if (ida_ret < 0) { |
Sukadev Bhattiprolu | 718a916 | 2008-04-30 00:54:21 -0700 | [diff] [blame] | 186 | mutex_unlock(&allocated_ptys_lock); |
Alexey Dobriyan | 7ee7c12 | 2008-07-26 11:42:16 +0400 | [diff] [blame] | 187 | if (ida_ret == -EAGAIN) |
Sukadev Bhattiprolu | 718a916 | 2008-04-30 00:54:21 -0700 | [diff] [blame] | 188 | goto retry; |
| 189 | return -EIO; |
| 190 | } |
| 191 | |
| 192 | if (index >= pty_limit) { |
Alexey Dobriyan | 7ee7c12 | 2008-07-26 11:42:16 +0400 | [diff] [blame] | 193 | ida_remove(&allocated_ptys, index); |
Sukadev Bhattiprolu | 718a916 | 2008-04-30 00:54:21 -0700 | [diff] [blame] | 194 | mutex_unlock(&allocated_ptys_lock); |
| 195 | return -EIO; |
| 196 | } |
| 197 | mutex_unlock(&allocated_ptys_lock); |
| 198 | return index; |
| 199 | } |
| 200 | |
Sukadev Bhattiprolu | 15f1a63 | 2008-10-13 10:42:59 +0100 | [diff] [blame] | 201 | void devpts_kill_index(struct inode *ptmx_inode, int idx) |
Sukadev Bhattiprolu | 718a916 | 2008-04-30 00:54:21 -0700 | [diff] [blame] | 202 | { |
| 203 | mutex_lock(&allocated_ptys_lock); |
Alexey Dobriyan | 7ee7c12 | 2008-07-26 11:42:16 +0400 | [diff] [blame] | 204 | ida_remove(&allocated_ptys, idx); |
Sukadev Bhattiprolu | 718a916 | 2008-04-30 00:54:21 -0700 | [diff] [blame] | 205 | mutex_unlock(&allocated_ptys_lock); |
| 206 | } |
| 207 | |
Sukadev Bhattiprolu | 15f1a63 | 2008-10-13 10:42:59 +0100 | [diff] [blame] | 208 | int devpts_pty_new(struct inode *ptmx_inode, struct tty_struct *tty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | { |
Sukadev Bhattiprolu | 718a916 | 2008-04-30 00:54:21 -0700 | [diff] [blame] | 210 | int number = tty->index; /* tty layer puts index from devpts_new_index() in here */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | struct tty_driver *driver = tty->driver; |
| 212 | dev_t device = MKDEV(driver->major, driver->minor_start+number); |
| 213 | struct dentry *dentry; |
| 214 | struct inode *inode = new_inode(devpts_mnt->mnt_sb); |
Sukadev Bhattiprolu | 89a52e10 | 2008-10-13 10:43:18 +0100 | [diff] [blame] | 215 | char s[12]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | |
| 217 | /* We're supposed to be given the slave end of a pty */ |
| 218 | BUG_ON(driver->type != TTY_DRIVER_TYPE_PTY); |
| 219 | BUG_ON(driver->subtype != PTY_TYPE_SLAVE); |
| 220 | |
| 221 | if (!inode) |
| 222 | return -ENOMEM; |
| 223 | |
| 224 | inode->i_ino = number+2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | inode->i_uid = config.setuid ? config.uid : current->fsuid; |
| 226 | inode->i_gid = config.setgid ? config.gid : current->fsgid; |
| 227 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; |
| 228 | init_special_inode(inode, S_IFCHR|config.mode, device); |
Theodore Ts'o | 8e18e29 | 2006-09-27 01:50:46 -0700 | [diff] [blame] | 229 | inode->i_private = tty; |
Sukadev Bhattiprolu | a6f37da | 2008-10-13 10:43:27 +0100 | [diff] [blame] | 230 | tty->driver_data = inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | |
Sukadev Bhattiprolu | 89a52e10 | 2008-10-13 10:43:18 +0100 | [diff] [blame] | 232 | sprintf(s, "%d", number); |
| 233 | |
| 234 | mutex_lock(&devpts_root->d_inode->i_mutex); |
| 235 | |
| 236 | dentry = d_alloc_name(devpts_root, s); |
| 237 | if (!IS_ERR(dentry)) { |
| 238 | d_add(dentry, inode); |
Florin Malita | 3972b7f | 2007-05-08 00:24:18 -0700 | [diff] [blame] | 239 | fsnotify_create(devpts_root->d_inode, dentry); |
| 240 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 242 | mutex_unlock(&devpts_root->d_inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | |
| 244 | return 0; |
| 245 | } |
| 246 | |
Sukadev Bhattiprolu | 15f1a63 | 2008-10-13 10:42:59 +0100 | [diff] [blame] | 247 | struct tty_struct *devpts_get_tty(struct inode *pts_inode, int number) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | { |
Sukadev Bhattiprolu | 527b3e4 | 2008-10-13 10:43:08 +0100 | [diff] [blame] | 249 | BUG_ON(pts_inode->i_rdev == MKDEV(TTYAUX_MAJOR, PTMX_MINOR)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | |
Sukadev Bhattiprolu | 527b3e4 | 2008-10-13 10:43:08 +0100 | [diff] [blame] | 251 | if (pts_inode->i_sb->s_magic == DEVPTS_SUPER_MAGIC) |
| 252 | return (struct tty_struct *)pts_inode->i_private; |
| 253 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | } |
| 255 | |
Sukadev Bhattiprolu | 15f1a63 | 2008-10-13 10:42:59 +0100 | [diff] [blame] | 256 | void devpts_pty_kill(struct tty_struct *tty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | { |
Sukadev Bhattiprolu | a6f37da | 2008-10-13 10:43:27 +0100 | [diff] [blame] | 258 | struct inode *inode = tty->driver_data; |
| 259 | struct dentry *dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | |
Sukadev Bhattiprolu | a6f37da | 2008-10-13 10:43:27 +0100 | [diff] [blame] | 261 | BUG_ON(inode->i_rdev == MKDEV(TTYAUX_MAJOR, PTMX_MINOR)); |
| 262 | |
| 263 | mutex_lock(&devpts_root->d_inode->i_mutex); |
| 264 | |
| 265 | dentry = d_find_alias(inode); |
| 266 | if (dentry && !IS_ERR(dentry)) { |
| 267 | inode->i_nlink--; |
| 268 | d_delete(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | dput(dentry); |
| 270 | } |
Sukadev Bhattiprolu | a6f37da | 2008-10-13 10:43:27 +0100 | [diff] [blame] | 271 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 272 | mutex_unlock(&devpts_root->d_inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | static int __init init_devpts_fs(void) |
| 276 | { |
| 277 | int err = register_filesystem(&devpts_fs_type); |
| 278 | if (!err) { |
| 279 | devpts_mnt = kern_mount(&devpts_fs_type); |
| 280 | if (IS_ERR(devpts_mnt)) |
| 281 | err = PTR_ERR(devpts_mnt); |
| 282 | } |
| 283 | return err; |
| 284 | } |
| 285 | |
| 286 | static void __exit exit_devpts_fs(void) |
| 287 | { |
| 288 | unregister_filesystem(&devpts_fs_type); |
| 289 | mntput(devpts_mnt); |
| 290 | } |
| 291 | |
| 292 | module_init(init_devpts_fs) |
| 293 | module_exit(exit_devpts_fs) |
| 294 | MODULE_LICENSE("GPL"); |