Skip to content

Commit

Permalink
smartdns: optimize smartdns.c readability
Browse files Browse the repository at this point in the history
  • Loading branch information
pymumu committed Dec 6, 2023
1 parent 9554b3d commit c17f5df
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 19 deletions.
46 changes: 32 additions & 14 deletions src/smartdns.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}

Expand Down Expand Up @@ -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();
Expand Down
16 changes: 12 additions & 4 deletions src/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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();
Expand All @@ -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
Expand Down
9 changes: 8 additions & 1 deletion src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit c17f5df

Please sign in to comment.