climanager.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 = true;
  26. int inpipefd[2];
  27. int outpipefd[2];
  28. char buf[1025];
  29. pid_t childpid;
  30. } // namespace CliManager
  31. void CliManager::writeToCli(QString command) {
  32. command += "\n";
  33. write(outpipefd[1], command.toUtf8().constData(), strlen(command.toUtf8().constData()));
  34. }
  35. void CliManager::onExit() {
  36. // stop threads
  37. CliManager::setProgramActive(false);
  38. readPipeLoopThread.join();
  39. notificationsLoopThread.join();
  40. statusLoopThread.join();
  41. writeToCli("exit");
  42. }
  43. void CliManager::setQmlHandler(QMLHandler *q) { qmlHandler = q; }
  44. void CliManager::init() {
  45. pipe(inpipefd);
  46. pipe(outpipefd);
  47. childpid = fork();
  48. if (childpid == 0) {
  49. // Child
  50. dup2(outpipefd[0], STDIN_FILENO);
  51. dup2(inpipefd[1], STDOUT_FILENO);
  52. // dup2(inpipefd[1], STDERR_FILENO);
  53. // ask kernel to deliver SIGTERM in case the parent dies
  54. prctl(PR_SET_PDEATHSIG, SIGTERM);
  55. execl(Config::getValue("CLI-Path").c_str(), "ccats-cli", "--machine", (char *)NULL);
  56. exit(1);
  57. }
  58. close(outpipefd[0]);
  59. close(inpipefd[1]);
  60. // start threads
  61. readPipeLoopThread = thread(&CliManager::readPipeLoop);
  62. notificationsLoopThread = thread(&CliManager::notificationsLoop);
  63. statusLoopThread = thread(&CliManager::statusLoop);
  64. }
  65. std::vector<std::string> tokenizeByNewlines(std::string in) {
  66. vector<string> res;
  67. size_t index;
  68. for (index = in.find("\n"); index != std::string::npos; index = in.find("\n")) {
  69. if (index != 0)
  70. res.push_back(in.substr(0, index));
  71. in = in.substr(index + 1);
  72. }
  73. if (in.length() > 0)
  74. res.push_back(in);
  75. return res;
  76. }
  77. void CliManager::readPipeLoop() {
  78. unsigned int readOffset = 0;
  79. unsigned int pollCount = 0;
  80. struct pollfd inPipeStatus;
  81. inPipeStatus.fd = inpipefd[0];
  82. inPipeStatus.events = POLLIN;
  83. vector<string> inputs;
  84. string pipeInput;
  85. while (programActive) {
  86. inputs = vector<string>();
  87. poll(&inPipeStatus, 1, 100);
  88. if (inPipeStatus.revents & POLLIN) {
  89. readOffset += read(inpipefd[0], buf + readOffset, 1);
  90. pollCount = 0;
  91. } else {
  92. pollCount++;
  93. }
  94. if (pollCount > 4 && (readOffset || pipeInput.size())) {
  95. pipeInput.append(buf);
  96. inputs = tokenizeByNewlines(pipeInput);
  97. for (string s : inputs) {
  98. emit qmlHandler->log(QString::fromStdString(s));
  99. qInfo() << QString::fromStdString(s);
  100. // handleJSON(s);
  101. JsonHandler::parseJSON(s);
  102. }
  103. pipeInput = string();
  104. memset(buf, 0, 1025);
  105. pollCount = 0;
  106. readOffset = 0;
  107. if (waitpid(childpid, NULL, WNOHANG)) {
  108. // nonzero means error or childid has changed state
  109. // for us that means child has exited -> CLI is dead
  110. break;
  111. }
  112. }
  113. if (readOffset >= 1024) {
  114. pipeInput.append(buf);
  115. inputs = tokenizeByNewlines(pipeInput);
  116. for (unsigned i = 0; i < inputs.size() - 1; i++) { // process all lines except the last, potentially incomplete one
  117. // 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
  118. //~ emit qmlHandler->log(QString::fromStdString(inputs[i]));
  119. qInfo() << QString::fromStdString(inputs[i]);
  120. // handleJSON(s);
  121. JsonHandler::parseJSON(inputs[i]);
  122. }
  123. if (pipeInput.back() == '\n') { // process last line if it was complete
  124. qInfo() << QString::fromStdString(inputs.back());
  125. // handleJSON(s);
  126. JsonHandler::parseJSON(inputs.back());
  127. pipeInput = string();
  128. } else {
  129. pipeInput = inputs[inputs.size() - 1]; // set last, potentially incomplete line, as current buffer
  130. }
  131. readOffset = 0;
  132. pollCount = 0;
  133. memset(buf, 0, 1025);
  134. }
  135. }
  136. }
  137. void CliManager::notificationsLoop() {
  138. while (programActive) {
  139. std::this_thread::sleep_for(std::chrono::milliseconds(3000));
  140. writeToCli("notifications");
  141. writeToCli("extendedlist");
  142. }
  143. }
  144. void CliManager::statusLoop() {
  145. while (programActive) {
  146. std::this_thread::sleep_for(std::chrono::seconds(1));
  147. writeToCli("status");
  148. writeToCli("extendedstatus");
  149. }
  150. }
  151. void CliManager::setProgramActive(bool active) { programActive = active; }