| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- // Linux SOL process bridge. stdin/stdout: raw serial bytes. stderr: JSON status.
- // Uses FreeIPMI libipmiconsole, with both three- and four-argument ctx_create.
- // No Qt dependency. Credentials arrive through the environment, never argv.
- #include <ipmiconsole.h>
- #include <poll.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <cerrno>
- #include <chrono>
- #include <csignal>
- #include <cstdio>
- #include <cstdlib>
- #include <cstring>
- #include <string>
- #include <type_traits>
- namespace {
- volatile sig_atomic_t stopped = 0;
- void stop_handler(int) { stopped = 1; }
- void status(const char *event, int code = 0) {
- std::fprintf(stderr, "{\"event\":\"%s\",\"code\":%d}\n", event, code);
- std::fflush(stderr);
- }
- template <typename T> struct Arity;
- template <typename R, typename... A> struct Arity<R (*)(A...)> {
- static const int value = sizeof...(A);
- };
- template <typename T> struct Fourth;
- template <typename R, typename A, typename B, typename C, typename D>
- struct Fourth<R (*)(A, B, C, D)> { typedef typename std::remove_pointer<D>::type type; };
- template <int N> struct Create;
- template <> struct Create<3> {
- template <typename F> static ipmiconsole_ctx_t call(F fn, const char *host,
- ipmiconsole_ipmi_config *ic, ipmiconsole_protocol_config *pc) {
- return fn(host, ic, pc);
- }
- };
- template <> struct Create<4> {
- template <typename F> static ipmiconsole_ctx_t call(F fn, const char *host,
- ipmiconsole_ipmi_config *ic, ipmiconsole_protocol_config *pc) {
- typename Fourth<F>::type ec;
- std::memset(&ec, 0, sizeof(ec));
- // Do not take over another session unless explicitly requested via STL_SOL_STEAL_INUSE=1
- const char *steal = std::getenv("STL_SOL_STEAL_INUSE");
- if (!steal || std::strcmp(steal, "1") != 0) {
- #ifdef IPMICONSOLE_BEHAVIOR_ERROR_ON_SOL_INUSE
- ec.behavior_flags = IPMICONSOLE_BEHAVIOR_ERROR_ON_SOL_INUSE;
- #endif
- }
- return fn(host, ic, pc, &ec);
- }
- };
- bool write_all(int fd, const char *data, size_t size) {
- while (size && !stopped) {
- ssize_t n = ::write(fd, data, size);
- if (n > 0) { data += n; size -= static_cast<size_t>(n); continue; }
- if (n < 0 && errno == EINTR) continue;
- if (n < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
- pollfd p = {fd, POLLOUT, 0};
- if (::poll(&p, 1, 5000) > 0 && (p.revents & POLLOUT)) continue;
- }
- return false;
- }
- return size == 0;
- }
- bool nonblock(int fd) {
- int flags = ::fcntl(fd, F_GETFL, 0);
- return flags >= 0 && ::fcntl(fd, F_SETFL, flags | O_NONBLOCK) == 0;
- }
- bool drain_console(int fd, bool &eof) {
- char buf[8192];
- eof = false;
- for (;;) {
- ssize_t n = ::read(fd, buf, sizeof(buf));
- if (n > 0) {
- if (!write_all(STDOUT_FILENO, buf, static_cast<size_t>(n))) return false;
- continue;
- }
- if (n == 0) { eof = true; return true; }
- if (errno == EINTR) continue;
- if (errno == EAGAIN || errno == EWOULDBLOCK) return true;
- return false;
- }
- }
- }
- int main(int argc, char **argv) {
- if (argc == 2 && std::strcmp(argv[1], "--help") == 0) {
- std::fprintf(stderr, "stl-sol-helper: STL_SOL_HOST, STL_SOL_USER, STL_SOL_PASSWORD; "
- "optional STL_SOL_CIPHER_SUITE (default 17). stdin/stdout are raw SOL bytes.\n");
- return 0;
- }
- if (argc != 1) { status("error", 1001); return 2; }
- const char *host_env = std::getenv("STL_SOL_HOST");
- const char *user_env = std::getenv("STL_SOL_USER");
- const char *pass_env = std::getenv("STL_SOL_PASSWORD");
- if (!host_env || !*host_env || !user_env || !*user_env || !pass_env || !*pass_env) {
- status("error", 1002); return 2;
- }
- std::string host(host_env), user(user_env), pass(pass_env);
- const char *suite_env = std::getenv("STL_SOL_CIPHER_SUITE");
- char *end = nullptr;
- long suite = suite_env ? std::strtol(suite_env, &end, 10) : 17;
- if ((suite_env && (!*suite_env || *end)) || suite < 0 || suite > 255) {
- status("error", 1003); return 2;
- }
- std::signal(SIGTERM, stop_handler);
- std::signal(SIGINT, stop_handler);
- std::signal(SIGPIPE, SIG_IGN);
- if (ipmiconsole_engine_init(1, 0) < 0) { status("error", 1004); return 2; }
- ipmiconsole_ipmi_config ic;
- ipmiconsole_protocol_config pc;
- std::memset(&ic, 0, sizeof(ic));
- std::memset(&pc, 0, sizeof(pc));
- ic.username = &user[0];
- ic.password = &pass[0];
- ic.privilege_level = IPMICONSOLE_PRIVILEGE_ADMIN;
- ic.cipher_suite_id = static_cast<int>(suite);
- pc.session_timeout_len = 60000;
- pc.retransmission_timeout_len = 500;
- pc.retransmission_backoff_count = 2;
- pc.keepalive_timeout_len = 20000;
- pc.retransmission_keepalive_timeout_len = 5000;
- pc.acceptable_packet_errors_count = 16;
- pc.maximum_retransmission_count = 16;
- ipmiconsole_ctx_t ctx = Create<Arity<decltype(&ipmiconsole_ctx_create)>::value>::call(
- &ipmiconsole_ctx_create, host.c_str(), &ic, &pc);
- int code = 0;
- if (!ctx) { status("error", 1005); code = 2; }
- else if (ipmiconsole_engine_submit(ctx, nullptr, nullptr) < 0) {
- status("error", ipmiconsole_ctx_errnum(ctx)); code = 2;
- } else {
- status("connecting");
- auto start = std::chrono::steady_clock::now();
- int fd = -1;
- while (!stopped) {
- ipmiconsole_ctx_status_t state = ipmiconsole_ctx_status(ctx);
- if (state == IPMICONSOLE_CTX_STATUS_SOL_ESTABLISHED) {
- fd = ipmiconsole_ctx_fd(ctx);
- if (fd < 0) { status("error", 1008); code = 4; }
- break;
- }
- if (state == IPMICONSOLE_CTX_STATUS_SOL_ERROR || state == IPMICONSOLE_CTX_STATUS_ERROR) {
- status("error", ipmiconsole_ctx_errnum(ctx)); code = 3; break;
- }
- if (std::chrono::duration_cast<std::chrono::seconds>(
- std::chrono::steady_clock::now() - start).count() >= 25) {
- status("error", 1006); code = 3; break;
- }
- ::poll(nullptr, 0, 100);
- }
- if (fd >= 0 && !stopped) {
- if (!nonblock(fd) || !nonblock(STDOUT_FILENO)) {
- status("error", 1007); code = 4;
- } else {
- status("ready");
- while (!stopped) {
- ipmiconsole_ctx_status_t state = ipmiconsole_ctx_status(ctx);
- if (state != IPMICONSOLE_CTX_STATUS_SOL_ESTABLISHED) {
- bool eof = false;
- drain_console(fd, eof);
- status("error", ipmiconsole_ctx_errnum(ctx)); code = 4; break;
- }
- pollfd fds[2] = {{fd, POLLIN, 0}, {STDIN_FILENO, POLLIN, 0}};
- int n = ::poll(fds, 2, 200);
- if (n < 0) { if (errno == EINTR) continue; code = 4; break; }
- char buf[8192];
- if (fds[0].revents & (POLLIN | POLLHUP | POLLERR)) {
- bool eof = false;
- if (!drain_console(fd, eof) || eof) { code = 4; break; }
- }
- if (fds[1].revents & POLLIN) {
- ssize_t count = ::read(STDIN_FILENO, buf, sizeof(buf));
- if (count > 0 && !write_all(fd, buf, static_cast<size_t>(count))) { code = 4; break; }
- if (count == 0) break;
- if (count < 0 && errno != EAGAIN && errno != EINTR) { code = 4; break; }
- }
- if (fds[0].revents & (POLLERR | POLLHUP | POLLNVAL)) { code = 4; break; }
- if (fds[1].revents & (POLLERR | POLLHUP | POLLNVAL)) break;
- }
- }
- }
- }
- if (ctx) ipmiconsole_ctx_destroy(ctx);
- ipmiconsole_engine_teardown(1);
- if (code) status("error", code);
- status("closed");
- return code;
- }
|