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) {
|
||||
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 @@ restart:
|
||||
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();
|
||||
|
||||
16
src/util.c
16
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
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user