From cbbf16a1a15fec835fe681459af29af036c18935 Mon Sep 17 00:00:00 2001 From: Jan-Piet Mens Date: Sat, 15 Aug 2015 16:27:19 +0200 Subject: [PATCH] use safewrite() for monitor hook file so that it cannot disappear temporarily --- Makefile | 12 ++++++------ misc.c | 11 ++++++----- safewrite.h | 3 +++ 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/Makefile b/Makefile index c432250..45f43b6 100644 --- a/Makefile +++ b/Makefile @@ -17,14 +17,14 @@ ot-reader: ot-reader.c json.o utstring.h ghash.o mkpath.o jget.o ot-recorder: ot-recorder.c json.o utarray.h utstring.h geo.o geohash.o mkpath.o file.o safewrite.o base64.o ghash.o config.h udata.h misc.o $(CC) $(CFLAGS) ot-recorder.c -o ot-recorder json.o geo.o geohash.o mkpath.o file.o safewrite.o base64.o ghash.o misc.o $(LIBS) -geo.o: geo.h geo.c udata.h -geohash.o: geohash.h geohash.c udata.h -file.o: file.h file.c config.h misc.h +geo.o: geo.h geo.c udata.h Makefile config.mk +geohash.o: geohash.h geohash.c udata.h Makefile config.mk +file.o: file.h file.c config.h misc.h Makefile config.mk base64.o: base64.h base64.c -ghash.o: ghash.h ghash.c config.h udata.h misc.h +ghash.o: ghash.h ghash.c config.h udata.h misc.h Makefile config.mk safewrite.o: safewrite.h safewrite.c -jget.o: jget.c jget.h json.h -misc.o: misc.c misc.h udata.h +jget.o: jget.c jget.h json.h Makefile config.mk +misc.o: misc.c misc.h udata.h Makefile config.mk clean: rm -f *.o diff --git a/misc.c b/misc.c index 84af322..a1510e2 100644 --- a/misc.c +++ b/misc.c @@ -4,6 +4,7 @@ #include "ctype.h" #include "udata.h" #include "misc.h" +#include "safewrite.h" char *bindump(char *buf, long buflen) { @@ -41,13 +42,13 @@ void monitorhook(struct udata *userdata, time_t now, char *topic) #else if (ud->usefiles) { char mpath[BUFSIZ]; - FILE *fp; + static UT_string *us = NULL; + + utstring_renew(us); + utstring_printf(us, "%ld %s\n", now, topic); sprintf(mpath, "%s/monitor", JSONDIR); - if ((fp = fopen(mpath, "w")) != NULL) { - fprintf(fp, "%ld %s\n", now, topic); - fclose(fp); - } + safewrite(mpath, utstring_body(us)); } #endif } diff --git a/safewrite.h b/safewrite.h index e5c73e0..a7b6e5d 100644 --- a/safewrite.h +++ b/safewrite.h @@ -1,3 +1,6 @@ +#ifndef SAFEWRITE_H_INCLUDED +# define SAFEWRITE_H_INCLUDED int safewrite(char *filename, char *buf); +#endif