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