chore(wave6a): reserve P2 error domains, guard IO pipe SIGPIPE, extend ProcessHelper, and add Wave 6A specs

This commit is contained in:
faumaray committed 2026-11-24 10:56:39 +03:00
1 parent 44799602c9
commit 3f67b1ecb7
6 files changed
+470 -2

No files matched your search

+354 -1
View File
@@ -1,9 +1,23 @@
#include <fcntl.h>
#include <poll.h>
#include <unistd.h>
#include <algorithm>
#include <array>
#include <cerrno>
#include <charconv>
#include <chrono>
#include <csignal>
#include <cstdint>
#include <cstring>
#include <iostream>
#include <limits>
#include <string>
#include <string_view>
#include <thread>
#include <vector>
extern char** environ;
namespace
{
@@ -25,6 +39,45 @@ int usage_error(std::string_view msg)
return 2;
}
bool write_all_fd(int fd, const void* data, std::size_t len) noexcept
{
const auto* ptr = static_cast<const std::uint8_t*>(data);
std::size_t written = 0;
while (written < len) {
const ssize_t n = ::write(fd, ptr + written, len - written);
if (n < 0) {
if (errno == EINTR) {
continue;
}
return false;
}
written += static_cast<std::size_t>(n);
}
return true;
}
bool read_exact_fd(int fd, void* data, std::size_t len, bool& clean_eof) noexcept
{
auto* ptr = static_cast<std::uint8_t*>(data);
std::size_t got = 0;
clean_eof = false;
while (got < len) {
const ssize_t n = ::read(fd, ptr + got, len - got);
if (n < 0) {
if (errno == EINTR) {
continue;
}
return false;
}
if (n == 0) {
clean_eof = (got == 0);
return false;
}
got += static_cast<std::size_t>(n);
}
return true;
}
int mode_echo(int argc, char** argv)
{
for (int i = 2; i < argc; ++i) {
@@ -126,6 +179,282 @@ int mode_lines(int argc, char** argv)
return 0;
}
int mode_ignore_sigterm(int argc, char** argv)
{
std::int64_t sleep_ms = 5000;
if (argc >= 3 && !parse_i64(argv[2], sleep_ms)) {
return usage_error("ignore-sigterm duration must be an integer");
}
if (sleep_ms < 0) {
sleep_ms = 0;
}
std::signal(SIGTERM, SIG_IGN);
std::cout << "ready\n" << std::flush;
std::this_thread::sleep_for(std::chrono::milliseconds{sleep_ms});
return 0;
}
int mode_simultaneous_io(int argc, char** argv)
{
if (argc != 3) {
return usage_error("simultaneous-io requires <bytes>");
}
std::int64_t total_i64 = 0;
if (!parse_i64(argv[2], total_i64) || total_i64 < 0) {
return usage_error("simultaneous-io <bytes> must be non-negative");
}
const auto total = static_cast<std::size_t>(total_i64);
for (int fd : {STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO}) {
const int fl = ::fcntl(fd, F_GETFL, 0);
if (fl >= 0) {
(void)::fcntl(fd, F_SETFL, fl | O_NONBLOCK);
}
}
std::string chunk_out(std::min<std::size_t>(4096, std::max<std::size_t>(1, total)), 'o');
std::string chunk_err(std::min<std::size_t>(4096, std::max<std::size_t>(1, total)), 'e');
std::size_t out_sent = 0;
std::size_t err_sent = 0;
std::size_t in_read = 0;
bool in_eof = false;
std::array<char, 4096> in_buf{};
while (!in_eof || out_sent < total || err_sent < total) {
std::array<struct pollfd, 3> pfds{};
nfds_t nfds = 0;
if (!in_eof) {
pfds[nfds++] = {STDIN_FILENO, POLLIN, 0};
}
if (out_sent < total) {
pfds[nfds++] = {STDOUT_FILENO, POLLOUT, 0};
}
if (err_sent < total) {
pfds[nfds++] = {STDERR_FILENO, POLLOUT, 0};
}
const int rc = ::poll(pfds.data(), nfds, -1);
if (rc < 0) {
if (errno == EINTR) {
continue;
}
return 1;
}
for (nfds_t i = 0; i < nfds; ++i) {
if (pfds[i].fd == STDIN_FILENO &&
(pfds[i].revents & (POLLIN | POLLHUP | POLLERR)) != 0) {
for (;;) {
const ssize_t n = ::read(STDIN_FILENO, in_buf.data(), in_buf.size());
if (n > 0) {
in_read += static_cast<std::size_t>(n);
continue;
}
if (n == 0) {
in_eof = true;
break;
}
if (errno == EINTR) {
continue;
}
if (errno == EAGAIN || errno == EWOULDBLOCK) {
break;
}
in_eof = true;
break;
}
} else if (pfds[i].fd == STDOUT_FILENO && (pfds[i].revents & POLLOUT) != 0) {
const std::size_t rem = std::min(chunk_out.size(), total - out_sent);
const ssize_t n = ::write(STDOUT_FILENO, chunk_out.data(), rem);
if (n > 0) {
out_sent += static_cast<std::size_t>(n);
} else if (n < 0 && errno != EINTR && errno != EAGAIN && errno != EWOULDBLOCK) {
return 1;
}
} else if (pfds[i].fd == STDERR_FILENO && (pfds[i].revents & POLLOUT) != 0) {
const std::size_t rem = std::min(chunk_err.size(), total - err_sent);
const ssize_t n = ::write(STDERR_FILENO, chunk_err.data(), rem);
if (n > 0) {
err_sent += static_cast<std::size_t>(n);
} else if (n < 0 && errno != EINTR && errno != EAGAIN && errno != EWOULDBLOCK) {
return 1;
}
}
}
}
return (in_read > 0 || total == 0) ? 0 : 0;
}
int mode_flood(int argc, char** argv)
{
if (argc != 4) {
return usage_error("flood requires <stdout_bytes> <stderr_bytes>");
}
std::int64_t out_n = 0;
std::int64_t err_n = 0;
if (!parse_i64(argv[2], out_n) || !parse_i64(argv[3], err_n) || out_n < 0 || err_n < 0) {
return usage_error("flood byte counts must be non-negative");
}
std::array<char, 4096> buf{};
buf.fill('O');
for (std::int64_t rem = out_n; rem > 0;) {
const auto chunk = static_cast<std::size_t>(std::min<std::int64_t>(rem, buf.size()));
if (!write_all_fd(STDOUT_FILENO, buf.data(), chunk)) {
return 1;
}
rem -= static_cast<std::int64_t>(chunk);
}
buf.fill('E');
for (std::int64_t rem = err_n; rem > 0;) {
const auto chunk = static_cast<std::size_t>(std::min<std::int64_t>(rem, buf.size()));
if (!write_all_fd(STDERR_FILENO, buf.data(), chunk)) {
return 1;
}
rem -= static_cast<std::int64_t>(chunk);
}
return 0;
}
int mode_long_line(int argc, char** argv)
{
if (argc != 3) {
return usage_error("long-line requires <bytes>");
}
std::int64_t len = 0;
if (!parse_i64(argv[2], len) || len < 0) {
return usage_error("long-line <bytes> must be non-negative");
}
std::array<char, 4096> buf{};
buf.fill('a');
for (std::int64_t rem = len; rem > 0;) {
const auto chunk = static_cast<std::size_t>(std::min<std::int64_t>(rem, buf.size()));
if (!write_all_fd(STDOUT_FILENO, buf.data(), chunk)) {
return 1;
}
rem -= static_cast<std::int64_t>(chunk);
}
const char nl = '\n';
return write_all_fd(STDOUT_FILENO, &nl, 1) ? 0 : 1;
}
int mode_check_fds()
{
std::vector<int> open_fds;
for (int fd = 3; fd <= 64; ++fd) {
if (::fcntl(fd, F_GETFD) != -1 || errno != EBADF) {
open_fds.push_back(fd);
}
}
if (open_fds.empty()) {
std::cout << "clean\n";
} else {
for (std::size_t i = 0; i < open_fds.size(); ++i) {
if (i > 0) {
std::cout << ' ';
}
std::cout << open_fds[i];
}
std::cout << '\n';
}
std::cout.flush();
return 0;
}
int mode_env_dump()
{
std::vector<std::string> vars;
if (environ != nullptr) {
for (char** p = environ; *p != nullptr; ++p) {
vars.emplace_back(*p);
}
}
std::sort(vars.begin(), vars.end());
for (const auto& entry : vars) {
std::cout << entry << '\n';
}
std::cout.flush();
return 0;
}
int mode_cwd()
{
std::array<char, 4096> buf{};
if (::getcwd(buf.data(), buf.size()) == nullptr) {
return 1;
}
std::cout << buf.data() << '\n' << std::flush;
return 0;
}
std::uint16_t read_u16_be(const std::uint8_t* p) noexcept
{
return static_cast<std::uint16_t>((static_cast<std::uint16_t>(p[0]) << 8) | p[1]);
}
std::uint32_t read_u32_be(const std::uint8_t* p) noexcept
{
return (static_cast<std::uint32_t>(p[0]) << 24) |
(static_cast<std::uint32_t>(p[1]) << 16) |
(static_cast<std::uint32_t>(p[2]) << 8) |
static_cast<std::uint32_t>(p[3]);
}
void write_u16_be(std::uint8_t* p, std::uint16_t v) noexcept
{
p[0] = static_cast<std::uint8_t>((v >> 8) & 0xffu);
p[1] = static_cast<std::uint8_t>(v & 0xffu);
}
void write_u32_be(std::uint8_t* p, std::uint32_t v) noexcept
{
p[0] = static_cast<std::uint8_t>((v >> 24) & 0xffu);
p[1] = static_cast<std::uint8_t>((v >> 16) & 0xffu);
p[2] = static_cast<std::uint8_t>((v >> 8) & 0xffu);
p[3] = static_cast<std::uint8_t>(v & 0xffu);
}
int mode_ipc_pipe_echo()
{
for (;;) {
std::array<std::uint8_t, 24> hdr{};
bool clean_eof = false;
if (!read_exact_fd(STDIN_FILENO, hdr.data(), hdr.size(), clean_eof)) {
return clean_eof ? 0 : 1;
}
if (hdr[0] != 'V' || hdr[1] != 'D' || hdr[2] != 'I' || hdr[3] != 'P') {
return 3;
}
const std::uint16_t version = read_u16_be(hdr.data() + 4);
const std::uint16_t flags_type = read_u16_be(hdr.data() + 6);
const std::uint32_t payload_len = read_u32_be(hdr.data() + 8);
const std::uint32_t stream_id = read_u32_be(hdr.data() + 12);
if (version != 1 || stream_id != 0 || payload_len > 2u * 1024u * 1024u) {
return 4;
}
std::vector<std::uint8_t> payload(payload_len);
if (payload_len > 0) {
bool eof2 = false;
if (!read_exact_fd(STDIN_FILENO, payload.data(), payload_len, eof2)) {
return 5;
}
}
const std::uint8_t frame_type = static_cast<std::uint8_t>(flags_type & 0xffu);
if (frame_type == 7) { // goaway
return 0;
}
if (frame_type == 1 || frame_type == 2) { // hello -> hello, request -> response
std::array<std::uint8_t, 24> out_hdr = hdr;
const std::uint8_t reply_type = (frame_type == 1) ? 1u : 3u;
write_u16_be(out_hdr.data() + 6, reply_type);
write_u32_be(out_hdr.data() + 8, payload_len);
if (!write_all_fd(STDOUT_FILENO, out_hdr.data(), out_hdr.size())) {
return 6;
}
if (payload_len > 0 && !write_all_fd(STDOUT_FILENO, payload.data(), payload.size())) {
return 7;
}
}
}
}
} // namespace
int main(int argc, char** argv)
@@ -153,5 +482,29 @@ int main(int argc, char** argv)
if (mode == "lines") {
return mode_lines(argc, argv);
}
if (mode == "ignore-sigterm") {
return mode_ignore_sigterm(argc, argv);
}
if (mode == "simultaneous-io") {
return mode_simultaneous_io(argc, argv);
}
if (mode == "flood") {
return mode_flood(argc, argv);
}
if (mode == "long-line") {
return mode_long_line(argc, argv);
}
if (mode == "check-fds") {
return mode_check_fds();
}
if (mode == "env-dump") {
return mode_env_dump();
}
if (mode == "cwd") {
return mode_cwd();
}
if (mode == "ipc-pipe-echo") {
return mode_ipc_pipe_echo();
}
return usage_error("unknown mode");
}
}