tc_port.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. /**
  2. * Tencent is pleased to support the open source community by making Tars available.
  3. *
  4. * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved.
  5. *
  6. * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
  7. * in compliance with the License. You may obtain a copy of the License at
  8. *
  9. * https://opensource.org/licenses/BSD-3-Clause
  10. *
  11. * Unless required by applicable law or agreed to in writing, software distributed
  12. * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  13. * CONDITIONS OF ANY KIND, either express or implied. See the License for the
  14. * specific language governing permissions and limitations under the License.
  15. */
  16. #include "util/tc_port.h"
  17. #include "util/tc_common.h"
  18. #include "util/tc_logger.h"
  19. #include <thread>
  20. #include <string.h>
  21. #if TARGET_PLATFORM_LINUX || TARGET_PLATFORM_IOS
  22. #include <signal.h>
  23. #include <limits.h>
  24. #include <sys/time.h>
  25. #else
  26. #pragma comment(lib, "ws2_32.lib")
  27. // #include <winsock.h>
  28. #include <time.h>
  29. #include <sys/timeb.h>
  30. #include "util/tc_strptime.h"
  31. #endif
  32. namespace tars
  33. {
  34. int TC_Port::strcmp(const char *s1, const char *s2)
  35. {
  36. #if TARGET_PLATFORM_WINDOWS
  37. return ::strcmp(s1, s2);
  38. #else
  39. return ::strcmp(s1, s2);
  40. #endif
  41. }
  42. int TC_Port::strncmp(const char *s1, const char *s2, size_t n)
  43. {
  44. #if TARGET_PLATFORM_WINDOWS
  45. return ::strncmp(s1, s2, n);
  46. #else
  47. return ::strncmp(s1, s2, n);
  48. #endif
  49. }
  50. const char* TC_Port::strnstr(const char* s1, const char* s2, int pos1)
  51. {
  52. int l1 = 0;
  53. int l2;
  54. l2 = strlen(s2);
  55. if (!l2)
  56. return (char *)s1;
  57. const char *p = s1;
  58. while(l1 < pos1 && *p++ != '\0')
  59. {
  60. ++l1;
  61. }
  62. // l1 = strlen(s1);
  63. // pos1 = (pos1 > l1)?l1:pos1;
  64. while (pos1 >= l2) {
  65. pos1--;
  66. if (!memcmp(s1, s2, l2))
  67. return s1;
  68. s1++;
  69. }
  70. return NULL;
  71. }
  72. int TC_Port::strcasecmp(const char *s1, const char *s2)
  73. {
  74. #if TARGET_PLATFORM_WINDOWS
  75. return ::_stricmp(s1, s2);
  76. #else
  77. return ::strcasecmp(s1, s2);
  78. #endif
  79. }
  80. int TC_Port::strncasecmp(const char *s1, const char *s2, size_t n)
  81. {
  82. #if TARGET_PLATFORM_WINDOWS
  83. return ::_strnicmp(s1, s2, n);
  84. #else
  85. return ::strncasecmp(s1, s2, n);
  86. #endif
  87. }
  88. void TC_Port::localtime_r(const time_t *clock, struct tm *result)
  89. {
  90. //带时区时间
  91. #if TARGET_PLATFORM_WINDOWS
  92. ::localtime_s(result, clock);
  93. #else
  94. ::localtime_r(clock, result);
  95. #endif
  96. }
  97. void TC_Port::gmtime_r(const time_t *clock, struct tm *result)
  98. {
  99. #if TARGET_PLATFORM_WINDOWS
  100. ::gmtime_s(result, clock);
  101. #else
  102. ::gmtime_r(clock, result);
  103. #endif
  104. }
  105. time_t TC_Port::timegm(struct tm *timeptr)
  106. {
  107. #if TARGET_PLATFORM_WINDOWS
  108. return ::_mkgmtime(timeptr);
  109. #else
  110. return ::timegm(timeptr);
  111. #endif
  112. }
  113. int TC_Port::gettimeofday(struct timeval &tv)
  114. {
  115. #if TARGET_PLATFORM_WINDOWS
  116. static const DWORDLONG FILETIME_to_timeval_skew = 116444736000000000;
  117. FILETIME tfile;
  118. ::GetSystemTimeAsFileTime(&tfile);
  119. ULARGE_INTEGER tmp;
  120. tmp.LowPart = tfile.dwLowDateTime;
  121. tmp.HighPart = tfile.dwHighDateTime;
  122. tmp.QuadPart -= FILETIME_to_timeval_skew;
  123. ULARGE_INTEGER largeInt;
  124. largeInt.QuadPart = tmp.QuadPart / (10000 * 1000);
  125. tv.tv_sec = (long)(tmp.QuadPart / (10000 * 1000));
  126. tv.tv_usec = (long)((tmp.QuadPart % (10000 * 1000)) / 10);
  127. return 0;
  128. #else
  129. return ::gettimeofday(&tv, 0);
  130. #endif
  131. }
  132. int TC_Port::chmod(const char *path, mode_t mode)
  133. {
  134. //带时区时间
  135. #if TARGET_PLATFORM_WINDOWS
  136. return ::_chmod(path, mode);
  137. #else
  138. return ::chmod(path, mode);
  139. #endif
  140. }
  141. FILE * TC_Port::fopen(const char * path, const char * mode)
  142. {
  143. #if TARGET_PLATFORM_WINDOWS
  144. FILE *fp;
  145. if (fopen_s(&fp, path, mode) != 0)
  146. {
  147. return NULL;
  148. }
  149. return fp;
  150. #else
  151. return ::fopen(path, mode);
  152. #endif
  153. }
  154. int TC_Port::lstat(const char * path, TC_Port::stat_t * buf)
  155. {
  156. #if TARGET_PLATFORM_WINDOWS
  157. return ::_stat(path, buf);
  158. #else
  159. return ::lstat(path, buf);
  160. #endif
  161. }
  162. int TC_Port::mkdir(const char *path)
  163. {
  164. #if TARGET_PLATFORM_WINDOWS
  165. int iRetCode = ::_mkdir(path);
  166. #else
  167. int iRetCode = ::mkdir(path, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
  168. #endif
  169. return iRetCode;
  170. }
  171. int TC_Port::rmdir(const char *path)
  172. {
  173. #if TARGET_PLATFORM_WINDOWS
  174. return ::_rmdir(path);
  175. #else
  176. return ::rmdir(path);
  177. #endif
  178. }
  179. int TC_Port::closeSocket(int fd)
  180. {
  181. #if TARGET_PLATFORM_WINDOWS
  182. return ::closesocket(fd);
  183. #else
  184. return ::close(fd);
  185. #endif
  186. }
  187. int64_t TC_Port::getpid()
  188. {
  189. #if TARGET_PLATFORM_WINDOWS
  190. int64_t pid = ::GetCurrentProcessId();
  191. #else
  192. int64_t pid = ::getpid();
  193. #endif
  194. return pid;
  195. }
  196. string TC_Port::getEnv(const string &name)
  197. {
  198. char* p = getenv(name.c_str());
  199. string str = p ? string(p) : "";
  200. return str;
  201. }
  202. void TC_Port::setEnv(const string &name, const string &value)
  203. {
  204. #if TARGET_PLATFORM_WINDOWS
  205. SetEnvironmentVariable(name.c_str(), value.c_str());
  206. #else
  207. setenv(name.c_str(), value.c_str(), true);
  208. #endif
  209. }
  210. string TC_Port::exec(const char *cmd)
  211. {
  212. string err;
  213. return exec(cmd, err);
  214. }
  215. std::string TC_Port::exec(const char* cmd, std::string &err)
  216. {
  217. string fileData;
  218. #if TARGET_PLATFORM_WINDOWS
  219. FILE* fp = _popen(cmd, "r");
  220. #else
  221. FILE* fp = popen(cmd, "r");
  222. #endif
  223. if(fp == NULL) {
  224. err = "open '" + string(cmd) + "' error";
  225. return "";
  226. }
  227. static size_t buf_len = 2 * 1024 * 1024;
  228. char *buf = new char[buf_len];
  229. memset(buf, 0, buf_len);
  230. fread(buf, sizeof(char), buf_len - 1, fp);
  231. #if TARGET_PLATFORM_WINDOWS
  232. _pclose(fp);
  233. #else
  234. pclose(fp);
  235. #endif
  236. fileData = string(buf);
  237. delete []buf;
  238. return fileData;
  239. }
  240. shared_ptr<TC_Port::SigInfo> TC_Port::_sigInfo = std::make_shared<TC_Port::SigInfo>();
  241. size_t TC_Port::registerSig(int sig, std::function<void()> callback)
  242. {
  243. std::lock_guard<std::mutex> lock(_sigInfo->_mutex);
  244. auto it = _sigInfo->_callbacks.find(sig);
  245. if(it == _sigInfo->_callbacks.end())
  246. {
  247. //没有注册过, 才注册
  248. registerSig(sig);
  249. }
  250. size_t id = ++_sigInfo->_callbackId;
  251. _sigInfo->_callbacks[sig][id] = callback;
  252. return id;
  253. }
  254. void TC_Port::unregisterSig(int sig, size_t id)
  255. {
  256. //注意_sigInfo是全局静态的, 有可能已经析构了, 需要特殊判断一下!
  257. if(_sigInfo && _sigInfo.use_count() > 0)
  258. {
  259. std::lock_guard<std::mutex> lock(_sigInfo->_mutex);
  260. auto it = _sigInfo->_callbacks.find(sig);
  261. if(it != _sigInfo->_callbacks.end())
  262. {
  263. it->second.erase(id);
  264. }
  265. }
  266. }
  267. size_t TC_Port::registerCtrlC(std::function<void()> callback)
  268. {
  269. #if TARGET_PLATFORM_LINUX || TARGET_PLATFORM_IOS
  270. return registerSig(SIGINT, callback);
  271. #else
  272. return registerSig(CTRL_C_EVENT, callback);
  273. #endif
  274. }
  275. void TC_Port::unregisterCtrlC(size_t id)
  276. {
  277. #if TARGET_PLATFORM_LINUX || TARGET_PLATFORM_IOS
  278. unregisterSig(SIGINT, id);
  279. #else
  280. unregisterSig(CTRL_C_EVENT, id);
  281. #endif
  282. }
  283. size_t TC_Port::registerTerm(std::function<void()> callback)
  284. {
  285. #if TARGET_PLATFORM_LINUX || TARGET_PLATFORM_IOS
  286. return registerSig(SIGTERM, callback);
  287. #else
  288. return registerSig(CTRL_SHUTDOWN_EVENT, callback);
  289. #endif
  290. }
  291. void TC_Port::unregisterTerm(size_t id)
  292. {
  293. #if TARGET_PLATFORM_LINUX || TARGET_PLATFORM_IOS
  294. unregisterSig(SIGTERM, id);
  295. #else
  296. unregisterSig(CTRL_SHUTDOWN_EVENT, id);
  297. #endif
  298. }
  299. void TC_Port::registerSig(int sig)
  300. {
  301. #if TARGET_PLATFORM_LINUX || TARGET_PLATFORM_IOS
  302. signal(sig, TC_Port::sighandler);
  303. // std::thread th(signal, sig, TC_Port::sighandler);
  304. // th.detach();
  305. #else
  306. SetConsoleCtrlHandler(TC_Port::HandlerRoutine, TRUE);
  307. // std::thread th([] {SetConsoleCtrlHandler(TC_Port::HandlerRoutine, TRUE); });
  308. // th.detach();
  309. #endif
  310. }
  311. #if TARGET_PLATFORM_LINUX || TARGET_PLATFORM_IOS
  312. void TC_Port::sighandler( int sig_no )
  313. {
  314. std::thread th([&]()
  315. {
  316. unordered_map<size_t, std::function<void()>> data;
  317. {
  318. std::lock_guard<std::mutex> lock(_sigInfo->_mutex);
  319. auto it = TC_Port::_sigInfo->_callbacks.find(sig_no);
  320. if (it != TC_Port::_sigInfo->_callbacks.end())
  321. {
  322. data = it->second;
  323. }
  324. }
  325. for (auto f : data)
  326. {
  327. try
  328. {
  329. f.second();
  330. }
  331. catch (...)
  332. {
  333. }
  334. }
  335. });
  336. th.detach();
  337. }
  338. #else
  339. BOOL WINAPI TC_Port::HandlerRoutine(DWORD dwCtrlType)
  340. {
  341. std::thread th([&]()
  342. {
  343. unordered_map<size_t, std::function<void()>> data;
  344. {
  345. std::lock_guard<std::mutex> lock(_sigInfo->_mutex);
  346. auto it = _sigInfo->_callbacks.find(dwCtrlType);
  347. if (it != _sigInfo->_callbacks.end())
  348. {
  349. data = it->second;
  350. }
  351. }
  352. for (auto f : data)
  353. {
  354. try
  355. {
  356. f.second();
  357. }
  358. catch (...)
  359. {
  360. }
  361. }
  362. });
  363. th.detach();
  364. return TRUE;
  365. }
  366. #endif
  367. }