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 struct vfsmount *devpts_mnt;
36 static struct dentry *devpts_root;
44 } config = {.mode = 0600};
46 static int devpts_remount(struct super_block *sb, int *flags, char *data)
56 while ((this_char = strsep(&data, ",")) != NULL) {
61 if (sscanf(this_char, "uid=%i%c", &n, &dummy) == 1) {
64 } else if (sscanf(this_char, "gid=%i%c", &n, &dummy) == 1) {
67 } else if (sscanf(this_char, "mode=%o%c", &n, &dummy) == 1)
70 printk("devpts: called with bogus options\n");
74 config.setuid = setuid;
75 config.setgid = setgid;
83 static int devpts_readdir(struct file * filp, void * dirent, filldir_t filldir)
85 struct dentry *dentry = filp->f_dentry;
86 struct dentry *cursor = filp->private_data;
87 struct list_head *p, *q = &cursor->d_child;
93 ino = dentry->d_inode->i_ino;
94 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
100 ino = parent_ino(dentry);
101 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
107 spin_lock(&dcache_lock);
108 if (filp->f_pos == 2) {
110 list_add(q, &dentry->d_subdirs);
112 for (p=q->next; p != &dentry->d_subdirs; p=p->next) {
114 next = list_entry(p, struct dentry, d_child);
115 if (d_unhashed(next) || !next->d_inode)
117 if (!vx_check(next->d_inode->i_xid, VX_IDENT))
120 spin_unlock(&dcache_lock);
121 if (filldir(dirent, next->d_name.name,
122 next->d_name.len, filp->f_pos,
123 next->d_inode->i_ino, DT_CHR) < 0)
125 spin_lock(&dcache_lock);
126 /* next is still alive */
132 spin_unlock(&dcache_lock);
137 static struct file_operations devpts_dir_operations = {
138 .open = dcache_dir_open,
139 .release = dcache_dir_close,
140 .llseek = dcache_dir_lseek,
141 .read = generic_read_dir,
142 .readdir = devpts_readdir,
145 static struct super_operations devpts_sops = {
146 .statfs = simple_statfs,
147 .remount_fs = devpts_remount,
151 devpts_fill_super(struct super_block *s, void *data, int silent)
153 struct inode * inode;
155 s->s_blocksize = 1024;
156 s->s_blocksize_bits = 10;
157 s->s_magic = DEVPTS_SUPER_MAGIC;
158 s->s_op = &devpts_sops;
159 s->s_xattr = devpts_xattr_handlers;
161 inode = new_inode(s);
165 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
167 inode->i_blksize = 1024;
168 inode->i_uid = inode->i_gid = 0;
169 inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR;
170 inode->i_op = &simple_dir_inode_operations;
171 inode->i_fop = &devpts_dir_operations;
173 inode->i_xid = vx_current_xid();
175 devpts_root = s->s_root = d_alloc_root(inode);
179 printk("devpts: get root dentry failed\n");
185 static struct super_block *devpts_get_sb(struct file_system_type *fs_type,
186 int flags, const char *dev_name, void *data)
188 return get_sb_single(fs_type, flags, data, devpts_fill_super);
191 static struct file_system_type devpts_fs_type = {
192 .owner = THIS_MODULE,
194 .get_sb = devpts_get_sb,
195 .kill_sb = kill_anon_super,
199 * The normal naming convention is simply /dev/pts/<number>; this conforms
200 * to the System V naming convention
203 static struct dentry *get_node(int num)
206 struct dentry *root = devpts_root;
207 down(&root->d_inode->i_sem);
208 return lookup_one_len(s, root, sprintf(s, "%d", num));
211 #ifdef CONFIG_DEVPTS_FS_XATTR
212 static int devpts_permission(struct inode *inode, int mask, struct nameidata *nd)
216 if (vx_check(inode->i_xid, VX_IDENT))
217 ret = generic_permission(inode, mask, NULL);
222 struct inode_operations devpts_file_inode_operations = {
223 #ifdef CONFIG_DEVPTS_FS_XATTR
224 .setxattr = generic_setxattr,
225 .getxattr = generic_getxattr,
226 .listxattr = generic_listxattr,
227 .removexattr = generic_removexattr,
228 .permission = devpts_permission,
232 int devpts_pty_new(struct tty_struct *tty)
234 int number = tty->index;
235 struct tty_driver *driver = tty->driver;
236 dev_t device = MKDEV(driver->major, driver->minor_start+number);
237 struct dentry *dentry;
238 struct inode *inode = new_inode(devpts_mnt->mnt_sb);
240 /* We're supposed to be given the slave end of a pty */
241 BUG_ON(driver->type != TTY_DRIVER_TYPE_PTY);
242 BUG_ON(driver->subtype != PTY_TYPE_SLAVE);
247 inode->i_ino = number+2;
248 inode->i_blksize = 1024;
249 inode->i_uid = config.setuid ? config.uid : current->fsuid;
250 inode->i_gid = config.setgid ? config.gid : current->fsgid;
251 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
252 init_special_inode(inode, S_IFCHR|config.mode, device);
253 inode->i_xid = vx_current_xid();
254 inode->i_op = &devpts_file_inode_operations;
255 inode->u.generic_ip = tty;
257 dentry = get_node(number);
258 if (!IS_ERR(dentry) && !dentry->d_inode)
259 d_instantiate(dentry, inode);
261 up(&devpts_root->d_inode->i_sem);
266 struct tty_struct *devpts_get_tty(int number)
268 struct dentry *dentry = get_node(number);
269 struct tty_struct *tty;
272 if (!IS_ERR(dentry)) {
274 tty = dentry->d_inode->u.generic_ip;
278 up(&devpts_root->d_inode->i_sem);
283 void devpts_pty_kill(int number)
285 struct dentry *dentry = get_node(number);
287 if (!IS_ERR(dentry)) {
288 struct inode *inode = dentry->d_inode;
296 up(&devpts_root->d_inode->i_sem);
299 static int __init init_devpts_fs(void)
301 int err = register_filesystem(&devpts_fs_type);
303 devpts_mnt = kern_mount(&devpts_fs_type);
304 if (IS_ERR(devpts_mnt))
305 err = PTR_ERR(devpts_mnt);
310 static void __exit exit_devpts_fs(void)
312 unregister_filesystem(&devpts_fs_type);
316 module_init(init_devpts_fs)
317 module_exit(exit_devpts_fs)
318 MODULE_LICENSE("GPL");