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