utils.cc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. #include "include/utils.h"
  2. #include <signal.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <sys/wait.h>
  7. #include <unistd.h>
  8. namespace smartdns
  9. {
  10. TempFile::TempFile()
  11. {
  12. pattern_ = "/tmp/smartdns-test-tmp.XXXXXX";
  13. }
  14. TempFile::TempFile(const std::string &line)
  15. {
  16. pattern_ = "/tmp/smartdns-test-tmp.XXXXXX";
  17. }
  18. TempFile::~TempFile()
  19. {
  20. if (ofs_.is_open()) {
  21. ofs_.close();
  22. ofs_.clear();
  23. }
  24. if (path_.length() > 0) {
  25. unlink(path_.c_str());
  26. }
  27. }
  28. void TempFile::SetPattern(const std::string &pattern)
  29. {
  30. pattern_ = pattern;
  31. }
  32. bool TempFile::Write(const std::string &line)
  33. {
  34. if (ofs_.is_open() == false) {
  35. if (NewTempFile() == false) {
  36. return false;
  37. }
  38. }
  39. ofs_.write(line.data(), line.size());
  40. if (ofs_.fail()) {
  41. return false;
  42. }
  43. ofs_.flush();
  44. return true;
  45. }
  46. bool TempFile::NewTempFile()
  47. {
  48. char filename[128];
  49. strncpy(filename, "/tmp/smartdns-test-tmp.XXXXXX", sizeof(filename));
  50. int fd = mkstemp(filename);
  51. if (fd < 0) {
  52. return false;
  53. }
  54. Defer
  55. {
  56. close(fd);
  57. };
  58. std::ofstream ofs(filename);
  59. if (ofs.is_open() == false) {
  60. return false;
  61. }
  62. ofs_ = std::move(ofs);
  63. path_ = filename;
  64. return true;
  65. }
  66. std::string TempFile::GetPath()
  67. {
  68. if (ofs_.is_open() == false) {
  69. if (NewTempFile() == false) {
  70. return "";
  71. }
  72. }
  73. return path_;
  74. }
  75. Commander::Commander() {}
  76. Commander::~Commander()
  77. {
  78. Kill();
  79. }
  80. bool Commander::Run(const std::string &cmd)
  81. {
  82. std::vector<std::string> args;
  83. if (ParserArg(cmd, args) != 0) {
  84. return false;
  85. }
  86. return Run(args);
  87. }
  88. bool Commander::Run(const std::vector<std::string> &cmds)
  89. {
  90. pid_t pid;
  91. if (pid_ > 0) {
  92. return false;
  93. }
  94. pid = fork();
  95. if (pid < 0) {
  96. return false;
  97. }
  98. if (pid == 0) {
  99. char *argv[cmds.size() + 1];
  100. for (int i = 0; i < cmds.size(); i++) {
  101. argv[i] = (char *)cmds[i].c_str();
  102. }
  103. argv[cmds.size()] = nullptr;
  104. execvp(argv[0], argv);
  105. _exit(1);
  106. }
  107. pid_ = pid;
  108. return true;
  109. }
  110. void Commander::Kill()
  111. {
  112. if (pid_ <= 0) {
  113. return;
  114. }
  115. kill(pid_, SIGKILL);
  116. }
  117. void Commander::Terminate()
  118. {
  119. if (pid_ <= 0) {
  120. return;
  121. }
  122. kill(pid_, SIGTERM);
  123. }
  124. int Commander::ExitCode()
  125. {
  126. int wstatus = 0;
  127. if (exit_code_ >= 0) {
  128. return exit_code_;
  129. }
  130. if (pid_ <= 0) {
  131. return -1;
  132. }
  133. if (waitpid(pid_, &wstatus, 0) == -1) {
  134. return -1;
  135. }
  136. exit_code_ = WEXITSTATUS(wstatus);
  137. return exit_code_;
  138. }
  139. int Commander::GetPid()
  140. {
  141. return pid_;
  142. }
  143. bool IsCommandExists(const std::string &cmd)
  144. {
  145. char *copy_path = nullptr;
  146. char cmd_path[4096];
  147. const char *env_path = getenv("PATH");
  148. char *save_ptr = nullptr;
  149. if (env_path == nullptr) {
  150. env_path = "/bin:/usr/bin:/usr/local/bin";
  151. }
  152. copy_path = strdup(env_path);
  153. if (copy_path == nullptr) {
  154. return false;
  155. }
  156. Defer
  157. {
  158. free(copy_path);
  159. };
  160. for (char *tok = strtok_r(copy_path, ":", &save_ptr); tok; tok = strtok_r(nullptr, ":", &save_ptr)) {
  161. snprintf(cmd_path, sizeof(cmd_path), "%s/%s", tok, cmd.c_str());
  162. if (access(cmd_path, X_OK) != 0) {
  163. continue;
  164. }
  165. return true;
  166. }
  167. return false;
  168. }
  169. std::string GenerateRandomString(int len)
  170. {
  171. std::string result;
  172. static const char alphanum[] = "0123456789"
  173. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  174. "abcdefghijklmnopqrstuvwxyz";
  175. result.resize(len);
  176. for (int i = 0; i < len; ++i) {
  177. result[i] = alphanum[rand() % (sizeof(alphanum) - 1)];
  178. }
  179. return result;
  180. }
  181. int ParserArg(const std::string &cmd, std::vector<std::string> &args)
  182. {
  183. std::string arg;
  184. char quoteChar = 0;
  185. for (char ch : cmd) {
  186. if (quoteChar == '\\') {
  187. arg.push_back(ch);
  188. quoteChar = 0;
  189. continue;
  190. }
  191. if (quoteChar && ch != quoteChar) {
  192. arg.push_back(ch);
  193. continue;
  194. }
  195. switch (ch) {
  196. case '\'':
  197. case '\"':
  198. case '\\':
  199. quoteChar = quoteChar ? 0 : ch;
  200. break;
  201. case ' ':
  202. case '\t':
  203. case '\n':
  204. if (!arg.empty()) {
  205. args.push_back(arg);
  206. arg.clear();
  207. }
  208. break;
  209. default:
  210. arg.push_back(ch);
  211. break;
  212. }
  213. }
  214. if (!arg.empty()) {
  215. args.push_back(arg);
  216. }
  217. return 0;
  218. }
  219. } // namespace smartdns