Show tun_name in all states and fix state_send()

This commit is contained in:
angt
2016-04-26 06:13:14 +00:00
parent c154a00358
commit fa9301da16
3 changed files with 31 additions and 27 deletions

View File

@@ -53,6 +53,7 @@
static struct {
long timeout;
int mptcp;
int state_fd;
} gt;
struct fdbuf {
@@ -995,7 +996,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 +1243,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,7 +1285,7 @@ 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) {
if (retry_count>=0 && retry>=retry_count+1) {
@@ -1350,7 +1353,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);
@@ -1523,7 +1526,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 *);