564 lines
16 KiB
C++
564 lines
16 KiB
C++
#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
|
|
{
|
|
|
|
bool parse_i64(std::string_view text, std::int64_t& out) noexcept
|
|
{
|
|
if (text.empty()) {
|
|
return false;
|
|
}
|
|
const char* begin = text.data();
|
|
const char* end = text.data() + text.size();
|
|
auto [ptr, ec] = std::from_chars(begin, end, out, 10);
|
|
return ec == std::errc{} && ptr == end;
|
|
}
|
|
|
|
int usage_error(std::string_view msg)
|
|
{
|
|
std::cerr << "VoidProcessHelper: " << msg << '\n';
|
|
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) {
|
|
if (i > 2) {
|
|
std::cout.put(' ');
|
|
}
|
|
std::cout << argv[i];
|
|
}
|
|
std::cout.put('\n');
|
|
std::cout.flush();
|
|
return 0;
|
|
}
|
|
|
|
int mode_cat()
|
|
{
|
|
std::array<char, 4096> buf{};
|
|
while (std::cin) {
|
|
std::cin.read(buf.data(), static_cast<std::streamsize>(buf.size()));
|
|
const std::streamsize got = std::cin.gcount();
|
|
if (got > 0) {
|
|
std::cout.write(buf.data(), got);
|
|
}
|
|
}
|
|
std::cout.flush();
|
|
return std::cout.good() ? 0 : 1;
|
|
}
|
|
|
|
int mode_seq(int argc, char** argv)
|
|
{
|
|
if (argc != 4) {
|
|
return usage_error("seq requires <start> <end>");
|
|
}
|
|
std::int64_t start = 0;
|
|
std::int64_t finish = 0;
|
|
if (!parse_i64(argv[2], start) || !parse_i64(argv[3], finish)) {
|
|
return usage_error("seq arguments must be integers");
|
|
}
|
|
if (start <= finish) {
|
|
for (std::int64_t v = start;; ++v) {
|
|
std::cout << v << '\n';
|
|
if (v == finish) {
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
std::cout.flush();
|
|
return 0;
|
|
}
|
|
|
|
int mode_streams()
|
|
{
|
|
std::cout << "to_stdout\n" << std::flush;
|
|
std::cerr << "to_stderr\n" << std::flush;
|
|
return 0;
|
|
}
|
|
|
|
int mode_exit(int argc, char** argv)
|
|
{
|
|
if (argc != 3) {
|
|
return usage_error("exit requires <code>");
|
|
}
|
|
std::int64_t code = 0;
|
|
if (!parse_i64(argv[2], code)) {
|
|
return usage_error("exit code must be an integer");
|
|
}
|
|
return static_cast<int>(code);
|
|
}
|
|
|
|
int mode_lines(int argc, char** argv)
|
|
{
|
|
bool no_newline = false;
|
|
bool to_stderr = false;
|
|
int idx = 2;
|
|
while (idx < argc) {
|
|
const std::string_view arg{argv[idx]};
|
|
if (arg == "--no-newline") {
|
|
no_newline = true;
|
|
++idx;
|
|
} else if (arg == "--stderr") {
|
|
to_stderr = true;
|
|
++idx;
|
|
} else {
|
|
break;
|
|
}
|
|
}
|
|
std::ostream& os = to_stderr ? std::cerr : std::cout;
|
|
const int count = argc - idx;
|
|
for (int i = idx; i < argc; ++i) {
|
|
os << argv[i];
|
|
const bool is_last = (i == argc - 1);
|
|
if (!is_last || !no_newline) {
|
|
os.put('\n');
|
|
}
|
|
}
|
|
if (count == 0 && !no_newline) {
|
|
os.put('\n');
|
|
}
|
|
os.flush();
|
|
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_grandchild_ignore_sigterm(int argc, char** argv)
|
|
{
|
|
if (argc < 3) {
|
|
return usage_error("grandchild-ignore-sigterm requires <marker_file>");
|
|
}
|
|
const char* marker_path = argv[2];
|
|
int ready_p[2]{-1, -1};
|
|
if (::pipe(ready_p) != 0) {
|
|
return 1;
|
|
}
|
|
const pid_t gc = ::fork();
|
|
if (gc < 0) {
|
|
(void)::close(ready_p[0]);
|
|
(void)::close(ready_p[1]);
|
|
return 1;
|
|
}
|
|
if (gc == 0) {
|
|
(void)::close(ready_p[0]);
|
|
std::signal(SIGTERM, SIG_IGN);
|
|
const int mfd = ::open(marker_path, O_WRONLY | O_CREAT | O_APPEND | O_CLOEXEC, 0644);
|
|
if (mfd < 0) {
|
|
(void)::close(ready_p[1]);
|
|
::_exit(1);
|
|
}
|
|
static constexpr char kTick[] = "tick\n";
|
|
(void)write_all_fd(mfd, kTick, sizeof(kTick) - 1);
|
|
static constexpr char kReady[] = "ready\n";
|
|
(void)write_all_fd(STDOUT_FILENO, kReady, sizeof(kReady) - 1);
|
|
const char one = '1';
|
|
(void)write_all_fd(ready_p[1], &one, 1);
|
|
(void)::close(ready_p[1]);
|
|
(void)::close(STDIN_FILENO);
|
|
(void)::close(STDOUT_FILENO);
|
|
(void)::close(STDERR_FILENO);
|
|
|
|
for (int iter = 0; iter < 500; ++iter) {
|
|
std::this_thread::sleep_for(std::chrono::milliseconds{10});
|
|
(void)write_all_fd(mfd, kTick, sizeof(kTick) - 1);
|
|
}
|
|
(void)::close(mfd);
|
|
::_exit(0);
|
|
}
|
|
|
|
(void)::close(ready_p[1]);
|
|
char dummy = 0;
|
|
bool eof = false;
|
|
(void)read_exact_fd(ready_p[0], &dummy, 1, eof);
|
|
(void)::close(ready_p[0]);
|
|
::_exit(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)
|
|
{
|
|
std::ios::sync_with_stdio(false);
|
|
if (argc < 2) {
|
|
return usage_error("missing mode");
|
|
}
|
|
const std::string_view mode{argv[1]};
|
|
if (mode == "echo") {
|
|
return mode_echo(argc, argv);
|
|
}
|
|
if (mode == "cat") {
|
|
return mode_cat();
|
|
}
|
|
if (mode == "seq") {
|
|
return mode_seq(argc, argv);
|
|
}
|
|
if (mode == "streams") {
|
|
return mode_streams();
|
|
}
|
|
if (mode == "exit") {
|
|
return mode_exit(argc, argv);
|
|
}
|
|
if (mode == "lines") {
|
|
return mode_lines(argc, argv);
|
|
}
|
|
if (mode == "ignore-sigterm") {
|
|
return mode_ignore_sigterm(argc, argv);
|
|
}
|
|
if (mode == "grandchild-ignore-sigterm") {
|
|
return mode_grandchild_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");
|
|
} |