changeset 9:2cc790b66359

libnumdb: implement refresh
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 26 Jun 2022 13:24:47 -0800
parents ffd48df829a7
children 960ffce6c542
files libnumdb/Makefile libnumdb/refresh.c
diffstat 2 files changed, 53 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/libnumdb/Makefile	Sun Jun 26 13:07:33 2022 -0800
+++ b/libnumdb/Makefile	Sun Jun 26 13:24:47 2022 -0800
@@ -1,6 +1,6 @@
 CC=	gcc
 CFLAGS=	-O2
-OBJS=	readbin.o
+OBJS=	readbin.o refresh.o
 LIB=	libnumdb.a
 
 all:	${LIB}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libnumdb/refresh.c	Sun Jun 26 13:24:47 2022 -0800
@@ -0,0 +1,52 @@
+/*
+ * Long-running ThemWi daemon processes need to be able to pick up updates
+ * to the number database without being restarted.  Whenever they need to
+ * consult the number db when handling a new call setup or equivalent,
+ * they will call refresh_number_db(), which does a stat on the file,
+ * followed by a re-read if the file has changed.
+ */
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <syslog.h>
+#include "../include/number_db_file.h"
+
+extern char numdb_pathname[];
+extern struct stat numdb_file_stat;
+extern uint64_t *numdb_owned_numbers;
+extern struct short_number_map *numdb_short_numbers;
+
+refresh_number_db()
+{
+	int rc;
+	struct stat st;
+
+	rc = stat(numdb_pathname, &st);
+	if (rc < 0) {
+		syslog(LOG_CRIT, "unable to stat %s for refresh: %m",
+			numdb_pathname);
+		return(-1);
+	}
+	if (st.st_mtime == numdb_file_stat.st_mtime &&
+	    st.st_ctime == numdb_file_stat.st_ctime &&
+	    st.st_size  == numdb_file_stat.st_size)
+		return(0);
+	if (numdb_owned_numbers) {
+		free(numdb_owned_numbers);
+		numdb_owned_numbers = 0;
+	}
+	if (numdb_short_numbers) {
+		free(numdb_short_numbers);
+		numdb_short_numbers = 0;
+	}
+	rc = read_number_db();
+	if (rc < 0) {
+		syslog(LOG_CRIT, "error reading %s on refresh!",
+			numdb_pathname);
+		exit(1);
+	}
+	return(1);
+}