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_readdir(struct file * filp, void * dirent, filldir_t filldir)
101 struct dentry *dentry = filp->f_dentry;
102 struct dentry *cursor = filp->private_data;
103 struct list_head *p, *q = &cursor->d_child;
109 ino = dentry->d_inode->i_ino;
110 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
116 ino = parent_ino(dentry);
117 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
123 spin_lock(&dcache_lock);
124 if (filp->f_pos == 2) {
126 list_add(q, &dentry->d_subdirs);
128 for (p=q->next; p != &dentry->d_subdirs; p=p->next) {
130 next = list_entry(p, struct dentry, d_child);
131 if (d_unhashed(next) || !next->d_inode)
133 if (!vx_check(next->d_inode->i_xid, VX_IDENT))
136 spin_unlock(&dcache_lock);
137 if (filldir(dirent, next->d_name.name,
138 next->d_name.len, filp->f_pos,
139 next->d_inode->i_ino, DT_CHR) < 0)
141 spin_lock(&dcache_lock);
142 /* next is still alive */
148 spin_unlock(&dcache_lock);
153 static struct file_operations devpts_dir_operations = {
154 .open = dcache_dir_open,
155 .release = dcache_dir_close,
156 .llseek = dcache_dir_lseek,
157 .read = generic_read_dir,
158 .readdir = devpts_readdir,
161 static struct super_operations devpts_sops = {
162 .statfs = simple_statfs,
163 .remount_fs = devpts_remount,
167 devpts_fill_super(struct super_block *s, void *data, int silent)
169 struct inode * inode;
171 s->s_blocksize = 1024;
172 s->s_blocksize_bits = 10;
173 s->s_magic = DEVPTS_SUPER_MAGIC;
174 s->s_op = &devpts_sops;
175 s->s_xattr = devpts_xattr_handlers;
178 inode = new_inode(s);
182 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
184 inode->i_blksize = 1024;
185 inode->i_uid = inode->i_gid = 0;
186 inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR;
187 inode->i_op = &simple_dir_inode_operations;
188 inode->i_fop = &devpts_dir_operations;
190 inode->i_xid = vx_current_xid();
192 devpts_root = s->s_root = d_alloc_root(inode);
196 printk("devpts: get root dentry failed\n");
202 static struct super_block *devpts_get_sb(struct file_system_type *fs_type,
203 int flags, const char *dev_name, void *data)
205 return get_sb_single(fs_type, flags, data, devpts_fill_super);
208 static struct file_system_type devpts_fs_type = {
209 .owner = THIS_MODULE,
211 .get_sb = devpts_get_sb,
212 .kill_sb = kill_anon_super,
216 * The normal naming convention is simply /dev/pts/<number>; this conforms
217 * to the System V naming convention
220 static struct dentry *get_node(int num)
223 struct dentry *root = devpts_root;
224 down(&root->d_inode->i_sem);
225 return lookup_one_len(s, root, sprintf(s, "%d", num));
228 int devpts_pty_new(struct tty_struct *tty)
230 int number = tty->index;
231 struct tty_driver *driver = tty->driver;
232 dev_t device = MKDEV(driver->major, driver->minor_start+number);
233 struct dentry *dentry;
234 struct inode *inode = new_inode(devpts_mnt->mnt_sb);
236 /* We're supposed to be given the slave end of a pty */
237 BUG_ON(driver->type != TTY_DRIVER_TYPE_PTY);
238 BUG_ON(driver->subtype != PTY_TYPE_SLAVE);
243 inode->i_ino = number+2;
244 inode->i_blksize = 1024;
245 inode->i_uid = config.setuid ? config.uid : current->fsuid;
246 inode->i_gid = config.setgid ? config.gid : current->fsgid;
247 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
248 init_special_inode(inode, S_IFCHR|config.mode, device);
249 inode->i_xid = vx_current_xid();
250 inode->i_op = &devpts_file_inode_operations;
251 inode->u.generic_ip = tty;
253 dentry = get_node(number);
254 if (!IS_ERR(dentry) && !dentry->d_inode)
255 d_instantiate(dentry, inode);
257 up(&devpts_root->d_inode->i_sem);
262 struct tty_struct *devpts_get_tty(int number)
264 struct dentry *dentry = get_node(number);
265 struct tty_struct *tty;
268 if (!IS_ERR(dentry)) {
270 tty = dentry->d_inode->u.generic_ip;
274 up(&devpts_root->d_inode->i_sem);
279 void devpts_pty_kill(int number)
281 struct dentry *dentry = get_node(number);
283 if (!IS_ERR(dentry)) {
284 struct inode *inode = dentry->d_inode;
292 up(&devpts_root->d_inode->i_sem);
295 static int __init init_devpts_fs(void)
297 int err = register_filesystem(&devpts_fs_type);
299 devpts_mnt = kern_mount(&devpts_fs_type);
300 if (IS_ERR(devpts_mnt))
301 err = PTR_ERR(devpts_mnt);
306 static void __exit exit_devpts_fs(void)
308 unregister_filesystem(&devpts_fs_type);
312 module_init(init_devpts_fs)
313 module_exit(exit_devpts_fs)
314 MODULE_LICENSE("GPL");