Skip to content

Commit

Permalink
force_quit switch.
Browse files Browse the repository at this point in the history
  • Loading branch information
ssrlive committed Apr 28, 2020
1 parent 57f1543 commit 37e0305
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 5 deletions.
30 changes: 29 additions & 1 deletion src/client/listener.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ struct ssr_client_state {
uv_signal_t *sigterm_watcher;

bool shutting_down;
bool force_quit;

int listener_count;
struct listener_t *listeners;
Expand Down Expand Up @@ -116,7 +117,11 @@ int ssr_run_loop_begin(struct server_config *cf, void(*feedback_state)(struct ss
pr_err("uv_run: %s", uv_strerror(err));
}

VERIFY(uv_loop_close(loop) == 0);
if (uv_loop_close(loop) != 0) {
if (state->force_quit == false) {
ASSERT(false);
}
}

ssr_cipher_env_release(state->env);

Expand All @@ -138,6 +143,23 @@ static void tcp_close_done_cb(uv_handle_t* handle) {
free((void *)((uv_tcp_t *)handle));
}

void state_set_force_quit(struct ssr_client_state *state, bool force_quit) {
state->force_quit = force_quit;
}

void sigint_watcher_close_cb(uv_handle_t* handle) {
// For some reason, uv_close may NOT always be work fine.
// sometimes uv_close_cb perhaps never called.
// so we have to call uv_stop to force exit the loop.
// it can caused memory leaking. but who cares it?
uv_stop(handle->loop);
free(handle);
}

void force_quit_timer_cb(uv_timer_t* handle) {
uv_close((uv_handle_t*)handle, sigint_watcher_close_cb);
}

void ssr_run_loop_shutdown(struct ssr_client_state *state) {
if (state==NULL) {
return;
Expand Down Expand Up @@ -177,6 +199,12 @@ void ssr_run_loop_shutdown(struct ssr_client_state *state) {

pr_info(" ");
pr_info("terminated.\n");

if (state->force_quit) {
uv_timer_t *t = (uv_timer_t*) calloc(1, sizeof(*t));
uv_timer_init(state->sigint_watcher->loop, t);
uv_timer_start(t, force_quit_timer_cb, 3000, 0); // wait 3 seconds.
}
}

int ssr_get_listen_socket_fd(struct ssr_client_state *state) {
Expand Down
9 changes: 6 additions & 3 deletions src/client/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,18 @@ static void usage(void);

struct ssr_client_state *g_state = NULL;
void feedback_state(struct ssr_client_state *state, void *p);
void state_set_force_quit(struct ssr_client_state *state, bool force_quit);
void print_remote_info(const struct server_config *config);

void fn_onexit(void) {
MEM_CHECK_DUMP_LEAKS();
}

struct cmd_line_info *cmds = NULL;

int main(int argc, char **argv) {
struct server_config *config = NULL;
int err = -1;
struct cmd_line_info *cmds = NULL;

MEM_CHECK_BEGIN();
MEM_CHECK_BREAK_ALLOC(63);
Expand Down Expand Up @@ -160,6 +162,7 @@ void print_remote_info(const struct server_config *config) {
void feedback_state(struct ssr_client_state *state, void *p) {
g_state = state;
(void)p;
state_set_force_quit(state, cmds->force_quit);
}

static void usage(void) {
Expand All @@ -173,8 +176,8 @@ static void usage(void) {
"Options:\n"
"\n"
" -d Run in background as a daemon.\n"
" -c <config file> Configure file path.\n"
" Default: " DEFAULT_CONF_PATH "\n"
" -c <config file> Configure file path. Default: " DEFAULT_CONF_PATH "\n"
" -f Force quit the program.\n"
" -h Show this help message.\n"
"",
get_app_name());
Expand Down
5 changes: 4 additions & 1 deletion src/cmd_line_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,17 @@ struct cmd_line_info * cmd_line_info_create(int argc, char * const argv[]) {

struct cmd_line_info *info = (struct cmd_line_info *)calloc(1, sizeof(*info));

while (-1 != (opt = getopt(argc, argv, "c:dh"))) {
while (-1 != (opt = getopt(argc, argv, "c:dfh"))) {
switch (opt) {
case 'c':
string_safe_assign(&info->cfg_file, optarg);
break;
case 'd':
info->daemon_flag = true;
break;
case 'f':
info->force_quit = true;
break;
case 'h':
default:
info->help_flag = true;
Expand Down
1 change: 1 addition & 0 deletions src/cmd_line_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ struct cmd_line_info {
char * cfg_file;
bool daemon_flag;
bool help_flag;
bool force_quit;
};

struct cmd_line_info * cmd_line_info_create(int argc, char * const argv[]);
Expand Down

0 comments on commit 37e0305

Please # to comment.