forked from ish-app/ish
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pty.c
297 lines (259 loc) · 7.97 KB
/
pty.c
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#include <string.h>
#include <limits.h>
#include <sys/stat.h>
#include <ctype.h>
#include "kernel/task.h"
#include "kernel/errno.h"
#include "fs/tty.h"
#include "fs/devices.h"
extern struct tty_driver pty_slave;
// the master holds a reference to the slave, so the slave will always be cleaned up second
// when the master cleans up it hangs up the slave, making any operation that references the master unreachable
static void pty_slave_init_inode(struct tty *tty) {
tty->pty.uid = current->euid;
// TODO make these mount options
tty->pty.gid = current->egid;
tty->pty.perms = 0620;
}
static int pty_master_init(struct tty *tty) {
tty->termios.iflags = 0;
tty->termios.oflags = 0;
tty->termios.lflags = 0;
struct tty *slave = tty_alloc(&pty_slave, TTY_PSEUDO_SLAVE_MAJOR, tty->num);
slave->refcount = 1;
pty_slave.ttys[tty->num] = slave;
tty->pty.other = slave;
slave->pty.other = tty;
slave->pty.locked = true;
pty_slave_init_inode(slave);
return 0;
}
static void pty_master_cleanup(struct tty *tty) {
struct tty *slave = tty->pty.other;
slave->pty.other = NULL;
lock(&slave->lock);
tty_hangup(slave);
unlock(&slave->lock);
tty_release(slave);
}
static int pty_slave_open(struct tty *tty) {
if (tty->pty.other == NULL)
return _EIO;
if (tty->pty.locked)
return _EIO;
return 0;
}
static int pty_master_ioctl(struct tty *tty, int cmd, void *arg) {
struct tty *slave = tty->pty.other;
switch (cmd) {
case TIOCSPTLCK_:
slave->pty.locked = !!*(dword_t *) arg;
break;
case TIOCGPTN_:
*(dword_t *) arg = slave->num;
break;
case TIOCPKT_:
tty->pty.packet_mode = !!*(dword_t *) arg;
break;
case TIOCGPKT_:
*(dword_t *) arg = tty->pty.packet_mode;
break;
default:
return _ENOTTY;
}
return 0;
}
static int pty_write(struct tty *tty, const void *buf, size_t len, bool blocking) {
return tty_input(tty->pty.other, buf, len, blocking);
}
static int pty_return_eio(struct tty *UNUSED(tty)) {
return _EIO;
}
#define MAX_PTYS (1 << 12)
const struct tty_driver_ops pty_master_ops = {
.init = pty_master_init,
.open = pty_return_eio,
.write = pty_write,
.ioctl = pty_master_ioctl,
.cleanup = pty_master_cleanup,
};
DEFINE_TTY_DRIVER(pty_master, &pty_master_ops, TTY_PSEUDO_MASTER_MAJOR, MAX_PTYS);
const struct tty_driver_ops pty_slave_ops = {
.init = pty_return_eio,
.open = pty_slave_open,
.write = pty_write,
};
DEFINE_TTY_DRIVER(pty_slave, &pty_slave_ops, TTY_PSEUDO_SLAVE_MAJOR, MAX_PTYS);
static int pty_reserve_next() {
int pty_num;
lock(&ttys_lock);
for (pty_num = 0; pty_num < MAX_PTYS; pty_num++) {
if (pty_slave.ttys[pty_num] == NULL)
break;
}
pty_slave.ttys[pty_num] = (void *) 1; // anything non-null to reserve it
unlock(&ttys_lock);
return pty_num;
}
int ptmx_open(struct fd *fd) {
int pty_num = pty_reserve_next();
if (pty_num == MAX_PTYS)
return _ENOSPC;
struct tty *master = tty_get(&pty_master, TTY_PSEUDO_MASTER_MAJOR, pty_num);
if (IS_ERR(master))
return PTR_ERR(master);
return tty_open(master, fd);
}
struct tty *pty_open_fake(struct tty_driver *driver) {
int pty_num = pty_reserve_next();
if (pty_num == MAX_PTYS)
return ERR_PTR(_ENOSPC);
// TODO this is a bit of a hack
driver->ttys = pty_slave.ttys;
driver->limit = pty_slave.limit;
driver->major = TTY_PSEUDO_SLAVE_MAJOR;
struct tty *tty = tty_get(driver, TTY_PSEUDO_SLAVE_MAJOR, pty_num);
if (IS_ERR(tty))
return tty;
pty_slave_init_inode(tty);
return tty;
}
static bool isdigits(const char *str) {
for (int i = 0; str[i] != '\0'; i++)
if (!isdigit(str[i]))
return false;
return true;
}
static const struct fd_ops devpts_fdops;
static bool devpts_pty_exists(int pty_num) {
if (pty_num < 0 || pty_num > MAX_PTYS)
return false;
lock(&ttys_lock);
bool exists = pty_slave.ttys[pty_num] != NULL;
unlock(&ttys_lock);
return exists;
}
// this has a slightly weird error returning convention
// I'm lucky that ENOENT is -2 and not -1
static int devpts_get_pty_num(const char *path) {
if (strcmp(path, "") == 0)
return -1; // root
if (path[0] != '/' || path[1] == '\0' || strchr(path + 1, '/') != NULL)
return _ENOENT;
// there's one path component here, which had better be a pty number
const char *name = path + 1; // skip the initial /
if (!isdigits(name))
return _ENOENT;
// it's not possible to correctly use atoi
long pty_long = atol(name);
if (pty_long > INT_MAX)
return _ENOENT;
int pty_num = (int) pty_long;
if (!devpts_pty_exists(pty_num))
return _ENOENT;
return pty_num;
}
static struct fd *devpts_open(struct mount *UNUSED(mount), const char *path, int UNUSED(flags), int UNUSED(mode)) {
int pty_num = devpts_get_pty_num(path);
if (pty_num == _ENOENT)
return ERR_PTR(_ENOENT);
struct fd *fd = fd_create(&devpts_fdops);
fd->devpts.num = pty_num;
return fd;
}
static int devpts_getpath(struct fd *fd, char *buf) {
if (fd->devpts.num == -1)
strcpy(buf, "");
else
sprintf(buf, "/%d", fd->devpts.num);
return 0;
}
static void devpts_stat_num(int pty_num, struct statbuf *stat) {
if (pty_num == -1) {
// root
stat->mode = S_IFDIR | 0755;
stat->inode = 1;
} else {
lock(&ttys_lock);
struct tty *tty = pty_slave.ttys[pty_num];
assert(tty != NULL);
lock(&tty->lock);
stat->mode = S_IFCHR | tty->pty.perms;
stat->uid = tty->pty.uid;
stat->gid = tty->pty.gid;
stat->inode = pty_num + 3;
stat->rdev = dev_make(TTY_PSEUDO_SLAVE_MAJOR, pty_num);
unlock(&tty->lock);
unlock(&ttys_lock);
}
}
static int devpts_setattr_num(int pty_num, struct attr attr) {
if (pty_num == -1)
return _EROFS;
if (attr.type == attr_size)
return _EINVAL;
lock(&ttys_lock);
struct tty *tty = pty_slave.ttys[pty_num];
assert(tty != NULL);
lock(&tty->lock);
switch (attr.type) {
case attr_uid:
tty->pty.uid = attr.uid;
break;
case attr_gid:
tty->pty.gid = attr.gid;
break;
case attr_mode:
tty->pty.perms = attr.mode;
break;
}
unlock(&tty->lock);
unlock(&ttys_lock);
return 0;
}
static int devpts_fstat(struct fd *fd, struct statbuf *stat) {
devpts_stat_num(fd->devpts.num, stat);
return 0;
}
static int devpts_stat(struct mount *UNUSED(mount), const char *path, struct statbuf *stat) {
int pty_num = devpts_get_pty_num(path);
if (pty_num == _ENOENT)
return _ENOENT;
devpts_stat_num(pty_num, stat);
return 0;
}
static int devpts_setattr(struct mount *UNUSED(mount), const char *path, struct attr attr) {
int pty_num = devpts_get_pty_num(path);
if (pty_num == _ENOENT)
return _ENOENT;
devpts_setattr_num(pty_num, attr);
return 0;
}
static int devpts_fsetattr(struct fd *fd, struct attr attr) {
devpts_setattr_num(fd->devpts.num, attr);
return 0;
}
static int devpts_readdir(struct fd *fd, struct dir_entry *entry) {
assert(fd->devpts.num == -1); // there shouldn't be anything to list but the root
int pty_num = fd->offset;
while (pty_num < MAX_PTYS && !devpts_pty_exists(pty_num))
pty_num++;
if (pty_num >= MAX_PTYS)
return 0;
fd->offset = pty_num + 1;
sprintf(entry->name, "%d", pty_num);
entry->inode = pty_num + 3;
return 1;
}
const struct fs_ops devptsfs = {
.name = "devpts", .magic = 0x1cd1,
.open = devpts_open,
.getpath = devpts_getpath,
.stat = devpts_stat,
.fstat = devpts_fstat,
.setattr = devpts_setattr,
.fsetattr = devpts_fsetattr,
};
static const struct fd_ops devpts_fdops = {
.readdir = devpts_readdir,
};