patch-2.6.6-vs1.9.0
[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/vinline.h>
22 #include "xattr.h"
23
24 #define DEVPTS_SUPER_MAGIC 0x1cd1
25
26 static struct vfsmount *devpts_mnt;
27 static struct dentry *devpts_root;
28
29 static struct {
30         int setuid;
31         int setgid;
32         uid_t   uid;
33         gid_t   gid;
34         umode_t mode;
35 } config = {.mode = 0600};
36
37 static int devpts_remount(struct super_block *sb, int *flags, char *data)
38 {
39         int setuid = 0;
40         int setgid = 0;
41         uid_t uid = 0;
42         gid_t gid = 0;
43         umode_t mode = 0600;
44         char *this_char;
45
46         this_char = NULL;
47         while ((this_char = strsep(&data, ",")) != NULL) {
48                 int n;
49                 char dummy;
50                 if (!*this_char)
51                         continue;
52                 if (sscanf(this_char, "uid=%i%c", &n, &dummy) == 1) {
53                         setuid = 1;
54                         uid = n;
55                 } else if (sscanf(this_char, "gid=%i%c", &n, &dummy) == 1) {
56                         setgid = 1;
57                         gid = n;
58                 } else if (sscanf(this_char, "mode=%o%c", &n, &dummy) == 1)
59                         mode = n & ~S_IFMT;
60                 else {
61                         printk("devpts: called with bogus options\n");
62                         return -EINVAL;
63                 }
64         }
65         config.setuid  = setuid;
66         config.setgid  = setgid;
67         config.uid     = uid;
68         config.gid     = gid;
69         config.mode    = mode;
70
71         return 0;
72 }
73
74 static struct super_operations devpts_sops = {
75         .statfs         = simple_statfs,
76         .remount_fs     = devpts_remount,
77 };
78
79 static int
80 devpts_fill_super(struct super_block *s, void *data, int silent)
81 {
82         struct inode * inode;
83
84         s->s_blocksize = 1024;
85         s->s_blocksize_bits = 10;
86         s->s_magic = DEVPTS_SUPER_MAGIC;
87         s->s_op = &devpts_sops;
88
89         inode = new_inode(s);
90         if (!inode)
91                 goto fail;
92         inode->i_ino = 1;
93         inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
94         inode->i_blocks = 0;
95         inode->i_blksize = 1024;
96         inode->i_uid = inode->i_gid = 0;
97         inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR;
98         inode->i_op = &simple_dir_inode_operations;
99         inode->i_fop = &simple_dir_operations;
100         inode->i_nlink = 2;
101
102         devpts_root = s->s_root = d_alloc_root(inode);
103         if (s->s_root)
104                 return 0;
105         
106         printk("devpts: get root dentry failed\n");
107         iput(inode);
108 fail:
109         return -ENOMEM;
110 }
111
112 static struct super_block *devpts_get_sb(struct file_system_type *fs_type,
113         int flags, const char *dev_name, void *data)
114 {
115         return get_sb_single(fs_type, flags, data, devpts_fill_super);
116 }
117
118 static struct file_system_type devpts_fs_type = {
119         .owner          = THIS_MODULE,
120         .name           = "devpts",
121         .get_sb         = devpts_get_sb,
122         .kill_sb        = kill_anon_super,
123 };
124
125 /*
126  * The normal naming convention is simply /dev/pts/<number>; this conforms
127  * to the System V naming convention
128  */
129
130 static struct dentry *get_node(int num)
131 {
132         char s[12];
133         struct dentry *root = devpts_root;
134         down(&root->d_inode->i_sem);
135         return lookup_one_len(s, root, sprintf(s, "%d", num));
136 }
137
138 static int devpts_permission(struct inode *inode, int mask, struct nameidata *nd)
139 {
140         int ret = -EACCES;
141         
142         if (vx_check(inode->i_xid, VX_IDENT))
143                 ret = vfs_permission(inode, mask);
144         return ret;
145 }
146
147 static struct inode_operations devpts_file_inode_operations = {
148         .setxattr       = devpts_setxattr,
149         .getxattr       = devpts_getxattr,
150         .listxattr      = devpts_listxattr,
151         .removexattr    = devpts_removexattr,
152         .permission     = devpts_permission,
153 };
154
155 int devpts_pty_new(struct tty_struct *tty)
156 {
157         int number = tty->index;
158         struct tty_driver *driver = tty->driver;
159         dev_t device = MKDEV(driver->major, driver->minor_start+number);
160         struct dentry *dentry;
161         struct inode *inode = new_inode(devpts_mnt->mnt_sb);
162
163         /* We're supposed to be given the slave end of a pty */
164         BUG_ON(driver->type != TTY_DRIVER_TYPE_PTY);
165         BUG_ON(driver->subtype != PTY_TYPE_SLAVE);
166
167         if (!inode)
168                 return -ENOMEM;
169
170         inode->i_ino = number+2;
171         inode->i_blksize = 1024;
172         inode->i_uid = config.setuid ? config.uid : current->fsuid;
173         inode->i_gid = config.setgid ? config.gid : current->fsgid;
174         inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
175         init_special_inode(inode, S_IFCHR|config.mode, device);
176         inode->i_xid = vx_current_xid();
177         inode->i_op = &devpts_file_inode_operations;
178         inode->u.generic_ip = tty;
179
180         dentry = get_node(number);
181         if (!IS_ERR(dentry) && !dentry->d_inode)
182                 d_instantiate(dentry, inode);
183
184         up(&devpts_root->d_inode->i_sem);
185
186         return 0;
187 }
188
189 struct tty_struct *devpts_get_tty(int number)
190 {
191         struct dentry *dentry = get_node(number);
192         struct tty_struct *tty;
193
194         tty = (IS_ERR(dentry) || !dentry->d_inode) ? NULL :
195                         dentry->d_inode->u.generic_ip;
196
197         up(&devpts_root->d_inode->i_sem);
198
199         return tty;
200 }
201
202 void devpts_pty_kill(int number)
203 {
204         struct dentry *dentry = get_node(number);
205
206         if (!IS_ERR(dentry)) {
207                 struct inode *inode = dentry->d_inode;
208                 if (inode) {
209                         inode->i_nlink--;
210                         d_delete(dentry);
211                         dput(dentry);
212                 }
213                 dput(dentry);
214         }
215         up(&devpts_root->d_inode->i_sem);
216 }
217
218 static int __init init_devpts_fs(void)
219 {
220         int err = init_devpts_xattr();
221         if (err)
222                 return err;
223         err = register_filesystem(&devpts_fs_type);
224         if (!err) {
225                 devpts_mnt = kern_mount(&devpts_fs_type);
226                 if (IS_ERR(devpts_mnt))
227                         err = PTR_ERR(devpts_mnt);
228         }
229         return err;
230 }
231
232 static void __exit exit_devpts_fs(void)
233 {
234         unregister_filesystem(&devpts_fs_type);
235         mntput(devpts_mnt);
236         exit_devpts_xattr();
237 }
238
239 module_init(init_devpts_fs)
240 module_exit(exit_devpts_fs)
241 MODULE_LICENSE("GPL");