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