ZLMediaKit/server/Process.cpp

303 lines
9.3 KiB
C++
Raw 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/xiongziliang/ZLMediaKit).
*
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-06-24 14:57:12 +08:00
#include <limits.h>
#include <sys/stat.h>
2020-01-07 14:37:18 +08:00
#ifndef _WIN32
2019-06-24 14:57:12 +08:00
#include <sys/resource.h>
2019-05-20 11:22:59 +08:00
#include <unistd.h>
2020-01-07 14:37:18 +08:00
#else
//#include <TlHelp32.h>
#include <windows.h>
#include <io.h>
2020-01-07 14:37:18 +08:00
#endif
2019-05-20 11:22:59 +08:00
#include <stdexcept>
#include <signal.h>
#include "Util/util.h"
#include "Util/File.h"
#include "Util/logger.h"
#include "Util/uv_errno.h"
2019-10-24 11:42:39 +08:00
#include "Thread/WorkThreadPool.h"
2019-05-20 11:22:59 +08:00
#include "Process.h"
using namespace toolkit;
void Process::run(const string &cmd, const string &log_file_tmp) {
2020-03-20 11:51:24 +08:00
kill(2000);
2020-01-07 14:37:18 +08:00
#ifdef _WIN32
STARTUPINFO si = {0};
PROCESS_INFORMATION pi = {0};
string log_file;
if (log_file_tmp.empty()) {
//未指定子进程日志文件时,重定向至/dev/null
log_file = "NUL";
2020-06-08 16:41:49 +08:00
} else {
log_file = StrPrinter << log_file_tmp << "." << getCurrentMillisecond();
}
2020-03-20 11:51:24 +08:00
//重定向shell日志至文件
auto fp = File::create_file(log_file.data(), "ab");
if (!fp) {
2020-06-08 16:41:49 +08:00
fprintf(stderr, "open log file %s failed:%d(%s)\r\n", log_file.data(), get_uv_error(), get_uv_errmsg());
} else {
auto log_fd = (HANDLE)(_get_osfhandle(fileno(fp)));
// dup to stdout and stderr.
si.wShowWindow = SW_HIDE;
// STARTF_USESHOWWINDOW:The wShowWindow member contains additional information.
// STARTF_USESTDHANDLES:The hStdInput, hStdOutput, and hStdError members contain additional information.
si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
si.hStdError = log_fd;
si.hStdOutput = log_fd;
}
2020-03-20 11:51:24 +08:00
LPTSTR lpDir = const_cast<char*>(cmd.data());
if (CreateProcess(NULL, lpDir, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi)){
2020-03-20 11:51:24 +08:00
//下面两行关闭句柄,解除本进程和新进程的关系,不然有可能 不小心调用TerminateProcess函数关掉子进程
CloseHandle(pi.hThread);
_pid = pi.dwProcessId;
2020-06-08 16:26:55 +08:00
_handle = pi.hProcess;
fprintf(fp, "\r\n\r\n#### pid=%d,cmd=%s #####\r\n\r\n", _pid, cmd.data());
2020-06-08 16:41:49 +08:00
InfoL << "start child process " << _pid << ", log file:" << log_file;
2020-03-20 11:51:24 +08:00
} else {
2020-06-08 16:41:49 +08:00
WarnL << "start child process fail: " << get_uv_errmsg();
2020-03-20 11:51:24 +08:00
}
2020-06-08 16:26:55 +08:00
fclose(fp);
#else
2020-03-20 11:51:24 +08:00
_pid = fork();
if (_pid < 0) {
2020-06-08 16:41:49 +08:00
throw std::runtime_error(StrPrinter << "fork child process failed,err:" << get_uv_errmsg());
2020-03-20 11:51:24 +08:00
}
if (_pid == 0) {
string log_file;
if (log_file_tmp.empty()) {
//未指定子进程日志文件时,重定向至/dev/null
log_file = "/dev/null";
} else {
log_file = StrPrinter << log_file_tmp << "." << getpid();
}
2020-03-20 11:51:24 +08:00
//子进程关闭core文件生成
2020-06-01 11:17:50 +08:00
struct rlimit rlim = {0, 0};
2020-03-20 11:51:24 +08:00
setrlimit(RLIMIT_CORE, &rlim);
//在启动子进程时暂时禁用SIGINT、SIGTERM信号
signal(SIGINT, SIG_IGN);
signal(SIGTERM, SIG_IGN);
2020-06-01 11:17:50 +08:00
//重定向shell日志至文件
auto fp = File::create_file(log_file.data(), "ab");
if (!fp) {
2020-06-08 16:41:49 +08:00
fprintf(stderr, "open log file %s failed:%d(%s)\r\n", log_file.data(), get_uv_error(), get_uv_errmsg());
2020-06-01 11:17:50 +08:00
} else {
auto log_fd = fileno(fp);
2020-03-20 11:51:24 +08:00
// dup to stdout and stderr.
if (dup2(log_fd, STDOUT_FILENO) < 0) {
2020-06-08 16:41:49 +08:00
fprintf(stderr, "dup2 stdout file %s failed:%d(%s)\r\n", log_file.data(), get_uv_error(), get_uv_errmsg());
2020-03-20 11:51:24 +08:00
}
if (dup2(log_fd, STDERR_FILENO) < 0) {
2020-06-08 16:41:49 +08:00
fprintf(stderr, "dup2 stderr file %s failed:%d(%s)\r\n", log_file.data(), get_uv_error(), get_uv_errmsg());
2020-03-20 11:51:24 +08:00
}
2020-06-01 11:17:50 +08:00
// 关闭日志文件
::fclose(fp);
2020-03-20 11:51:24 +08:00
}
fprintf(stderr, "\r\n\r\n#### pid=%d,cmd=%s #####\r\n\r\n", getpid(), cmd.data());
2020-06-01 11:17:50 +08:00
//关闭父进程继承的fd
2020-03-20 11:51:24 +08:00
for (int i = 3; i < 1024; i++) {
::close(i);
}
auto params = split(cmd, " ");
// memory leak in child process, it's ok.
char **charpv_params = new char *[params.size() + 1];
2020-06-01 11:17:50 +08:00
for (int i = 0; i < (int) params.size(); i++) {
2020-03-20 11:51:24 +08:00
std::string &p = params[i];
2020-06-01 11:17:50 +08:00
charpv_params[i] = (char *) p.data();
2020-03-20 11:51:24 +08:00
}
// EOF: NULL
charpv_params[params.size()] = NULL;
// TODO: execv or execvp
auto ret = execv(params[0].c_str(), charpv_params);
if (ret < 0) {
2020-06-08 16:41:49 +08:00
fprintf(stderr, "fork process failed:%d(%s)\r\n", get_uv_error(), get_uv_errmsg());
2020-03-20 11:51:24 +08:00
}
exit(ret);
}
2020-06-08 16:41:49 +08:00
string log_file;
if (log_file_tmp.empty()) {
//未指定子进程日志文件时,重定向至/dev/null
log_file = "/dev/null";
} else {
log_file = StrPrinter << log_file_tmp << "." << _pid;
}
2020-06-08 16:41:49 +08:00
InfoL << "start child process " << _pid << ", log file:" << log_file;
2020-01-07 14:37:18 +08:00
#endif // _WIN32
2019-05-20 11:22:59 +08:00
}
2019-10-24 11:42:39 +08:00
/**
*
* @param pid
* @param exit_code_ptr
* @param block
* @return
*/
2020-06-08 16:26:55 +08:00
static bool s_wait(pid_t pid, void *handle, int *exit_code_ptr, bool block) {
2019-10-24 11:42:39 +08:00
if (pid <= 0) {
return false;
}
2020-01-07 14:37:18 +08:00
#ifdef _WIN32
DWORD code = 0;
if (block) {
//一直等待
2020-06-08 16:26:55 +08:00
code = WaitForSingleObject(handle, INFINITE);
} else {
2020-06-08 16:26:55 +08:00
code = WaitForSingleObject(handle, 0);
}
2020-06-08 16:26:55 +08:00
if(code == WAIT_FAILED || code == WAIT_OBJECT_0){
//子进程已经退出了,获取子进程退出代码
DWORD exitCode = 0;
2020-06-08 16:26:55 +08:00
if(exit_code_ptr && GetExitCodeProcess(handle, &exitCode)){
*exit_code_ptr = exitCode;
}
2020-03-20 11:51:24 +08:00
return false;
}
2020-01-07 14:37:18 +08:00
if(code == WAIT_TIMEOUT){
//子进程还在线
return true;
}
2020-06-08 16:26:55 +08:00
//不太可能运行到此处
WarnL << "WaitForSingleObject ret:" << code;
return false;
2020-01-07 14:37:18 +08:00
#else
int status = 0;
2020-03-20 11:51:24 +08:00
pid_t p = waitpid(pid, &status, block ? 0 : WNOHANG);
int exit_code = (status & 0xFF00) >> 8;
if (exit_code_ptr) {
2020-05-14 10:23:12 +08:00
*exit_code_ptr = exit_code;
2020-03-20 11:51:24 +08:00
}
if (p < 0) {
WarnL << "waitpid failed, pid=" << pid << ", err=" << get_uv_errmsg();
return false;
}
if (p > 0) {
InfoL << "process terminated, pid=" << pid << ", exit code=" << exit_code;
return false;
}
return true;
2020-01-07 14:37:18 +08:00
#endif // _WIN32
}
2020-01-07 14:37:18 +08:00
#ifdef _WIN32
// Inspired from http://stackoverflow.com/a/15281070/1529139
// and http://stackoverflow.com/q/40059902/1529139
bool signalCtrl(DWORD dwProcessId, DWORD dwCtrlEvent){
bool success = false;
DWORD thisConsoleId = GetCurrentProcessId();
// Leave current console if it exists
// (otherwise AttachConsole will return ERROR_ACCESS_DENIED)
bool consoleDetached = (FreeConsole() != FALSE);
if (AttachConsole(dwProcessId) != FALSE){
// 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
SetConsoleCtrlHandler(nullptr, true);
success = (GenerateConsoleCtrlEvent(dwCtrlEvent, 0) != FALSE);
FreeConsole();
}
if (consoleDetached){
// Create a new console if previous was deleted by OS
if (AttachConsole(thisConsoleId) == FALSE){
int errorCode = GetLastError();
if (errorCode == 31){
// 31=ERROR_GEN_FAILURE
AllocConsole();
}
}
}
return success;
2019-10-24 11:42:39 +08:00
}
#endif // _WIN32
2019-10-24 11:42:39 +08:00
2020-06-08 16:26:55 +08:00
static void s_kill(pid_t pid, void *handle, int max_delay, bool force) {
2019-10-24 11:42:39 +08:00
if (pid <= 0) {
//pid无效
2019-05-20 11:22:59 +08:00
return;
}
2020-01-07 14:37:18 +08:00
#ifdef _WIN32
2020-06-08 15:49:32 +08:00
//windows下目前没有比较好的手段往子进程发送SIGTERM或信号
//所以杀死子进程的方式全部强制为立即关闭
force = true;
if(force){
2020-06-08 16:26:55 +08:00
//强制关闭子进程
TerminateProcess(handle, 0);
}else{
2020-06-08 16:26:55 +08:00
//非强制关闭发送Ctr+C信号
signalCtrl(pid, CTRL_C_EVENT);
2020-03-20 11:51:24 +08:00
}
2020-01-07 14:37:18 +08:00
#else
2020-03-20 11:51:24 +08:00
if (::kill(pid, force ? SIGKILL : SIGTERM) == -1) {
//进程可能已经退出了
WarnL << "kill process " << pid << " failed:" << get_uv_errmsg();
return;
}
2020-01-07 14:37:18 +08:00
#endif // _WIN32
2019-10-24 11:42:39 +08:00
2020-06-08 16:26:55 +08:00
if (force) {
2019-10-24 11:42:39 +08:00
//发送SIGKILL信号后阻塞等待退出
2020-06-08 16:26:55 +08:00
s_wait(pid, handle, nullptr, true);
2019-10-24 11:42:39 +08:00
DebugL << "force kill " << pid << " success!";
return;
}
//发送SIGTERM信号后2秒后检查子进程是否已经退出
2020-06-08 16:26:55 +08:00
WorkThreadPool::Instance().getPoller()->doDelayTask(max_delay, [pid, handle]() {
if (!s_wait(pid, handle, nullptr, false)) {
2019-10-24 11:42:39 +08:00
//进程已经退出了
2019-05-20 11:22:59 +08:00
return 0;
2019-10-24 11:42:39 +08:00
}
//进程还在运行
WarnL << "process still working,force kill it:" << pid;
2020-06-08 16:26:55 +08:00
s_kill(pid, handle, 0, true);
2019-10-24 11:42:39 +08:00
return 0;
});
}
2020-06-08 16:26:55 +08:00
void Process::kill(int max_delay, bool force) {
2019-10-24 11:42:39 +08:00
if (_pid <= 0) {
return;
2019-05-20 11:22:59 +08:00
}
2020-06-08 16:26:55 +08:00
s_kill(_pid, _handle, max_delay, force);
2019-05-20 11:22:59 +08:00
_pid = -1;
2020-06-08 16:26:55 +08:00
#ifdef _WIN32
if(_handle){
CloseHandle(_handle);
_handle = nullptr;
}
#endif
2019-05-20 11:22:59 +08:00
}
Process::~Process() {
kill(2000);
}
2019-10-24 11:42:39 +08:00
Process::Process() {}
2019-05-20 11:22:59 +08:00
bool Process::wait(bool block) {
2020-06-08 16:26:55 +08:00
return s_wait(_pid, _handle, &_exit_code, block);
2019-05-20 11:22:59 +08:00
}
int Process::exit_code() {
return _exit_code;
}