Skip to content

Commit

Permalink
Fix: terminate subprocess when service is terminated
Browse files Browse the repository at this point in the history
  • Loading branch information
MakingL committed Jan 9, 2020
1 parent 6ac34f7 commit d56a796
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 4 deletions.
17 changes: 17 additions & 0 deletions src/command_handle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,19 @@
#include "speed_barrier.h"
#include "configure.h"

/* 用于处理信号 */
static CLCommandHandle *g_p_command_handler = nullptr;

void CLCommandHandle::on_exit_signal(int sig) {
int save_errno = errno;
if (g_p_command_handler) {
g_p_command_handler->m_b_stop = true;
}
errno = save_errno;
/* 不立即停止,如果该进程有文件正在传输,则等到文件传输结束再结束进程 */
// exit(EXIT_SUCCESS);
}

CLCommandHandle::CLCommandHandle(int command_fd, int read_pipe_fd) :
m_pipe_fd(read_pipe_fd), m_cmd_fd(command_fd), m_b_stop(false),
m_data_type(ASCII), m_resume_point(0), m_b_authored(false) {
Expand Down Expand Up @@ -58,6 +71,10 @@ CLCommandHandle::CLCommandHandle(int command_fd, int read_pipe_fd) :
m_cmd_need_auth = {"CWD", "CDUP", "PORT", "PASV", "TYPE",
"RNFR", "RNTO", "DELE", "RMD", "MKD",
"PWD", "LIST", "NLST", "FEAT", "SIZE"};

g_p_command_handler = this;
add_signal(SIGTERM, on_exit_signal);
add_signal(SIGINT, on_exit_signal);
}

CLCommandHandle::~CLCommandHandle() {
Expand Down
16 changes: 16 additions & 0 deletions src/command_handle.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,22 @@ class CLCommandHandle {

inline static bool is_file_existed(const char *file_name);

public:
static void add_signal(int sig, void(*handler)(int), bool restart=true) {
struct sigaction sa;
memset(&sa, '\0', sizeof(sa));
sa.sa_handler = handler;
if (restart) {
sa.sa_flags |= SA_RESTART;
}
sigfillset(&sa.sa_mask);
if (sigaction(sig, &sa, nullptr) == -1) {
utility::unix_error("Add signal error");
}
}

static void on_exit_signal(int sig);

private:
/* 访问控制命令 */
void do_user(); /* 用户名 */
Expand Down
2 changes: 0 additions & 2 deletions src/ftp_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ void CLFtpHandler::start_handle() {
}

if (pid == 0) {
/* 父进程退出,给子进程发送 SIGKILL 信号 */
prctl(PR_SET_PDEATHSIG, SIGKILL);
close(m_command_fd);
/* 关闭管道读的一端 */
close(m_p_pipe_fd[0]);
Expand Down
4 changes: 2 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ int main() {
CLSignalWrapper signal_handle;
signal_handle.add_signal(SIGCHLD, signal_handler);
signal_handle.add_signal(SIGTERM, signal_handler);
signal_handle.add_signal(SIGINT, signal_handler);
// signal_handle.add_signal(SIGINT, signal_handler);
signal_handle.ignore_signal(SIGPIPE);

std::unordered_map<pid_t , unsigned int> client_process_mapper; /* client 进程到 ip 的映射 */
Expand Down Expand Up @@ -142,5 +142,5 @@ int main() {
}
}
}
return 0;
exit(EXIT_SUCCESS);
}

0 comments on commit d56a796

Please sign in to comment.