1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "nacl_io/devfs/tty_node.h"
12 #include <sys/ioctl.h>
17 #include "nacl_io/filesystem.h"
18 #include "nacl_io/ioctl.h"
19 #include "nacl_io/kernel_handle.h"
20 #include "nacl_io/kernel_intercept.h"
21 #include "nacl_io/log.h"
22 #include "nacl_io/pepper_interface.h"
23 #include "sdk_util/auto_lock.h"
25 #define CHECK_LFLAG(TERMIOS, FLAG) (TERMIOS.c_lflag& FLAG)
27 #define IS_ECHO CHECK_LFLAG(termios_, ECHO)
28 #define IS_ECHOE CHECK_LFLAG(termios_, ECHOE)
29 #define IS_ECHONL CHECK_LFLAG(termios_, ECHONL)
30 #define IS_ECHOCTL CHECK_LFLAG(termios_, ECHOCTL)
31 #define IS_ICANON CHECK_LFLAG(termios_, ICANON)
33 #define DEFAULT_TTY_COLS 80
34 #define DEFAULT_TTY_ROWS 30
38 TtyNode::TtyNode(Filesystem* filesystem)
39 : CharNode(filesystem),
40 emitter_(new EventEmitter),
41 rows_(DEFAULT_TTY_ROWS),
42 cols_(DEFAULT_TTY_COLS) {
43 output_handler_.handler = NULL;
46 // Output will never block
47 emitter_->RaiseEvents_Locked(POLLOUT);
50 void TtyNode::InitTermios() {
51 // Some sane values that produce good result.
52 termios_.c_iflag = ICRNL | IXON | IXOFF | IUTF8;
53 termios_.c_oflag = OPOST | ONLCR;
54 termios_.c_cflag = CREAD | 077;
56 ISIG | ICANON | ECHO | ECHOE | ECHOK | ECHOCTL | ECHOKE | IEXTEN;
57 #if !defined(__BIONIC__)
58 termios_.c_ispeed = B38400;
59 termios_.c_ospeed = B38400;
61 termios_.c_cc[VINTR] = 3;
62 termios_.c_cc[VQUIT] = 28;
63 termios_.c_cc[VERASE] = 127;
64 termios_.c_cc[VKILL] = 21;
65 termios_.c_cc[VEOF] = 4;
66 termios_.c_cc[VTIME] = 0;
67 termios_.c_cc[VMIN] = 1;
68 termios_.c_cc[VSWTC] = 0;
69 termios_.c_cc[VSTART] = 17;
70 termios_.c_cc[VSTOP] = 19;
71 termios_.c_cc[VSUSP] = 26;
72 termios_.c_cc[VEOL] = 0;
73 termios_.c_cc[VREPRINT] = 18;
74 termios_.c_cc[VDISCARD] = 15;
75 termios_.c_cc[VWERASE] = 23;
76 termios_.c_cc[VLNEXT] = 22;
77 termios_.c_cc[VEOL2] = 0;
80 EventEmitter* TtyNode::GetEventEmitter() {
81 return emitter_.get();
84 Error TtyNode::Write(const HandleAttr& attr,
88 AUTO_LOCK(output_lock_);
91 // No handler registered.
92 if (output_handler_.handler == NULL) {
93 // No error here; many of the tests trigger this message.
94 LOG_TRACE("No output handler registered.");
98 int rtn = output_handler_.handler(
99 static_cast<const char*>(buf), count, output_handler_.user_data);
101 // Negative return value means an error occured and the return
102 // value is a negated errno value.
110 Error TtyNode::Read(const HandleAttr& attr,
114 EventListenerLock wait(GetEventEmitter());
117 // If interrupted, return
118 Error err = wait.WaitOnEvent(POLLIN, -1);
119 if (err == ETIMEDOUT)
124 size_t bytes_to_copy = std::min(count, input_buffer_.size());
126 // Only read up to (and including) the first newline
127 std::deque<char>::iterator nl =
128 std::find(input_buffer_.begin(), input_buffer_.end(), '\n');
130 if (nl != input_buffer_.end()) {
131 // We found a newline in the buffer, adjust bytes_to_copy accordingly
132 size_t line_len = static_cast<size_t>(nl - input_buffer_.begin()) + 1;
133 bytes_to_copy = std::min(bytes_to_copy, line_len);
137 // Copies data from the input buffer into buf.
138 std::copy(input_buffer_.begin(),
139 input_buffer_.begin() + bytes_to_copy,
140 static_cast<char*>(buf));
141 *out_bytes = bytes_to_copy;
142 input_buffer_.erase(input_buffer_.begin(),
143 input_buffer_.begin() + bytes_to_copy);
145 // mark input as no longer readable if we consumed
146 // the entire buffer or, in the case of buffered input,
147 // we consumed the final \n char.
150 avail = std::find(input_buffer_.begin(), input_buffer_.end(), '\n') !=
153 avail = input_buffer_.size() > 0;
156 emitter_->ClearEvents_Locked(POLLIN);
161 Error TtyNode::Echo(const char* string, int count) {
164 Error error = Write(data, string, count, &wrote);
165 if (error != 0 || wrote != count) {
166 // TOOD(sbc): Do something more useful in response to a
174 Error TtyNode::ProcessInput(PP_Var message) {
175 if (message.type != PP_VARTYPE_STRING) {
176 LOG_ERROR("Expected VarString but got %d.", message.type);
180 PepperInterface* ppapi = filesystem_->ppapi();
182 LOG_ERROR("ppapi is NULL.");
186 VarInterface* var_iface = ppapi->GetVarInterface();
188 LOG_ERROR("Got NULL interface: Var");
193 const char* buffer = var_iface->VarToUtf8(message, &num_bytes);
194 Error error = ProcessInput(buffer, num_bytes);
198 Error TtyNode::ProcessInput(const char* buffer, size_t num_bytes) {
199 AUTO_LOCK(emitter_->GetLock())
201 for (size_t i = 0; i < num_bytes; i++) {
203 // Transform characters according to input flags.
205 if (termios_.c_iflag & IGNCR)
207 if (termios_.c_iflag & ICRNL)
209 } else if (c == '\n') {
210 if (termios_.c_iflag & INLCR)
216 // ICANON mode means we wait for a newline before making the
219 if (IS_ECHOE && c == termios_.c_cc[VERASE]) {
220 // Remove previous character in the line if any.
221 if (!input_buffer_.empty()) {
222 char char_to_delete = input_buffer_.back();
223 if (char_to_delete != '\n') {
224 input_buffer_.pop_back();
228 // When ECHOCTL is set the echo buffer contains an extra
229 // char for each control char.
230 if (IS_ECHOCTL && iscntrl(char_to_delete))
235 } else if (IS_ECHO || (IS_ECHONL && c == '\n')) {
236 if (c == termios_.c_cc[VEOF]) {
237 // VEOF sequence is not echoed, nor is it sent as
240 } else if (c != '\n' && iscntrl(c) && IS_ECHOCTL) {
241 // In ECHOCTL mode a control char C is echoed as '^'
242 // followed by the ascii char which at C + 0x40.
243 char visible_char = c + 0x40;
245 Echo(&visible_char, 1);
253 input_buffer_.push_back(c);
255 if (c == '\n' || c == termios_.c_cc[VEOF] || !IS_ICANON)
256 emitter_->RaiseEvents_Locked(POLLIN);
262 Error TtyNode::VIoctl(int request, va_list args) {
264 case TIOCNACLOUTPUT: {
265 struct tioc_nacl_output* arg = va_arg(args, struct tioc_nacl_output*);
266 AUTO_LOCK(output_lock_);
268 output_handler_.handler = NULL;
271 if (output_handler_.handler != NULL) {
272 LOG_ERROR("Output handler already set.");
275 output_handler_ = *arg;
278 case NACL_IOC_HANDLEMESSAGE: {
279 struct PP_Var* message = va_arg(args, struct PP_Var*);
280 return ProcessInput(*message);
283 struct winsize* size = va_arg(args, struct winsize*);
285 AUTO_LOCK(node_lock_);
286 if (rows_ == size->ws_row && cols_ == size->ws_col)
288 rows_ = size->ws_row;
289 cols_ = size->ws_col;
291 ki_kill(getpid(), SIGWINCH);
293 // Wake up any thread waiting on Read with POLLERR then immediate
294 // clear it to signal EINTR.
295 AUTO_LOCK(emitter_->GetLock())
296 emitter_->RaiseEvents_Locked(POLLERR);
297 emitter_->ClearEvents_Locked(POLLERR);
302 struct winsize* size = va_arg(args, struct winsize*);
303 size->ws_row = rows_;
304 size->ws_col = cols_;
308 LOG_ERROR("TtyNode:VIoctl: Unknown request: %#x", request);
315 Error TtyNode::Tcgetattr(struct termios* termios_p) {
316 AUTO_LOCK(node_lock_);
317 *termios_p = termios_;
321 Error TtyNode::Tcsetattr(int optional_actions,
322 const struct termios* termios_p) {
323 AUTO_LOCK(node_lock_);
324 termios_ = *termios_p;
328 } // namespace nacl_io