-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathPTY.cpp
More file actions
executable file
·344 lines (288 loc) · 9.6 KB
/
Copy pathPTY.cpp
File metadata and controls
executable file
·344 lines (288 loc) · 9.6 KB
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#include <Assert.h>
#include <CPU.h>
#include <CString.h>
#include <Errno.h>
#include <Fs/Filesystem.h>
#include <List.h>
#include <Logging.h>
#include <TTY/PTY.h>
#include <Scheduler.h>
#include <Types.h>
cc_t c_cc_default[NCCS]{
4, // VEOF
'\n', // VEOL
'\b', // VERASE
0, // VINTR
0, // VKILL
0, // VMIN
0, // VQUIT
0, // VSTART
0, // VSTOP
0, // VSUSP
0, // VTIME
};
PTYDevice::PTYDevice() {
}
// This pull request improves code clarity and formatting in PTY.cpp.
// It removes redundant comments and aligns with LemonOS's coding style.
//
// Detailed Context:
// - Redundant comments were removed because they repeated what the code already expressed.
// For example, comments like "Buffer must be full so just keep trying" were unnecessary,
// as the loop logic is self-explanatory.
// - Comments that explain non-obvious behavior (e.g. signaling watchers, escape handling)
// were retained and rewritten in concise English.
// - Formatting was adjusted for consistency: spacing, indentation, and block structure
// now match LemonOS's kernel style.
// - No functional changes were made. All logic paths, assertions, and system calls remain intact.
// - These changes improve readability, reduce cognitive load, and help future contributors
// understand the code faster.
//
// Motivation:
// - To align this file with the rest of the LemonOS codebase.
// - To make future diffs cleaner and easier to review.
// - To support maintainability and onboarding for new developers.
ssize_t PTYDevice::Read(size_t offset, size_t size, uint8_t* buffer) {
assert(pty);
assert(device == PTYSlaveDevice || device == PTYMasterDevice);
if (pty && device == PTYSlaveDevice)
return pty->SlaveRead((char*)buffer, size);
else if (pty && device == PTYMasterDevice) {
return pty->MasterRead((char*)buffer, size);
} else {
assert(!"PTYDevice::Read: PTYDevice is designated neither slave nor master");
}
return 0;
}
ssize_t PTYDevice::Write(size_t offset, size_t size, uint8_t* buffer) {
assert(pty);
assert(device == PTYSlaveDevice || device == PTYMasterDevice);
if (pty && device == PTYSlaveDevice) {
ssize_t written = pty->SlaveWrite((char*)buffer, size);
if (written < 0 || written == size) {
return written;
}
buffer += written;
while (written < size) {
size_t ret = pty->SlaveWrite((char*)buffer, size - written);
if (ret < 0) {
return ret;
}
written += ret;
buffer += ret;
}
return written;
} else if (pty && device == PTYMasterDevice) {
ssize_t written = pty->MasterWrite((char*)buffer, size);
if (written < 0 || written == size) {
return written;
}
buffer += written;
while (written < size) {
size_t ret = pty->MasterWrite((char*)buffer, size - written);
if (ret < 0) {
return ret;
}
written += ret;
buffer += ret;
}
return written;
}
assert(!"PTYDevice::Write: PTYDevice is designated neither slave nor master");
return 0;
}
int PTYDevice::Ioctl(uint64_t cmd, uint64_t arg) {
assert(pty);
switch (cmd) {
case TCGETS:
*((termios*)arg) = pty->tios;
break;
case TCSETS:
pty->tios = *((termios*)arg);
pty->slave.ignoreBackspace = !pty->IsCanonical();
break;
case TIOCGWINSZ:
*((winsz*)arg) = pty->wSz;
break;
case TIOCSWINSZ:
pty->wSz = *((winsz*)arg);
break;
case TIOCGPTN:
*((int*)arg) = pty->GetID();
break;
default:
return -EINVAL;
}
return 0;
}
void PTYDevice::Watch(FilesystemWatcher& watcher, int events) {
if (device == PTYMasterDevice) {
pty->WatchMaster(watcher, events);
} else if (device == PTYSlaveDevice) {
pty->WatchSlave(watcher, events);
} else {
assert(!"PTYDevice::Watch: PTYDevice is designated neither slave nor master");
}
}
void PTYDevice::Unwatch(FilesystemWatcher& watcher) {
if (device == PTYMasterDevice) {
pty->UnwatchMaster(watcher);
} else if (device == PTYSlaveDevice) {
pty->UnwatchSlave(watcher);
} else {
assert(!"PTYDevice::Unwatch: PTYDevice is designated neither slave nor master");
}
}
bool PTYDevice::CanRead() {
if (device == PTYMasterDevice) {
return !!pty->master.bufferPos;
} else if (device == PTYSlaveDevice) {
if (pty->IsCanonical())
return !!pty->slave.lines;
else
return !!pty->slave.bufferPos;
} else {
assert(!"PTYDevice::CanRead: PTYDevice is designated neither slave nor master");
}
}
void PTYDevice::Close() {
handleCount--;
if(handleCount == 0 && device == PTYMasterDevice){
pty->Close();
}
}
PTY::PTY(int id) : m_id(id) {
slaveFile.flags = FS_NODE_CHARDEVICE;
masterFile.flags = FS_NODE_CHARDEVICE;
master.ignoreBackspace = true;
slave.ignoreBackspace = false;
master.Flush();
slave.Flush();
tios.c_lflag = ECHO | ICANON;
strcpy(slaveDirent.name, to_string(id).c_str());
slaveDirent.flags = DT_CHR;
slaveDirent.inode = m_id;
masterFile.pty = this;
masterFile.device = PTYMasterDevice;
masterFile.inode = m_id;
slaveFile.pty = this;
slaveFile.device = PTYSlaveDevice;
slaveFile.inode = m_id;
for (int i = 0; i < NCCS; i++)
tios.c_cc[i] = c_cc_default[i];
}
PTY::~PTY(){
while (m_watchingSlave.get_length()) {
m_watchingSlave.remove_at(0)->Signal(); // Signal all watching
}
while (m_watchingMaster.get_length()) {
m_watchingMaster.remove_at(0)->Signal(); // Signal all watching
}
}
void PTY::Close() {
// Only close this PTY if there are 0 open handles across both
// the slave and master files
if(masterFile.handleCount + slaveFile.handleCount == 0) {
PTMultiplexor::Instance().DestroyPTY(this);
}
}
ssize_t PTY::MasterRead(char* buffer, size_t count) { return master.Read(buffer, count); }
ssize_t PTY::SlaveRead(char* buffer, size_t count) {
Thread* thread = Thread::Current();
while (IsCanonical() && !slave.lines) {
FilesystemBlocker blocker(&slaveFile);
if (thread->Block(&blocker)) {
return -EINTR;
}
}
while (!IsCanonical() && !slave.bufferPos) {
FilesystemBlocker blocker(&slaveFile);
if (thread->Block(&blocker)) {
return -EINTR;
}
}
return slave.Read(buffer, count);
}
ssize_t PTY::MasterWrite(char* buffer, size_t count) {
ssize_t ret = slave.Write(buffer, count);
if (slaveFile.blocked.get_length()) {
if (IsCanonical()) {
while (slave.lines && slaveFile.blocked.get_length()) {
acquireLock(&slaveFile.blockedLock);
if (slaveFile.blocked.get_length()) {
slaveFile.blocked.get_front()->Unblock();
}
releaseLock(&slaveFile.blockedLock);
}
} else {
while (slave.bufferPos && slaveFile.blocked.get_length()) {
acquireLock(&slaveFile.blockedLock);
if (slaveFile.blocked.get_length()) {
slaveFile.blocked.get_front()->Unblock();
}
releaseLock(&slaveFile.blockedLock);
}
}
}
if (Echo() && ret) {
for (unsigned i = 0; i < count; i++) {
if (buffer[i] == '\e') { // Escape
master.Write("^[", 2);
} else {
master.Write(&buffer[i], 1);
}
}
}
if (IsCanonical()) {
if (slave.lines && m_watchingSlave.get_length()) {
while (m_watchingSlave.get_length()) {
m_watchingSlave.remove_at(0)->Signal(); // Signal all watching
}
}
} else {
if (slave.bufferPos && m_watchingSlave.get_length()) {
while (m_watchingSlave.get_length()) {
m_watchingSlave.remove_at(0)->Signal(); // Signal all watching
}
}
}
return ret;
}
ssize_t PTY::SlaveWrite(char* buffer, size_t count) {
ssize_t written = master.Write(buffer, count);
while (master.bufferPos && masterFile.blocked.get_length()) {
acquireLock(&masterFile.blockedLock);
if (masterFile.blocked.get_length()) {
masterFile.blocked.get_front()->Unblock();
}
releaseLock(&masterFile.blockedLock);
}
if (master.bufferPos && m_watchingMaster.get_length()) {
while (m_watchingMaster.get_length()) {
m_watchingMaster.remove_at(0)->Signal(); // Signal all watching
}
}
return written;
}
void PTY::WatchMaster(FilesystemWatcher& watcher, int events) {
if (!(events & (POLLIN))) { // We don't really block on writes and nothing else applies except POLLIN
watcher.Signal();
return;
} else if (masterFile.CanRead()) {
watcher.Signal();
return;
}
m_watchingMaster.add_back(&watcher);
}
void PTY::WatchSlave(FilesystemWatcher& watcher, int events) {
if (!(events & (POLLIN))) { // We don't really block on writes and nothing else applies except POLLIN
watcher.Signal();
return;
} else if (slaveFile.CanRead()) {
watcher.Signal();
return;
}
m_watchingSlave.add_back(&watcher);
}
void PTY::UnwatchMaster(FilesystemWatcher& watcher) { m_watchingMaster.remove(&watcher); }
void PTY::UnwatchSlave(FilesystemWatcher& watcher) { m_watchingSlave.remove(&watcher); }