climanager.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. // TODO: Remove
  49. qInfo() << QString::fromStdString(string("InitCLI with path ") + Config::getValue("CLI-Path"));
  50. emit qmlHandler->log(QString::fromStdString(string("InitCLI with path ") + Config::getValue("CLI-Path")));
  51. if (!Config::getValue("CLI-Path").size()) {
  52. qInfo() << "Empty CLI-Path!";
  53. return;
  54. }
  55. pipe(inpipefd);
  56. pipe(outpipefd);
  57. childpid = fork();
  58. if (childpid == 0) {
  59. // Child
  60. dup2(outpipefd[0], STDIN_FILENO);
  61. dup2(inpipefd[1], STDOUT_FILENO);
  62. // dup2(inpipefd[1], STDERR_FILENO);
  63. // ask kernel to deliver SIGTERM in case the parent dies
  64. prctl(PR_SET_PDEATHSIG, SIGTERM);
  65. execl(Config::getValue("CLI-Path").c_str(), "ccats-cli", "--machine", (char *)NULL);
  66. exit(1);
  67. }
  68. close(outpipefd[0]);
  69. close(inpipefd[1]);
  70. setProgramActive(true);
  71. if (Config::getValue("Keyfile-Path").size()) {
  72. QString cmd = QString("keyfile ") + Config::getValue("Keyfile-Path").c_str();
  73. writeToCli(cmd);
  74. }
  75. // start threads
  76. readPipeLoopThread = thread(&CliManager::readPipeLoop);
  77. notificationsLoopThread = thread(&CliManager::notificationsLoop);
  78. statusLoopThread = thread(&CliManager::statusLoop);
  79. }
  80. std::vector<std::string> tokenizeByNewlines(std::string in) {
  81. vector<string> res;
  82. size_t index;
  83. for (index = in.find("\n"); index != std::string::npos; index = in.find("\n")) {
  84. if (index != 0)
  85. res.push_back(in.substr(0, index));
  86. in = in.substr(index + 1);
  87. }
  88. if (in.length() > 0)
  89. res.push_back(in);
  90. return res;
  91. }
  92. void CliManager::readPipeLoop() {
  93. unsigned int readOffset = 0;
  94. unsigned int pollCount = 0;
  95. struct pollfd inPipeStatus;
  96. inPipeStatus.fd = inpipefd[0];
  97. inPipeStatus.events = POLLIN;
  98. vector<string> inputs;
  99. string pipeInput;
  100. while (programActive) {
  101. inputs = vector<string>();
  102. poll(&inPipeStatus, 1, 100);
  103. if (inPipeStatus.revents & POLLIN) {
  104. readOffset += read(inpipefd[0], buf + readOffset, 1);
  105. pollCount = 0;
  106. } else {
  107. pollCount++;
  108. }
  109. if (pollCount > 4 && (readOffset || pipeInput.size())) {
  110. pipeInput.append(buf);
  111. inputs = tokenizeByNewlines(pipeInput);
  112. for (string s : inputs) {
  113. JsonHandler::parseJSON(s);
  114. }
  115. pipeInput = string();
  116. memset(buf, 0, 1025);
  117. pollCount = 0;
  118. readOffset = 0;
  119. if (waitpid(childpid, NULL, WNOHANG)) {
  120. // TODO: Remove
  121. qInfo() << "CLI has exited/did not launch";
  122. emit qmlHandler->log("CLI has exited/did not launch");
  123. emit qmlHandler->footerSetError("CLI crashed. Please restart the application.");
  124. setProgramActive(false);
  125. // nonzero means error or childid has changed state
  126. // for us that means child has exited -> CLI is dead
  127. break;
  128. }
  129. }
  130. if (readOffset >= 1024) {
  131. pipeInput.append(buf);
  132. inputs = tokenizeByNewlines(pipeInput);
  133. for (unsigned i = 0; i < inputs.size() - 1; i++) { // process all lines except the last, potentially incomplete one
  134. JsonHandler::parseJSON(inputs[i]);
  135. }
  136. if (pipeInput.back() == '\n') { // process last line if it was complete
  137. JsonHandler::parseJSON(inputs.back());
  138. pipeInput = string();
  139. } else {
  140. pipeInput = inputs[inputs.size() - 1]; // set last, potentially incomplete line, as current buffer
  141. }
  142. readOffset = 0;
  143. pollCount = 0;
  144. memset(buf, 0, 1025);
  145. }
  146. }
  147. }
  148. void CliManager::notificationsLoop() {
  149. while (programActive) {
  150. std::this_thread::sleep_for(std::chrono::seconds(3));
  151. if (loggedin) {
  152. writeToCli("notifications");
  153. writeToCli("extendedlist");
  154. }
  155. }
  156. }
  157. void CliManager::statusLoop() {
  158. while (programActive) {
  159. std::this_thread::sleep_for(std::chrono::seconds(1));
  160. if (loggedin) {
  161. writeToCli("status");
  162. writeToCli("extendedstatus");
  163. }
  164. }
  165. }
  166. void CliManager::setProgramActive(bool active) { programActive = active; }