utils.cc 4.8 KB

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