vserver 2.0 rc7
[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_filter(struct dentry *de)
100 {
101         return vx_check(de->d_inode->i_xid, VX_IDENT);
102 }
103
104 static int devpts_readdir(struct file * filp, void * dirent, filldir_t filldir)
105 {
106         return dcache_readdir_filter(filp, dirent, filldir, devpts_filter);
107 }
108
109 static struct file_operations devpts_dir_operations = {
110         .open           = dcache_dir_open,
111         .release        = dcache_dir_close,
112         .llseek         = dcache_dir_lseek,
113         .read           = generic_read_dir,
114         .readdir        = devpts_readdir,
115 };
116
117 static struct super_operations devpts_sops = {
118         .statfs         = simple_statfs,
119         .remount_fs     = devpts_remount,
120 };
121
122 static int
123 devpts_fill_super(struct super_block *s, void *data, int silent)
124 {
125         struct inode * inode;
126
127         s->s_blocksize = 1024;
128         s->s_blocksize_bits = 10;
129         s->s_magic = DEVPTS_SUPER_MAGIC;
130         s->s_op = &devpts_sops;
131         s->s_xattr = devpts_xattr_handlers;
132         s->s_time_gran = 1;
133
134         inode = new_inode(s);
135         if (!inode)
136                 goto fail;
137         inode->i_ino = 1;
138         inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
139         inode->i_blocks = 0;
140         inode->i_blksize = 1024;
141         inode->i_uid = inode->i_gid = 0;
142         inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR;
143         inode->i_op = &simple_dir_inode_operations;
144         inode->i_fop = &devpts_dir_operations;
145         inode->i_nlink = 2;
146         inode->i_xid = vx_current_xid();
147
148         devpts_root = s->s_root = d_alloc_root(inode);
149         if (s->s_root)
150                 return 0;
151         
152         printk("devpts: get root dentry failed\n");
153         iput(inode);
154 fail:
155         return -ENOMEM;
156 }
157
158 static struct super_block *devpts_get_sb(struct file_system_type *fs_type,
159         int flags, const char *dev_name, void *data)
160 {
161         return get_sb_single(fs_type, flags, data, devpts_fill_super);
162 }
163
164 static struct file_system_type devpts_fs_type = {
165         .owner          = THIS_MODULE,
166         .name           = "devpts",
167         .get_sb         = devpts_get_sb,
168         .kill_sb        = kill_anon_super,
169 };
170
171 /*
172  * The normal naming convention is simply /dev/pts/<number>; this conforms
173  * to the System V naming convention
174  */
175
176 static struct dentry *get_node(int num)
177 {
178         char s[12];
179         struct dentry *root = devpts_root;
180         down(&root->d_inode->i_sem);
181         return lookup_one_len(s, root, sprintf(s, "%d", num));
182 }
183
184 int devpts_pty_new(struct tty_struct *tty)
185 {
186         int number = tty->index;
187         struct tty_driver *driver = tty->driver;
188         dev_t device = MKDEV(driver->major, driver->minor_start+number);
189         struct dentry *dentry;
190         struct inode *inode = new_inode(devpts_mnt->mnt_sb);
191
192         /* We're supposed to be given the slave end of a pty */
193         BUG_ON(driver->type != TTY_DRIVER_TYPE_PTY);
194         BUG_ON(driver->subtype != PTY_TYPE_SLAVE);
195
196         if (!inode)
197                 return -ENOMEM;
198
199         inode->i_ino = number+2;
200         inode->i_blksize = 1024;
201         inode->i_uid = config.setuid ? config.uid : current->fsuid;
202         inode->i_gid = config.setgid ? config.gid : current->fsgid;
203         inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
204         init_special_inode(inode, S_IFCHR|config.mode, device);
205         inode->i_xid = vx_current_xid();
206         inode->i_op = &devpts_file_inode_operations;
207         inode->u.generic_ip = tty;
208
209         dentry = get_node(number);
210         if (!IS_ERR(dentry) && !dentry->d_inode)
211                 d_instantiate(dentry, inode);
212
213         up(&devpts_root->d_inode->i_sem);
214
215         return 0;
216 }
217
218 struct tty_struct *devpts_get_tty(int number)
219 {
220         struct dentry *dentry = get_node(number);
221         struct tty_struct *tty;
222
223         tty = NULL;
224         if (!IS_ERR(dentry)) {
225                 if (dentry->d_inode)
226                         tty = dentry->d_inode->u.generic_ip;
227                 dput(dentry);
228         }
229
230         up(&devpts_root->d_inode->i_sem);
231
232         return tty;
233 }
234
235 void devpts_pty_kill(int number)
236 {
237         struct dentry *dentry = get_node(number);
238
239         if (!IS_ERR(dentry)) {
240                 struct inode *inode = dentry->d_inode;
241                 if (inode) {
242                         inode->i_nlink--;
243                         d_delete(dentry);
244                         dput(dentry);
245                 }
246                 dput(dentry);
247         }
248         up(&devpts_root->d_inode->i_sem);
249 }
250
251 static int __init init_devpts_fs(void)
252 {
253         int err = register_filesystem(&devpts_fs_type);
254         if (!err) {
255                 devpts_mnt = kern_mount(&devpts_fs_type);
256                 if (IS_ERR(devpts_mnt))
257                         err = PTR_ERR(devpts_mnt);
258         }
259         return err;
260 }
261
262 static void __exit exit_devpts_fs(void)
263 {
264         unregister_filesystem(&devpts_fs_type);
265         mntput(devpts_mnt);
266 }
267
268 module_init(init_devpts_fs)
269 module_exit(exit_devpts_fs)
270 MODULE_LICENSE("GPL");