优化进程管理代码: #1518

This commit is contained in:
xiongziliang 2022-03-27 21:47:00 +08:00
parent 43c5d05d8f
commit 4b9b022690

View File

@ -14,81 +14,100 @@
#include <sys/resource.h> #include <sys/resource.h>
#include <unistd.h> #include <unistd.h>
#else #else
//#include <TlHelp32.h>
#include <windows.h>
#include <io.h> #include <io.h>
#include <windows.h>
#endif #endif
#include <csignal>
#include <stdexcept> #include <stdexcept>
#include <signal.h> #include "Process.h"
#include "Util/util.h"
#include "Util/File.h" #include "Util/File.h"
#include "Util/util.h"
#include "Util/logger.h" #include "Util/logger.h"
#include "Util/uv_errno.h" #include "Util/uv_errno.h"
#include "Poller/EventPoller.h" #include "Poller/EventPoller.h"
#include "Process.h"
#define STACK_SIZE (8192*1024) #define STACK_SIZE (8192 * 1024)
using namespace toolkit;
using namespace std; using namespace std;
using namespace toolkit;
#ifndef _WIN32 #ifndef _WIN32
typedef struct
{
const char *cmd_path;
char **cmd_args;
const char *log_file;
} params_t;
static void setupChildProcess() {
static int /* Start function for cloned child */
childFunc(params_t *params)
{
//取消cpu亲和性设置防止FFmpeg进程cpu占用率不能超过100%的问题 //取消cpu亲和性设置防止FFmpeg进程cpu占用率不能超过100%的问题
setThreadAffinity(-1); setThreadAffinity(-1);
//子进程关闭core文件生成 //子进程关闭core文件生成
struct rlimit rlim = {0, 0}; struct rlimit rlim = { 0, 0 };
setrlimit(RLIMIT_CORE, &rlim); setrlimit(RLIMIT_CORE, &rlim);
//重定向shell日志至文件 //子进程恢复默认信号处理
char log_path[strlen(params->log_file) + 20]; signal(SIGINT, SIG_DFL);
memset(log_path,0,sizeof(log_path)); signal(SIGTERM, SIG_DFL);
if (strlen(params->log_file) <= 1) { signal(SIGSEGV, SIG_DFL);
signal(SIGABRT, SIG_DFL);
}
/* Start function for cloned child */
static int runChildProcess(const string &cmd, string &log_file_tmp) {
setupChildProcess();
string log_file;
if (log_file_tmp.empty()) {
//未指定子进程日志文件时,重定向至/dev/null //未指定子进程日志文件时,重定向至/dev/null
snprintf(log_path, sizeof(log_path), "%s", "/dev/null"); log_file = "/dev/null";
} else { } else {
snprintf(log_path, sizeof(log_path), "%s.%d", params->log_file, getpid()); log_file = StrPrinter << log_file_tmp << "." << getpid();
} }
auto fp = File::create_file(params->log_file, "ab");
//重定向shell日志至文件
auto fp = File::create_file(log_file.data(), "ab");
if (!fp) { if (!fp) {
fprintf(stderr, "open log file %s failed:%d(%s)\r\n", log_path, get_uv_error(), get_uv_errmsg()); fprintf(stderr, "open log file %s failed:%d(%s)\r\n", log_file.data(), get_uv_error(), get_uv_errmsg());
} else { } else {
auto log_fd = fileno(fp); auto log_fd = fileno(fp);
// dup to stdout and stderr. // dup to stdout and stderr.
if (dup2(log_fd, STDOUT_FILENO) < 0) { if (dup2(log_fd, STDOUT_FILENO) < 0) {
fprintf(stderr, "dup2 stdout file %s failed:%d(%s)\r\n", log_path, get_uv_error(), get_uv_errmsg()); fprintf(stderr, "dup2 stdout file %s failed:%d(%s)\r\n", log_file.data(), get_uv_error(), get_uv_errmsg());
} }
if (dup2(log_fd, STDERR_FILENO) < 0) { if (dup2(log_fd, STDERR_FILENO) < 0) {
fprintf(stderr, "dup2 stderr file %s failed:%d(%s)\r\n", log_path, get_uv_error(), get_uv_errmsg()); fprintf(stderr, "dup2 stderr file %s failed:%d(%s)\r\n", log_file.data(), get_uv_error(), get_uv_errmsg());
} }
// 关闭日志文件 // 关闭日志文件
::fclose(fp); ::fclose(fp);
} }
fprintf(stderr, "\r\n\r\n#### pid=%d,cmd=%s%s #####\r\n\r\n", getpid(), params->cmd_path, *params->cmd_args); fprintf(stderr, "\r\n\r\n#### pid=%d,cmd=%s #####\r\n\r\n", getpid(), cmd.data());
auto params = split(cmd, " ");
// memory leak in child process, it's ok.
char **charpv_params = new char *[params.size() + 1];
for (int i = 0; i < (int)params.size(); i++) {
std::string &p = params[i];
charpv_params[i] = (char *)p.data();
}
// EOF: NULL
charpv_params[params.size()] = NULL;
// TODO: execv or execvp // TODO: execv or execvp
auto ret = execv(params->cmd_path, params->cmd_args); auto ret = execv(params[0].c_str(), charpv_params);
delete[] charpv_params;
if (ret < 0) { if (ret < 0) {
fprintf(stderr, "clone process failed:%d(%s)\r\n", get_uv_error(), get_uv_errmsg()); fprintf(stderr, "execv process failed:%d(%s)\r\n", get_uv_error(), get_uv_errmsg());
} }
return ret; return ret;
} }
static int cloneFunc(void *ptr) {
auto pair = reinterpret_cast<std::pair<string, string> *>(ptr);
return runChildProcess(pair->first, pair->second);
}
#endif #endif
void Process::run(const string &cmd, string &log_file_tmp) { void Process::run(const string &cmd, string &log_file_tmp) {
kill(2000); kill(2000);
#ifdef _WIN32 #ifdef _WIN32
STARTUPINFO si = {0}; STARTUPINFO si = { 0 };
PROCESS_INFORMATION pi = {0}; PROCESS_INFORMATION pi = { 0 };
string log_file; string log_file;
if (log_file_tmp.empty()) { if (log_file_tmp.empty()) {
//未指定子进程日志文件时,重定向至/dev/null //未指定子进程日志文件时,重定向至/dev/null
@ -113,8 +132,8 @@ void Process::run(const string &cmd, string &log_file_tmp) {
si.hStdOutput = log_fd; si.hStdOutput = log_fd;
} }
LPTSTR lpDir = const_cast<char*>(cmd.data()); LPTSTR lpDir = const_cast<char *>(cmd.data());
if (CreateProcess(NULL, lpDir, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi)){ if (CreateProcess(NULL, lpDir, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi)) {
//下面两行关闭句柄,解除本进程和新进程的关系,不然有可能 不小心调用TerminateProcess函数关掉子进程 //下面两行关闭句柄,解除本进程和新进程的关系,不然有可能 不小心调用TerminateProcess函数关掉子进程
CloseHandle(pi.hThread); CloseHandle(pi.hThread);
_pid = pi.dwProcessId; _pid = pi.dwProcessId;
@ -127,89 +146,27 @@ void Process::run(const string &cmd, string &log_file_tmp) {
fclose(fp); fclose(fp);
#elif ((defined(__linux) || defined(__linux__))) #elif ((defined(__linux) || defined(__linux__)))
_process_stack = malloc(STACK_SIZE); _process_stack = malloc(STACK_SIZE);
auto params = split(cmd, " "); auto args = std::make_pair(cmd, log_file_tmp);
// memory leak in child process, it's ok. int pid = clone(reinterpret_cast<int (*)(void *)>(&cloneFunc), (char *)_process_stack + STACK_SIZE, CLONE_FS | SIGCHLD, (void *)(&args));
char **char_params = new char *[params.size() + 1]; if (pid == -1) {
for (int i = 0; i < (int) params.size(); i++) {
std::string &p = params[i];
char_params[i] = (char *) p.data();
}
// EOF: NULL
char_params[params.size()] = NULL;
params_t cmd_params = {params[0].c_str(), char_params, log_file_tmp.c_str()};
int clonePid = clone(reinterpret_cast<int (*)(void *)>(&childFunc), (char *)_process_stack + STACK_SIZE, CLONE_FS | SIGCHLD, (void *) (&cmd_params));
delete[] char_params;
if (clonePid == -1) {
WarnL << "clone process failed:" << get_uv_errmsg(); WarnL << "clone process failed:" << get_uv_errmsg();
free(_process_stack); free(_process_stack);
throw std::runtime_error(StrPrinter << "fork child process failed,err:" << get_uv_errmsg()); throw std::runtime_error(StrPrinter << "fork child process failed,err:" << get_uv_errmsg());
} }
_pid = clonePid; _pid = pid;
if(log_file_tmp.empty()) { if (log_file_tmp.empty()) {
InfoL << "start child process " << _pid << ", log file:" << "/dev/null"; InfoL << "start child process " << _pid << ", log file:" << "/dev/null";
}else { } else {
InfoL << "start child process " << _pid << ", log file:" << log_file_tmp.c_str() << "." << _pid; InfoL << "start child process " << _pid << ", log file:" << log_file_tmp.c_str() << "." << _pid;
} }
#else #else
_pid = vfork(); _pid = fork();
if (_pid < 0) { if (_pid < 0) {
throw std::runtime_error(StrPrinter << "fork child process failed,err:" << get_uv_errmsg()); throw std::runtime_error(StrPrinter << "fork child process failed,err:" << get_uv_errmsg());
} }
if (_pid == 0) { if (_pid == 0) {
//取消cpu亲和性设置防止FFmpeg进程cpu占用率不能超过100%的问题 //子进程
setThreadAffinity(-1); exit(runChildProcess(cmd, log_file_tmp));
string log_file;
if (log_file_tmp.empty()) {
//未指定子进程日志文件时,重定向至/dev/null
log_file = "/dev/null";
} else {
log_file = StrPrinter << log_file_tmp << "." << getpid();
}
//子进程关闭core文件生成
struct rlimit rlim = {0, 0};
setrlimit(RLIMIT_CORE, &rlim);
//在启动子进程时暂时禁用SIGINT、SIGTERM信号
signal(SIGINT, SIG_DFL);
signal(SIGTERM, SIG_DFL);
signal(SIGSEGV, SIG_DFL);
signal(SIGABRT, SIG_DFL);
//重定向shell日志至文件
auto fp = File::create_file(log_file.data(), "ab");
if (!fp) {
fprintf(stderr, "open log file %s failed:%d(%s)\r\n", log_file.data(), get_uv_error(), get_uv_errmsg());
} else {
auto log_fd = fileno(fp);
// dup to stdout and stderr.
if (dup2(log_fd, STDOUT_FILENO) < 0) {
fprintf(stderr, "dup2 stdout file %s failed:%d(%s)\r\n", log_file.data(), get_uv_error(), get_uv_errmsg());
}
if (dup2(log_fd, STDERR_FILENO) < 0) {
fprintf(stderr, "dup2 stderr file %s failed:%d(%s)\r\n", log_file.data(), get_uv_error(), get_uv_errmsg());
}
// 关闭日志文件
::fclose(fp);
}
fprintf(stderr, "\r\n\r\n#### pid=%d,cmd=%s #####\r\n\r\n", getpid(), cmd.data());
auto params = split(cmd, " ");
// memory leak in child process, it's ok.
char **charpv_params = new char *[params.size() + 1];
for (int i = 0; i < (int) params.size(); i++) {
std::string &p = params[i];
charpv_params[i] = (char *) p.data();
}
// EOF: NULL
charpv_params[params.size()] = NULL;
// TODO: execv or execvp
auto ret = execv(params[0].c_str(), charpv_params);
if (ret < 0) {
fprintf(stderr, "fork process failed:%d(%s)\r\n", get_uv_error(), get_uv_errmsg());
}
exit(ret);
} }
string log_file; string log_file;
@ -244,16 +201,16 @@ static bool s_wait(pid_t pid, void *handle, int *exit_code_ptr, bool block) {
code = WaitForSingleObject(handle, 0); code = WaitForSingleObject(handle, 0);
} }
if(code == WAIT_FAILED || code == WAIT_OBJECT_0){ if (code == WAIT_FAILED || code == WAIT_OBJECT_0) {
//子进程已经退出了,获取子进程退出代码 //子进程已经退出了,获取子进程退出代码
DWORD exitCode = 0; DWORD exitCode = 0;
if(exit_code_ptr && GetExitCodeProcess(handle, &exitCode)){ if (exit_code_ptr && GetExitCodeProcess(handle, &exitCode)) {
*exit_code_ptr = exitCode; *exit_code_ptr = exitCode;
} }
return false; return false;
} }
if(code == WAIT_TIMEOUT){ if (code == WAIT_TIMEOUT) {
//子进程还在线 //子进程还在线
return true; return true;
} }
@ -282,14 +239,14 @@ static bool s_wait(pid_t pid, void *handle, int *exit_code_ptr, bool block) {
#ifdef _WIN32 #ifdef _WIN32
// Inspired from http://stackoverflow.com/a/15281070/1529139 // Inspired from http://stackoverflow.com/a/15281070/1529139
// and http://stackoverflow.com/q/40059902/1529139 // and http://stackoverflow.com/q/40059902/1529139
bool signalCtrl(DWORD dwProcessId, DWORD dwCtrlEvent){ bool signalCtrl(DWORD dwProcessId, DWORD dwCtrlEvent) {
bool success = false; bool success = false;
DWORD thisConsoleId = GetCurrentProcessId(); DWORD thisConsoleId = GetCurrentProcessId();
// Leave current console if it exists // Leave current console if it exists
// (otherwise AttachConsole will return ERROR_ACCESS_DENIED) // (otherwise AttachConsole will return ERROR_ACCESS_DENIED)
bool consoleDetached = (FreeConsole() != FALSE); bool consoleDetached = (FreeConsole() != FALSE);
if (AttachConsole(dwProcessId) != FALSE){ if (AttachConsole(dwProcessId) != FALSE) {
// Add a fake Ctrl-C handler for avoid instant kill is this console // Add a fake Ctrl-C handler for avoid instant kill is this console
// WARNING: do not revert it or current program will be also killed // WARNING: do not revert it or current program will be also killed
SetConsoleCtrlHandler(nullptr, true); SetConsoleCtrlHandler(nullptr, true);
@ -297,11 +254,11 @@ bool signalCtrl(DWORD dwProcessId, DWORD dwCtrlEvent){
FreeConsole(); FreeConsole();
} }
if (consoleDetached){ if (consoleDetached) {
// Create a new console if previous was deleted by OS // Create a new console if previous was deleted by OS
if (AttachConsole(thisConsoleId) == FALSE){ if (AttachConsole(thisConsoleId) == FALSE) {
int errorCode = GetLastError(); int errorCode = GetLastError();
if (errorCode == 31){ if (errorCode == 31) {
// 31=ERROR_GEN_FAILURE // 31=ERROR_GEN_FAILURE
AllocConsole(); AllocConsole();
} }
@ -313,17 +270,17 @@ bool signalCtrl(DWORD dwProcessId, DWORD dwCtrlEvent){
static void s_kill(pid_t pid, void *handle, int max_delay, bool force) { static void s_kill(pid_t pid, void *handle, int max_delay, bool force) {
if (pid <= 0) { if (pid <= 0) {
//pid无效 // pid无效
return; return;
} }
#ifdef _WIN32 #ifdef _WIN32
//windows下目前没有比较好的手段往子进程发送SIGTERM或信号 // windows下目前没有比较好的手段往子进程发送SIGTERM或信号
//所以杀死子进程的方式全部强制为立即关闭 //所以杀死子进程的方式全部强制为立即关闭
force = true; force = true;
if(force){ if (force) {
//强制关闭子进程 //强制关闭子进程
TerminateProcess(handle, 0); TerminateProcess(handle, 0);
}else{ } else {
//非强制关闭发送Ctr+C信号 //非强制关闭发送Ctr+C信号
signalCtrl(pid, CTRL_C_EVENT); signalCtrl(pid, CTRL_C_EVENT);
} }
@ -362,12 +319,12 @@ void Process::kill(int max_delay, bool force) {
s_kill(_pid, _handle, max_delay, force); s_kill(_pid, _handle, max_delay, force);
_pid = -1; _pid = -1;
#ifdef _WIN32 #ifdef _WIN32
if(_handle){ if (_handle) {
CloseHandle(_handle); CloseHandle(_handle);
_handle = nullptr; _handle = nullptr;
} }
#elif ((defined(__linux) || defined(__linux__))) #elif ((defined(__linux) || defined(__linux__)))
if(_process_stack){ if (_process_stack) {
free(_process_stack); free(_process_stack);
_process_stack = nullptr; _process_stack = nullptr;
} }