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>
24 static struct vfsmount *devpts_mnt;
25 static struct dentry *devpts_root;
33 } config = {.mode = 0600};
35 static int devpts_remount(struct super_block *sb, int *flags, char *data)
45 while ((this_char = strsep(&data, ",")) != NULL) {
50 if (sscanf(this_char, "uid=%i%c", &n, &dummy) == 1) {
53 } else if (sscanf(this_char, "gid=%i%c", &n, &dummy) == 1) {
56 } else if (sscanf(this_char, "mode=%o%c", &n, &dummy) == 1)
59 printk("devpts: called with bogus options\n");
63 config.setuid = setuid;
64 config.setgid = setgid;
72 static int devpts_readdir(struct file * filp, void * dirent, filldir_t filldir)
74 struct dentry *dentry = filp->f_dentry;
75 struct dentry *cursor = filp->private_data;
76 struct list_head *p, *q = &cursor->d_child;
82 ino = dentry->d_inode->i_ino;
83 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
89 ino = parent_ino(dentry);
90 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
96 spin_lock(&dcache_lock);
97 if (filp->f_pos == 2) {
99 list_add(q, &dentry->d_subdirs);
101 for (p=q->next; p != &dentry->d_subdirs; p=p->next) {
103 next = list_entry(p, struct dentry, d_child);
104 if (d_unhashed(next) || !next->d_inode)
106 if (!vx_check(next->d_inode->i_xid, VX_IDENT))
109 spin_unlock(&dcache_lock);
110 if (filldir(dirent, next->d_name.name,
111 next->d_name.len, filp->f_pos,
112 next->d_inode->i_ino, DT_CHR) < 0)
114 spin_lock(&dcache_lock);
115 /* next is still alive */
121 spin_unlock(&dcache_lock);
126 static struct file_operations devpts_dir_operations = {
127 .open = dcache_dir_open,
128 .release = dcache_dir_close,
129 .llseek = dcache_dir_lseek,
130 .read = generic_read_dir,
131 .readdir = devpts_readdir,
134 static struct super_operations devpts_sops = {
135 .statfs = simple_statfs,
136 .remount_fs = devpts_remount,
140 devpts_fill_super(struct super_block *s, void *data, int silent)
142 struct inode * inode;
144 s->s_blocksize = 1024;
145 s->s_blocksize_bits = 10;
146 s->s_magic = DEVPTS_SUPER_MAGIC;
147 s->s_op = &devpts_sops;
149 inode = new_inode(s);
153 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
155 inode->i_blksize = 1024;
156 inode->i_uid = inode->i_gid = 0;
157 inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR;
158 inode->i_op = &simple_dir_inode_operations;
159 inode->i_fop = &devpts_dir_operations;
161 inode->i_xid = vx_current_xid();
163 devpts_root = s->s_root = d_alloc_root(inode);
167 printk("devpts: get root dentry failed\n");
173 static struct super_block *devpts_get_sb(struct file_system_type *fs_type,
174 int flags, const char *dev_name, void *data)
176 return get_sb_single(fs_type, flags, data, devpts_fill_super);
179 static struct file_system_type devpts_fs_type = {
180 .owner = THIS_MODULE,
182 .get_sb = devpts_get_sb,
183 .kill_sb = kill_anon_super,
187 * The normal naming convention is simply /dev/pts/<number>; this conforms
188 * to the System V naming convention
191 static struct dentry *get_node(int num)
194 struct dentry *root = devpts_root;
195 down(&root->d_inode->i_sem);
196 return lookup_one_len(s, root, sprintf(s, "%d", num));
199 static int devpts_permission(struct inode *inode, int mask, struct nameidata *nd)
203 if (vx_check(inode->i_xid, VX_IDENT))
204 ret = vfs_permission(inode, mask);
208 static struct inode_operations devpts_file_inode_operations = {
209 .setxattr = devpts_setxattr,
210 .getxattr = devpts_getxattr,
211 .listxattr = devpts_listxattr,
212 .removexattr = devpts_removexattr,
213 .permission = devpts_permission,
216 int devpts_pty_new(struct tty_struct *tty)
218 int number = tty->index;
219 struct tty_driver *driver = tty->driver;
220 dev_t device = MKDEV(driver->major, driver->minor_start+number);
221 struct dentry *dentry;
222 struct inode *inode = new_inode(devpts_mnt->mnt_sb);
224 /* We're supposed to be given the slave end of a pty */
225 BUG_ON(driver->type != TTY_DRIVER_TYPE_PTY);
226 BUG_ON(driver->subtype != PTY_TYPE_SLAVE);
231 inode->i_ino = number+2;
232 inode->i_blksize = 1024;
233 inode->i_uid = config.setuid ? config.uid : current->fsuid;
234 inode->i_gid = config.setgid ? config.gid : current->fsgid;
235 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
236 init_special_inode(inode, S_IFCHR|config.mode, device);
237 inode->i_xid = vx_current_xid();
238 inode->i_op = &devpts_file_inode_operations;
239 inode->u.generic_ip = tty;
241 dentry = get_node(number);
242 if (!IS_ERR(dentry) && !dentry->d_inode)
243 d_instantiate(dentry, inode);
245 up(&devpts_root->d_inode->i_sem);
250 struct tty_struct *devpts_get_tty(int number)
252 struct dentry *dentry = get_node(number);
253 struct tty_struct *tty;
256 if (!IS_ERR(dentry)) {
258 tty = dentry->d_inode->u.generic_ip;
262 up(&devpts_root->d_inode->i_sem);
267 void devpts_pty_kill(int number)
269 struct dentry *dentry = get_node(number);
271 if (!IS_ERR(dentry)) {
272 struct inode *inode = dentry->d_inode;
280 up(&devpts_root->d_inode->i_sem);
283 static int __init init_devpts_fs(void)
285 int err = init_devpts_xattr();
288 err = register_filesystem(&devpts_fs_type);
290 devpts_mnt = kern_mount(&devpts_fs_type);
291 if (IS_ERR(devpts_mnt))
292 err = PTR_ERR(devpts_mnt);
297 static void __exit exit_devpts_fs(void)
299 unregister_filesystem(&devpts_fs_type);
304 module_init(init_devpts_fs)
305 module_exit(exit_devpts_fs)
306 MODULE_LICENSE("GPL");