smartdns: optimize smartdns.c readability
This commit is contained in:
@@ -828,12 +828,12 @@ static smartdns_run_monitor_ret _smartdns_run_monitor(int restart_when_crash, in
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (is_run_as_daemon) {
|
if (is_run_as_daemon) {
|
||||||
int daemon_ret = daemon_run();
|
switch (daemon_run(NULL)) {
|
||||||
if (daemon_ret != -2) {
|
case DAEMON_RET_CHILD_OK:
|
||||||
if (daemon_ret == 0) {
|
break;
|
||||||
return SMARTDNS_RUN_MONITOR_EXIT;
|
case DAEMON_RET_PARENT_OK:
|
||||||
}
|
return SMARTDNS_RUN_MONITOR_EXIT;
|
||||||
|
default:
|
||||||
return SMARTDNS_RUN_MONITOR_ERROR;
|
return SMARTDNS_RUN_MONITOR_ERROR;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -898,6 +898,16 @@ restart:
|
|||||||
return SMARTDNS_RUN_MONITOR_ERROR;
|
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
|
#ifdef TEST
|
||||||
|
|
||||||
static smartdns_post_func _smartdns_post = NULL;
|
static smartdns_post_func _smartdns_post = NULL;
|
||||||
@@ -1030,16 +1040,21 @@ int main(int argc, char *argv[])
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (is_run_as_daemon) {
|
if (is_run_as_daemon) {
|
||||||
int daemon_ret = daemon_run();
|
int child_status = -1;
|
||||||
if (daemon_ret != -2) {
|
switch (daemon_run(&child_status)) {
|
||||||
char buff[4096];
|
case DAEMON_RET_CHILD_OK:
|
||||||
char *log_path = realpath(_smartdns_log_path(), buff);
|
break;
|
||||||
|
case DAEMON_RET_PARENT_OK: {
|
||||||
if (log_path != NULL && access(log_path, F_OK) == 0 && daemon_ret != -3 && daemon_ret != 0) {
|
if (child_status != 0 && child_status != -3) {
|
||||||
fprintf(stderr, "run daemon failed, please check log at %s\n", log_path);
|
_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:
|
errout:
|
||||||
if (is_run_as_daemon) {
|
if (is_run_as_daemon) {
|
||||||
daemon_kickoff(ret, dns_conf_log_console | verbose_screen);
|
daemon_kickoff(ret, dns_conf_log_console | verbose_screen);
|
||||||
|
} else {
|
||||||
|
_smartdns_print_error_tip();
|
||||||
|
printf("ret = %d\n", ret);
|
||||||
}
|
}
|
||||||
smartdns_test_notify(2);
|
smartdns_test_notify(2);
|
||||||
_smartdns_exit();
|
_smartdns_exit();
|
||||||
|
|||||||
16
src/util.c
16
src/util.c
@@ -1703,7 +1703,7 @@ int daemon_keepalive(void)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int daemon_run(void)
|
daemon_ret daemon_run(int *wstatus)
|
||||||
{
|
{
|
||||||
pid_t pid = 0;
|
pid_t pid = 0;
|
||||||
int fds[2] = {0};
|
int fds[2] = {0};
|
||||||
@@ -1753,11 +1753,16 @@ int daemon_run(void)
|
|||||||
pid = msg.value;
|
pid = msg.value;
|
||||||
continue;
|
continue;
|
||||||
} else if (msg.type == DAEMON_MSG_KICKOFF) {
|
} else if (msg.type == DAEMON_MSG_KICKOFF) {
|
||||||
return msg.value;
|
if (wstatus != NULL) {
|
||||||
|
*wstatus = msg.value;
|
||||||
|
}
|
||||||
|
return DAEMON_RET_PARENT_OK;
|
||||||
} else {
|
} else {
|
||||||
goto errout;
|
goto errout;
|
||||||
}
|
}
|
||||||
} while (true);
|
} while (true);
|
||||||
|
|
||||||
|
return DAEMON_RET_ERR;
|
||||||
}
|
}
|
||||||
|
|
||||||
setsid();
|
setsid();
|
||||||
@@ -1782,10 +1787,13 @@ int daemon_run(void)
|
|||||||
close(fds[0]);
|
close(fds[0]);
|
||||||
|
|
||||||
daemon_fd = fds[1];
|
daemon_fd = fds[1];
|
||||||
return -2;
|
return DAEMON_RET_CHILD_OK;
|
||||||
errout:
|
errout:
|
||||||
kill(pid, SIGKILL);
|
kill(pid, SIGKILL);
|
||||||
return -1;
|
if (wstatus != NULL) {
|
||||||
|
*wstatus = -1;
|
||||||
|
}
|
||||||
|
return DAEMON_RET_ERR;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
|
|||||||
@@ -147,7 +147,14 @@ void print_stack(void);
|
|||||||
|
|
||||||
void close_all_fd(int keepfd);
|
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);
|
int daemon_kickoff(int status, int no_close);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user