Process.cpp 9.34 KB
Newer Older
1
/*
xiongziliang committed
2
 * Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
xiongziliang committed
3 4 5
 *
 * This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
 *
xiongziliang committed
6 7 8
 * 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.
xiongziliang committed
9 10
 */

11
#include <limits.h>
12
#include <sys/stat.h>
13
#ifndef _WIN32
14
#include <sys/resource.h>
15
#include <unistd.h>
16 17 18
#else
//#include <TlHelp32.h>
#include <windows.h>
19
#include <io.h>
20 21
#endif

22 23 24 25 26 27
#include <stdexcept>
#include <signal.h>
#include "Util/util.h"
#include "Util/File.h"
#include "Util/logger.h"
#include "Util/uv_errno.h"
28
#include "Thread/WorkThreadPool.h"
29 30 31 32
#include "Process.h"
using namespace toolkit;

void Process::run(const string &cmd, const string &log_file_tmp) {
33
    kill(2000);
34
#ifdef _WIN32
35 36 37 38 39 40
    STARTUPINFO si = {0};
    PROCESS_INFORMATION pi = {0};
    string log_file;
    if (log_file_tmp.empty()) {
        //未指定子进程日志文件时,重定向至/dev/null
        log_file = "NUL";
xiongziliang committed
41
    } else {
42 43
        log_file = StrPrinter << log_file_tmp << "." << getCurrentMillisecond();
    }
44

45 46 47
    //重定向shell日志至文件
    auto fp = File::create_file(log_file.data(), "ab");
    if (!fp) {
xiongziliang committed
48
        fprintf(stderr, "open log file %s failed:%d(%s)\r\n", log_file.data(), get_uv_error(), get_uv_errmsg());
49 50 51 52 53 54 55 56 57 58
    } 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;
    }
59

60 61
    LPTSTR lpDir = const_cast<char*>(cmd.data());
    if (CreateProcess(NULL, lpDir, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi)){
62 63 64
        //下面两行关闭句柄,解除本进程和新进程的关系,不然有可能 不小心调用TerminateProcess函数关掉子进程 
        CloseHandle(pi.hThread);
        _pid = pi.dwProcessId;
xiongziliang committed
65 66
        _handle = pi.hProcess;
        fprintf(fp, "\r\n\r\n#### pid=%d,cmd=%s #####\r\n\r\n", _pid, cmd.data());
xiongziliang committed
67
        InfoL << "start child process " << _pid << ", log file:" << log_file;
68
    } else {
xiongziliang committed
69
        WarnL << "start child process fail: " << get_uv_errmsg();
70
    }
xiongziliang committed
71 72
    fclose(fp);
#else
73 74
    _pid = fork();
    if (_pid < 0) {
xiongziliang committed
75
        throw std::runtime_error(StrPrinter << "fork child process failed,err:" << get_uv_errmsg());
76 77
    }
    if (_pid == 0) {
78 79 80 81 82 83 84
        string log_file;
        if (log_file_tmp.empty()) {
            //未指定子进程日志文件时,重定向至/dev/null
            log_file = "/dev/null";
        } else {
            log_file = StrPrinter << log_file_tmp << "." << getpid();
        }
85
        //子进程关闭core文件生成
86
        struct rlimit rlim = {0, 0};
87 88 89 90 91 92
        setrlimit(RLIMIT_CORE, &rlim);

        //在启动子进程时,暂时禁用SIGINT、SIGTERM信号
        signal(SIGINT, SIG_IGN);
        signal(SIGTERM, SIG_IGN);

93 94 95
        //重定向shell日志至文件
        auto fp = File::create_file(log_file.data(), "ab");
        if (!fp) {
xiongziliang committed
96
            fprintf(stderr, "open log file %s failed:%d(%s)\r\n", log_file.data(), get_uv_error(), get_uv_errmsg());
97 98
        } else {
            auto log_fd = fileno(fp);
99 100
            // dup to stdout and stderr.
            if (dup2(log_fd, STDOUT_FILENO) < 0) {
xiongziliang committed
101
                fprintf(stderr, "dup2 stdout file %s failed:%d(%s)\r\n", log_file.data(), get_uv_error(), get_uv_errmsg());
102 103
            }
            if (dup2(log_fd, STDERR_FILENO) < 0) {
xiongziliang committed
104
                fprintf(stderr, "dup2 stderr file  %s failed:%d(%s)\r\n", log_file.data(), get_uv_error(), get_uv_errmsg());
105
            }
106 107
            // 关闭日志文件
            ::fclose(fp);
108 109 110
        }
        fprintf(stderr, "\r\n\r\n#### pid=%d,cmd=%s #####\r\n\r\n", getpid(), cmd.data());

111
        //关闭父进程继承的fd
112 113 114 115 116 117 118
        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];
119
        for (int i = 0; i < (int) params.size(); i++) {
120
            std::string &p = params[i];
121
            charpv_params[i] = (char *) p.data();
122 123 124 125 126 127 128
        }
        // EOF: NULL
        charpv_params[params.size()] = NULL;

        // TODO: execv or execvp
        auto ret = execv(params[0].c_str(), charpv_params);
        if (ret < 0) {
xiongziliang committed
129
            fprintf(stderr, "fork process failed:%d(%s)\r\n", get_uv_error(), get_uv_errmsg());
130 131 132
        }
        exit(ret);
    }
