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/xattr.h>
23 extern struct xattr_handler devpts_xattr_security_handler;
25 static struct xattr_handler *devpts_xattr_handlers[] = {
26 #ifdef CONFIG_DEVPTS_FS_SECURITY
27 &devpts_xattr_security_handler,
32 static int devpts_permission(struct inode *inode, int mask, struct nameidata *nd)
36 if (vx_check(inode->i_xid, VX_IDENT))
37 ret = generic_permission(inode, mask, NULL);
41 static struct inode_operations devpts_file_inode_operations = {
42 #ifdef CONFIG_DEVPTS_FS_XATTR
43 .setxattr = generic_setxattr,
44 .getxattr = generic_getxattr,
45 .listxattr = generic_listxattr,
46 .removexattr = generic_removexattr,
48 .permission = devpts_permission,
51 static struct vfsmount *devpts_mnt;
52 static struct dentry *devpts_root;
60 } config = {.mode = 0600};
62 static int devpts_remount(struct super_block *sb, int *flags, char *data)
72 while ((this_char = strsep(&data, ",")) != NULL) {
77 if (sscanf(this_char, "uid=%i%c", &n, &dummy) == 1) {
80 } else if (sscanf(this_char, "gid=%i%c", &n, &dummy) == 1) {
83 } else if (sscanf(this_char, "mode=%o%c", &n, &dummy) == 1)
86 printk("devpts: called with bogus options\n");
90 config.setuid = setuid;
91 config.setgid = setgid;
99 static int devpts_filter(struct dentry *de)
101 return vx_check(de->d_inode->i_xid, VX_IDENT);
104 static int devpts_readdir(struct file * filp, void * dirent, filldir_t filldir)
106 return dcache_readdir_filter(filp, dirent, filldir, devpts_filter);
109 static struct file_operations devpts_dir_operations = {
110 .open = dcache_dir_open,
111 .release = dcache_dir_close,
112 .llseek = dcache_dir_lseek,
113 .read = generic_read_dir,
114 .readdir = devpts_readdir,
117 static struct super_operations devpts_sops = {
118 .statfs = simple_statfs,
119 .remount_fs = devpts_remount,
123 devpts_fill_super(struct super_block *s, void *data, int silent)
125 struct inode * inode;
127 s->s_blocksize = 1024;
128 s->s_blocksize_bits = 10;
129 s->s_magic = DEVPTS_SUPER_MAGIC;
130 s->s_op = &devpts_sops;
131 s->s_xattr = devpts_xattr_handlers;
134 inode = new_inode(s);
138 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
140 inode->i_blksize = 1024;
141 inode->i_uid = inode->i_gid = 0;
142 inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR;
143 inode->i_op = &simple_dir_inode_operations;
144 inode->i_fop = &devpts_dir_operations;
146 inode->i_xid = vx_current_xid();
148 devpts_root = s->s_root = d_alloc_root(inode);
152 printk("devpts: get root dentry failed\n");
158 static struct super_block *devpts_get_sb(struct file_system_type *fs_type,
159 int flags, const char *dev_name, void *data)
161 return get_sb_single(fs_type, flags, data, devpts_fill_super);
164 static struct file_system_type devpts_fs_type = {
165 .owner = THIS_MODULE,
167 .get_sb = devpts_get_sb,
168 .kill_sb = kill_anon_super,
172 * The normal naming convention is simply /dev/pts/<number>; this conforms
173 * to the System V naming convention
176 static struct dentry *get_node(int num)
179 struct dentry *root = devpts_root;
180 down(&root->d_inode->i_sem);
181 return lookup_one_len(s, root, sprintf(s, "%d", num));
185 int devpts_pty_new(struct tty_struct *tty)
187 int number = tty->index;
188 struct tty_driver *driver = tty->driver;
189 dev_t device = MKDEV(driver->major, driver->minor_start+number);
190 struct dentry *dentry;
191 struct inode *inode = new_inode(devpts_mnt->mnt_sb);
193 /* We're supposed to be given the slave end of a pty */
194 BUG_ON(driver->type != TTY_DRIVER_TYPE_PTY);
195 BUG_ON(driver->subtype != PTY_TYPE_SLAVE);
200 inode->i_ino = number+2;
201 inode->i_blksize = 1024;
202 inode->i_uid = config.setuid ? config.uid : current->fsuid;
203 inode->i_gid = config.setgid ? config.gid : current->fsgid;
204 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
205 init_special_inode(inode, S_IFCHR|config.mode, device);
206 inode->i_xid = vx_current_xid();
207 inode->i_op = &devpts_file_inode_operations;
208 inode->u.generic_ip = tty;
210 dentry = get_node(number);
211 if (!IS_ERR(dentry) && !dentry->d_inode)
212 d_instantiate(dentry, inode);
214 up(&devpts_root->d_inode->i_sem);
219 struct tty_struct *devpts_get_tty(int number)
221 struct dentry *dentry = get_node(number);
222 struct tty_struct *tty;
225 if (!IS_ERR(dentry)) {
227 tty = dentry->d_inode->u.generic_ip;
231 up(&devpts_root->d_inode->i_sem);
236 void devpts_pty_kill(int number)
238 struct dentry *dentry = get_node(number);
240 if (!IS_ERR(dentry)) {
241 struct inode *inode = dentry->d_inode;
249 up(&devpts_root->d_inode->i_sem);
252 static int __init init_devpts_fs(void)
254 int err = register_filesystem(&devpts_fs_type);
256 devpts_mnt = kern_mount(&devpts_fs_type);
257 if (IS_ERR(devpts_mnt))
258 err = PTR_ERR(devpts_mnt);
263 static void __exit exit_devpts_fs(void)
265 unregister_filesystem(&devpts_fs_type);
269 module_init(init_devpts_fs)
270 module_exit(exit_devpts_fs)
271 MODULE_LICENSE("GPL");