You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

175 lines
5.0KB

  1. /*
  2. * Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
  3. *
  4. * This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
  5. *
  6. * Use of this source code is governed by MIT license that can be found in the
  7. * LICENSE file in the root of the source tree. All contributing project authors
  8. * may be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #if !defined(_WIN32)
  11. #include <limits.h>
  12. #include <sys/resource.h>
  13. #include <sys/wait.h>
  14. #if !defined(ANDROID)
  15. #include <execinfo.h>
  16. #endif//!defined(ANDROID)
  17. #endif//!defined(_WIN32)
  18. #include <cstdlib>
  19. #include <csignal>
  20. #include <map>
  21. #include <iostream>
  22. #include "Util/logger.h"
  23. #include "Util/uv_errno.h"
  24. #include "System.h"
  25. #include "Common/macros.h"
  26. using namespace std;
  27. using namespace toolkit;
  28. using namespace mediakit;
  29. #ifdef _WIN32
  30. #define popen _popen
  31. #define pclose _pclose
  32. #endif
  33. string System::execute(const string &cmd) {
  34. FILE *fPipe = NULL;
  35. fPipe = popen(cmd.data(), "r");
  36. if(!fPipe){
  37. return "";
  38. }
  39. string ret;
  40. char buff[1024] = {0};
  41. while(fgets(buff, sizeof(buff) - 1, fPipe)){
  42. ret.append(buff);
  43. }
  44. pclose(fPipe);
  45. return ret;
  46. }
  47. #if !defined(ANDROID) && !defined(_WIN32)
  48. static constexpr int MAX_STACK_FRAMES = 128;
  49. static void sig_crash(int sig) {
  50. signal(sig, SIG_DFL);
  51. void *array[MAX_STACK_FRAMES];
  52. int size = backtrace(array, MAX_STACK_FRAMES);
  53. char ** strings = backtrace_symbols(array, size);
  54. vector<vector<string> > stack(size);
  55. for (int i = 0; i < size; ++i) {
  56. auto &ref = stack[i];
  57. std::string symbol(strings[i]);
  58. ref.emplace_back(symbol);
  59. #if defined(__linux) || defined(__linux__)
  60. static auto addr2line = [](const string &address) {
  61. string cmd = StrPrinter << "addr2line -C -f -e " << exePath() << " " << address;
  62. return System::execute(cmd);
  63. };
  64. size_t pos1 = symbol.find_first_of("[");
  65. size_t pos2 = symbol.find_last_of("]");
  66. std::string address = symbol.substr(pos1 + 1, pos2 - pos1 - 1);
  67. ref.emplace_back(addr2line(address));
  68. #endif//__linux
  69. }
  70. free(strings);
  71. stringstream ss;
  72. ss << "## crash date:" << getTimeStr("%Y-%m-%d %H:%M:%S") << endl;
  73. ss << "## exe: " << exeName() << endl;
  74. ss << "## signal: " << sig << endl;
  75. ss << "## version: " << kServerName << endl;
  76. ss << "## stack: " << endl;
  77. for (size_t i = 0; i < stack.size(); ++i) {
  78. ss << "[" << i << "]: ";
  79. for (auto &str : stack[i]){
  80. ss << str << endl;
  81. }
  82. }
  83. string stack_info = ss.str();
  84. ofstream out(StrPrinter << exeDir() << "/crash." << getpid(), ios::out | ios::binary | ios::trunc);
  85. out << stack_info;
  86. out.flush();
  87. cerr << stack_info << endl;
  88. }
  89. #endif // !defined(ANDROID) && !defined(_WIN32)
  90. void System::startDaemon(bool &kill_parent_if_failed) {
  91. kill_parent_if_failed = true;
  92. #ifndef _WIN32
  93. static pid_t pid;
  94. do {
  95. pid = fork();
  96. if (pid == -1) {
  97. WarnL << "fork失败:" << get_uv_errmsg();
  98. //休眠1秒再试
  99. sleep(1);
  100. continue;
  101. }
  102. if (pid == 0) {
  103. //子进程
  104. return;
  105. }
  106. //父进程,监视子进程是否退出
  107. DebugL << "启动子进程:" << pid;
  108. signal(SIGINT, [](int) {
  109. WarnL << "收到主动退出信号,关闭父进程与子进程";
  110. kill(pid, SIGINT);
  111. exit(0);
  112. });
  113. do {
  114. int status = 0;
  115. if (waitpid(pid, &status, 0) >= 0) {
  116. WarnL << "子进程退出";
  117. //休眠3秒再启动子进程
  118. sleep(3);
  119. //重启子进程,如果子进程重启失败,那么不应该杀掉守护进程,这样守护进程可以一直尝试重启子进程
  120. kill_parent_if_failed = false;
  121. break;
  122. }
  123. DebugL << "waitpid被中断:" << get_uv_errmsg();
  124. } while (true);
  125. } while (true);
  126. #endif // _WIN32
  127. }
  128. void System::systemSetup(){
  129. #if !defined(_WIN32)
  130. struct rlimit rlim,rlim_new;
  131. if (getrlimit(RLIMIT_CORE, &rlim)==0) {
  132. rlim_new.rlim_cur = rlim_new.rlim_max = RLIM_INFINITY;
  133. if (setrlimit(RLIMIT_CORE, &rlim_new)!=0) {
  134. rlim_new.rlim_cur = rlim_new.rlim_max = rlim.rlim_max;
  135. setrlimit(RLIMIT_CORE, &rlim_new);
  136. }
  137. InfoL << "core文件大小设置为:" << rlim_new.rlim_cur;
  138. }
  139. if (getrlimit(RLIMIT_NOFILE, &rlim)==0) {
  140. rlim_new.rlim_cur = rlim_new.rlim_max = RLIM_INFINITY;
  141. if (setrlimit(RLIMIT_NOFILE, &rlim_new)!=0) {
  142. rlim_new.rlim_cur = rlim_new.rlim_max = rlim.rlim_max;
  143. setrlimit(RLIMIT_NOFILE, &rlim_new);
  144. }
  145. InfoL << "文件最大描述符个数设置为:" << rlim_new.rlim_cur;
  146. }
  147. #ifndef ANDROID
  148. signal(SIGSEGV, sig_crash);
  149. signal(SIGABRT, sig_crash);
  150. //忽略挂起信号
  151. signal(SIGHUP, SIG_IGN);
  152. #endif// ANDROID
  153. #endif//!defined(_WIN32)
  154. }