Commit | Line | Data |
---|---|---|
1da177e4 LT |
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> | |
718a9163 SB |
20 | #include <linux/mutex.h> |
21 | #include <linux/idr.h> | |
1da177e4 | 22 | #include <linux/devpts_fs.h> |
7a673c6b | 23 | #include <linux/parser.h> |
3972b7f6 | 24 | #include <linux/fsnotify.h> |
b87a267e | 25 | #include <linux/seq_file.h> |
1da177e4 LT |
26 | |
27 | #define DEVPTS_SUPER_MAGIC 0x1cd1 | |
28 | ||
b87a267e | 29 | #define DEVPTS_DEFAULT_MODE 0600 |
527b3e47 | 30 | #define PTMX_MINOR 2 |
b87a267e | 31 | |
718a9163 | 32 | extern int pty_limit; /* Config limit on Unix98 ptys */ |
718a9163 SB |
33 | static DEFINE_MUTEX(allocated_ptys_lock); |
34 | ||
1da177e4 | 35 | static struct vfsmount *devpts_mnt; |
1da177e4 | 36 | |
31af0abb | 37 | struct pts_mount_opts { |
1da177e4 LT |
38 | int setuid; |
39 | int setgid; | |
40 | uid_t uid; | |
41 | gid_t gid; | |
42 | umode_t mode; | |
31af0abb | 43 | }; |
1da177e4 | 44 | |
7a673c6b DP |
45 | enum { |
46 | Opt_uid, Opt_gid, Opt_mode, | |
47 | Opt_err | |
48 | }; | |
49 | ||
a447c093 | 50 | static const match_table_t tokens = { |
7a673c6b DP |
51 | {Opt_uid, "uid=%u"}, |
52 | {Opt_gid, "gid=%u"}, | |
53 | {Opt_mode, "mode=%o"}, | |
54 | {Opt_err, NULL} | |
55 | }; | |
56 | ||
e76b7c01 SB |
57 | struct pts_fs_info { |
58 | struct ida allocated_ptys; | |
31af0abb | 59 | struct pts_mount_opts mount_opts; |
e76b7c01 SB |
60 | }; |
61 | ||
62 | static inline struct pts_fs_info *DEVPTS_SB(struct super_block *sb) | |
63 | { | |
64 | return sb->s_fs_info; | |
65 | } | |
66 | ||
59e55e6c SB |
67 | static inline struct super_block *pts_sb_from_inode(struct inode *inode) |
68 | { | |
69 | if (inode->i_sb->s_magic == DEVPTS_SUPER_MAGIC) | |
70 | return inode->i_sb; | |
71 | ||
72 | return devpts_mnt->mnt_sb; | |
73 | } | |
74 | ||
1da177e4 LT |
75 | static int devpts_remount(struct super_block *sb, int *flags, char *data) |
76 | { | |
7a673c6b | 77 | char *p; |
31af0abb SB |
78 | struct pts_fs_info *fsi = DEVPTS_SB(sb); |
79 | struct pts_mount_opts *opts = &fsi->mount_opts; | |
7a673c6b | 80 | |
31af0abb SB |
81 | opts->setuid = 0; |
82 | opts->setgid = 0; | |
83 | opts->uid = 0; | |
84 | opts->gid = 0; | |
85 | opts->mode = DEVPTS_DEFAULT_MODE; | |
7a673c6b DP |
86 | |
87 | while ((p = strsep(&data, ",")) != NULL) { | |
88 | substring_t args[MAX_OPT_ARGS]; | |
89 | int token; | |
90 | int option; | |
91 | ||
92 | if (!*p) | |
1da177e4 | 93 | continue; |
7a673c6b DP |
94 | |
95 | token = match_token(p, tokens, args); | |
96 | switch (token) { | |
97 | case Opt_uid: | |
98 | if (match_int(&args[0], &option)) | |
99 | return -EINVAL; | |
31af0abb SB |
100 | opts->uid = option; |
101 | opts->setuid = 1; | |
7a673c6b DP |
102 | break; |
103 | case Opt_gid: | |
104 | if (match_int(&args[0], &option)) | |
105 | return -EINVAL; | |
31af0abb SB |
106 | opts->gid = option; |
107 | opts->setgid = 1; | |
7a673c6b DP |
108 | break; |
109 | case Opt_mode: | |
110 | if (match_octal(&args[0], &option)) | |
111 | return -EINVAL; | |
31af0abb | 112 | opts->mode = option & S_IALLUGO; |
7a673c6b DP |
113 | break; |
114 | default: | |
115 | printk(KERN_ERR "devpts: called with bogus options\n"); | |
1da177e4 LT |
116 | return -EINVAL; |
117 | } | |
118 | } | |
1da177e4 LT |
119 | |
120 | return 0; | |
121 | } | |
122 | ||
b87a267e MS |
123 | static int devpts_show_options(struct seq_file *seq, struct vfsmount *vfs) |
124 | { | |
31af0abb SB |
125 | struct pts_fs_info *fsi = DEVPTS_SB(vfs->mnt_sb); |
126 | struct pts_mount_opts *opts = &fsi->mount_opts; | |
127 | ||
128 | if (opts->setuid) | |
129 | seq_printf(seq, ",uid=%u", opts->uid); | |
130 | if (opts->setgid) | |
131 | seq_printf(seq, ",gid=%u", opts->gid); | |
132 | seq_printf(seq, ",mode=%03o", opts->mode); | |
b87a267e MS |
133 | |
134 | return 0; | |
135 | } | |
136 | ||
ee9b6d61 | 137 | static const struct super_operations devpts_sops = { |
1da177e4 LT |
138 | .statfs = simple_statfs, |
139 | .remount_fs = devpts_remount, | |
b87a267e | 140 | .show_options = devpts_show_options, |
1da177e4 LT |
141 | }; |
142 | ||
e76b7c01 SB |
143 | static void *new_pts_fs_info(void) |
144 | { | |
145 | struct pts_fs_info *fsi; | |
146 | ||
147 | fsi = kzalloc(sizeof(struct pts_fs_info), GFP_KERNEL); | |
148 | if (!fsi) | |
149 | return NULL; | |
150 | ||
151 | ida_init(&fsi->allocated_ptys); | |
31af0abb | 152 | fsi->mount_opts.mode = DEVPTS_DEFAULT_MODE; |
e76b7c01 SB |
153 | |
154 | return fsi; | |
155 | } | |
156 | ||
1da177e4 LT |
157 | static int |
158 | devpts_fill_super(struct super_block *s, void *data, int silent) | |
159 | { | |
160 | struct inode * inode; | |
161 | ||
162 | s->s_blocksize = 1024; | |
163 | s->s_blocksize_bits = 10; | |
164 | s->s_magic = DEVPTS_SUPER_MAGIC; | |
165 | s->s_op = &devpts_sops; | |
1da177e4 LT |
166 | s->s_time_gran = 1; |
167 | ||
e76b7c01 SB |
168 | s->s_fs_info = new_pts_fs_info(); |
169 | if (!s->s_fs_info) | |
170 | goto fail; | |
171 | ||
1da177e4 LT |
172 | inode = new_inode(s); |
173 | if (!inode) | |
e76b7c01 | 174 | goto free_fsi; |
1da177e4 LT |
175 | inode->i_ino = 1; |
176 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; | |
177 | inode->i_blocks = 0; | |
1da177e4 LT |
178 | inode->i_uid = inode->i_gid = 0; |
179 | inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR; | |
180 | inode->i_op = &simple_dir_inode_operations; | |
181 | inode->i_fop = &simple_dir_operations; | |
182 | inode->i_nlink = 2; | |
183 | ||
59e55e6c | 184 | s->s_root = d_alloc_root(inode); |
1da177e4 LT |
185 | if (s->s_root) |
186 | return 0; | |
187 | ||
188 | printk("devpts: get root dentry failed\n"); | |
189 | iput(inode); | |
e76b7c01 SB |
190 | |
191 | free_fsi: | |
192 | kfree(s->s_fs_info); | |
1da177e4 LT |
193 | fail: |
194 | return -ENOMEM; | |
195 | } | |
196 | ||
454e2398 DH |
197 | static int devpts_get_sb(struct file_system_type *fs_type, |
198 | int flags, const char *dev_name, void *data, struct vfsmount *mnt) | |
1da177e4 | 199 | { |
454e2398 | 200 | return get_sb_single(fs_type, flags, data, devpts_fill_super, mnt); |
1da177e4 LT |
201 | } |
202 | ||
e76b7c01 SB |
203 | static void devpts_kill_sb(struct super_block *sb) |
204 | { | |
205 | struct pts_fs_info *fsi = DEVPTS_SB(sb); | |
206 | ||
207 | kfree(fsi); | |
208 | kill_anon_super(sb); | |
209 | } | |
210 | ||
1da177e4 LT |
211 | static struct file_system_type devpts_fs_type = { |
212 | .owner = THIS_MODULE, | |
213 | .name = "devpts", | |
214 | .get_sb = devpts_get_sb, | |
e76b7c01 | 215 | .kill_sb = devpts_kill_sb, |
1da177e4 LT |
216 | }; |
217 | ||
218 | /* | |
219 | * The normal naming convention is simply /dev/pts/<number>; this conforms | |
220 | * to the System V naming convention | |
221 | */ | |
222 | ||
15f1a633 | 223 | int devpts_new_index(struct inode *ptmx_inode) |
718a9163 | 224 | { |
e76b7c01 SB |
225 | struct super_block *sb = pts_sb_from_inode(ptmx_inode); |
226 | struct pts_fs_info *fsi = DEVPTS_SB(sb); | |
718a9163 | 227 | int index; |
7ee7c12b | 228 | int ida_ret; |
718a9163 SB |
229 | |
230 | retry: | |
e76b7c01 | 231 | if (!ida_pre_get(&fsi->allocated_ptys, GFP_KERNEL)) { |
718a9163 SB |
232 | return -ENOMEM; |
233 | } | |
234 | ||
235 | mutex_lock(&allocated_ptys_lock); | |
e76b7c01 | 236 | ida_ret = ida_get_new(&fsi->allocated_ptys, &index); |
7ee7c12b | 237 | if (ida_ret < 0) { |
718a9163 | 238 | mutex_unlock(&allocated_ptys_lock); |
7ee7c12b | 239 | if (ida_ret == -EAGAIN) |
718a9163 SB |
240 | goto retry; |
241 | return -EIO; | |
242 | } | |
243 | ||
244 | if (index >= pty_limit) { | |
e76b7c01 | 245 | ida_remove(&fsi->allocated_ptys, index); |
718a9163 SB |
246 | mutex_unlock(&allocated_ptys_lock); |
247 | return -EIO; | |
248 | } | |
249 | mutex_unlock(&allocated_ptys_lock); | |
250 | return index; | |
251 | } | |
252 | ||
15f1a633 | 253 | void devpts_kill_index(struct inode *ptmx_inode, int idx) |
718a9163 | 254 | { |
e76b7c01 SB |
255 | struct super_block *sb = pts_sb_from_inode(ptmx_inode); |
256 | struct pts_fs_info *fsi = DEVPTS_SB(sb); | |
257 | ||
718a9163 | 258 | mutex_lock(&allocated_ptys_lock); |
e76b7c01 | 259 | ida_remove(&fsi->allocated_ptys, idx); |
718a9163 SB |
260 | mutex_unlock(&allocated_ptys_lock); |
261 | } | |
262 | ||
15f1a633 | 263 | int devpts_pty_new(struct inode *ptmx_inode, struct tty_struct *tty) |
1da177e4 | 264 | { |
718a9163 | 265 | int number = tty->index; /* tty layer puts index from devpts_new_index() in here */ |
1da177e4 LT |
266 | struct tty_driver *driver = tty->driver; |
267 | dev_t device = MKDEV(driver->major, driver->minor_start+number); | |
268 | struct dentry *dentry; | |
59e55e6c SB |
269 | struct super_block *sb = pts_sb_from_inode(ptmx_inode); |
270 | struct inode *inode = new_inode(sb); | |
271 | struct dentry *root = sb->s_root; | |
31af0abb SB |
272 | struct pts_fs_info *fsi = DEVPTS_SB(sb); |
273 | struct pts_mount_opts *opts = &fsi->mount_opts; | |
89a52e10 | 274 | char s[12]; |
1da177e4 LT |
275 | |
276 | /* We're supposed to be given the slave end of a pty */ | |
277 | BUG_ON(driver->type != TTY_DRIVER_TYPE_PTY); | |
278 | BUG_ON(driver->subtype != PTY_TYPE_SLAVE); | |
279 | ||
280 | if (!inode) | |
281 | return -ENOMEM; | |
282 | ||
283 | inode->i_ino = number+2; | |
ec4c2aac DH |
284 | inode->i_uid = config.setuid ? config.uid : current_fsuid(); |
285 | inode->i_gid = config.setgid ? config.gid : current_fsgid(); | |
1da177e4 | 286 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; |
31af0abb | 287 | init_special_inode(inode, S_IFCHR|opts->mode, device); |
8e18e294 | 288 | inode->i_private = tty; |
a6f37daa | 289 | tty->driver_data = inode; |
1da177e4 | 290 | |
89a52e10 SB |
291 | sprintf(s, "%d", number); |
292 | ||
59e55e6c | 293 | mutex_lock(&root->d_inode->i_mutex); |
89a52e10 | 294 | |
59e55e6c | 295 | dentry = d_alloc_name(root, s); |
89a52e10 SB |
296 | if (!IS_ERR(dentry)) { |
297 | d_add(dentry, inode); | |
59e55e6c | 298 | fsnotify_create(root->d_inode, dentry); |
3972b7f6 | 299 | } |
1da177e4 | 300 | |
59e55e6c | 301 | mutex_unlock(&root->d_inode->i_mutex); |
1da177e4 LT |
302 | |
303 | return 0; | |
304 | } | |
305 | ||
15f1a633 | 306 | struct tty_struct *devpts_get_tty(struct inode *pts_inode, int number) |
1da177e4 | 307 | { |
527b3e47 | 308 | BUG_ON(pts_inode->i_rdev == MKDEV(TTYAUX_MAJOR, PTMX_MINOR)); |
1da177e4 | 309 | |
527b3e47 SB |
310 | if (pts_inode->i_sb->s_magic == DEVPTS_SUPER_MAGIC) |
311 | return (struct tty_struct *)pts_inode->i_private; | |
312 | return NULL; | |
1da177e4 LT |
313 | } |
314 | ||
15f1a633 | 315 | void devpts_pty_kill(struct tty_struct *tty) |
1da177e4 | 316 | { |
a6f37daa | 317 | struct inode *inode = tty->driver_data; |
59e55e6c SB |
318 | struct super_block *sb = pts_sb_from_inode(inode); |
319 | struct dentry *root = sb->s_root; | |
a6f37daa | 320 | struct dentry *dentry; |
1da177e4 | 321 | |
a6f37daa SB |
322 | BUG_ON(inode->i_rdev == MKDEV(TTYAUX_MAJOR, PTMX_MINOR)); |
323 | ||
59e55e6c | 324 | mutex_lock(&root->d_inode->i_mutex); |
a6f37daa SB |
325 | |
326 | dentry = d_find_alias(inode); | |
327 | if (dentry && !IS_ERR(dentry)) { | |
328 | inode->i_nlink--; | |
329 | d_delete(dentry); | |
1da177e4 LT |
330 | dput(dentry); |
331 | } | |
a6f37daa | 332 | |
59e55e6c | 333 | mutex_unlock(&root->d_inode->i_mutex); |
1da177e4 LT |
334 | } |
335 | ||
336 | static int __init init_devpts_fs(void) | |
337 | { | |
338 | int err = register_filesystem(&devpts_fs_type); | |
339 | if (!err) { | |
340 | devpts_mnt = kern_mount(&devpts_fs_type); | |
341 | if (IS_ERR(devpts_mnt)) | |
342 | err = PTR_ERR(devpts_mnt); | |
343 | } | |
344 | return err; | |
345 | } | |
346 | ||
347 | static void __exit exit_devpts_fs(void) | |
348 | { | |
349 | unregister_filesystem(&devpts_fs_type); | |
350 | mntput(devpts_mnt); | |
351 | } | |
352 | ||
353 | module_init(init_devpts_fs) | |
354 | module_exit(exit_devpts_fs) | |
355 | MODULE_LICENSE("GPL"); |