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
|
#include <iostream>
#include <string>
#include <thread>
#include <boost/asio/steady_timer.hpp>
#include <boost/process/v2/environment.hpp>
extern char **environ;
#if defined(BOOST_PROCESS_V2_WINDOWS)
#include <windows.h>
#else
#include <unistd.h>
#endif
int main(int argc, char * argv[])
{
std::string mode = argv[1];
if (mode == "exit-code")
return std::stoi(argv[2]);
else if (mode == "sleep")
{
const auto delay = std::chrono::milliseconds(std::stoi(argv[2]));
std::this_thread::sleep_for(delay);
return 0;
}
else if (mode == "print-args")
for (auto i = 0; i < argc; i++)
{
std::cout << argv[i] << std::endl;
std::cerr << argv[i] << std::endl;
if (!std::cout || !std::cerr)
return 1;
}
else if (mode == "echo")
std::cout << std::cin.rdbuf();
else if (mode == "print-cwd")
{
#if defined(BOOST_PROCESS_V2_WINDOWS)
wchar_t buf[65535];
const auto sz = ::GetCurrentDirectoryW(sizeof(buf), buf);
std::wcout << boost::process::v2::wstring_view(buf, sz) << std::flush;
#else
char buf[65535];
printf(::getcwd(buf, sizeof(buf)));
#endif
return 0;
}
else if (mode == "check-eof")
{
std::string st;
std::cin >> st;
return std::cin.eof() ? 0 : 1;
}
else if (mode == "print-env")
{
auto p = ::getenv(argv[2]);
if (p && *p)
printf("%s", p);
else
{
printf("Can't find %s in environment\n", argv[2]);
for (auto e = environ; e != nullptr; e++)
printf(" %s\n", *e);
return 3;
}
}
#if defined(BOOST_PROCESS_V2_WINDOWS)
else if (mode == "showwindow")
{
STARTUPINFO si;
GetStartupInfo(&si);
return static_cast<int>(si.wShowWindow);
}
#endif
else if (mode == "sigterm")
{
boost::asio::io_context ctx;
boost::asio::steady_timer tim{ctx, std::chrono::seconds(10)};
static boost::asio::steady_timer * tim_p = &tim;
#if defined(BOOST_PROCESS_V2_WINDOWS)
SetConsoleCtrlHandler(
[](DWORD kind)
{
if (kind == CTRL_CLOSE_EVENT)
{
// windows doesn't like us doing anything else
::exit(0);
if (tim_p != nullptr)
tim_p->cancel();
return TRUE;
}
else
return FALSE;
}, TRUE);
#else
signal(SIGTERM, [](int) { if (tim_p != nullptr) tim_p->cancel();});
#endif
boost::system::error_code ec;
tim.async_wait([&](boost::system::error_code ec_) { ec = ec_; });
ctx.run();
tim_p = nullptr;
return ec ? EXIT_SUCCESS : 32;
}
else if (mode == "sigint")
{
boost::asio::io_context ctx;
boost::asio::steady_timer tim{ctx, std::chrono::seconds(10)};
static boost::asio::steady_timer * tim_p = &tim;
#if defined(BOOST_PROCESS_V2_WINDOWS)
SetConsoleCtrlHandler(NULL, FALSE);
auto res = SetConsoleCtrlHandler(
[](DWORD kind)
{
if (kind == CTRL_C_EVENT)
{
if (tim_p != nullptr)
tim_p->cancel();
return TRUE;
}
else
return FALSE;
}, TRUE);
BOOST_ASSERT(res != FALSE);
#else
signal(SIGINT, [](int) { if (tim_p != nullptr) tim_p->cancel();});
#endif
boost::system::error_code ec;
tim.async_wait([&](boost::system::error_code ec_) { ec = ec_; });
ctx.run();
tim_p = nullptr;
return ec ? EXIT_SUCCESS : 33;
}
else
return 34;
return 0;
}
|