climanager.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #include <QDebug>
  2. #include <QGuiApplication>
  3. #include <csignal>
  4. #include <cstdio>
  5. #include <cstdlib>
  6. #include <iostream>
  7. #include <poll.h>
  8. #include <string>
  9. #include <sys/prctl.h>
  10. #include <sys/stat.h>
  11. #include <sys/wait.h>
  12. #include <thread>
  13. #include <unistd.h>
  14. #include <boost/asio.hpp>
  15. #include <boost/filesystem.hpp>
  16. #include <boost/lexical_cast.hpp>
  17. #include <iostream>
  18. #include <json/json.h>
  19. #include "../include/climanager.h"
  20. #include "../include/jsonhandler.h"
  21. using namespace std;
  22. namespace CliManager {
  23. QMLHandler *qmlHandler;
  24. bool programActive = true;
  25. int inpipefd[2];
  26. int outpipefd[2];
  27. char buf[1025];
  28. pid_t childpid;
  29. } // namespace CliManager
  30. void CliManager::writeToCli(QString command) {
  31. command += "\n";
  32. write(outpipefd[1], command.toUtf8().constData(), strlen(command.toUtf8().constData()));
  33. }
  34. void CliManager::onExit() { writeToCli("exit"); }
  35. void CliManager::setQmlHandler(QMLHandler *q) { qmlHandler = q; }
  36. void CliManager::startCli() {
  37. pipe(inpipefd);
  38. pipe(outpipefd);
  39. childpid = fork();
  40. if (childpid == 0) {
  41. // Child
  42. dup2(outpipefd[0], STDIN_FILENO);
  43. dup2(inpipefd[1], STDOUT_FILENO);
  44. // dup2(inpipefd[1], STDERR_FILENO);
  45. // ask kernel to deliver SIGTERM in case the parent dies
  46. prctl(PR_SET_PDEATHSIG, SIGTERM);
  47. execl(Config::getValue("CLI-Path").c_str(), "ccats-cli", "--machine", (char *)NULL);
  48. exit(1);
  49. }
  50. close(outpipefd[0]);
  51. close(inpipefd[1]);
  52. std::thread(&CliManager::readPipeLoop).detach();
  53. }
  54. std::vector<std::string> tokenizeByNewlines(std::string in) {
  55. vector<string> res;
  56. size_t index;
  57. for (index = in.find("\n"); index != std::string::npos; index = in.find("\n")) {
  58. if (index != 0)
  59. res.push_back(in.substr(0, index));
  60. in = in.substr(index + 1);
  61. }
  62. if (in.length() > 0)
  63. res.push_back(in);
  64. return res;
  65. }
  66. void CliManager::readPipeLoop() {
  67. unsigned int readOffset = 0;
  68. unsigned int pollCount = 0;
  69. struct pollfd inPipeStatus;
  70. inPipeStatus.fd = inpipefd[0];
  71. inPipeStatus.events = POLLIN;
  72. vector<string> inputs;
  73. string pipeInput;
  74. while (programActive) {
  75. inputs = vector<string>();
  76. poll(&inPipeStatus, 1, 100);
  77. if (inPipeStatus.revents & POLLIN) {
  78. readOffset += read(inpipefd[0], buf + readOffset, 1);
  79. pollCount = 0;
  80. } else {
  81. pollCount++;
  82. }
  83. if (pollCount > 4 && (readOffset || pipeInput.size())) {
  84. pipeInput.append(buf);
  85. inputs = tokenizeByNewlines(pipeInput);
  86. for (string s : inputs) {
  87. emit qmlHandler->log(QString::fromStdString(s));
  88. qInfo() << QString::fromStdString(s);
  89. // handleJSON(s);
  90. JsonHandler::parseJSON(s);
  91. }
  92. pipeInput = string();
  93. memset(buf, 0, 1025);
  94. pollCount = 0;
  95. readOffset = 0;
  96. if (waitpid(childpid, NULL, WNOHANG)) {
  97. // nonzero means error or childid has changed state
  98. // for us that means child has exited -> CLI is dead
  99. break;
  100. }
  101. }
  102. if (readOffset >= 1024) {
  103. pipeInput.append(buf);
  104. readOffset = 0;
  105. pollCount = 0;
  106. memset(buf, 0, 1025);
  107. }
  108. }
  109. }
  110. void CliManager::setProgramActive(bool active) { programActive = active; }