-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathio_linux.cpp
More file actions
145 lines (108 loc) · 2.83 KB
/
io_linux.cpp
File metadata and controls
145 lines (108 loc) · 2.83 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
#include "io.hpp"
#include <chrono>
#include <iostream>
#include <string>
#include <ctype.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <unistd.h>
namespace io {
PseudoTerminal::PseudoTerminal(data_read_cb data_cb)
: _data_cb(data_cb), write_buffer(1024, '\0'),
read_buffer(1024, '\0'), stream{service}, work{service}
{}
PseudoTerminal::~PseudoTerminal() {
service.stop();
t.join();
}
void PseudoTerminal::read_complete() {
stream.async_read_some(
boost::asio::buffer(read_buffer.data(), read_buffer.size()),
[this](const boost::system::error_code &err, long unsigned int length) {
if (err) {
std::cerr << "Error message: " << err.message() << "\n";
this->_data_cb(this, nullptr, 0u);
return;
}
this->_data_cb(this, read_buffer.data(), length);
});
}
void PseudoTerminal::write(char data) { write(&data, 1u); }
void PseudoTerminal::write(std::string_view data) {
write(data.data(), data.size());
}
void PseudoTerminal::write(const char *data, size_t len) {
#ifdef PEACHTERM_IS_VERBOSE
std::cout << "Write pt length= " << len << ": ";
size_t _len = len;
for (const char *d = data; _len-- > 0; d++) {
if (::isprint(*d)) {
std::cout << *d;
} else {
std::cout << '\\' << 'x' << std::hex << (int)(*d & 0xFF) << std::dec;
}
}
std::cout << '\n';
#endif
boost::asio::write(stream, boost::asio::buffer(data, len));
}
bool PseudoTerminal::start() {
std::cout << "Start PT\n";
parentfd = posix_openpt(O_RDWR | O_NOCTTY);
if (parentfd == -1) {
return false;
}
fcntl(parentfd, F_SETFL, fcntl(parentfd, F_GETFL) | O_NONBLOCK);
if (grantpt(parentfd) == -1) {
return false;
}
if (unlockpt(parentfd) == -1) {
return false;
}
char *pts_name = ptsname(parentfd);
if (pts_name == nullptr) {
return false;
}
childfd = open(pts_name, O_RDWR | O_NOCTTY);
if (childfd == -1) {
return false;
}
return true;
}
bool PseudoTerminal::set_size(int rows, int cols) {
struct winsize ws;
ws.ws_row = rows;
ws.ws_col = cols;
return -1 != ioctl(parentfd, TIOCSWINSZ, &ws);
return true;
}
bool PseudoTerminal::fork_child() {
pid_t p = fork();
if (p == 0) {
close(parentfd);
setsid();
if (ioctl(childfd, TIOCSCTTY, nullptr) == -1) {
return false;
}
dup2(childfd, 0);
dup2(childfd, 1);
dup2(childfd, 2);
close(childfd);
::setenv("TERM", "xterm-256color", 1);
execlp("/usr/bin/bash", "-/usr/bin/bash", nullptr);
return false;
} else if (p > 0) {
close(childfd);
stream.assign(parentfd);
std::thread thread{[this]() {
std::cout << "IO thread\n";
this->service.run();
std::cout << "IO thread exiting\n";
}};
std::swap(t, thread);
read_complete();
return true;
}
return true;
}
} // namespace io