Add state.[ch]
This commit is contained in:
@@ -16,7 +16,9 @@ glorytun_SOURCES = \
|
|||||||
src/tun.c \
|
src/tun.c \
|
||||||
src/tun.h \
|
src/tun.h \
|
||||||
src/db.c \
|
src/db.c \
|
||||||
src/db.h
|
src/db.h \
|
||||||
|
src/state.c \
|
||||||
|
src/state.h
|
||||||
|
|
||||||
EXTRA_DIST = \
|
EXTRA_DIST = \
|
||||||
LICENSE \
|
LICENSE \
|
||||||
|
|||||||
52
src/main.c
52
src/main.c
@@ -6,6 +6,7 @@
|
|||||||
#include "option.h"
|
#include "option.h"
|
||||||
#include "tun.h"
|
#include "tun.h"
|
||||||
#include "db.h"
|
#include "db.h"
|
||||||
|
#include "state.h"
|
||||||
|
|
||||||
#include <inttypes.h>
|
#include <inttypes.h>
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
@@ -15,7 +16,6 @@
|
|||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <sys/time.h>
|
#include <sys/time.h>
|
||||||
#include <sys/stat.h>
|
|
||||||
|
|
||||||
#ifndef __FAVOR_BSD
|
#ifndef __FAVOR_BSD
|
||||||
#define __FAVOR_BSD
|
#define __FAVOR_BSD
|
||||||
@@ -387,15 +387,6 @@ static ssize_t fd_write (int fd, const void *data, size_t size)
|
|||||||
return ret;
|
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)
|
static size_t fd_read_all (int fd, void *data, size_t size)
|
||||||
{
|
{
|
||||||
size_t done = 0;
|
size_t done = 0;
|
||||||
@@ -1214,34 +1205,13 @@ int main (int argc, char **argv)
|
|||||||
chdir("/");
|
chdir("/");
|
||||||
}
|
}
|
||||||
|
|
||||||
int state_fd = -1;
|
if (state_init(statefile))
|
||||||
|
return 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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
long retry = 0;
|
long retry = 0;
|
||||||
uint8_t *db = NULL;
|
uint8_t *db = NULL;
|
||||||
|
|
||||||
state_write(state_fd, "INITIALIZED\n");
|
state("INITIALIZED", NULL);
|
||||||
|
|
||||||
while (!gt_close) {
|
while (!gt_close) {
|
||||||
if (retry_count>=0 && retry>=retry_count+1) {
|
if (retry_count>=0 && retry>=retry_count+1) {
|
||||||
@@ -1311,7 +1281,7 @@ int main (int argc, char **argv)
|
|||||||
|
|
||||||
retry = 0;
|
retry = 0;
|
||||||
|
|
||||||
state_write(state_fd, "STARTED\n");
|
state("STARTED", sockname);
|
||||||
|
|
||||||
fd_set rfds;
|
fd_set rfds;
|
||||||
FD_ZERO(&rfds);
|
FD_ZERO(&rfds);
|
||||||
@@ -1476,17 +1446,17 @@ int main (int argc, char **argv)
|
|||||||
}
|
}
|
||||||
|
|
||||||
restart:
|
restart:
|
||||||
if (sockname) {
|
|
||||||
free(sockname);
|
|
||||||
sockname = NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (sock.fd!=-1) {
|
if (sock.fd!=-1) {
|
||||||
close(sock.fd);
|
close(sock.fd);
|
||||||
sock.fd = -1;
|
sock.fd = -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
state_write(state_fd, "STOPPED\n");
|
state("STOPPED", sockname);
|
||||||
|
|
||||||
|
if (sockname) {
|
||||||
|
free(sockname);
|
||||||
|
sockname = NULL;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
freeaddrinfo(ai);
|
freeaddrinfo(ai);
|
||||||
|
|||||||
61
src/state.c
Normal file
61
src/state.c
Normal 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
4
src/state.h
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
int state_init (const char *);
|
||||||
|
void state (const char *, const char *);
|
||||||
Reference in New Issue
Block a user