Add state.[ch]

This commit is contained in:
angt
2016-01-18 15:59:18 +01:00
parent e2b3dc1b46
commit 88f314bc75
4 changed files with 79 additions and 42 deletions

View File

@@ -6,6 +6,7 @@
#include "option.h"
#include "tun.h"
#include "db.h"
#include "state.h"
#include <inttypes.h>
#include <limits.h>
@@ -15,7 +16,6 @@
#include <fcntl.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/stat.h>
#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);

61
src/state.c Normal file
View File

@@ -0,0 +1,61 @@
#include "common.h"
#include "state.h"
#include "str.h"
#include <stdio.h>
#include <fcntl.h>
#include <sys/stat.h>
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");
}
}

4
src/state.h Normal file
View File

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