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 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
|
#include <EventsIo.h>
#include <cstring>
extern "C"
{
#include <errno.h>
}
// epoll_event datastructure slot indices
const int EventsIo::EVENTS_CTL_INDEX = 0;
const int EventsIo::SIG_CTL_INDEX = 1;
const int EventsIo::STDIN_CTL_INDEX = 2;
// Number of epoll_event datastructure slots
const int EventsIo::CTL_SLOTS_COUNT = 3;
// @throws std::bad_alloc, std::ios_base::failure
EventsIo::EventsIo(int events_input_fd):
events_fd(events_input_fd),
ctl_events(new struct epoll_event[CTL_SLOTS_COUNT]),
fired_events(new struct epoll_event[CTL_SLOTS_COUNT]),
signal_buffer(new struct signalfd_siginfo),
events_buffer(new char[MAX_LINE_LENGTH])
{
try
{
// Zero termios datastructures
static_cast<void> (std::memset(static_cast<void*> (&orig_termios), 0,
sizeof (orig_termios)));
static_cast<void> (std::memset(static_cast<void*> (&adjusted_termios), 0,
sizeof (adjusted_termios)));
// Initialize the poll file descriptor
poll_fd = epoll_create1(EPOLL_CLOEXEC);
if (poll_fd == -1)
{
throw EventsIoException();
}
// Initialize signalfds to enable polling for signals
// Block the default signal handlers for the same signals
sigset_t mask;
checked_int_rc(sigemptyset(&mask));
checked_int_rc(sigaddset(&mask, SIGTERM));
checked_int_rc(sigaddset(&mask, SIGHUP));
checked_int_rc(sigaddset(&mask, SIGINT));
checked_int_rc(sigaddset(&mask, SIGWINCH));
checked_int_rc(sigaddset(&mask, SIGCHLD));
checked_int_rc(sigprocmask(SIG_BLOCK, &mask, nullptr));
sig_fd = signalfd(-1, &mask, SFD_NONBLOCK | SFD_CLOEXEC);
if (sig_fd == -1)
{
throw EventsIoException();
}
// Initialize poll event data structures
static_cast<void> (std::memset(static_cast<void*> (ctl_events.get()), 0,
sizeof (struct epoll_event) * CTL_SLOTS_COUNT));
static_cast<void> (std::memset(static_cast<void*> (fired_events.get()), 0,
sizeof (struct epoll_event) * CTL_SLOTS_COUNT));
// Register the epoll() events
register_poll(events_fd, &(ctl_events[EVENTS_CTL_INDEX]), EPOLLIN);
register_poll(sig_fd, &(ctl_events[SIG_CTL_INDEX]), EPOLLIN);
register_poll(stdin_fd, &(ctl_events[STDIN_CTL_INDEX]), EPOLLIN);
}
catch (EventsIoException&)
{
// Cleanup modifies errno, so the relevant information must be cached
bool os_call_oom = (errno == ENOMEM);
cleanup();
if (os_call_oom)
{
throw std::bad_alloc();
}
throw;
}
catch (std::bad_alloc&)
{
cleanup();
throw;
}
}
EventsIo::~EventsIo() noexcept
{
cleanup();
}
// @throws EventsIoException
void EventsIo::register_poll(int fd, struct epoll_event* event_ctl_slot, uint32_t event_mask)
{
event_ctl_slot->data.fd = fd;
event_ctl_slot->events = event_mask;
int rc = epoll_ctl(poll_fd, EPOLL_CTL_ADD, fd, event_ctl_slot);
if (rc != 0)
{
throw EventsIoException();
}
}
// @throws EventsIoException
EventsIo::event EventsIo::wait_event()
{
EventsIo::event event_id = EventsIo::event::NONE;
// If data is available in the buffer, look for event lines
// This check is turned off by prepare_line() if all the available
// data has been searched and more data must be read to find another
// event lines
// read_events() turns this check back on if more data was read
if (data_pending)
{
if (prepare_line())
{
event_id = EventsIo::event::EVENT_LINE;
}
}
while (event_id == EventsIo::event::NONE)
{
if (!pending_events)
{
current_event = 0;
do
{
errno = 0;
event_count = epoll_wait(poll_fd, fired_events.get(), CTL_SLOTS_COUNT, -1);
if (event_count > 0)
{
pending_events = true;
}
else
if (errno != 0 && errno != EINTR)
{
throw EventsIoException();
}
}
while (!pending_events);
}
int fired_fd = fired_events[current_event].data.fd;
if (fired_fd == events_fd)
{
// Events source data available
if (events_eof)
{
throw EventsIoException();
}
read_events();
if (prepare_line())
{
event_id = EventsIo::event::EVENT_LINE;
}
}
else
if (fired_fd == sig_fd)
{
// Signal available
event_id = EventsIo::event::SIGNAL;
}
else
if (fired_fd == stdin_fd)
{
// Data available on stdin
event_id = EventsIo::event::STDIN;
}
else
{
// Unknown data source ready, this is not supposed to happen
throw EventsIoException();
}
// Select the next event for processing
++current_event;
if (current_event >= event_count)
{
pending_events = false;
}
}
return event_id;
}
// @throws EventsIoException
int EventsIo::get_signal()
{
int signal_id = 0;
errno = 0;
ssize_t read_size = 0;
do
{
read_size = read(sig_fd, signal_buffer.get(), sizeof (struct signalfd_siginfo));
if (read_size == sizeof (struct signalfd_siginfo))
{
signal_id = static_cast<int> (signal_buffer->ssi_signo);
}
else
if (read_size == 0)
{
throw EventsIoException();
}
}
while (read_size == -1 && errno == EINTR);
if (read_size == -1)
{
throw EventsIoException();
}
return signal_id;
}
// @throws EventsIoException
void EventsIo::adjust_terminal()
{
// Store current terminal settings
checked_int_rc(tcgetattr(STDIN_FILENO, &orig_termios));
have_orig_termios = true;
adjusted_termios = orig_termios;
// Non-canonical input without echo
tcflag_t flags_on = ISIG;
tcflag_t flags_off = ICANON | ECHO | ECHONL;
adjusted_termios.c_lflag |= flags_on;
adjusted_termios.c_lflag = (adjusted_termios.c_lflag | flags_off) ^ flags_off;
checked_int_rc(tcsetattr(STDIN_FILENO, TCSANOW, &adjusted_termios));
}
// @throws EventsIoException
void EventsIo::restore_terminal()
{
if (have_orig_termios)
{
// Restore original terminal settings
checked_int_rc(tcsetattr(STDIN_FILENO, TCSANOW, &orig_termios));
}
}
// @throws EventsIoException
void EventsIo::read_events()
{
// If there is space remaining in the buffer,
// read data into the buffer.
// If the buffer is full, it must be compacted or emptied by
// calls to get_event_line()
if (events_length < MAX_LINE_LENGTH)
{
size_t length = MAX_LINE_LENGTH - events_length;
int read_count = 0;
do
{
read_count = read(events_fd, &(events_buffer[events_length]), length);
}
while (read_count == -1 && errno == EINTR);
if (read_count == 0)
{
events_eof = true;
}
else
if (read_count == -1)
{
if (errno != EAGAIN)
{
throw EventsIoException();
}
}
else
{
// Indicate that more data has been made available and
// should be checked for complete lines using prepare_line()
data_pending = true;
events_length += read_count;
}
}
}
std::string* EventsIo::get_event_line()
{
if (event_line == nullptr)
{
if (!data_pending)
{
read_events();
}
prepare_line();
}
line_pending = false;
return event_line.get();
}
void EventsIo::free_event_line()
{
event_line = nullptr;
}
// @throws std::bad_alloc, EventsIoException
bool EventsIo::prepare_line()
{
if (!line_pending)
{
if (event_line != nullptr)
{
event_line = nullptr;
}
bool have_line = false;
size_t event_end_pos = 0;
// Check for new complete lines in the buffer
char* input_data = events_buffer.get();
for (size_t index = event_begin_pos; index < events_length; ++index)
{
if (input_data[index] == '\n')
{
have_line = true;
event_end_pos = index + 1;
break;
}
}
if (have_line)
{
if (discard_line)
{
discard_line = false;
}
else
{
event_line = std::unique_ptr<std::string>(
new std::string(&(input_data[event_begin_pos]), event_end_pos - event_begin_pos - 1)
);
line_pending = true;
}
event_begin_pos = event_end_pos;
}
else
{
// Indicate that all available data has been searched for event lines
// and more data needs to be read to find another event line
data_pending = false;
if (event_begin_pos > 0)
{
// No more lines in the buffer, compact buffer
size_t src_index = event_begin_pos;
size_t dst_index = 0;
while (src_index < events_length)
{
input_data[dst_index] = input_data[src_index];
++src_index;
++dst_index;
}
events_length = events_length - event_begin_pos;
event_begin_pos = 0;
}
else
if (events_length == MAX_LINE_LENGTH)
{
// Buffer is full, but no lines were found
// Too long line, discard input
event_begin_pos = 0;
events_length = 0;
discard_line = true;
// Currently, a too long line is considered an error
// If throwing the exception is removed, too long lines
// will simply be discarded instead
throw EventsIoException();
}
}
}
return line_pending;
}
// Throws EventsIoException if rc is not equal to 0
// @throws EventsIoException
void EventsIo::checked_int_rc(int rc) const
{
if (rc != 0)
{
throw EventsIoException();
}
}
void EventsIo::cleanup() noexcept
{
if (poll_fd != -1)
{
close(poll_fd);
poll_fd = -1;
}
if (events_fd != -1)
{
close(events_fd);
events_fd = -1;
}
if (sig_fd != -1)
{
close(sig_fd);
sig_fd = -1;
}
// stdin_fd is not closed
}
|