vendor_base.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. #include "vendor_base.h"
  2. #include "vendor_global.h"
  3. #include <errno.h>
  4. #include <pthread.h>
  5. #include <signal.h>
  6. #include <string.h>
  7. #include <utils/logger.h>
  8. namespace vendor {
  9. LongOpts::LongOpts(const MyOpt opts[], const int size)
  10. {
  11. myOpt = opts;
  12. sizeOpt = size;
  13. }
  14. void LongOpts::GetOptions(option *opts, std::string &shorts)
  15. {
  16. for (int i = 0; i < sizeOpt; i++) {
  17. // printf("val: %d(%c) ", myOpt[i].val, myOpt[i].val);
  18. opts[i].name = strdup(myOpt[i].name);
  19. opts[i].has_arg = myOpt[i].has_arg;
  20. opts[i].flag = NULL;
  21. opts[i].val = myOpt[i].val;
  22. if (myOpt[i].val == 0) {
  23. continue;
  24. }
  25. if (myOpt[i].has_arg == no_argument) {
  26. shorts.push_back(static_cast<char>(myOpt[i].val));
  27. } else if (myOpt[i].has_arg == required_argument) {
  28. shorts.push_back(static_cast<char>(myOpt[i].val));
  29. shorts.push_back(':');
  30. } else {
  31. shorts.push_back(static_cast<char>(myOpt[i].val));
  32. }
  33. }
  34. }
  35. MyOpt LongOpts::GetOption(char ch, int index)
  36. {
  37. if (ch == 0) {
  38. return myOpt[index];
  39. }
  40. for (int i = 0; i < sizeOpt; i++) {
  41. if (myOpt[i].val == ch) {
  42. return myOpt[i];
  43. }
  44. }
  45. return myOpt[0];
  46. }
  47. VendorBase::VendorBase()
  48. {
  49. HTELINK_LOG_ENABLE(true);
  50. uvLoop_ = uv_default_loop();
  51. if (uvLoop_ == nullptr) {
  52. HTELINK_LOG_ERR("event new failed, %s", strerror(errno));
  53. }
  54. }
  55. VendorBase::~VendorBase()
  56. {
  57. if (uvLoop_ == nullptr) {
  58. return;
  59. }
  60. for (auto sig : uvSignals_) {
  61. uv_close(reinterpret_cast<uv_handle_t *>(sig), nullptr);
  62. delete sig;
  63. }
  64. uvSignals_.clear();
  65. uv_loop_close(uvLoop_);
  66. Exit(SIGINT);
  67. }
  68. void VendorBase::ListenSignal(int signal)
  69. {
  70. uv_signal_t *uvSigal = new uv_signal_t;
  71. uv_signal_init(uvLoop_, uvSigal);
  72. uvSigal->data = this;
  73. uv_signal_start(uvSigal, VendorBase::SignalHandler, signal);
  74. uvSignals_.push_back(reinterpret_cast<uv_handle_t *>(uvSigal));
  75. }
  76. int VendorBase::Exec()
  77. {
  78. HTELINK_LOG_INFO("register event...");
  79. // 注册信号事件
  80. ListenSignal(SIGSEGV);
  81. ListenSignal(SIGFPE);
  82. ListenSignal(SIGINT);
  83. ListenSignal(SIGTERM);
  84. #ifndef WIN32
  85. ListenSignal(SIGTSTP);
  86. signal(SIGPIPE, SIG_IGN);
  87. #endif
  88. AppendTimerEvent(10 * 60 * 1000, [] {
  89. /* move files if disk full */
  90. utils::check_disk_log(VENDOR_LOG_PATH);
  91. });
  92. uv_work_t workReq;
  93. workReq.data = this;
  94. uv_queue_work(
  95. uvLoop_, &workReq,
  96. [](uv_work_t *req) {
  97. VendorBase *vb = reinterpret_cast<VendorBase *>(req->data);
  98. HTELINK_LOG_INFO("start to Run");
  99. vb->Run();
  100. },
  101. [](uv_work_t *req, int status) {});
  102. HTELINK_LOG_INFO("start to deamon...");
  103. return uv_run(uvLoop_, UV_RUN_DEFAULT);
  104. }
  105. void VendorBase::Exit(int signal)
  106. {
  107. Stop(signal);
  108. if (uvLoop_ == nullptr) {
  109. return;
  110. }
  111. uv_stop(uvLoop_);
  112. }
  113. uv_handle_t *VendorBase::AppendEvent(int fd, int ev, VendorIf *vif)
  114. {
  115. uv_async_t *async = new uv_async_t;
  116. uv_async_cb asyncCb = [](uv_async_t *handle) {
  117. VendorIf *vif = static_cast<VendorIf *>(handle->data);
  118. if (vif) {
  119. vif->OnEvent(nullptr);
  120. }
  121. };
  122. uv_async_init(uvLoop_, async, asyncCb);
  123. async->data = vif;
  124. uv_async_send(async);
  125. uvSignals_.push_back(reinterpret_cast<uv_handle_t *>(async));
  126. return (uv_handle_t *)async;
  127. }
  128. uv_handle_t *VendorBase::AppendTimerEvent(int millionsecs, VendorIf *vif)
  129. {
  130. uv_timer_t *timerReq = new uv_timer_t;
  131. uv_timer_init(uvLoop_, timerReq);
  132. timerReq->data = vif;
  133. uv_timer_cb timerCb = [](uv_timer_t *handle) {
  134. VendorIf *vif = static_cast<VendorIf *>(handle->data);
  135. if (vif) {
  136. vif->OnTimer(nullptr);
  137. }
  138. };
  139. uv_timer_start(timerReq, timerCb, millionsecs, 1);
  140. uvSignals_.push_back(reinterpret_cast<uv_handle_t *>(timerReq));
  141. return (uv_handle_t *)(timerReq);
  142. }
  143. uv_handle_t *VendorBase::AppendTimerEvent(
  144. int millionsecs, std::function<void()> timerFunc)
  145. {
  146. class VendorTimerIf : public VendorIf {
  147. public:
  148. VendorTimerIf(std::function<void()> timerFunc)
  149. {
  150. timerFunc_ = timerFunc;
  151. }
  152. virtual void OnEvent(void *args) {}
  153. virtual void OnTimer(void *args) { timerFunc_(); }
  154. virtual void OnShot(void *args) {}
  155. private:
  156. std::function<void()> timerFunc_;
  157. };
  158. // 存在内存泄露问题,待更好的方案
  159. return AppendTimerEvent(millionsecs, new VendorTimerIf(timerFunc));
  160. }
  161. void VendorBase::StopEvent(uv_handle_t *handle)
  162. {
  163. if (handle == nullptr) {
  164. return;
  165. }
  166. if (handle->type == UV_TIMER) {
  167. uv_timer_stop((uv_timer_t *)handle);
  168. } else {
  169. uv_close(handle, nullptr);
  170. }
  171. delete handle;
  172. }
  173. void VendorBase::SignalHandler(uv_signal_t *handle, int signum)
  174. {
  175. void *l_buffer[1024];
  176. char **l_ptrace;
  177. int signal = signum;
  178. VendorBase *vendorBase = static_cast<VendorBase *>(handle->data);
  179. if (vendorBase) {
  180. vendorBase->Exit(signal);
  181. }
  182. HTELINK_LOG_ERR("\r\n=========>>>catch signal %d, pid: %ld <<<=========", signal, getpid());
  183. HTELINK_LOG_ERR("Dump stack start...");
  184. #ifndef WIN32
  185. int size = backtrace(l_buffer, 1024);
  186. l_ptrace = backtrace_symbols(l_buffer, size);
  187. if (NULL == l_ptrace) {
  188. HTELINK_LOG_ERR("backtrace_symbols");
  189. exit(signal);
  190. }
  191. for (int i = 0; i < size; i++) {
  192. HTELINK_LOG_ERR(" [%02d] %s", i, l_ptrace[i]);
  193. }
  194. HTELINK_LOG_ERR("Dump stack end...");
  195. free(l_ptrace);
  196. #endif
  197. abort();
  198. }
  199. int32_t VendorBase::ParseOptionArgs(VendorBase *vendor, int argc, char *argv[])
  200. {
  201. int longindex = 0;
  202. std::string shorts;
  203. struct option longopts[vendor->GetOptSize()];
  204. LongOpts longOpts(vendor->GetOpts(), vendor->GetOptSize());
  205. longOpts.GetOptions(longopts, shorts);
  206. int ch;
  207. while ((ch = getopt_long(argc, argv, shorts.c_str(), longopts, &longindex)) != -1) {
  208. MyOpt myOpt = longOpts.GetOption(ch, longindex);
  209. printf("longindex :%d, opt %d(%c), name:%s , optarg :%s\n", longindex, myOpt.val, myOpt.val, myOpt.name,
  210. optarg == nullptr ? "nullptr" : optarg);
  211. if (strcmp(myOpt.name, "help") == 0) {
  212. MyOpt *myOpts = vendor->GetOpts();
  213. printf("Usage: %s [OPTION] -l URL\n\nOPTIONS:\n", vendor->Name().c_str());
  214. for (int i = 1; i < vendor->GetOptSize(); i++) {
  215. if (myOpts[i].val == 0) {
  216. printf("--%s: %s\n", myOpts[i].name, myOpts[i].desc);
  217. } else {
  218. printf("-%c, --%s: %s\n", myOpts[i].val, myOpts[i].name, myOpts[i].desc);
  219. }
  220. }
  221. return -1;
  222. }
  223. if (vendor->ParseCmdline(myOpt.name, optarg == nullptr ? "" : optarg) != 0) {
  224. printf("parse failed!!!\n");
  225. return -1;
  226. }
  227. }
  228. return 0;
  229. }
  230. }; // namespace vendor