diff --git a/Makefile.am b/Makefile.am index dfb762c..a935081 100644 --- a/Makefile.am +++ b/Makefile.am @@ -16,7 +16,9 @@ glorytun_SOURCES = \ src/tun.c \ src/tun.h \ src/db.c \ - src/db.h + src/db.h \ + src/state.c \ + src/state.h EXTRA_DIST = \ LICENSE \ diff --git a/src/main.c b/src/main.c index cae31b4..a2a4674 100644 --- a/src/main.c +++ b/src/main.c @@ -6,6 +6,7 @@ #include "option.h" #include "tun.h" #include "db.h" +#include "state.h" #include #include @@ -15,7 +16,6 @@ #include #include #include -#include #ifndef __FAVOR_BSD #define __FAVOR_BSD @@ -387,15 +387,6 @@ static ssize_t fd_write (int fd, const void *data, size_t size) return ret; } -static void state_write (int fd, const char *str) -{ - if (fd==-1) { - gt_print("state: %s", str); - } else { - fd_write(fd, str, str_len(str)); - } -} - static size_t fd_read_all (int fd, void *data, size_t size) { size_t done = 0; @@ -1214,34 +1205,13 @@ int main (int argc, char **argv) chdir("/"); } - int state_fd = -1; - - if (statefile) { - state_fd = open(statefile, O_WRONLY); - - if (state_fd==-1) { - if (errno!=EINTR) - perror("open statefile"); - return 1; - } - - struct stat st = {0}; - - if (fstat(state_fd, &st)==-1) { - perror("stat statefile"); - return 1; - } - - if (!S_ISFIFO(st.st_mode)) { - gt_log("`%s' is not a fifo\n", statefile); - return 1; - } - } + if (state_init(statefile)) + return 1; long retry = 0; uint8_t *db = NULL; - state_write(state_fd, "INITIALIZED\n"); + state("INITIALIZED", NULL); while (!gt_close) { if (retry_count>=0 && retry>=retry_count+1) { @@ -1311,7 +1281,7 @@ int main (int argc, char **argv) retry = 0; - state_write(state_fd, "STARTED\n"); + state("STARTED", sockname); fd_set rfds; FD_ZERO(&rfds); @@ -1476,17 +1446,17 @@ int main (int argc, char **argv) } restart: - if (sockname) { - free(sockname); - sockname = NULL; - } - if (sock.fd!=-1) { close(sock.fd); sock.fd = -1; } - state_write(state_fd, "STOPPED\n"); + state("STOPPED", sockname); + + if (sockname) { + free(sockname); + sockname = NULL; + } } freeaddrinfo(ai); diff --git a/src/state.c b/src/state.c new file mode 100644 index 0000000..0a74d69 --- /dev/null +++ b/src/state.c @@ -0,0 +1,61 @@ +#include "common.h" + +#include "state.h" +#include "str.h" + +#include +#include +#include + +static int state_fd = -1; + +int state_init (const char *filename) +{ + if (str_empty(filename)) + return 0; + + state_fd = open(filename, O_WRONLY); + + if (state_fd==-1) { + if (errno!=EINTR) + perror("open"); + return -1; + } + + struct stat st = {0}; + + if (fstat(state_fd, &st)==-1) { + perror("fstat"); + close(state_fd); + state_fd = -1; + return -1; + } + + if (!S_ISFIFO(st.st_mode)) { + gt_log("`%s' is not a fifo\n", filename); + close(state_fd); + state_fd = -1; + return -1; + } + + return 0; +} + +void state (const char *state, const char *info) +{ + if (str_empty(state)) + return; + + const char *strs[] = { state, " ", info, "\n" }; + char *str = str_cat(strs, COUNT(strs)); + + if (!str) + return; + + if (state_fd==-1) { + gt_print("%s", str); + } else { + if (write(state_fd, str, str_len(str))==-1 && errno!=EINTR) + perror("write"); + } +} diff --git a/src/state.h b/src/state.h new file mode 100644 index 0000000..a5f2583 --- /dev/null +++ b/src/state.h @@ -0,0 +1,4 @@ +#pragma once + +int state_init (const char *); +void state (const char *, const char *);