vserver 1.9.5.x5
[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/xattr.h>
22
23 extern struct xattr_handler devpts_xattr_security_handler;
24
25 static struct xattr_handler *devpts_xattr_handlers[] = {
26 #ifdef CONFIG_DEVPTS_FS_SECURITY
27         &devpts_xattr_security_handler,
28 #endif
29         NULL
30 };
31
32 static int devpts_permission(struct inode *inode, int mask, struct nameidata *nd)
33 {
34         int ret = -EACCES;
35
36         if (vx_check(inode->i_xid, VX_IDENT))
37                 ret = generic_permission(inode, mask, NULL);
38         return ret;
39 }
40
41 static struct inode_operations devpts_file_inode_operations = {
42 #ifdef CONFIG_DEVPTS_FS_XATTR
43         .setxattr       = generic_setxattr,
44         .getxattr       = generic_getxattr,
45         .listxattr      = generic_listxattr,
46         .removexattr    = generic_removexattr,
47 #endif
48         .permission     = devpts_permission,
49 };
50
51 static struct vfsmount *devpts_mnt;
52 static struct dentry *devpts_root;
53
54 static struct {
55         int setuid;
56         int setgid;
57         uid_t   uid;
58         gid_t   gid;
59         umode_t mode;
60 } config = {.mode = 0600};
61
62 static int devpts_remount(struct super_block *sb, int *flags, char *data)
63 {
64         int setuid = 0;
65         int setgid = 0;
66         uid_t uid = 0;
67         gid_t gid = 0;
68         umode_t mode = 0600;
69         char *this_char;
70
71         this_char = NULL;
72         while ((this_char = strsep(&data, ",")) != NULL) {
73                 int n;
74                 char dummy;
75                 if (!*this_char)
76                         continue;
77                 if (sscanf(this_char, "uid=%i%c", &n, &dummy) == 1) {
78                         setuid = 1;
79                         uid = n;
80                 } else if (sscanf(this_char, "gid=%i%c", &n, &dummy) == 1) {
81                         setgid = 1;
82                         gid = n;
83                 } else if (sscanf(this_char, "mode=%o%c", &n, &dummy) == 1)
84                         mode = n & ~S_IFMT;
85                 else {
86                         printk("devpts: called with bogus options\n");
87                         return -EINVAL;
88                 }
89         }
90         config.setuid  = setuid;
91         config.setgid  = setgid;
92         config.uid     = uid;
93         config.gid     = gid;
94         config.mode    = mode;
95
96         return 0;
97 }
98
99 static int devpts_readdir(struct file * filp, void * dirent, filldir_t filldir)
100 {
101         struct dentry *dentry = filp->f_dentry;
102         struct dentry *cursor = filp->private_data;
103         struct list_head *p, *q = &cursor->d_child;
104         ino_t ino;
105         int i = filp->f_pos;
106
107         switch (i) {
108                 case 0:
109                         ino = dentry->d_inode->i_ino;
110                         if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
111                                 break;
112                         filp->f_pos++;
113                         i++;
114                         /* fallthrough */
115                 case 1:
116                         ino = parent_ino(dentry);
117                         if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
118                                 break;
119                         filp->f_pos++;
120                         i++;
121                         /* fallthrough */
122                 default:
123                         spin_lock(&dcache_lock);
124                         if (filp->f_pos == 2) {
125                                 list_del(q);
126                                 list_add(q, &dentry->d_subdirs);
127                         }
128                         for (p=q->next; p != &dentry->d_subdirs; p=p->next) {
129                                 struct dentry *next;
130                                 next = list_entry(p, struct dentry, d_child);
131                                 if (d_unhashed(next) || !next->d_inode)
132                                         continue;
133                                 if (!vx_check(next->d_inode->i_xid, VX_IDENT))
134                                         continue;
135
136                                 spin_unlock(&dcache_lock);
137                                 if (filldir(dirent, next->d_name.name,
138                                         next->d_name.len, filp->f_pos,
139                                         next->d_inode->i_ino, DT_CHR) < 0)
140                                         return 0;
141                                 spin_lock(&dcache_lock);
142                                 /* next is still alive */
143                                 list_del(q);
144                                 list_add(q, p);
145                                 p = q;
146                                 filp->f_pos++;
147                         }
148                         spin_unlock(&dcache_lock);
149         }
150         return 0;
151 }
152
153 static struct file_operations devpts_dir_operations = {
154         .open           = dcache_dir_open,
155         .release        = dcache_dir_close,
156         .llseek         = dcache_dir_lseek,
157         .read           = generic_read_dir,
158         .readdir        = devpts_readdir,
159 };
160
161 static struct super_operations devpts_sops = {
162         .statfs         = simple_statfs,
163         .remount_fs     = devpts_remount,
164 };
165
166 static int
167 devpts_fill_super(struct super_block *s, void *data, int silent)
168 {
169         struct inode * inode;
170
171         s->s_blocksize = 1024;
172         s->s_blocksize_bits = 10;
173         s->s_magic = DEVPTS_SUPER_MAGIC;
174         s->s_op = &devpts_sops;
175         s->s_xattr = devpts_xattr_handlers;
176         s->s_time_gran = 1;
177
178         inode = new_inode(s);
179         if (!inode)
180                 goto fail;
181         inode->i_ino = 1;
182         inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
183         inode->i_blocks = 0;
184         inode->i_blksize = 1024;
185         inode->i_uid = inode->i_gid = 0;
186         inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR;
187         inode->i_op = &simple_dir_inode_operations;
188         inode->i_fop = &devpts_dir_operations;
189         inode->i_nlink = 2;
190         inode->i_xid = vx_current_xid();
191
192         devpts_root = s->s_root = d_alloc_root(inode);
193         if (s->s_root)
194                 return 0;
195         
196         printk("devpts: get root dentry failed\n");
197         iput(inode);
198 fail:
199         return -ENOMEM;
200 }
201
202 static struct super_block *devpts_get_sb(struct file_system_type *fs_type,
203         int flags, const char *dev_name, void *data)
204 {
205         return get_sb_single(fs_type, flags, data, devpts_fill_super);
206 }
207
208 static struct file_system_type devpts_fs_type = {
209         .owner          = THIS_MODULE,
210         .name           = "devpts",
211         .get_sb         = devpts_get_sb,
212         .kill_sb        = kill_anon_super,
213 };
214
215 /*
216  * The normal naming convention is simply /dev/pts/<number>; this conforms
217  * to the System V naming convention
218  */
219
220 static struct dentry *get_node(int num)
221 {
222         char s[12];
223         struct dentry *root = devpts_root;
224         down(&root->d_inode->i_sem);
225         return lookup_one_len(s, root, sprintf(s, "%d", num));
226 }
227
228 int devpts_pty_new(struct tty_struct *tty)
229 {
230         int number = tty->index;
231         struct tty_driver *driver = tty->driver;
232         dev_t device = MKDEV(driver->major, driver->minor_start+number);
233         struct dentry *dentry;
234         struct inode *inode = new_inode(devpts_mnt->mnt_sb);
235
236         /* We're supposed to be given the slave end of a pty */
237         BUG_ON(driver->type != TTY_DRIVER_TYPE_PTY);
238         BUG_ON(driver->subtype != PTY_TYPE_SLAVE);
239
240         if (!inode)
241                 return -ENOMEM;
242
243         inode->i_ino = number+2;
244         inode->i_blksize = 1024;
245         inode->i_uid = config.setuid ? config.uid : current->fsuid;
246         inode->i_gid = config.setgid ? config.gid : current->fsgid;
247         inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
248         init_special_inode(inode, S_IFCHR|config.mode, device);
249         inode->i_xid = vx_current_xid();
250         inode->i_op = &devpts_file_inode_operations;
251         inode->u.generic_ip = tty;
252
253         dentry = get_node(number);
254         if (!IS_ERR(dentry) && !dentry->d_inode)
255                 d_instantiate(dentry, inode);
256
257         up(&devpts_root->d_inode->i_sem);
258
259         return 0;
260 }
261
262 struct tty_struct *devpts_get_tty(int number)
263 {
264         struct dentry *dentry = get_node(number);
265         struct tty_struct *tty;
266
267         tty = NULL;
268         if (!IS_ERR(dentry)) {
269                 if (dentry->d_inode)
270                         tty = dentry->d_inode->u.generic_ip;
271                 dput(dentry);
272         }
273
274         up(&devpts_root->d_inode->i_sem);
275
276         return tty;
277 }
278
279 void devpts_pty_kill(int number)
280 {
281         struct dentry *dentry = get_node(number);
282
283         if (!IS_ERR(dentry)) {
284                 struct inode *inode = dentry->d_inode;
285                 if (inode) {
286                         inode->i_nlink--;
287                         d_delete(dentry);
288                         dput(dentry);
289                 }
290                 dput(dentry);
291         }
292         up(&devpts_root->d_inode->i_sem);
293 }
294
295 static int __init init_devpts_fs(void)
296 {
297         int err = register_filesystem(&devpts_fs_type);
298         if (!err) {
299                 devpts_mnt = kern_mount(&devpts_fs_type);
300                 if (IS_ERR(devpts_mnt))
301                         err = PTR_ERR(devpts_mnt);
302         }
303         return err;
304 }
305
306 static void __exit exit_devpts_fs(void)
307 {
308         unregister_filesystem(&devpts_fs_type);
309         mntput(devpts_mnt);
310 }
311
312 module_init(init_devpts_fs)
313 module_exit(exit_devpts_fs)
314 MODULE_LICENSE("GPL");