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