vserver 1.9.3
[linux-2.6.git] / fs / devpts / inode.c
1 /* -*- linux-c -*- --------------------------------------------------------- *
2  *
3  * linux/fs/devpts/inode.c
4  *
5  *  Copyright 1998-2004 H. Peter Anvin -- All Rights Reserved
6  *
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.
10  *
11  * ------------------------------------------------------------------------- */
12
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/fs.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 "xattr.h"
23
24 static struct vfsmount *devpts_mnt;
25 static struct dentry *devpts_root;
26
27 static struct {
28         int setuid;
29         int setgid;
30         uid_t   uid;
31         gid_t   gid;
32         umode_t mode;
33 } config = {.mode = 0600};
34
35 static int devpts_remount(struct super_block *sb, int *flags, char *data)
36 {
37         int setuid = 0;
38         int setgid = 0;
39         uid_t uid = 0;
40         gid_t gid = 0;
41         umode_t mode = 0600;
42         char *this_char;
43
44         this_char = NULL;
45         while ((this_char = strsep(&data, ",")) != NULL) {
46                 int n;
47                 char dummy;
48                 if (!*this_char)
49                         continue;
50                 if (sscanf(this_char, "uid=%i%c", &n, &dummy) == 1) {
51                         setuid = 1;
52                         uid = n;
53                 } else if (sscanf(this_char, "gid=%i%c", &n, &dummy) == 1) {
54                         setgid = 1;
55                         gid = n;
56                 } else if (sscanf(this_char, "mode=%o%c", &n, &dummy) == 1)
57                         mode = n & ~S_IFMT;
58                 else {
59                         printk("devpts: called with bogus options\n");
60                         return -EINVAL;
61                 }
62         }
63         config.setuid  = setuid;
64         config.setgid  = setgid;
65         config.uid     = uid;
66         config.gid     = gid;
67         config.mode    = mode;
68
69         return 0;
70 }
71
72 static int devpts_readdir(struct file * filp, void * dirent, filldir_t filldir)
73 {
74         struct dentry *dentry = filp->f_dentry;
75         struct dentry *cursor = filp->private_data;
76         struct list_head *p, *q = &cursor->d_child;
77         ino_t ino;
78         int i = filp->f_pos;
79
80         switch (i) {
81                 case 0:
82                         ino = dentry->d_inode->i_ino;
83                         if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
84                                 break;
85                         filp->f_pos++;
86                         i++;
87                         /* fallthrough */
88                 case 1:
89                         ino = parent_ino(dentry);
90                         if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
91                                 break;
92                         filp->f_pos++;
93                         i++;
94                         /* fallthrough */
95                 default:
96                         spin_lock(&dcache_lock);
97                         if (filp->f_pos == 2) {
98                                 list_del(q);
99                                 list_add(q, &dentry->d_subdirs);
100                         }
101                         for (p=q->next; p != &dentry->d_subdirs; p=p->next) {
102                                 struct dentry *next;
103                                 next = list_entry(p, struct dentry, d_child);
104                                 if (d_unhashed(next) || !next->d_inode)
105                                         continue;
106                                 if (!vx_check(next->d_inode->i_xid, VX_IDENT))
107                                         continue;
108
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)
113                                         return 0;
114                                 spin_lock(&dcache_lock);
115                                 /* next is still alive */
116                                 list_del(q);
117                                 list_add(q, p);
118                                 p = q;
119                                 filp->f_pos++;
120                         }
121                         spin_unlock(&dcache_lock);
122         }
123         return 0;
124 }
125
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,
132 };
133
134 static struct super_operations devpts_sops = {
135         .statfs         = simple_statfs,
136         .remount_fs     = devpts_remount,
137 };
138
139 static int
140 devpts_fill_super(struct super_block *s, void *data, int silent)
141 {
142         struct inode * inode;
143
144         s->s_blocksize = 1024;
145         s->s_blocksize_bits = 10;
146         s->s_magic = DEVPTS_SUPER_MAGIC;
147         s->s_op = &devpts_sops;
148
149         inode = new_inode(s);
150         if (!inode)
151                 goto fail;
152         inode->i_ino = 1;
153         inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
154         inode->i_blocks = 0;
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;
160         inode->i_nlink = 2;
161         inode->i_xid = vx_current_xid();
162
163         devpts_root = s->s_root = d_alloc_root(inode);
164         if (s->s_root)
165                 return 0;
166         
167         printk("devpts: get root dentry failed\n");
168         iput(inode);
169 fail:
170         return -ENOMEM;
171 }
172
173 static struct super_block *devpts_get_sb(struct file_system_type *fs_type,
174         int flags, const char *dev_name, void *data)
175 {
176         return get_sb_single(fs_type, flags, data, devpts_fill_super);
177 }
178
179 static struct file_system_type devpts_fs_type = {
180         .owner          = THIS_MODULE,
181         .name           = "devpts",
182         .get_sb         = devpts_get_sb,
183         .kill_sb        = kill_anon_super,
184 };
185
186 /*
187  * The normal naming convention is simply /dev/pts/<number>; this conforms
188  * to the System V naming convention
189  */
190
191 static struct dentry *get_node(int num)
192 {
193         char s[12];
194         struct dentry *root = devpts_root;
195         down(&root->d_inode->i_sem);
196         return lookup_one_len(s, root, sprintf(s, "%d", num));
197 }
198
199 static int devpts_permission(struct inode *inode, int mask, struct nameidata *nd)
200 {
201         int ret = -EACCES;
202
203         if (vx_check(inode->i_xid, VX_IDENT))
204                 ret = vfs_permission(inode, mask);
205         return ret;
206 }
207
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,
214 };
215
216 int devpts_pty_new(struct tty_struct *tty)
217 {
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);
223
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);
227
228         if (!inode)
229                 return -ENOMEM;
230
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;
240
241         dentry = get_node(number);
242         if (!IS_ERR(dentry) && !dentry->d_inode)
243                 d_instantiate(dentry, inode);
244
245         up(&devpts_root->d_inode->i_sem);
246
247         return 0;
248 }
249
250 struct tty_struct *devpts_get_tty(int number)
251 {
252         struct dentry *dentry = get_node(number);
253         struct tty_struct *tty;
254
255         tty = NULL;
256         if (!IS_ERR(dentry)) {
257                 if (dentry->d_inode)
258                         tty = dentry->d_inode->u.generic_ip;
259                 dput(dentry);
260         }
261
262         up(&devpts_root->d_inode->i_sem);
263
264         return tty;
265 }
266
267 void devpts_pty_kill(int number)
268 {
269         struct dentry *dentry = get_node(number);
270
271         if (!IS_ERR(dentry)) {
272                 struct inode *inode = dentry->d_inode;
273                 if (inode) {
274                         inode->i_nlink--;
275                         d_delete(dentry);
276                         dput(dentry);
277                 }
278                 dput(dentry);
279         }
280         up(&devpts_root->d_inode->i_sem);
281 }
282
283 static int __init init_devpts_fs(void)
284 {
285         int err = init_devpts_xattr();
286         if (err)
287                 return err;
288         err = register_filesystem(&devpts_fs_type);
289         if (!err) {
290                 devpts_mnt = kern_mount(&devpts_fs_type);
291                 if (IS_ERR(devpts_mnt))
292                         err = PTR_ERR(devpts_mnt);
293         }
294         return err;
295 }
296
297 static void __exit exit_devpts_fs(void)
298 {
299         unregister_filesystem(&devpts_fs_type);
300         mntput(devpts_mnt);
301         exit_devpts_xattr();
302 }
303
304 module_init(init_devpts_fs)
305 module_exit(exit_devpts_fs)
306 MODULE_LICENSE("GPL");