ZLMediaKit/server/System.cpp

175 lines
5.0 KiB
C++
Raw Permalink Normal View History

2020-01-07 15:10:59 +08:00
/*
2020-04-04 20:30:09 +08:00
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
2019-06-11 09:25:54 +08:00
*
* This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
2019-06-11 09:25:54 +08:00
*
2020-04-04 20:30:09 +08:00
* Use of this source code is governed by MIT license that can be found in the
* LICENSE file in the root of the source tree. All contributing project authors
* may be found in the AUTHORS file in the root of the source tree.
2019-06-11 09:25:54 +08:00
*/
2019-05-20 11:22:59 +08:00
2020-01-07 14:37:18 +08:00
#if !defined(_WIN32)
2019-05-20 11:22:59 +08:00
#include <limits.h>
#include <sys/resource.h>
#include <sys/wait.h>
2020-01-07 14:37:18 +08:00
#if !defined(ANDROID)
2019-05-20 11:22:59 +08:00
#include <execinfo.h>
2020-01-07 14:37:18 +08:00
#endif//!defined(ANDROID)
#endif//!defined(_WIN32)
2022-02-16 10:30:58 +08:00
#include <cstdlib>
#include <csignal>
2019-05-20 11:22:59 +08:00
#include <map>
#include <iostream>
2019-05-20 11:22:59 +08:00
#include "Util/logger.h"
#include "Util/uv_errno.h"
#include "System.h"
#include "Common/macros.h"
2019-05-20 11:22:59 +08:00
using namespace std;
using namespace toolkit;
using namespace mediakit;
2019-05-20 11:22:59 +08:00
2020-01-07 14:37:18 +08:00
#ifdef _WIN32
#define popen _popen
#define pclose _pclose
2019-05-20 11:22:59 +08:00
#endif
string System::execute(const string &cmd) {
2020-01-07 14:37:18 +08:00
FILE *fPipe = NULL;
fPipe = popen(cmd.data(), "r");
2019-05-20 11:22:59 +08:00
if(!fPipe){
return "";
}
string ret;
char buff[1024] = {0};
2019-06-20 18:36:55 +08:00
while(fgets(buff, sizeof(buff) - 1, fPipe)){
2019-05-20 11:22:59 +08:00
ret.append(buff);
}
pclose(fPipe);
return ret;
}
2020-01-07 14:37:18 +08:00
#if !defined(ANDROID) && !defined(_WIN32)
static constexpr int MAX_STACK_FRAMES = 128;
2019-05-20 11:22:59 +08:00
static void sig_crash(int sig) {
signal(sig, SIG_DFL);
void *array[MAX_STACK_FRAMES];
int size = backtrace(array, MAX_STACK_FRAMES);
char ** strings = backtrace_symbols(array, size);
vector<vector<string> > stack(size);
for (int i = 0; i < size; ++i) {
auto &ref = stack[i];
std::string symbol(strings[i]);
ref.emplace_back(symbol);
#if defined(__linux) || defined(__linux__)
2021-01-17 20:15:08 +08:00
static auto addr2line = [](const string &address) {
string cmd = StrPrinter << "addr2line -C -f -e " << exePath() << " " << address;
return System::execute(cmd);
};
2019-05-20 11:22:59 +08:00
size_t pos1 = symbol.find_first_of("[");
size_t pos2 = symbol.find_last_of("]");
std::string address = symbol.substr(pos1 + 1, pos2 - pos1 - 1);
ref.emplace_back(addr2line(address));
#endif//__linux
}
free(strings);
stringstream ss;
ss << "## crash date:" << getTimeStr("%Y-%m-%d %H:%M:%S") << endl;
ss << "## exe: " << exeName() << endl;
ss << "## signal: " << sig << endl;
ss << "## version: " << kServerName << endl;
ss << "## stack: " << endl;
for (size_t i = 0; i < stack.size(); ++i) {
ss << "[" << i << "]: ";
for (auto &str : stack[i]){
ss << str << endl;
}
}
string stack_info = ss.str();
ofstream out(StrPrinter << exeDir() << "/crash." << getpid(), ios::out | ios::binary | ios::trunc);
out << stack_info;
out.flush();
cerr << stack_info << endl;
2019-05-20 11:22:59 +08:00
}
2020-01-07 14:37:18 +08:00
#endif // !defined(ANDROID) && !defined(_WIN32)
2019-09-24 15:21:20 +08:00
2019-05-20 11:22:59 +08:00
void System::startDaemon(bool &kill_parent_if_failed) {
kill_parent_if_failed = true;
2020-01-07 14:37:18 +08:00
#ifndef _WIN32
2019-05-20 22:22:05 +08:00
static pid_t pid;
do {
2019-05-20 22:22:05 +08:00
pid = fork();
if (pid == -1) {
2019-05-20 11:22:59 +08:00
WarnL << "fork失败:" << get_uv_errmsg();
//休眠1秒再试
sleep(1);
continue;
}
2019-05-20 22:22:05 +08:00
if (pid == 0) {
2019-05-20 11:22:59 +08:00
//子进程
return;
}
//父进程,监视子进程是否退出
DebugL << "启动子进程:" << pid;
2019-05-20 22:22:05 +08:00
signal(SIGINT, [](int) {
WarnL << "收到主动退出信号,关闭父进程与子进程";
kill(pid, SIGINT);
2019-05-20 22:22:05 +08:00
exit(0);
});
do {
2019-05-20 22:22:05 +08:00
int status = 0;
if (waitpid(pid, &status, 0) >= 0) {
2019-05-20 22:22:05 +08:00
WarnL << "子进程退出";
//休眠3秒再启动子进程
sleep(3);
//重启子进程,如果子进程重启失败,那么不应该杀掉守护进程,这样守护进程可以一直尝试重启子进程
kill_parent_if_failed = false;
2019-05-20 22:22:05 +08:00
break;
2019-05-20 11:22:59 +08:00
}
2019-05-20 22:22:05 +08:00
DebugL << "waitpid被中断:" << get_uv_errmsg();
} while (true);
} while (true);
2020-01-07 14:37:18 +08:00
#endif // _WIN32
2019-05-20 11:22:59 +08:00
}
void System::systemSetup(){
2020-01-07 14:37:18 +08:00
#if !defined(_WIN32)
2019-05-20 11:22:59 +08:00
struct rlimit rlim,rlim_new;
if (getrlimit(RLIMIT_CORE, &rlim)==0) {
rlim_new.rlim_cur = rlim_new.rlim_max = RLIM_INFINITY;
if (setrlimit(RLIMIT_CORE, &rlim_new)!=0) {
rlim_new.rlim_cur = rlim_new.rlim_max = rlim.rlim_max;
setrlimit(RLIMIT_CORE, &rlim_new);
}
InfoL << "core文件大小设置为:" << rlim_new.rlim_cur;
}
if (getrlimit(RLIMIT_NOFILE, &rlim)==0) {
rlim_new.rlim_cur = rlim_new.rlim_max = RLIM_INFINITY;
if (setrlimit(RLIMIT_NOFILE, &rlim_new)!=0) {
rlim_new.rlim_cur = rlim_new.rlim_max = rlim.rlim_max;
setrlimit(RLIMIT_NOFILE, &rlim_new);
}
InfoL << "文件最大描述符个数设置为:" << rlim_new.rlim_cur;
}
2019-09-24 15:21:20 +08:00
#ifndef ANDROID
2019-05-20 11:22:59 +08:00
signal(SIGSEGV, sig_crash);
signal(SIGABRT, sig_crash);
2022-05-29 21:44:53 +08:00
//忽略挂起信号
signal(SIGHUP, SIG_IGN);
2020-01-07 14:37:18 +08:00
#endif// ANDROID
#endif//!defined(_WIN32)
2019-05-20 11:22:59 +08:00
}