xiongziliang committed
133

134 135 136 137 138 139 140
    string log_file;
    if (log_file_tmp.empty()) {
        //未指定子进程日志文件时,重定向至/dev/null
        log_file = "/dev/null";
    } else {
        log_file = StrPrinter << log_file_tmp << "." << _pid;
    }
xiongziliang committed
141
    InfoL << "start child process " << _pid << ", log file:" << log_file;
142
#endif // _WIN32
143 144
}

145 146 147 148 149 150 151
/**
 * 获取进程是否存活状态
 * @param pid 进程号
 * @param exit_code_ptr 进程返回代码
 * @param block 是否阻塞等待
 * @return 进程是否还在运行
 */
xiongziliang committed
152
static bool s_wait(pid_t pid, void *handle, int *exit_code_ptr, bool block) {
153 154 155
    if (pid <= 0) {
        return false;
    }
156
#ifdef _WIN32
157 158 159
    DWORD code = 0;
    if (block) {
        //一直等待
xiongziliang committed
160
        code = WaitForSingleObject(handle, INFINITE);
161
    } else {
xiongziliang committed
162
        code = WaitForSingleObject(handle, 0);
163
    }
xiongziliang committed
164

165 166 167
    if(code == WAIT_FAILED || code == WAIT_OBJECT_0){
        //子进程已经退出了,获取子进程退出代码
        DWORD exitCode = 0;
xiongziliang committed
168
        if(exit_code_ptr && GetExitCodeProcess(handle, &exitCode)){
169 170
            *exit_code_ptr = exitCode;
        }
171 172
        return false;
    }
173

174 175 176 177
    if(code == WAIT_TIMEOUT){
        //子进程还在线
        return true;
    }
xiongziliang committed
178
    //不太可能运行到此处
179 180
    WarnL << "WaitForSingleObject ret:" << code;
    return false;
181
#else
182
    int status = 0;
183 184 185
    pid_t p = waitpid(pid, &status, block ? 0 : WNOHANG);
    int exit_code = (status & 0xFF00) >> 8;
    if (exit_code_ptr) {
xiongziliang committed
186
        *exit_code_ptr = exit_code;
187 188 189 190 191 192 193 194 195
    }
    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;
    }
196
    return true;
197
#endif // _WIN32
198
}
199

200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
#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;
229
}
230
#endif // _WIN32
231

xiongziliang committed
232
static void s_kill(pid_t pid, void *handle, int max_delay, bool force) {
233 234
    if (pid <= 0) {
        //pid无效
235 236
        return;
    }
237
#ifdef _WIN32
238 239 240
    //windows下目前没有比较好的手段往子进程发送SIGTERM或信号
    //所以杀死子进程的方式全部强制为立即关闭
    force = true;
241
    if(force){
xiongziliang committed
242 243
        //强制关闭子进程
        TerminateProcess(handle, 0);
244
    }else{
xiongziliang committed
245
        //非强制关闭,发送Ctr+C信号
246
        signalCtrl(pid, CTRL_C_EVENT);
247
    }
248
#else
249 250 251 252 253
    if (::kill(pid, force ? SIGKILL : SIGTERM) == -1) {
        //进程可能已经退出了
        WarnL << "kill process " << pid << " failed:" << get_uv_errmsg();
        return;
    }
254
#endif // _WIN32
255

xiongziliang committed
256
    if (force) {
257
        //发送SIGKILL信号后,阻塞等待退出
xiongziliang committed
258
        s_wait(pid, handle, nullptr, true);
259 260 261 262 263
        DebugL << "force kill " << pid << " success!";
        return;
    }

    //发送SIGTERM信号后,2秒后检查子进程是否已经退出
xiongziliang committed
264 265
    WorkThreadPool::Instance().getPoller()->doDelayTask(max_delay, [pid, handle]() {
        if (!s_wait(pid, handle, nullptr, false)) {
266
            //进程已经退出了
267
            return 0;
268 269 270
        }
        //进程还在运行
        WarnL << "process still working,force kill it:" << pid;
xiongziliang committed
271
        s_kill(pid, handle, 0, true);
272 273 274 275
        return 0;
    });
}

xiongziliang committed
276
void Process::kill(int max_delay, bool force) {
277 278
    if (_pid <= 0) {
        return;
279
    }
xiongziliang committed
280
    s_kill(_pid, _handle, max_delay, force);
281
    _pid = -1;
xiongziliang committed
282 283 284 285 286 287
#ifdef _WIN32
    if(_handle){
        CloseHandle(_handle);
        _handle = nullptr;
    }
#endif
288 289 290 291 292 293
}

Process::~Process() {
    kill(2000);
}

294
Process::Process() {}
295 296

bool Process::wait(bool block) {
xiongziliang committed
297
    return s_wait(_pid, _handle, &_exit_code, block);
298 299 300 301 302
}

int Process::exit_code() {
    return _exit_code;
}