mirror of
https://github.com/ZLMediaKit/ZLMediaKit.git
synced 2024-11-26 04:31:37 +08:00
优化进程管理代码: #1518
This commit is contained in:
parent
43c5d05d8f
commit
4b9b022690
@ -14,74 +14,93 @@
|
||||
#include <sys/resource.h>
|
||||
#include <unistd.h>
|
||||
#else
|
||||
//#include <TlHelp32.h>
|
||||
#include <windows.h>
|
||||
#include <io.h>
|
||||
#include <windows.h>
|
||||
#endif
|
||||
|
||||
#include <csignal>
|
||||
#include <stdexcept>
|
||||
#include <signal.h>
|
||||
#include "Util/util.h"
|
||||
#include "Process.h"
|
||||
#include "Util/File.h"
|
||||
#include "Util/util.h"
|
||||
#include "Util/logger.h"
|
||||
#include "Util/uv_errno.h"
|
||||
#include "Poller/EventPoller.h"
|
||||
#include "Process.h"
|
||||
|
||||
#define STACK_SIZE (8192 * 1024)
|
||||
using namespace toolkit;
|
||||
|
||||
using namespace std;
|
||||
using namespace toolkit;
|
||||
|
||||
#ifndef _WIN32
|
||||
typedef struct
|
||||
{
|
||||
const char *cmd_path;
|
||||
char **cmd_args;
|
||||
const char *log_file;
|
||||
} params_t;
|
||||
|
||||
|
||||
static int /* Start function for cloned child */
|
||||
childFunc(params_t *params)
|
||||
{
|
||||
static void setupChildProcess() {
|
||||
//取消cpu亲和性设置,防止FFmpeg进程cpu占用率不能超过100%的问题
|
||||
setThreadAffinity(-1);
|
||||
//子进程关闭core文件生成
|
||||
struct rlimit rlim = { 0, 0 };
|
||||
setrlimit(RLIMIT_CORE, &rlim);
|
||||
//重定向shell日志至文件
|
||||
char log_path[strlen(params->log_file) + 20];
|
||||
memset(log_path,0,sizeof(log_path));
|
||||
if (strlen(params->log_file) <= 1) {
|
||||
//未指定子进程日志文件时,重定向至/dev/null
|
||||
snprintf(log_path, sizeof(log_path), "%s", "/dev/null");
|
||||
} else {
|
||||
snprintf(log_path, sizeof(log_path), "%s.%d", params->log_file, getpid());
|
||||
//子进程恢复默认信号处理
|
||||
signal(SIGINT, SIG_DFL);
|
||||
signal(SIGTERM, SIG_DFL);
|
||||
signal(SIGSEGV, SIG_DFL);
|
||||
signal(SIGABRT, SIG_DFL);
|
||||
}
|
||||
auto fp = File::create_file(params->log_file, "ab");
|
||||
|
||||
/* 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
|
||||
log_file = "/dev/null";
|
||||
} else {
|
||||
log_file = StrPrinter << log_file_tmp << "." << getpid();
|
||||
}
|
||||
|
||||
//重定向shell日志至文件
|
||||
auto fp = File::create_file(log_file.data(), "ab");
|
||||
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 {
|
||||
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_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) {
|
||||
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);
|
||||
}
|
||||
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
|
||||
auto ret = execv(params->cmd_path, params->cmd_args);
|
||||
auto ret = execv(params[0].c_str(), charpv_params);
|
||||
delete[] charpv_params;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
static int cloneFunc(void *ptr) {
|
||||
auto pair = reinterpret_cast<std::pair<string, string> *>(ptr);
|
||||
return runChildProcess(pair->first, pair->second);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
void Process::run(const string &cmd, string &log_file_tmp) {
|
||||
@ -127,89 +146,27 @@ void Process::run(const string &cmd, string &log_file_tmp) {
|
||||
fclose(fp);
|
||||
#elif ((defined(__linux) || defined(__linux__)))
|
||||
_process_stack = malloc(STACK_SIZE);
|
||||
auto params = split(cmd, " ");
|
||||
// memory leak in child process, it's ok.
|
||||
char **char_params = new char *[params.size() + 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) {
|
||||
auto args = std::make_pair(cmd, log_file_tmp);
|
||||
int pid = clone(reinterpret_cast<int (*)(void *)>(&cloneFunc), (char *)_process_stack + STACK_SIZE, CLONE_FS | SIGCHLD, (void *)(&args));
|
||||
if (pid == -1) {
|
||||
WarnL << "clone process failed:" << get_uv_errmsg();
|
||||
free(_process_stack);
|
||||
throw std::runtime_error(StrPrinter << "fork child process failed,err:" << get_uv_errmsg());
|
||||
}
|
||||
_pid = clonePid;
|
||||
_pid = pid;
|
||||
if (log_file_tmp.empty()) {
|
||||
InfoL << "start child process " << _pid << ", log file:" << "/dev/null";
|
||||
} else {
|
||||
InfoL << "start child process " << _pid << ", log file:" << log_file_tmp.c_str() << "." << _pid;
|
||||
}
|
||||
|
||||
#else
|
||||
_pid = vfork();
|
||||
_pid = fork();
|
||||
if (_pid < 0) {
|
||||
throw std::runtime_error(StrPrinter << "fork child process failed,err:" << get_uv_errmsg());
|
||||
}
|
||||
if (_pid == 0) {
|
||||
//取消cpu亲和性设置,防止FFmpeg进程cpu占用率不能超过100%的问题
|
||||
setThreadAffinity(-1);
|
||||
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);
|
||||
//子进程
|
||||
exit(runChildProcess(cmd, log_file_tmp));
|
||||
}
|
||||
|
||||
string log_file;
|
||||
|
Loading…
Reference in New Issue
Block a user