climanager.cpp 4.9 KB

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