climanager.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. #include <QDebug>
  2. #include <QGuiApplication>
  3. #include <chrono>
  4. #include <csignal>
  5. #include <cstdio>
  6. #include <cstdlib>
  7. #include <iostream>
  8. #include <poll.h>
  9. #include <string>
  10. #include <sys/prctl.h>
  11. #include <sys/stat.h>
  12. #include <sys/wait.h>
  13. #include <thread>
  14. #include <unistd.h>
  15. #include <iostream>
  16. #include <json/json.h>
  17. #include "../include/climanager.h"
  18. #include "../include/jsonhandler.h"
  19. using namespace std;
  20. thread readPipeLoopThread;
  21. thread notificationsLoopThread;
  22. thread statusLoopThread;
  23. namespace CliManager {
  24. QMLHandler *qmlHandler;
  25. bool programActive = false;
  26. bool loggedin = false;
  27. int inpipefd[2];
  28. int outpipefd[2];
  29. char buf[1025];
  30. pid_t childpid;
  31. } // namespace CliManager
  32. void CliManager::writeToCli(QString command) {
  33. command += "\n";
  34. write(outpipefd[1], command.toUtf8().constData(), strlen(command.toUtf8().constData()));
  35. }
  36. void CliManager::onExit() {
  37. // stop threads
  38. if (programActive) {
  39. writeToCli("exit");
  40. CliManager::setProgramActive(false);
  41. readPipeLoopThread.join();
  42. notificationsLoopThread.join();
  43. statusLoopThread.join();
  44. }
  45. }
  46. void CliManager::setQmlHandler(QMLHandler *q) { qmlHandler = q; }
  47. void CliManager::init() {
  48. qInfo() << QString::fromStdString(string("InitCLI with path ") + Config::getValue("CLI-Path"));
  49. if (!Config::getValue("CLI-Path").size()) {
  50. qInfo() << "Empty CLI-Path!";
  51. return;
  52. }
  53. pipe(inpipefd);
  54. pipe(outpipefd);
  55. childpid = fork();
  56. if (childpid == 0) {
  57. // Child
  58. dup2(outpipefd[0], STDIN_FILENO);
  59. dup2(inpipefd[1], STDOUT_FILENO);
  60. // dup2(inpipefd[1], STDERR_FILENO);
  61. // ask kernel to deliver SIGTERM in case the parent dies
  62. prctl(PR_SET_PDEATHSIG, SIGTERM);
  63. execl(Config::getValue("CLI-Path").c_str(), "ccats-cli", "--machine", (char *)NULL);
  64. exit(1);
  65. }
  66. close(outpipefd[0]);
  67. close(inpipefd[1]);
  68. setProgramActive(true);
  69. if (Config::getValue("Keyfile-Path").size()) {
  70. QString cmd = QString("keyfile ") + Config::getValue("Keyfile-Path").c_str();
  71. writeToCli(cmd);
  72. }
  73. // start threads
  74. readPipeLoopThread = thread(&CliManager::readPipeLoop);
  75. notificationsLoopThread = thread(&CliManager::notificationsLoop);
  76. statusLoopThread = thread(&CliManager::statusLoop);
  77. }
  78. std::vector<std::string> tokenizeByNewlines(std::string in) {
  79. vector<string> res;
  80. size_t index;
  81. for (index = in.find("\n"); index != std::string::npos; index = in.find("\n")) {
  82. if (index != 0)
  83. res.push_back(in.substr(0, index));
  84. in = in.substr(index + 1);
  85. }
  86. if (in.length() > 0)
  87. res.push_back(in);
  88. return res;
  89. }
  90. void CliManager::readPipeLoop() {
  91. unsigned int readOffset = 0;
  92. unsigned int pollCount = 0;
  93. struct pollfd inPipeStatus;
  94. inPipeStatus.fd = inpipefd[0];
  95. inPipeStatus.events = POLLIN;
  96. vector<string> inputs;
  97. string pipeInput;
  98. while (programActive) {
  99. inputs = vector<string>();
  100. poll(&inPipeStatus, 1, 100);
  101. if (inPipeStatus.revents & POLLIN) {
  102. readOffset += read(inpipefd[0], buf + readOffset, 1);
  103. pollCount = 0;
  104. } else {
  105. pollCount++;
  106. }
  107. if (pollCount > 4 && (readOffset || pipeInput.size())) {
  108. pipeInput.append(buf);
  109. inputs = tokenizeByNewlines(pipeInput);
  110. for (string s : inputs) {
  111. emit qmlHandler->log(QString::fromStdString(s));
  112. qInfo() << QString::fromStdString(s);
  113. // handleJSON(s);
  114. JsonHandler::parseJSON(s);
  115. }
  116. pipeInput = string();
  117. memset(buf, 0, 1025);
  118. pollCount = 0;
  119. readOffset = 0;
  120. if (waitpid(childpid, NULL, WNOHANG)) {
  121. qInfo() << "CLI has exited/did not launch";
  122. setProgramActive(false);
  123. // nonzero means error or childid has changed state
  124. // for us that means child has exited -> CLI is dead
  125. break;
  126. }
  127. }
  128. if (readOffset >= 1024) {
  129. pipeInput.append(buf);
  130. inputs = tokenizeByNewlines(pipeInput);
  131. for (unsigned i = 0; i < inputs.size() - 1; i++) { // process all lines except the last, potentially incomplete one
  132. // do not log these lines, Qt/Quick has no simple way of limiting the number of lines in the logging window so we might go OoM real quick
  133. //~ emit qmlHandler->log(QString::fromStdString(inputs[i]));
  134. qInfo() << QString::fromStdString(inputs[i]);
  135. // handleJSON(s);
  136. JsonHandler::parseJSON(inputs[i]);
  137. }
  138. if (pipeInput.back() == '\n') { // process last line if it was complete
  139. qInfo() << QString::fromStdString(inputs.back());
  140. // handleJSON(s);
  141. JsonHandler::parseJSON(inputs.back());
  142. pipeInput = string();
  143. } else {
  144. pipeInput = inputs[inputs.size() - 1]; // set last, potentially incomplete line, as current buffer
  145. }
  146. readOffset = 0;
  147. pollCount = 0;
  148. memset(buf, 0, 1025);
  149. }
  150. }
  151. }
  152. void CliManager::notificationsLoop() {
  153. while (programActive) {
  154. std::this_thread::sleep_for(std::chrono::seconds(3));
  155. if (loggedin) {
  156. writeToCli("notifications");
  157. writeToCli("extendedlist");
  158. }
  159. }
  160. }
  161. void CliManager::statusLoop() {
  162. while (programActive) {
  163. std::this_thread::sleep_for(std::chrono::seconds(1));
  164. if (loggedin) {
  165. writeToCli("status");
  166. writeToCli("extendedstatus");
  167. }
  168. }
  169. }
  170. void CliManager::setProgramActive(bool active) { programActive = active; }