summaryrefslogtreecommitdiff
path: root/server
diff options
context:
space:
mode:
authortomsmeding <tom.smeding@gmail.com>2017-10-11 22:28:00 +0200
committertomsmeding <tom.smeding@gmail.com>2017-10-11 22:28:00 +0200
commit54d371e4e897fa58c316fcc3b6dc7bde5af6868c (patch)
treed44b2cde84cf59ddf8f34fe2d59031dd3d46214f /server
parent34b0e1a8adf19f695a85280ebb69b062d71b48f7 (diff)
Diffstat (limited to 'server')
-rw-r--r--server/data_stream.c23
-rw-r--r--server/main.c7
2 files changed, 19 insertions, 11 deletions
diff --git a/server/data_stream.c b/server/data_stream.c
index 3d25104..2719d2a 100644
--- a/server/data_stream.c
+++ b/server/data_stream.c
@@ -5,10 +5,13 @@
#include <sys/time.h>
#include <libproc.h>
#include "data_stream.h"
+#include "util.h"
struct data_stream {
int fd;
+ FILE *mirrorfile;
+
i64 pidbufsize;
pid_t *pids;
i64 namebufsize;
@@ -21,11 +24,11 @@ struct data_stream {
static bool callback_error=false;
-static void write_data_fd(u8 *data,i64 len,void *fdp){
- int fd=*(int*)fdp;
+static void write_data_callback(u8 *data,i64 len,void *streamp){
+ struct data_stream *s=(struct data_stream*)streamp;
i64 cursor=0;
while(cursor<len){
- i64 nwr=write(fd,data+cursor,len-cursor);
+ i64 nwr=write(s->fd,data+cursor,len-cursor);
if(nwr<0){
perror("write");
callback_error=true;
@@ -33,7 +36,8 @@ static void write_data_fd(u8 *data,i64 len,void *fdp){
}
cursor+=nwr;
}
- fprintf(stderr,"write_data_fd(len=%" PRIi64 ")\n",len);
+ fwrite(data,1,len,s->mirrorfile);
+ fprintf(stderr,"write_data_callback(len=%" PRIi64 ")\n",len);
}
static void bufappend(u8 **buf,i64 *cursor,i64 *cap,u8 *data,i64 datalen){
@@ -57,6 +61,14 @@ static void serialise4(u8 *buf,i32 value){
struct data_stream* data_stream_init(int fd){
struct data_stream *s=malloc(1,struct data_stream);
s->fd=fd;
+ char fnamebuf[128];
+ snprintf(fnamebuf,sizeof fnamebuf,"logmirror_%" PRIi64 ".zz",make_timestamp());
+ printf("Using log mirror file '%s'\n",fnamebuf);
+ s->mirrorfile=fopen(fnamebuf,"w");
+ if(s->mirrorfile==NULL){
+ fprintf(stderr,"Cannot open log mirror!\n");
+ exit(1);
+ }
s->pidbufsize=1024;
s->pids=malloc(s->pidbufsize,pid_t);
s->namebufsize=64;
@@ -64,7 +76,7 @@ struct data_stream* data_stream_init(int fd){
s->writebufsize=4096;
s->writebuf=malloc(s->writebufsize,u8);
s->cursor=0;
- s->z=zbuffer_init(write_data_fd,&s->fd);
+ s->z=zbuffer_init(write_data_callback,s);
callback_error=false;
@@ -74,6 +86,7 @@ struct data_stream* data_stream_init(int fd){
int data_stream_finish_destroy(struct data_stream *s){
callback_error=false;
zbuffer_finish_destroy(s->z);
+ fclose(s->mirrorfile);
free(s->pids);
free(s->namebuf);
free(s->writebuf);
diff --git a/server/main.c b/server/main.c
index ffa16cd..cb4e2b4 100644
--- a/server/main.c
+++ b/server/main.c
@@ -12,6 +12,7 @@
#include "data_stream.h"
#include "line_reader.h"
#include "zbuffer.h"
+#include "util.h"
#define PORT 57575
@@ -38,12 +39,6 @@
int32_t pti_priority; task priority
};*/
-static i64 make_timestamp(void){
- struct timeval tv;
- gettimeofday(&tv,NULL);
- return tv.tv_sec*(i64)1000000+tv.tv_usec;
-}
-
static void connection_handler(int sock){
struct data_stream *stream=data_stream_init(sock);
struct line_reader *reader=line_reader_init(sock);