blob: 06ef9a255c76f7c8657176bc850b5f4c81909f75 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* -*- 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>
Domen Puncer7a673c62006-03-23 03:00:59 -080021#include <linux/parser.h>
Florin Malita3972b7f2007-05-08 00:24:18 -070022#include <linux/fsnotify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24#define DEVPTS_SUPER_MAGIC 0x1cd1
25
Linus Torvalds1da177e2005-04-16 15:20:36 -070026static struct vfsmount *devpts_mnt;
27static struct dentry *devpts_root;
28
29static struct {
30 int setuid;
31 int setgid;
32 uid_t uid;
33 gid_t gid;
34 umode_t mode;
35} config = {.mode = 0600};
36
Domen Puncer7a673c62006-03-23 03:00:59 -080037enum {
38 Opt_uid, Opt_gid, Opt_mode,
39 Opt_err
40};
41
42static match_table_t tokens = {
43 {Opt_uid, "uid=%u"},
44 {Opt_gid, "gid=%u"},
45 {Opt_mode, "mode=%o"},
46 {Opt_err, NULL}
47};
48
Linus Torvalds1da177e2005-04-16 15:20:36 -070049static int devpts_remount(struct super_block *sb, int *flags, char *data)
50{
Domen Puncer7a673c62006-03-23 03:00:59 -080051 char *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Domen Puncer7a673c62006-03-23 03:00:59 -080053 config.setuid = 0;
54 config.setgid = 0;
55 config.uid = 0;
56 config.gid = 0;
57 config.mode = 0600;
58
59 while ((p = strsep(&data, ",")) != NULL) {
60 substring_t args[MAX_OPT_ARGS];
61 int token;
62 int option;
63
64 if (!*p)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 continue;
Domen Puncer7a673c62006-03-23 03:00:59 -080066
67 token = match_token(p, tokens, args);
68 switch (token) {
69 case Opt_uid:
70 if (match_int(&args[0], &option))
71 return -EINVAL;
72 config.uid = option;
73 config.setuid = 1;
74 break;
75 case Opt_gid:
76 if (match_int(&args[0], &option))
77 return -EINVAL;
78 config.gid = option;
79 config.setgid = 1;
80 break;
81 case Opt_mode:
82 if (match_octal(&args[0], &option))
83 return -EINVAL;
84 config.mode = option & ~S_IFMT;
85 break;
86 default:
87 printk(KERN_ERR "devpts: called with bogus options\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 return -EINVAL;
89 }
90 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92 return 0;
93}
94
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -080095static const struct super_operations devpts_sops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 .statfs = simple_statfs,
97 .remount_fs = devpts_remount,
98};
99
100static int
101devpts_fill_super(struct super_block *s, void *data, int silent)
102{
103 struct inode * inode;
104
105 s->s_blocksize = 1024;
106 s->s_blocksize_bits = 10;
107 s->s_magic = DEVPTS_SUPER_MAGIC;
108 s->s_op = &devpts_sops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 s->s_time_gran = 1;
110
111 inode = new_inode(s);
112 if (!inode)
113 goto fail;
114 inode->i_ino = 1;
115 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
116 inode->i_blocks = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 inode->i_uid = inode->i_gid = 0;
118 inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR;
119 inode->i_op = &simple_dir_inode_operations;
120 inode->i_fop = &simple_dir_operations;
121 inode->i_nlink = 2;
122
123 devpts_root = s->s_root = d_alloc_root(inode);
124 if (s->s_root)
125 return 0;
126
127 printk("devpts: get root dentry failed\n");
128 iput(inode);
129fail:
130 return -ENOMEM;
131}
132
David Howells454e2392006-06-23 02:02:57 -0700133static int devpts_get_sb(struct file_system_type *fs_type,
134 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135{
David Howells454e2392006-06-23 02:02:57 -0700136 return get_sb_single(fs_type, flags, data, devpts_fill_super, mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137}
138
139static struct file_system_type devpts_fs_type = {
140 .owner = THIS_MODULE,
141 .name = "devpts",
142 .get_sb = devpts_get_sb,
143 .kill_sb = kill_anon_super,
144};
145
146/*
147 * The normal naming convention is simply /dev/pts/<number>; this conforms
148 * to the System V naming convention
149 */
150
151static struct dentry *get_node(int num)
152{
153 char s[12];
154 struct dentry *root = devpts_root;
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800155 mutex_lock(&root->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 return lookup_one_len(s, root, sprintf(s, "%d", num));
157}
158
159int devpts_pty_new(struct tty_struct *tty)
160{
161 int number = tty->index;
162 struct tty_driver *driver = tty->driver;
163 dev_t device = MKDEV(driver->major, driver->minor_start+number);
164 struct dentry *dentry;
165 struct inode *inode = new_inode(devpts_mnt->mnt_sb);
166
167 /* We're supposed to be given the slave end of a pty */
168 BUG_ON(driver->type != TTY_DRIVER_TYPE_PTY);
169 BUG_ON(driver->subtype != PTY_TYPE_SLAVE);
170
171 if (!inode)
172 return -ENOMEM;
173
174 inode->i_ino = number+2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 inode->i_uid = config.setuid ? config.uid : current->fsuid;
176 inode->i_gid = config.setgid ? config.gid : current->fsgid;
177 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
178 init_special_inode(inode, S_IFCHR|config.mode, device);
Theodore Ts'o8e18e292006-09-27 01:50:46 -0700179 inode->i_private = tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
181 dentry = get_node(number);
Florin Malita3972b7f2007-05-08 00:24:18 -0700182 if (!IS_ERR(dentry) && !dentry->d_inode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 d_instantiate(dentry, inode);
Florin Malita3972b7f2007-05-08 00:24:18 -0700184 fsnotify_create(devpts_root->d_inode, dentry);
185 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800187 mutex_unlock(&devpts_root->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
189 return 0;
190}
191
192struct tty_struct *devpts_get_tty(int number)
193{
194 struct dentry *dentry = get_node(number);
195 struct tty_struct *tty;
196
197 tty = NULL;
198 if (!IS_ERR(dentry)) {
199 if (dentry->d_inode)
Theodore Ts'o8e18e292006-09-27 01:50:46 -0700200 tty = dentry->d_inode->i_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 dput(dentry);
202 }
203
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800204 mutex_unlock(&devpts_root->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
206 return tty;
207}
208
209void devpts_pty_kill(int number)
210{
211 struct dentry *dentry = get_node(number);
212
213 if (!IS_ERR(dentry)) {
214 struct inode *inode = dentry->d_inode;
215 if (inode) {
216 inode->i_nlink--;
217 d_delete(dentry);
218 dput(dentry);
219 }
220 dput(dentry);
221 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800222 mutex_unlock(&devpts_root->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223}
224
225static int __init init_devpts_fs(void)
226{
227 int err = register_filesystem(&devpts_fs_type);
228 if (!err) {
229 devpts_mnt = kern_mount(&devpts_fs_type);
230 if (IS_ERR(devpts_mnt))
231 err = PTR_ERR(devpts_mnt);
232 }
233 return err;
234}
235
236static void __exit exit_devpts_fs(void)
237{
238 unregister_filesystem(&devpts_fs_type);
239 mntput(devpts_mnt);
240}
241
242module_init(init_devpts_fs)
243module_exit(exit_devpts_fs)
244MODULE_LICENSE("GPL");