upgrade to vserver 1.9.3.17
[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 <linux/xattr.h>
23
24 #define DEVPTS_SUPER_MAGIC 0x1cd1
25
26 extern struct xattr_handler devpts_xattr_security_handler;
27
28 static struct xattr_handler *devpts_xattr_handlers[] = {
29 #ifdef CONFIG_DEVPTS_FS_SECURITY
30         &devpts_xattr_security_handler,
31 #endif
32         NULL
33 };
34
35 static int devpts_permission(struct inode *inode, int mask, struct nameidata *nd)
36 {
37         int ret = -EACCES;
38
39         if (vx_check(inode->i_xid, VX_IDENT))
40                 ret = generic_permission(inode, mask, NULL);
41         return ret;
42 }
43
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,
50 #endif
51         .permission     = devpts_permission,
52 };
53
54 static struct vfsmount *devpts_mnt;
55 static struct dentry *devpts_root;
56
57 static struct {
58         int setuid;
59         int setgid;
60         uid_t   uid;
61         gid_t   gid;
62         umode_t mode;
63 } config = {.mode = 0600};
64
65 static int devpts_remount(struct super_block *sb, int *flags, char *data)
66 {
67         int setuid = 0;
68         int setgid = 0;
69         uid_t uid = 0;
70         gid_t gid = 0;
71         umode_t mode = 0600;
72         char *this_char;
73
74         this_char = NULL;
75         while ((this_char = strsep(&data, ",")) != NULL) {
76                 int n;
77                 char dummy;
78                 if (!*this_char)
79                         continue;
80                 if (sscanf(this_char, "uid=%i%c", &n, &dummy) == 1) {
81                         setuid = 1;
82                         uid = n;
83                 } else if (sscanf(this_char, "gid=%i%c", &n, &dummy) == 1) {
84                         setgid = 1;
85                         gid = n;
86                 } else if (sscanf(this_char, "mode=%o%c", &n, &dummy) == 1)
87                         mode = n & ~S_IFMT;
88                 else {
89                         printk("devpts: called with bogus options\n");
90                         return -EINVAL;
91                 }
92         }
93         config.setuid  = setuid;
94         config.setgid  = setgid;
95         config.uid     = uid;
96         config.gid     = gid;
97         config.mode    = mode;
98
99         return 0;
100 }
101
102 static int devpts_readdir(struct file * filp, void * dirent, filldir_t filldir)
103 {
104         struct dentry *dentry = filp->f_dentry;
105         struct dentry *cursor = filp->private_data;
106         struct list_head *p, *q = &cursor->d_child;
107         ino_t ino;
108         int i = filp->f_pos;
109
110         switch (i) {
111                 case 0:
112                         ino = dentry->d_inode->i_ino;
113                         if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
114                                 break;
115                         filp->f_pos++;
116                         i++;
117                         /* fallthrough */
118                 case 1:
119                         ino = parent_ino(dentry);
120                         if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
121                                 break;
122                         filp->f_pos++;
123                         i++;
124                         /* fallthrough */
125                 default:
126                         spin_lock(&dcache_lock);
127                         if (filp->f_pos == 2) {
128                                 list_del(q);
129                                 list_add(q, &dentry->d_subdirs);
130                         }
131                         for (p=q->next; p != &dentry->d_subdirs; p=p->next) {
132                                 struct dentry *next;
133                                 next = list_entry(p, struct dentry, d_child);
134                                 if (d_unhashed(next) || !next->d_inode)
135                                         continue;
136                                 if (!vx_check(next->d_inode->i_xid, VX_IDENT))
137                                         continue;
138
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)
143                                         return 0;
144                                 spin_lock(&dcache_lock);
145                                 /* next is still alive */
146                                 list_del(q);
147                                 list_add(q, p);
148                                 p = q;
149                                 filp->f_pos++;
150                         }
151                         spin_unlock(&dcache_lock);
152         }
153         return 0;
154 }
155
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,
162 };
163
164 static struct super_operations devpts_sops = {
165         .statfs         = simple_statfs,
166         .remount_fs     = devpts_remount,
167 };
168
169 static int
170 devpts_fill_super(struct super_block *s, void *data, int silent)
171 {
172         struct inode * inode;
173
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;
179
180         inode = new_inode(s);
181         if (!inode)
182                 goto fail;
183         inode->i_ino = 1;
184         inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
185         inode->i_blocks = 0;
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;
191         inode->i_nlink = 2;
192         inode->i_xid = vx_current_xid();
193
194         devpts_root = s->s_root = d_alloc_root(inode);
195         if (s->s_root)
196                 return 0;
197         
198         printk("devpts: get root dentry failed\n");
199         iput(inode);
200 fail:
201         return -ENOMEM;
202 }
203
204 static struct super_block *devpts_get_sb(struct file_system_type *fs_type,
205         int flags, const char *dev_name, void *data)
206 {
207         return get_sb_single(fs_type, flags, data, devpts_fill_super);
208 }
209
210 static struct file_system_type devpts_fs_type = {
211         .owner          = THIS_MODULE,
212         .name           = "devpts",
213         .get_sb         = devpts_get_sb,
214         .kill_sb        = kill_anon_super,
215 };
216
217 /*
218  * The normal naming convention is simply /dev/pts/<number>; this conforms
219  * to the System V naming convention
220  */
221
222 static struct dentry *get_node(int num)
223 {
224         char s[12];
225         struct dentry *root = devpts_root;
226         down(&root->d_inode->i_sem);
227         return lookup_one_len(s, root, sprintf(s, "%d", num));
228 }
229
230
231 int devpts_pty_new(struct tty_struct *tty)
232 {
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);
238
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);
242
243         if (!inode)
244                 return -ENOMEM;
245
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;
255
256         dentry = get_node(number);
257         if (!IS_ERR(dentry) && !dentry->d_inode)
258                 d_instantiate(dentry, inode);
259
260         up(&devpts_root->d_inode->i_sem);
261
262         return 0;
263 }
264
265 struct tty_struct *devpts_get_tty(int number)
266 {
267         struct dentry *dentry = get_node(number);
268         struct tty_struct *tty;
269
270         tty = NULL;
271         if (!IS_ERR(dentry)) {
272                 if (dentry->d_inode)
273                         tty = dentry->d_inode->u.generic_ip;
274                 dput(dentry);
275         }
276
277         up(&devpts_root->d_inode->i_sem);
278
279         return tty;
280 }
281
282 void devpts_pty_kill(int number)
283 {
284         struct dentry *dentry = get_node(number);
285
286         if (!IS_ERR(dentry)) {
287                 struct inode *inode = dentry->d_inode;
288                 if (inode) {
289                         inode->i_nlink--;
290                         d_delete(dentry);
291                         dput(dentry);
292                 }
293                 dput(dentry);
294         }
295         up(&devpts_root->d_inode->i_sem);
296 }
297
298 static int __init init_devpts_fs(void)
299 {
300         int err = register_filesystem(&devpts_fs_type);
301         if (!err) {
302                 devpts_mnt = kern_mount(&devpts_fs_type);
303                 if (IS_ERR(devpts_mnt))
304                         err = PTR_ERR(devpts_mnt);
305         }
306         return err;
307 }
308
309 static void __exit exit_devpts_fs(void)
310 {
311         unregister_filesystem(&devpts_fs_type);
312         mntput(devpts_mnt);
313 }
314
315 module_init(init_devpts_fs)
316 module_exit(exit_devpts_fs)
317 MODULE_LICENSE("GPL");