Compare commits

..

2 Commits

Author SHA1 Message Date
angt
6f36424d12 Code cleanup 2016-04-26 06:18:12 +00:00
angt
fa9301da16 Show tun_name in all states and fix state_send() 2016-04-26 06:13:14 +00:00
3 changed files with 39 additions and 36 deletions

View File

@@ -51,8 +51,11 @@
#define MPTCP_ENABLED (26)
static struct {
volatile sig_atomic_t quit;
volatile sig_atomic_t info;
long timeout;
int mptcp;
int state_fd;
} gt;
struct fdbuf {
@@ -70,9 +73,6 @@ struct crypto_ctx {
int chacha;
};
volatile sig_atomic_t gt_close = 0;
volatile sig_atomic_t gt_info = 0;
_pure_
static int64_t dt_ms (struct timeval *ta, struct timeval *tb)
{
@@ -365,10 +365,10 @@ static void gt_sa_handler (int sig)
case SIGINT:
case SIGQUIT:
case SIGTERM:
gt_close = 1;
gt.quit = 1;
break;
case SIGUSR1:
gt_info = 1;
gt.info = 1;
break;
}
}
@@ -995,7 +995,7 @@ static int gt_setup_secretkey (struct crypto_ctx *ctx, char *keyfile)
randombytes_buf(ctx->skey, size);
gt_tohex(buf, sizeof(buf), ctx->skey, size);
state("SECRETKEY", buf);
state_send(gt.state_fd, "SECRETKEY", buf);
return 0;
}
@@ -1242,7 +1242,9 @@ int main (int argc, char **argv)
if (!ai)
return 1;
if (state_init(statefile))
gt.state_fd = state_create(statefile);
if (statefile && gt.state_fd==-1)
return 1;
struct fdbuf tun = { .fd = -1 };
@@ -1282,9 +1284,9 @@ int main (int argc, char **argv)
long retry = 0;
uint8_t *db = NULL;
state("INITIALIZED", tun_name);
state_send(gt.state_fd, "INITIALIZED", tun_name);
while (!gt_close) {
while (!gt.quit) {
if (retry_count>=0 && retry>=retry_count+1) {
gt_log("couldn't %s (%d attempt%s)\n", listener?"listen":"connect",
(int)retry, (retry>1)?"s":"");
@@ -1350,7 +1352,7 @@ int main (int argc, char **argv)
retry = 0;
state("STARTED", sockname);
state_send(gt.state_fd, "STARTED", tun_name);
fd_set rfds;
FD_ZERO(&rfds);
@@ -1361,7 +1363,7 @@ int main (int argc, char **argv)
buffer_format(&sock.read);
while (1) {
if _0_(gt_close)
if _0_(gt.quit)
stop_loop |= 1;
if _0_(stop_loop) {
@@ -1415,7 +1417,7 @@ int main (int argc, char **argv)
const ssize_t r = tun_read(tun.fd, tun.read.write, GT_MTU_MAX);
if (r<=0) {
gt_close |= !r;
gt.quit |= !r;
break;
}
@@ -1511,7 +1513,7 @@ int main (int argc, char **argv)
if (r==ic.size)
tun.write.read += r;
} else {
gt_close |= !r;
gt.quit |= !r;
break;
}
}
@@ -1523,7 +1525,7 @@ int main (int argc, char **argv)
sock.fd = -1;
}
state("STOPPED", sockname);
state_send(gt.state_fd, "STOPPED", tun_name);
if (sockname) {
free(sockname);

View File

@@ -7,16 +7,14 @@
#include <fcntl.h>
#include <sys/stat.h>
static int state_fd = -1;
int state_init (const char *filename)
int state_create (const char *filename)
{
if (str_empty(filename))
return 0;
return -1;
state_fd = open(filename, O_WRONLY);
int fd = open(filename, O_WRONLY);
if (state_fd==-1) {
if (fd==-1) {
if (errno!=EINTR)
perror("open");
return -1;
@@ -24,38 +22,41 @@ int state_init (const char *filename)
struct stat st = {0};
if (fstat(state_fd, &st)==-1) {
if (fstat(fd, &st)==-1) {
perror("fstat");
close(state_fd);
state_fd = -1;
close(fd);
return -1;
}
if (!S_ISFIFO(st.st_mode)) {
gt_log("`%s' is not a fifo\n", filename);
close(state_fd);
state_fd = -1;
close(fd);
return -1;
}
return 0;
return fd;
}
void state (const char *state, const char *info)
void state_send (int fd, const char *state, const char *info)
{
if (str_empty(state))
return;
if (fd==-1) {
gt_print("%s %s\n", state, info);
return;
}
const char *strs[] = { state, " ", info, "\n" };
char *str = str_cat(strs, COUNT(strs));
if (!str)
if (!str) {
perror("str_cat");
return;
if (state_fd==-1) {
gt_print("%s", str);
} else {
if (write(state_fd, str, str_len(str))==-1 && errno!=EINTR)
perror("write");
}
if (write(fd, str, str_len(str))==-1 && errno!=EINTR)
perror("write");
free(str);
}

View File

@@ -1,4 +1,4 @@
#pragma once
int state_init (const char *);
void state (const char *, const char *);
int state_create (const char *);
void state_send (int, const char *, const char *);