comparison libnumdb2/refresh.c @ 234:44f178901a46

libnumdb2: port refresh.c
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 14 Aug 2023 13:17:07 -0800
parents libnumdb/refresh.c@2cc790b66359
children
comparison
equal deleted inserted replaced
233:60f6e561bfb8 234:44f178901a46
1 /*
2 * Long-running ThemWi daemon processes need to be able to pick up updates
3 * to the number database without being restarted. Whenever they need to
4 * consult the number db when handling a new call setup or equivalent,
5 * they will call refresh_number_db(), which does a stat on the file,
6 * followed by a re-read if the file has changed.
7 */
8
9 #include <sys/types.h>
10 #include <sys/stat.h>
11 #include <stdio.h>
12 #include <stdint.h>
13 #include <stdlib.h>
14 #include <syslog.h>
15 #include "../include/number_db_v2.h"
16
17 extern char numdb_pathname[];
18 extern struct stat numdb_file_stat;
19 extern struct owned_number_rec *numdb_owned_numbers;
20 extern struct short_number_rec *numdb_short_numbers;
21
22 refresh_number_db()
23 {
24 int rc;
25 struct stat st;
26
27 rc = stat(numdb_pathname, &st);
28 if (rc < 0) {
29 syslog(LOG_CRIT, "unable to stat %s for refresh: %m",
30 numdb_pathname);
31 return(-1);
32 }
33 if (st.st_mtime == numdb_file_stat.st_mtime &&
34 st.st_ctime == numdb_file_stat.st_ctime &&
35 st.st_size == numdb_file_stat.st_size)
36 return(0);
37 if (numdb_owned_numbers) {
38 free(numdb_owned_numbers);
39 numdb_owned_numbers = 0;
40 }
41 if (numdb_short_numbers) {
42 free(numdb_short_numbers);
43 numdb_short_numbers = 0;
44 }
45 rc = read_number_db();
46 if (rc < 0) {
47 syslog(LOG_CRIT, "error reading %s on refresh!",
48 numdb_pathname);
49 exit(1);
50 }
51 return(1);
52 }