1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-03-09 15:49:59 +00:00

for #374, use SIGINT then SIGKILL to try to kill FFMPEG gracefullly.

This commit is contained in:
winlin 2015-05-30 09:32:03 +08:00
parent 5caafadd45
commit db57a519a9
4 changed files with 80 additions and 22 deletions

View file

@ -27,6 +27,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#include <unistd.h>
#include <ifaddrs.h>
#include <arpa/inet.h>
#include <signal.h>
#ifdef SRS_OSX
#include <sys/sysctl.h>
@ -222,6 +223,63 @@ void srs_parse_endpoint(string ip_port, string& ip, int& port)
port = ::atoi(the_port.c_str());
}
#define SRS_PROCESS_QUIT_TIMEOUT_MS 1000
int srs_kill_forced(int& pid)
{
int ret = ERROR_SUCCESS;
if (pid <= 0) {
return ret;
}
// first, try kill by SIGINT.
if (kill(pid, SIGINT) < 0) {
return ERROR_SYSTEM_KILL;
}
// wait to quit.
srs_trace("send SIGINT to pid=%d", pid);
for (int i = 0; i < SRS_PROCESS_QUIT_TIMEOUT_MS / 10; i++) {
int status = 0;
pid_t qpid = -1;
if ((qpid = waitpid(pid, &status, WNOHANG)) < 0) {
return ERROR_SYSTEM_KILL;
}
// 0 is not quit yet.
if (qpid == 0) {
st_usleep(10 * 1000);
continue;
}
// killed, set pid to -1.
srs_trace("SIGINT stop process pid=%d ok.", pid);
pid = -1;
return ret;
}
// then, try kill by SIGKILL.
if (kill(pid, SIGKILL) < 0) {
return ERROR_SYSTEM_KILL;
}
// wait for the process to quit.
// for example, ffmpeg will gracefully quit if signal is:
// 1) SIGHUP 2) SIGINT 3) SIGQUIT
// other signals, directly exit(123), for example:
// 9) SIGKILL 15) SIGTERM
int status = 0;
if (waitpid(pid, &status, 0) < 0) {
return ERROR_SYSTEM_KILL;
}
srs_trace("SIGKILL stop process pid=%d ok.", pid);
pid = -1;
return ret;
}
static SrsRusage _srs_system_rusage;
SrsRusage::SrsRusage()
@ -422,7 +480,7 @@ void srs_update_proc_stat()
// @see https://github.com/simple-rtmp-server/srs/issues/397
static int user_hz = 0;
if (user_hz <= 0) {
user_hz = sysconf(_SC_CLK_TCK);
user_hz = (int)sysconf(_SC_CLK_TCK);
srs_trace("USER_HZ=%d", user_hz);
srs_assert(user_hz > 0);
}
@ -646,12 +704,12 @@ void srs_update_disk_stat()
if (o.pgpgin > 0 && r.pgpgin > o.pgpgin && duration_ms > 0) {
// KBps = KB * 1000 / ms = KB/s
r.in_KBps = (r.pgpgin - o.pgpgin) * 1000 / duration_ms;
r.in_KBps = (int)((r.pgpgin - o.pgpgin) * 1000 / duration_ms);
}
if (o.pgpgout > 0 && r.pgpgout > o.pgpgout && duration_ms > 0) {
// KBps = KB * 1000 / ms = KB/s
r.out_KBps = (r.pgpgout - o.pgpgout) * 1000 / duration_ms;
r.out_KBps = (int)((r.pgpgout - o.pgpgout) * 1000 / duration_ms);
}
}
@ -771,8 +829,8 @@ SrsCpuInfo* srs_get_cpuinfo()
// initialize cpu info.
cpu = new SrsCpuInfo();
cpu->ok = true;
cpu->nb_processors = sysconf(_SC_NPROCESSORS_CONF);
cpu->nb_processors_online = sysconf(_SC_NPROCESSORS_ONLN);
cpu->nb_processors = (int)sysconf(_SC_NPROCESSORS_CONF);
cpu->nb_processors_online = (int)sysconf(_SC_NPROCESSORS_ONLN);
return cpu;
}