From c17f5df6cd520170dccbfdbebd8703626effb2bf Mon Sep 17 00:00:00 2001 From: Nick Peng Date: Wed, 6 Dec 2023 23:03:32 +0800 Subject: [PATCH] smartdns: optimize smartdns.c readability --- src/smartdns.c | 46 ++++++++++++++++++++++++++++++++-------------- src/util.c | 16 ++++++++++++---- src/util.h | 9 ++++++++- 3 files changed, 52 insertions(+), 19 deletions(-) diff --git a/src/smartdns.c b/src/smartdns.c index 5431f109df..561c4682fd 100644 --- a/src/smartdns.c +++ b/src/smartdns.c @@ -828,12 +828,12 @@ static smartdns_run_monitor_ret _smartdns_run_monitor(int restart_when_crash, in } if (is_run_as_daemon) { - int daemon_ret = daemon_run(); - if (daemon_ret != -2) { - if (daemon_ret == 0) { - return SMARTDNS_RUN_MONITOR_EXIT; - } - + switch (daemon_run(NULL)) { + case DAEMON_RET_CHILD_OK: + break; + case DAEMON_RET_PARENT_OK: + return SMARTDNS_RUN_MONITOR_EXIT; + default: return SMARTDNS_RUN_MONITOR_ERROR; } } @@ -898,6 +898,16 @@ static smartdns_run_monitor_ret _smartdns_run_monitor(int restart_when_crash, in return SMARTDNS_RUN_MONITOR_ERROR; } +static void _smartdns_print_error_tip(void) +{ + char buff[4096]; + char *log_path = realpath(_smartdns_log_path(), buff); + + if (log_path != NULL && access(log_path, F_OK) == 0) { + fprintf(stderr, "run daemon failed, please check log at %s\n", log_path); + } +} + #ifdef TEST static smartdns_post_func _smartdns_post = NULL; @@ -1030,16 +1040,21 @@ int main(int argc, char *argv[]) } if (is_run_as_daemon) { - int daemon_ret = daemon_run(); - if (daemon_ret != -2) { - char buff[4096]; - char *log_path = realpath(_smartdns_log_path(), buff); - - if (log_path != NULL && access(log_path, F_OK) == 0 && daemon_ret != -3 && daemon_ret != 0) { - fprintf(stderr, "run daemon failed, please check log at %s\n", log_path); + int child_status = -1; + switch (daemon_run(&child_status)) { + case DAEMON_RET_CHILD_OK: + break; + case DAEMON_RET_PARENT_OK: { + if (child_status != 0 && child_status != -3) { + _smartdns_print_error_tip(); } - return daemon_ret; + return child_status; + } break; + case DAEMON_RET_ERR: + default: + fprintf(stderr, "run daemon failed.\n"); + goto errout; } } @@ -1097,6 +1112,9 @@ int main(int argc, char *argv[]) errout: if (is_run_as_daemon) { daemon_kickoff(ret, dns_conf_log_console | verbose_screen); + } else { + _smartdns_print_error_tip(); + printf("ret = %d\n", ret); } smartdns_test_notify(2); _smartdns_exit(); diff --git a/src/util.c b/src/util.c index 526c1af3a4..91b9348523 100644 --- a/src/util.c +++ b/src/util.c @@ -1703,7 +1703,7 @@ int daemon_keepalive(void) return 0; } -int daemon_run(void) +daemon_ret daemon_run(int *wstatus) { pid_t pid = 0; int fds[2] = {0}; @@ -1753,11 +1753,16 @@ int daemon_run(void) pid = msg.value; continue; } else if (msg.type == DAEMON_MSG_KICKOFF) { - return msg.value; + if (wstatus != NULL) { + *wstatus = msg.value; + } + return DAEMON_RET_PARENT_OK; } else { goto errout; } } while (true); + + return DAEMON_RET_ERR; } setsid(); @@ -1782,10 +1787,13 @@ int daemon_run(void) close(fds[0]); daemon_fd = fds[1]; - return -2; + return DAEMON_RET_CHILD_OK; errout: kill(pid, SIGKILL); - return -1; + if (wstatus != NULL) { + *wstatus = -1; + } + return DAEMON_RET_ERR; } #ifdef DEBUG diff --git a/src/util.h b/src/util.h index 7a417de14f..8783815d98 100644 --- a/src/util.h +++ b/src/util.h @@ -147,7 +147,14 @@ void print_stack(void); void close_all_fd(int keepfd); -int daemon_run(void); +typedef enum daemon_ret { + DAEMON_RET_OK = 0, + DAEMON_RET_ERR = -1, + DAEMON_RET_CHILD_OK = -2, + DAEMON_RET_PARENT_OK = -3, +} daemon_ret; + +daemon_ret daemon_run(int *wstatus); int daemon_kickoff(int status, int no_close);