1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
|
/* -*- linux-c -*- --------------------------------------------------------- *
*
* linux/fs/devpts/inode.c
*
* Copyright 1998 H. Peter Anvin -- All Rights Reserved
*
* This file is part of the Linux kernel and is made available under
* the terms of the GNU General Public License, version 2, or at your
* option, any later version, incorporated herein by reference.
*
* ------------------------------------------------------------------------- */
#include <linux/module.h>
#include <linux/string.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/kdev_t.h>
#include <linux/kernel.h>
#include <linux/locks.h>
#include <linux/major.h>
#include <linux/slab.h>
#include <linux/stat.h>
#include <linux/tty.h>
#include <asm/bitops.h>
#include <asm/uaccess.h>
#include "devpts_i.h"
static struct vfsmount *devpts_mnt;
static void devpts_put_super(struct super_block *sb)
{
struct devpts_sb_info *sbi = SBI(sb);
struct inode *inode;
int i;
for ( i = 0 ; i < sbi->max_ptys ; i++ ) {
if ( (inode = sbi->inodes[i]) ) {
if ( atomic_read(&inode->i_count) != 1 )
printk("devpts_put_super: badness: entry %d count %d\n",
i, atomic_read(&inode->i_count));
inode->i_nlink--;
iput(inode);
}
}
kfree(sbi->inodes);
kfree(sbi);
}
static int devpts_statfs(struct super_block *sb, struct statfs *buf);
static int devpts_remount (struct super_block * sb, int * flags, char * data);
static struct super_operations devpts_sops = {
put_super: devpts_put_super,
statfs: devpts_statfs,
remount_fs: devpts_remount,
};
static int devpts_parse_options(char *options, struct devpts_sb_info *sbi)
{
int setuid = 0;
int setgid = 0;
uid_t uid = 0;
gid_t gid = 0;
umode_t mode = 0600;
char *this_char, *value;
this_char = NULL;
if ( options )
this_char = strtok(options,",");
for ( ; this_char; this_char = strtok(NULL,",")) {
if ((value = strchr(this_char,'=')) != NULL)
*value++ = 0;
if (!strcmp(this_char,"uid")) {
if (!value || !*value)
return 1;
uid = simple_strtoul(value,&value,0);
if (*value)
return 1;
setuid = 1;
}
else if (!strcmp(this_char,"gid")) {
if (!value || !*value)
return 1;
gid = simple_strtoul(value,&value,0);
if (*value)
return 1;
setgid = 1;
}
else if (!strcmp(this_char,"mode")) {
if (!value || !*value)
return 1;
mode = simple_strtoul(value,&value,8);
if (*value)
return 1;
}
else
return 1;
}
sbi->setuid = setuid;
sbi->setgid = setgid;
sbi->uid = uid;
sbi->gid = gid;
sbi->mode = mode & ~S_IFMT;
return 0;
}
static int devpts_remount(struct super_block * sb, int * flags, char * data)
{
struct devpts_sb_info *sbi = sb->u.generic_sbp;
int res = devpts_parse_options(data,sbi);
if (res) {
printk("devpts: called with bogus options\n");
return -EINVAL;
}
return 0;
}
struct super_block *devpts_read_super(struct super_block *s, void *data,
int silent)
{
struct inode * inode;
struct devpts_sb_info *sbi;
sbi = (struct devpts_sb_info *) kmalloc(sizeof(struct devpts_sb_info), GFP_KERNEL);
if ( !sbi )
goto fail;
sbi->magic = DEVPTS_SBI_MAGIC;
sbi->max_ptys = unix98_max_ptys;
sbi->inodes = kmalloc(sizeof(struct inode *) * sbi->max_ptys, GFP_KERNEL);
if ( !sbi->inodes )
goto fail_free;
memset(sbi->inodes, 0, sizeof(struct inode *) * sbi->max_ptys);
if ( devpts_parse_options(data,sbi) && !silent) {
printk("devpts: called with bogus options\n");
goto fail_free;
}
inode = new_inode(s);
if (!inode)
goto fail_free;
inode->i_ino = 1;
inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
inode->i_blocks = 0;
inode->i_blksize = 1024;
inode->i_uid = inode->i_gid = 0;
inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR;
inode->i_op = &devpts_root_inode_operations;
inode->i_fop = &devpts_root_operations;
inode->i_nlink = 2;
s->u.generic_sbp = (void *) sbi;
s->s_blocksize = 1024;
s->s_blocksize_bits = 10;
s->s_magic = DEVPTS_SUPER_MAGIC;
s->s_op = &devpts_sops;
s->s_root = d_alloc_root(inode);
if (s->s_root)
return s;
printk("devpts: get root dentry failed\n");
iput(inode);
fail_free:
kfree(sbi);
fail:
return NULL;
}
static int devpts_statfs(struct super_block *sb, struct statfs *buf)
{
buf->f_type = DEVPTS_SUPER_MAGIC;
buf->f_bsize = 1024;
buf->f_namelen = NAME_MAX;
return 0;
}
static DECLARE_FSTYPE(devpts_fs_type, "devpts", devpts_read_super, FS_SINGLE);
void devpts_pty_new(int number, kdev_t device)
{
struct super_block *sb = devpts_mnt->mnt_sb;
struct devpts_sb_info *sbi = SBI(sb);
struct inode *inode;
if ( sbi->inodes[number] )
return; /* Already registered, this does happen */
inode = new_inode(sb);
if (!inode)
return;
inode->i_ino = number+2;
inode->i_blocks = 0;
inode->i_blksize = 1024;
inode->i_uid = sbi->setuid ? sbi->uid : current->fsuid;
inode->i_gid = sbi->setgid ? sbi->gid : current->fsgid;
inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
init_special_inode(inode, S_IFCHR|sbi->mode, kdev_t_to_nr(device));
if ( sbi->inodes[number] ) {
iput(inode);
return;
}
sbi->inodes[number] = inode;
}
void devpts_pty_kill(int number)
{
struct super_block *sb = devpts_mnt->mnt_sb;
struct devpts_sb_info *sbi = SBI(sb);
struct inode *inode = sbi->inodes[number];
if ( inode ) {
sbi->inodes[number] = NULL;
inode->i_nlink--;
iput(inode);
}
}
int __init init_devpts_fs(void)
{
int err = register_filesystem(&devpts_fs_type);
if (!err) {
devpts_mnt = kern_mount(&devpts_fs_type);
err = PTR_ERR(devpts_mnt);
if (!IS_ERR(devpts_mnt))
err = 0;
}
return err;
}
#ifdef MODULE
int init_module(void)
{
int err = init_devpts_fs();
if ( !err ) {
devpts_upcall_new = devpts_pty_new;
devpts_upcall_kill = devpts_pty_kill;
}
return err;
}
void cleanup_module(void)
{
devpts_upcall_new = NULL;
devpts_upcall_kill = NULL;
unregister_filesystem(&devpts_fs_type);
kern_umount(devpts_mnt);
}
#endif
|