1 /* -*- linux-c -*- --------------------------------------------------------- *
3 * linux/fs/devpts/inode.c
5 * Copyright 1998-2004 H. Peter Anvin -- All Rights Reserved
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.
11 * ------------------------------------------------------------------------- */
13 #include <linux/module.h>
14 #include <linux/init.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>
21 #include <linux/parser.h>
22 #include <linux/vs_base.h>
25 static int devpts_permission(struct inode *inode, int mask, struct nameidata *nd)
29 /* devpts is xid tagged */
30 if (vx_check((xid_t)inode->i_tag, VS_WATCH_P|VS_IDENT))
31 ret = generic_permission(inode, mask, NULL);
35 static struct inode_operations devpts_file_inode_operations = {
36 .permission = devpts_permission,
39 static struct vfsmount *devpts_mnt;
40 static struct dentry *devpts_root;
48 } config = {.mode = 0600};
51 Opt_uid, Opt_gid, Opt_mode,
55 static match_table_t tokens = {
58 {Opt_mode, "mode=%o"},
62 static int devpts_remount(struct super_block *sb, int *flags, char *data)
72 while ((p = strsep(&data, ",")) != NULL) {
73 substring_t args[MAX_OPT_ARGS];
80 token = match_token(p, tokens, args);
83 if (match_int(&args[0], &option))
89 if (match_int(&args[0], &option))
95 if (match_octal(&args[0], &option))
97 config.mode = option & ~S_IFMT;
100 printk(KERN_ERR "devpts: called with bogus options\n");
108 static int devpts_filter(struct dentry *de)
110 /* devpts is xid tagged */
111 return vx_check((xid_t)de->d_inode->i_tag, VS_WATCH_P|VS_IDENT);
114 static int devpts_readdir(struct file * filp, void * dirent, filldir_t filldir)
116 return dcache_readdir_filter(filp, dirent, filldir, devpts_filter);
119 static struct file_operations devpts_dir_operations = {
120 .open = dcache_dir_open,
121 .release = dcache_dir_close,
122 .llseek = dcache_dir_lseek,
123 .read = generic_read_dir,
124 .readdir = devpts_readdir,
127 static struct super_operations devpts_sops = {
128 .statfs = simple_statfs,
129 .remount_fs = devpts_remount,
133 devpts_fill_super(struct super_block *s, void *data, int silent)
135 struct inode * inode;
137 s->s_blocksize = 1024;
138 s->s_blocksize_bits = 10;
139 s->s_magic = DEVPTS_SUPER_MAGIC;
140 s->s_op = &devpts_sops;
143 inode = new_inode(s);
147 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
149 inode->i_uid = inode->i_gid = 0;
150 inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR;
151 inode->i_op = &simple_dir_inode_operations;
152 inode->i_fop = &devpts_dir_operations;
154 /* devpts is xid tagged */
155 inode->i_tag = (tag_t)vx_current_xid();
157 devpts_root = s->s_root = d_alloc_root(inode);
161 printk("devpts: get root dentry failed\n");
167 static int devpts_get_sb(struct file_system_type *fs_type,
168 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
170 return get_sb_single(fs_type, flags, data, devpts_fill_super, mnt);
173 static struct file_system_type devpts_fs_type = {
174 .owner = THIS_MODULE,
176 .get_sb = devpts_get_sb,
177 .kill_sb = kill_anon_super,
181 * The normal naming convention is simply /dev/pts/<number>; this conforms
182 * to the System V naming convention
185 static struct dentry *get_node(int num)
188 struct dentry *root = devpts_root;
189 mutex_lock(&root->d_inode->i_mutex);
190 return lookup_one_len(s, root, sprintf(s, "%d", num));
193 int devpts_pty_new(struct tty_struct *tty)
195 int number = tty->index;
196 struct tty_driver *driver = tty->driver;
197 dev_t device = MKDEV(driver->major, driver->minor_start+number);
198 struct dentry *dentry;
199 struct inode *inode = new_inode(devpts_mnt->mnt_sb);
201 /* We're supposed to be given the slave end of a pty */
202 BUG_ON(driver->type != TTY_DRIVER_TYPE_PTY);
203 BUG_ON(driver->subtype != PTY_TYPE_SLAVE);
208 inode->i_ino = number+2;
209 inode->i_uid = config.setuid ? config.uid : current->fsuid;
210 inode->i_gid = config.setgid ? config.gid : current->fsgid;
211 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
212 init_special_inode(inode, S_IFCHR|config.mode, device);
213 /* devpts is xid tagged */
214 inode->i_tag = (tag_t)vx_current_xid();
215 inode->i_op = &devpts_file_inode_operations;
216 inode->i_private = tty;
218 dentry = get_node(number);
219 if (!IS_ERR(dentry) && !dentry->d_inode)
220 d_instantiate(dentry, inode);
222 mutex_unlock(&devpts_root->d_inode->i_mutex);
227 struct tty_struct *devpts_get_tty(int number)
229 struct dentry *dentry = get_node(number);
230 struct tty_struct *tty;
233 if (!IS_ERR(dentry)) {
235 tty = dentry->d_inode->i_private;
239 mutex_unlock(&devpts_root->d_inode->i_mutex);
244 void devpts_pty_kill(int number)
246 struct dentry *dentry = get_node(number);
248 if (!IS_ERR(dentry)) {
249 struct inode *inode = dentry->d_inode;
257 mutex_unlock(&devpts_root->d_inode->i_mutex);
260 static int __init init_devpts_fs(void)
262 int err = register_filesystem(&devpts_fs_type);
264 devpts_mnt = kern_mount(&devpts_fs_type);
265 if (IS_ERR(devpts_mnt))
266 err = PTR_ERR(devpts_mnt);
271 static void __exit exit_devpts_fs(void)
273 unregister_filesystem(&devpts_fs_type);
277 module_init(init_devpts_fs)
278 module_exit(exit_devpts_fs)
279 MODULE_LICENSE("GPL");