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 #define DEVPTS_SUPER_MAGIC 0x1cd1
26 extern struct xattr_handler devpts_xattr_security_handler;
28 static struct xattr_handler *devpts_xattr_handlers[] = {
29 #ifdef CONFIG_DEVPTS_FS_SECURITY
30 &devpts_xattr_security_handler,
35 static int devpts_permission(struct inode *inode, int mask, struct nameidata *nd)
39 if (vx_check(inode->i_xid, VX_IDENT))
40 ret = generic_permission(inode, mask, NULL);
44 struct inode_operations devpts_file_inode_operations = {
45 #ifdef CONFIG_DEVPTS_FS_XATTR
46 .setxattr = generic_setxattr,
47 .getxattr = generic_getxattr,
48 .listxattr = generic_listxattr,
49 .removexattr = generic_removexattr,
51 .permission = devpts_permission,
54 static struct vfsmount *devpts_mnt;
55 static struct dentry *devpts_root;
63 } config = {.mode = 0600};
65 static int devpts_remount(struct super_block *sb, int *flags, char *data)
75 while ((this_char = strsep(&data, ",")) != NULL) {
80 if (sscanf(this_char, "uid=%i%c", &n, &dummy) == 1) {
83 } else if (sscanf(this_char, "gid=%i%c", &n, &dummy) == 1) {
86 } else if (sscanf(this_char, "mode=%o%c", &n, &dummy) == 1)
89 printk("devpts: called with bogus options\n");
93 config.setuid = setuid;
94 config.setgid = setgid;
102 static int devpts_readdir(struct file * filp, void * dirent, filldir_t filldir)
104 struct dentry *dentry = filp->f_dentry;
105 struct dentry *cursor = filp->private_data;
106 struct list_head *p, *q = &cursor->d_child;
112 ino = dentry->d_inode->i_ino;
113 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
119 ino = parent_ino(dentry);
120 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
126 spin_lock(&dcache_lock);
127 if (filp->f_pos == 2) {
129 list_add(q, &dentry->d_subdirs);
131 for (p=q->next; p != &dentry->d_subdirs; p=p->next) {
133 next = list_entry(p, struct dentry, d_child);
134 if (d_unhashed(next) || !next->d_inode)
136 if (!vx_check(next->d_inode->i_xid, VX_IDENT))
139 spin_unlock(&dcache_lock);
140 if (filldir(dirent, next->d_name.name,
141 next->d_name.len, filp->f_pos,
142 next->d_inode->i_ino, DT_CHR) < 0)
144 spin_lock(&dcache_lock);
145 /* next is still alive */
151 spin_unlock(&dcache_lock);
156 static struct file_operations devpts_dir_operations = {
157 .open = dcache_dir_open,
158 .release = dcache_dir_close,
159 .llseek = dcache_dir_lseek,
160 .read = generic_read_dir,
161 .readdir = devpts_readdir,
164 static struct super_operations devpts_sops = {
165 .statfs = simple_statfs,
166 .remount_fs = devpts_remount,
170 devpts_fill_super(struct super_block *s, void *data, int silent)
172 struct inode * inode;
174 s->s_blocksize = 1024;
175 s->s_blocksize_bits = 10;
176 s->s_magic = DEVPTS_SUPER_MAGIC;
177 s->s_op = &devpts_sops;
178 s->s_xattr = devpts_xattr_handlers;
180 inode = new_inode(s);
184 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
186 inode->i_blksize = 1024;
187 inode->i_uid = inode->i_gid = 0;
188 inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR;
189 inode->i_op = &simple_dir_inode_operations;
190 inode->i_fop = &devpts_dir_operations;
192 inode->i_xid = vx_current_xid();
194 devpts_root = s->s_root = d_alloc_root(inode);
198 printk("devpts: get root dentry failed\n");
204 static struct super_block *devpts_get_sb(struct file_system_type *fs_type,
205 int flags, const char *dev_name, void *data)
207 return get_sb_single(fs_type, flags, data, devpts_fill_super);
210 static struct file_system_type devpts_fs_type = {
211 .owner = THIS_MODULE,
213 .get_sb = devpts_get_sb,
214 .kill_sb = kill_anon_super,
218 * The normal naming convention is simply /dev/pts/<number>; this conforms
219 * to the System V naming convention
222 static struct dentry *get_node(int num)
225 struct dentry *root = devpts_root;
226 down(&root->d_inode->i_sem);
227 return lookup_one_len(s, root, sprintf(s, "%d", num));
231 int devpts_pty_new(struct tty_struct *tty)
233 int number = tty->index;
234 struct tty_driver *driver = tty->driver;
235 dev_t device = MKDEV(driver->major, driver->minor_start+number);
236 struct dentry *dentry;
237 struct inode *inode = new_inode(devpts_mnt->mnt_sb);
239 /* We're supposed to be given the slave end of a pty */
240 BUG_ON(driver->type != TTY_DRIVER_TYPE_PTY);
241 BUG_ON(driver->subtype != PTY_TYPE_SLAVE);
246 inode->i_ino = number+2;
247 inode->i_blksize = 1024;
248 inode->i_uid = config.setuid ? config.uid : current->fsuid;
249 inode->i_gid = config.setgid ? config.gid : current->fsgid;
250 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
251 init_special_inode(inode, S_IFCHR|config.mode, device);
252 inode->i_xid = vx_current_xid();
253 inode->i_op = &devpts_file_inode_operations;
254 inode->u.generic_ip = tty;
256 dentry = get_node(number);
257 if (!IS_ERR(dentry) && !dentry->d_inode)
258 d_instantiate(dentry, inode);
260 up(&devpts_root->d_inode->i_sem);
265 struct tty_struct *devpts_get_tty(int number)
267 struct dentry *dentry = get_node(number);
268 struct tty_struct *tty;
271 if (!IS_ERR(dentry)) {
273 tty = dentry->d_inode->u.generic_ip;
277 up(&devpts_root->d_inode->i_sem);
282 void devpts_pty_kill(int number)
284 struct dentry *dentry = get_node(number);
286 if (!IS_ERR(dentry)) {
287 struct inode *inode = dentry->d_inode;
295 up(&devpts_root->d_inode->i_sem);
298 static int __init init_devpts_fs(void)
300 int err = register_filesystem(&devpts_fs_type);
302 devpts_mnt = kern_mount(&devpts_fs_type);
303 if (IS_ERR(devpts_mnt))
304 err = PTR_ERR(devpts_mnt);
309 static void __exit exit_devpts_fs(void)
311 unregister_filesystem(&devpts_fs_type);
315 module_init(init_devpts_fs)
316 module_exit(exit_devpts_fs)
317 MODULE_LICENSE("GPL");