sol_helper.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. // Linux SOL process bridge. stdin/stdout: raw serial bytes. stderr: JSON status.
  2. // Uses FreeIPMI libipmiconsole, with both three- and four-argument ctx_create.
  3. // No Qt dependency. Credentials arrive through the environment, never argv.
  4. #include <ipmiconsole.h>
  5. #include <poll.h>
  6. #include <unistd.h>
  7. #include <fcntl.h>
  8. #include <cerrno>
  9. #include <chrono>
  10. #include <csignal>
  11. #include <cstdio>
  12. #include <cstdlib>
  13. #include <cstring>
  14. #include <string>
  15. #include <type_traits>
  16. namespace {
  17. volatile sig_atomic_t stopped = 0;
  18. void stop_handler(int) { stopped = 1; }
  19. void status(const char *event, int code = 0) {
  20. std::fprintf(stderr, "{\"event\":\"%s\",\"code\":%d}\n", event, code);
  21. std::fflush(stderr);
  22. }
  23. template <typename T> struct Arity;
  24. template <typename R, typename... A> struct Arity<R (*)(A...)> {
  25. static const int value = sizeof...(A);
  26. };
  27. template <typename T> struct Fourth;
  28. template <typename R, typename A, typename B, typename C, typename D>
  29. struct Fourth<R (*)(A, B, C, D)> { typedef typename std::remove_pointer<D>::type type; };
  30. template <int N> struct Create;
  31. template <> struct Create<3> {
  32. template <typename F> static ipmiconsole_ctx_t call(F fn, const char *host,
  33. ipmiconsole_ipmi_config *ic, ipmiconsole_protocol_config *pc) {
  34. return fn(host, ic, pc);
  35. }
  36. };
  37. template <> struct Create<4> {
  38. template <typename F> static ipmiconsole_ctx_t call(F fn, const char *host,
  39. ipmiconsole_ipmi_config *ic, ipmiconsole_protocol_config *pc) {
  40. typename Fourth<F>::type ec;
  41. std::memset(&ec, 0, sizeof(ec));
  42. // Do not take over another session unless explicitly requested via STL_SOL_STEAL_INUSE=1
  43. const char *steal = std::getenv("STL_SOL_STEAL_INUSE");
  44. if (!steal || std::strcmp(steal, "1") != 0) {
  45. #ifdef IPMICONSOLE_BEHAVIOR_ERROR_ON_SOL_INUSE
  46. ec.behavior_flags = IPMICONSOLE_BEHAVIOR_ERROR_ON_SOL_INUSE;
  47. #endif
  48. }
  49. return fn(host, ic, pc, &ec);
  50. }
  51. };
  52. bool write_all(int fd, const char *data, size_t size) {
  53. while (size && !stopped) {
  54. ssize_t n = ::write(fd, data, size);
  55. if (n > 0) { data += n; size -= static_cast<size_t>(n); continue; }
  56. if (n < 0 && errno == EINTR) continue;
  57. if (n < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
  58. pollfd p = {fd, POLLOUT, 0};
  59. if (::poll(&p, 1, 5000) > 0 && (p.revents & POLLOUT)) continue;
  60. }
  61. return false;
  62. }
  63. return size == 0;
  64. }
  65. bool nonblock(int fd) {
  66. int flags = ::fcntl(fd, F_GETFL, 0);
  67. return flags >= 0 && ::fcntl(fd, F_SETFL, flags | O_NONBLOCK) == 0;
  68. }
  69. bool drain_console(int fd, bool &eof) {
  70. char buf[8192];
  71. eof = false;
  72. for (;;) {
  73. ssize_t n = ::read(fd, buf, sizeof(buf));
  74. if (n > 0) {
  75. if (!write_all(STDOUT_FILENO, buf, static_cast<size_t>(n))) return false;
  76. continue;
  77. }
  78. if (n == 0) { eof = true; return true; }
  79. if (errno == EINTR) continue;
  80. if (errno == EAGAIN || errno == EWOULDBLOCK) return true;
  81. return false;
  82. }
  83. }
  84. }
  85. int main(int argc, char **argv) {
  86. if (argc == 2 && std::strcmp(argv[1], "--help") == 0) {
  87. std::fprintf(stderr, "stl-sol-helper: STL_SOL_HOST, STL_SOL_USER, STL_SOL_PASSWORD; "
  88. "optional STL_SOL_CIPHER_SUITE (default 17). stdin/stdout are raw SOL bytes.\n");
  89. return 0;
  90. }
  91. if (argc != 1) { status("error", 1001); return 2; }
  92. const char *host_env = std::getenv("STL_SOL_HOST");
  93. const char *user_env = std::getenv("STL_SOL_USER");
  94. const char *pass_env = std::getenv("STL_SOL_PASSWORD");
  95. if (!host_env || !*host_env || !user_env || !*user_env || !pass_env || !*pass_env) {
  96. status("error", 1002); return 2;
  97. }
  98. std::string host(host_env), user(user_env), pass(pass_env);
  99. const char *suite_env = std::getenv("STL_SOL_CIPHER_SUITE");
  100. char *end = nullptr;
  101. long suite = suite_env ? std::strtol(suite_env, &end, 10) : 17;
  102. if ((suite_env && (!*suite_env || *end)) || suite < 0 || suite > 255) {
  103. status("error", 1003); return 2;
  104. }
  105. std::signal(SIGTERM, stop_handler);
  106. std::signal(SIGINT, stop_handler);
  107. std::signal(SIGPIPE, SIG_IGN);
  108. if (ipmiconsole_engine_init(1, 0) < 0) { status("error", 1004); return 2; }
  109. ipmiconsole_ipmi_config ic;
  110. ipmiconsole_protocol_config pc;
  111. std::memset(&ic, 0, sizeof(ic));
  112. std::memset(&pc, 0, sizeof(pc));
  113. ic.username = &user[0];
  114. ic.password = &pass[0];
  115. ic.privilege_level = IPMICONSOLE_PRIVILEGE_ADMIN;
  116. ic.cipher_suite_id = static_cast<int>(suite);
  117. pc.session_timeout_len = 60000;
  118. pc.retransmission_timeout_len = 500;
  119. pc.retransmission_backoff_count = 2;
  120. pc.keepalive_timeout_len = 20000;
  121. pc.retransmission_keepalive_timeout_len = 5000;
  122. pc.acceptable_packet_errors_count = 16;
  123. pc.maximum_retransmission_count = 16;
  124. ipmiconsole_ctx_t ctx = Create<Arity<decltype(&ipmiconsole_ctx_create)>::value>::call(
  125. &ipmiconsole_ctx_create, host.c_str(), &ic, &pc);
  126. int code = 0;
  127. if (!ctx) { status("error", 1005); code = 2; }
  128. else if (ipmiconsole_engine_submit(ctx, nullptr, nullptr) < 0) {
  129. status("error", ipmiconsole_ctx_errnum(ctx)); code = 2;
  130. } else {
  131. status("connecting");
  132. auto start = std::chrono::steady_clock::now();
  133. int fd = -1;
  134. while (!stopped) {
  135. ipmiconsole_ctx_status_t state = ipmiconsole_ctx_status(ctx);
  136. if (state == IPMICONSOLE_CTX_STATUS_SOL_ESTABLISHED) {
  137. fd = ipmiconsole_ctx_fd(ctx);
  138. if (fd < 0) { status("error", 1008); code = 4; }
  139. break;
  140. }
  141. if (state == IPMICONSOLE_CTX_STATUS_SOL_ERROR || state == IPMICONSOLE_CTX_STATUS_ERROR) {
  142. status("error", ipmiconsole_ctx_errnum(ctx)); code = 3; break;
  143. }
  144. if (std::chrono::duration_cast<std::chrono::seconds>(
  145. std::chrono::steady_clock::now() - start).count() >= 25) {
  146. status("error", 1006); code = 3; break;
  147. }
  148. ::poll(nullptr, 0, 100);
  149. }
  150. if (fd >= 0 && !stopped) {
  151. if (!nonblock(fd) || !nonblock(STDOUT_FILENO)) {
  152. status("error", 1007); code = 4;
  153. } else {
  154. status("ready");
  155. while (!stopped) {
  156. ipmiconsole_ctx_status_t state = ipmiconsole_ctx_status(ctx);
  157. if (state != IPMICONSOLE_CTX_STATUS_SOL_ESTABLISHED) {
  158. bool eof = false;
  159. drain_console(fd, eof);
  160. status("error", ipmiconsole_ctx_errnum(ctx)); code = 4; break;
  161. }
  162. pollfd fds[2] = {{fd, POLLIN, 0}, {STDIN_FILENO, POLLIN, 0}};
  163. int n = ::poll(fds, 2, 200);
  164. if (n < 0) { if (errno == EINTR) continue; code = 4; break; }
  165. char buf[8192];
  166. if (fds[0].revents & (POLLIN | POLLHUP | POLLERR)) {
  167. bool eof = false;
  168. if (!drain_console(fd, eof) || eof) { code = 4; break; }
  169. }
  170. if (fds[1].revents & POLLIN) {
  171. ssize_t count = ::read(STDIN_FILENO, buf, sizeof(buf));
  172. if (count > 0 && !write_all(fd, buf, static_cast<size_t>(count))) { code = 4; break; }
  173. if (count == 0) break;
  174. if (count < 0 && errno != EAGAIN && errno != EINTR) { code = 4; break; }
  175. }
  176. if (fds[0].revents & (POLLERR | POLLHUP | POLLNVAL)) { code = 4; break; }
  177. if (fds[1].revents & (POLLERR | POLLHUP | POLLNVAL)) break;
  178. }
  179. }
  180. }
  181. }
  182. if (ctx) ipmiconsole_ctx_destroy(ctx);
  183. ipmiconsole_engine_teardown(1);
  184. if (code) status("error", code);
  185. status("closed");
  186. return code;
  187. }