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/vs_base.h>
22 #include <linux/xattr.h>
24 extern struct xattr_handler devpts_xattr_security_handler;
26 static struct xattr_handler *devpts_xattr_handlers[] = {
27 #ifdef CONFIG_DEVPTS_FS_SECURITY
28 &devpts_xattr_security_handler,
33 static int devpts_permission(struct inode *inode, int mask, struct nameidata *nd)
37 if (vx_check(inode->i_xid, VX_IDENT))
38 ret = generic_permission(inode, mask, NULL);
42 static struct inode_operations devpts_file_inode_operations = {
43 #ifdef CONFIG_DEVPTS_FS_XATTR
44 .setxattr = generic_setxattr,
45 .getxattr = generic_getxattr,
46 .listxattr = generic_listxattr,
47 .removexattr = generic_removexattr,
49 .permission = devpts_permission,
52 static struct vfsmount *devpts_mnt;
53 static struct dentry *devpts_root;
61 } config = {.mode = 0600};
63 static int devpts_remount(struct super_block *sb, int *flags, char *data)
73 while ((this_char = strsep(&data, ",")) != NULL) {
78 if (sscanf(this_char, "uid=%i%c", &n, &dummy) == 1) {
81 } else if (sscanf(this_char, "gid=%i%c", &n, &dummy) == 1) {
84 } else if (sscanf(this_char, "mode=%o%c", &n, &dummy) == 1)
87 printk("devpts: called with bogus options\n");
91 config.setuid = setuid;
92 config.setgid = setgid;
100 static int devpts_filter(struct dentry *de)
102 return vx_check(de->d_inode->i_xid, VX_IDENT);
105 static int devpts_readdir(struct file * filp, void * dirent, filldir_t filldir)
107 return dcache_readdir_filter(filp, dirent, filldir, devpts_filter);
110 static struct file_operations devpts_dir_operations = {
111 .open = dcache_dir_open,
112 .release = dcache_dir_close,
113 .llseek = dcache_dir_lseek,
114 .read = generic_read_dir,
115 .readdir = devpts_readdir,
118 static struct super_operations devpts_sops = {
119 .statfs = simple_statfs,
120 .remount_fs = devpts_remount,
124 devpts_fill_super(struct super_block *s, void *data, int silent)
126 struct inode * inode;
128 s->s_blocksize = 1024;
129 s->s_blocksize_bits = 10;
130 s->s_magic = DEVPTS_SUPER_MAGIC;
131 s->s_op = &devpts_sops;
132 s->s_xattr = devpts_xattr_handlers;
135 inode = new_inode(s);
139 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
141 inode->i_blksize = 1024;
142 inode->i_uid = inode->i_gid = 0;
143 inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR;
144 inode->i_op = &simple_dir_inode_operations;
145 inode->i_fop = &devpts_dir_operations;
147 inode->i_xid = vx_current_xid();
149 devpts_root = s->s_root = d_alloc_root(inode);
153 printk("devpts: get root dentry failed\n");
159 static struct super_block *devpts_get_sb(struct file_system_type *fs_type,
160 int flags, const char *dev_name, void *data)
162 return get_sb_single(fs_type, flags, data, devpts_fill_super);
165 static struct file_system_type devpts_fs_type = {
166 .owner = THIS_MODULE,
168 .get_sb = devpts_get_sb,
169 .kill_sb = kill_anon_super,
173 * The normal naming convention is simply /dev/pts/<number>; this conforms
174 * to the System V naming convention
177 static struct dentry *get_node(int num)
180 struct dentry *root = devpts_root;
181 down(&root->d_inode->i_sem);
182 return lookup_one_len(s, root, sprintf(s, "%d", num));
186 int devpts_pty_new(struct tty_struct *tty)
188 int number = tty->index;
189 struct tty_driver *driver = tty->driver;
190 dev_t device = MKDEV(driver->major, driver->minor_start+number);
191 struct dentry *dentry;
192 struct inode *inode = new_inode(devpts_mnt->mnt_sb);
194 /* We're supposed to be given the slave end of a pty */
195 BUG_ON(driver->type != TTY_DRIVER_TYPE_PTY);
196 BUG_ON(driver->subtype != PTY_TYPE_SLAVE);
201 inode->i_ino = number+2;
202 inode->i_blksize = 1024;
203 inode->i_uid = config.setuid ? config.uid : current->fsuid;
204 inode->i_gid = config.setgid ? config.gid : current->fsgid;
205 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
206 init_special_inode(inode, S_IFCHR|config.mode, device);
207 inode->i_xid = vx_current_xid();
208 inode->i_op = &devpts_file_inode_operations;
209 inode->u.generic_ip = tty;
211 dentry = get_node(number);
212 if (!IS_ERR(dentry) && !dentry->d_inode)
213 d_instantiate(dentry, inode);
215 up(&devpts_root->d_inode->i_sem);
220 struct tty_struct *devpts_get_tty(int number)
222 struct dentry *dentry = get_node(number);
223 struct tty_struct *tty;
226 if (!IS_ERR(dentry)) {
228 tty = dentry->d_inode->u.generic_ip;
232 up(&devpts_root->d_inode->i_sem);
237 void devpts_pty_kill(int number)
239 struct dentry *dentry = get_node(number);
241 if (!IS_ERR(dentry)) {
242 struct inode *inode = dentry->d_inode;
250 up(&devpts_root->d_inode->i_sem);
253 static int __init init_devpts_fs(void)
255 int err = register_filesystem(&devpts_fs_type);
257 devpts_mnt = kern_mount(&devpts_fs_type);
258 if (IS_ERR(devpts_mnt))
259 err = PTR_ERR(devpts_mnt);
264 static void __exit exit_devpts_fs(void)
266 unregister_filesystem(&devpts_fs_type);
270 module_init(init_devpts_fs)
271 module_exit(exit_devpts_fs)
272 MODULE_LICENSE("GPL");