summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLaurent Bercot <ska-skaware@skarnet.org>2020-12-29 23:07:18 +0000
committerLaurent Bercot <ska-skaware@skarnet.org>2020-12-29 23:07:18 +0000
commite5f5b81d65a5ed9b34910a58707b870d677c6183 (patch)
tree6e87cfda96222560751e200ca5ef7dd8d7cd65f4
parent5b063a4db3e631d40c05b77ecc7554ebdf5fff92 (diff)
downloads6-dns-e5f5b81d65a5ed9b34910a58707b870d677c6183.tar.xz
Refactor libdcache, expose dcache_clean_expired()
-rw-r--r--package/deps.mak13
-rw-r--r--src/caches/dcache-internal.h18
-rw-r--r--src/caches/dcache_add.c28
-rw-r--r--src/caches/dcache_clean_expired.c20
-rw-r--r--src/caches/dcache_delete.c19
-rw-r--r--src/caches/dcache_free.c3
-rw-r--r--src/caches/dcache_save.c3
-rw-r--r--src/caches/dcache_search.c4
-rw-r--r--src/caches/deps-lib/dcache2
-rw-r--r--src/include/s6-dns/dcache.h2
10 files changed, 75 insertions, 37 deletions
diff --git a/package/deps.mak b/package/deps.mak
index c6c0b92..eb634f0 100644
--- a/package/deps.mak
+++ b/package/deps.mak
@@ -12,13 +12,16 @@ src/include/s6-dns/s6dns-rci.h: src/include/s6-dns/s6dns-constants.h src/include
src/include/s6-dns/s6dns-resolve.h: src/include/s6-dns/s6dns-constants.h src/include/s6-dns/s6dns-domain.h src/include/s6-dns/s6dns-engine.h src/include/s6-dns/s6dns-ip46.h src/include/s6-dns/s6dns-message.h src/include/s6-dns/s6dns-rci.h
src/include/s6-dns/s6dns.h: src/include/s6-dns/s6dns-constants.h src/include/s6-dns/s6dns-domain.h src/include/s6-dns/s6dns-engine.h src/include/s6-dns/s6dns-fmt.h src/include/s6-dns/s6dns-ip46.h src/include/s6-dns/s6dns-message.h src/include/s6-dns/s6dns-rci.h src/include/s6-dns/s6dns-resolve.h
src/include/s6-dns/skadns.h: src/include/s6-dns/config.h src/include/s6-dns/s6dns-domain.h
+src/caches/dcache-internal.h: src/include/s6-dns/dcache.h
src/clients/s6dns-generic-filter.h: src/include/s6-dns/s6dns-domain.h
-src/caches/dcache_add.o src/caches/dcache_add.lo: src/caches/dcache_add.c src/include/s6-dns/dcache.h
+src/caches/dcache_add.o src/caches/dcache_add.lo: src/caches/dcache_add.c src/caches/dcache-internal.h src/include/s6-dns/dcache.h
+src/caches/dcache_clean_expired.o src/caches/dcache_clean_expired.lo: src/caches/dcache_clean_expired.c src/caches/dcache-internal.h src/include/s6-dns/dcache.h
+src/caches/dcache_delete.o src/caches/dcache_delete.lo: src/caches/dcache_delete.c src/caches/dcache-internal.h src/include/s6-dns/dcache.h
src/caches/dcache_free.o src/caches/dcache_free.lo: src/caches/dcache_free.c src/include/s6-dns/dcache.h
src/caches/dcache_init.o src/caches/dcache_init.lo: src/caches/dcache_init.c src/include/s6-dns/dcache.h
src/caches/dcache_load.o src/caches/dcache_load.lo: src/caches/dcache_load.c src/include/s6-dns/dcache.h
src/caches/dcache_save.o src/caches/dcache_save.lo: src/caches/dcache_save.c src/include/s6-dns/dcache.h
-src/caches/dcache_search.o src/caches/dcache_search.lo: src/caches/dcache_search.c src/include/s6-dns/dcache.h
+src/caches/dcache_search.o src/caches/dcache_search.lo: src/caches/dcache_search.c src/caches/dcache-internal.h src/include/s6-dns/dcache.h
src/caches/shibari.o src/caches/shibari.lo: src/caches/shibari.c src/include/s6-dns/s6dns.h
src/caches/shibari_whitelist_add6.o src/caches/shibari_whitelist_add6.lo: src/caches/shibari_whitelist_add6.c src/caches/shibari-internal.h
src/caches/shibari_whitelist_ip4_match.o src/caches/shibari_whitelist_ip4_match.lo: src/caches/shibari_whitelist_ip4_match.c src/caches/shibari-internal.h
@@ -150,12 +153,12 @@ src/skadns/skadns_zero.o src/skadns/skadns_zero.lo: src/skadns/skadns_zero.c src
src/skadns/skadnsd.o src/skadns/skadnsd.lo: src/skadns/skadnsd.c src/include/s6-dns/s6dns.h src/include/s6-dns/skadns.h
ifeq ($(strip $(STATIC_LIBS_ARE_PIC)),)
-libdcache.a.xyzzy: src/caches/dcache_add.o src/caches/dcache_free.o src/caches/dcache_init.o src/caches/dcache_load.o src/caches/dcache_save.o src/caches/dcache_search.o
+libdcache.a.xyzzy: src/caches/dcache_add.o src/caches/dcache_clean_expired.o src/caches/dcache_delete.o src/caches/dcache_free.o src/caches/dcache_init.o src/caches/dcache_load.o src/caches/dcache_save.o src/caches/dcache_search.o
else
-libdcache.a.xyzzy: src/caches/dcache_add.lo src/caches/dcache_free.lo src/caches/dcache_init.lo src/caches/dcache_load.lo src/caches/dcache_save.lo src/caches/dcache_search.lo
+libdcache.a.xyzzy: src/caches/dcache_add.lo src/caches/dcache_clean_expired.lo src/caches/dcache_delete.lo src/caches/dcache_free.lo src/caches/dcache_init.lo src/caches/dcache_load.lo src/caches/dcache_save.lo src/caches/dcache_search.lo
endif
libdcache.so.xyzzy: EXTRA_LIBS :=
-libdcache.so.xyzzy: src/caches/dcache_add.lo src/caches/dcache_free.lo src/caches/dcache_init.lo src/caches/dcache_load.lo src/caches/dcache_save.lo src/caches/dcache_search.lo
+libdcache.so.xyzzy: src/caches/dcache_add.lo src/caches/dcache_clean_expired.lo src/caches/dcache_delete.lo src/caches/dcache_free.lo src/caches/dcache_init.lo src/caches/dcache_load.lo src/caches/dcache_save.lo src/caches/dcache_search.lo
ifeq ($(strip $(STATIC_LIBS_ARE_PIC)),)
libshibari.a.xyzzy: src/caches/shibari_whitelist_add6.o src/caches/shibari_whitelist_ip4_match.o src/caches/shibari_whitelist_ip6_match.o src/caches/shibari_whitelist_read.o
else
diff --git a/src/caches/dcache-internal.h b/src/caches/dcache-internal.h
new file mode 100644
index 0000000..4fdfa4c
--- /dev/null
+++ b/src/caches/dcache-internal.h
@@ -0,0 +1,18 @@
+/* ISC license. */
+
+#ifndef S6DNS_DCACHE_INTERNAL_H
+#define S6DNS_DCACHE_INTERNAL_H
+
+#include <stdint.h>
+
+#include <skalibs/avlnode.h>
+#include <skalibs/gensetdyn.h>
+
+#include <s6-dns/dcache.h>
+
+#define DNODE(z, i) GENSETDYN_P(dcache_node_t, &(z)->storage, i)
+#define DCACHE_NODE_OVERHEAD (32 + sizeof(dcache_node_t) + 3 * sizeof(avlnode))
+
+extern void dcache_delete (dcache_t *, uint32_t) ;
+
+#endif
diff --git a/src/caches/dcache_add.c b/src/caches/dcache_add.c
index bbe5fea..b69b1ca 100644
--- a/src/caches/dcache_add.c
+++ b/src/caches/dcache_add.c
@@ -12,9 +12,7 @@
#include <skalibs/avltree.h>
#include <s6-dns/dcache.h>
-
-#define DNODE(z, i) GENSETDYN_P(dcache_node_t, &(z)->storage, i)
-#define DCACHE_NODE_OVERHEAD (32 + sizeof(dcache_node_t) + 3 * sizeof(avlnode))
+#include "dcache-internal.h"
static void uniquify (avltree const *tree, tain_t *stamp)
{
@@ -24,17 +22,6 @@ static void uniquify (avltree const *tree, tain_t *stamp)
tain_add(stamp, stamp, &nano) ;
}
-static void dcache_delete (dcache_t *z, uint32_t i)
-{
- dcache_node_t *y = DNODE(z, i) ;
- avltree_delete(&z->by_expire, &y->expire) ;
- avltree_delete(&z->by_entry, &y->entry) ;
- avltree_delete(&z->by_key, &y->key) ;
- alloc_free(y->key.s) ;
- z->size -= DCACHE_NODE_OVERHEAD + y->key.len + y->datalen ;
- gensetdyn_delete(&z->storage, i) ;
-}
-
static inline void dcache_gc_by_entry (dcache_t *z, uint64_t max)
{
while (z->size > max)
@@ -45,17 +32,6 @@ static inline void dcache_gc_by_entry (dcache_t *z, uint64_t max)
}
}
-static inline void dcache_gc_by_expire (dcache_t *z, tain_t const *stamp)
-{
- for (;;)
- {
- uint32_t i ;
- if (!avltree_min(&z->by_expire, &i)) break ;
- if (tain_less(stamp, &DNODE(z, i)->expire)) break ;
- dcache_delete(z, i) ;
- }
-}
-
static inline int dcache_add_node (dcache_t *z, dcache_node_t const *node)
{
uint32_t i ;
@@ -104,7 +80,7 @@ int dcache_add (dcache_t *z, uint64_t max, char const *key, uint16_t keylen, cha
{
uint64_t size = DCACHE_NODE_OVERHEAD + keylen + datalen ;
if (size > max) return (errno = EINVAL, 0) ;
- if (z->size > max - size) dcache_gc_by_expire(z, stamp) ;
+ if (z->size > max - size) dcache_clean_expired(z, stamp) ;
if (z->size > max - size) dcache_gc_by_entry(z, max - size) ;
return dcache_add_unbounded(z, key, keylen, data, datalen, expire, stamp) ;
}
diff --git a/src/caches/dcache_clean_expired.c b/src/caches/dcache_clean_expired.c
new file mode 100644
index 0000000..5308d8a
--- /dev/null
+++ b/src/caches/dcache_clean_expired.c
@@ -0,0 +1,20 @@
+/* ISC license. */
+
+#include <stdint.h>
+
+#include <skalibs/tai.h>
+#include <skalibs/avltree.h>
+
+#include <s6-dns/dcache.h>
+#include "dcache-internal.h"
+
+void dcache_clean_expired (dcache_t *z, tain_t const *stamp)
+{
+ for (;;)
+ {
+ uint32_t i ;
+ if (!avltree_min(&z->by_expire, &i)) break ;
+ if (tain_less(stamp, &DNODE(z, i)->expire)) break ;
+ dcache_delete(z, i) ;
+ }
+}
diff --git a/src/caches/dcache_delete.c b/src/caches/dcache_delete.c
new file mode 100644
index 0000000..e0dca4e
--- /dev/null
+++ b/src/caches/dcache_delete.c
@@ -0,0 +1,19 @@
+/* ISC license. */
+
+#include <skalibs/alloc.h>
+#include <skalibs/gensetdyn.h>
+#include <skalibs/avltree.h>
+
+#include <s6-dns/dcache.h>
+#include "dcache-internal.h"
+
+void dcache_delete (dcache_t *z, uint32_t i)
+{
+ dcache_node_t *y = DNODE(z, i) ;
+ avltree_delete(&z->by_expire, &y->expire) ;
+ avltree_delete(&z->by_entry, &y->entry) ;
+ avltree_delete(&z->by_key, &y->key) ;
+ alloc_free(y->key.s) ;
+ z->size -= DCACHE_NODE_OVERHEAD + y->key.len + y->datalen ;
+ gensetdyn_delete(&z->storage, i) ;
+}
diff --git a/src/caches/dcache_free.c b/src/caches/dcache_free.c
index 7937332..1ba8041 100644
--- a/src/caches/dcache_free.c
+++ b/src/caches/dcache_free.c
@@ -6,8 +6,6 @@
#include <s6-dns/dcache.h>
-static dcache_t const dcache_zero = DCACHE_ZERO ;
-
static void dcache_node_free (void *p)
{
alloc_free(((dcache_node_t *)p)->key.s) ;
@@ -15,6 +13,7 @@ static void dcache_node_free (void *p)
void dcache_free (dcache_t *z)
{
+ static dcache_t const dcache_zero = DCACHE_ZERO ;
avltree_free(&z->by_expire) ;
avltree_free(&z->by_entry) ;
avltree_free(&z->by_key) ;
diff --git a/src/caches/dcache_save.c b/src/caches/dcache_save.c
index 1ef35f0..04d1dac 100644
--- a/src/caches/dcache_save.c
+++ b/src/caches/dcache_save.c
@@ -1,6 +1,7 @@
/* ISC license. */
#include <stdint.h>
+#include <unistd.h>
#include <stdio.h>
#include <skalibs/posixplz.h>
@@ -57,7 +58,7 @@ int dcache_save (dcache_t const *z, char const *file)
fd = open_excl(sa.s) ;
if (fd == -1) goto err0 ;
buffer_init(&b, &buffer_write, fd, buf, N) ;
- if (!dcache_save_to_buffer(z, &b)) goto err2 ;
+ if (!dcache_save_to_buffer(z, &b) || fsync(fd) < 0) goto err2 ;
fd_close(fd) ;
if (rename(sa.s, file) == -1) goto err1 ;
stralloc_free(&sa) ;
diff --git a/src/caches/dcache_search.c b/src/caches/dcache_search.c
index 0c1894a..7ca8324 100644
--- a/src/caches/dcache_search.c
+++ b/src/caches/dcache_search.c
@@ -2,12 +2,10 @@
#include <stdint.h>
-#include <skalibs/gensetdyn.h>
#include <skalibs/avltree.h>
#include <s6-dns/dcache.h>
-
-#define DNODE(z, i) GENSETDYN_P(dcache_node_t, &(z)->storage, i)
+#include "dcache-internal.h"
dcache_node_t *dcache_search (dcache_t *z, char const *key, uint16_t keylen)
{
diff --git a/src/caches/deps-lib/dcache b/src/caches/deps-lib/dcache
index d842e17..fcc09a0 100644
--- a/src/caches/deps-lib/dcache
+++ b/src/caches/deps-lib/dcache
@@ -1,4 +1,6 @@
dcache_add.o
+dcache_clean_expired.o
+dcache_delete.o
dcache_free.o
dcache_init.o
dcache_load.o
diff --git a/src/include/s6-dns/dcache.h b/src/include/s6-dns/dcache.h
index 177102e..5209750 100644
--- a/src/include/s6-dns/dcache.h
+++ b/src/include/s6-dns/dcache.h
@@ -44,6 +44,8 @@ extern void dcache_init (dcache_t *, uint64_t) ;
extern dcache_node_t *dcache_search (dcache_t *, char const *, uint16_t) ;
extern int dcache_add (dcache_t *, uint64_t, char const *, uint16_t, char const *, uint16_t, tain_t const *, tain_t const *) ;
#define dcache_add_g(d, max, key, keylen, data, datalen, expire) dcache_add(d, max, key, keylen, data, datalen, (expire), &STAMP)
+extern void dcache_clean_expired (dcache_t *, tain_t const *) ;
+#define dcache_clean_expired_g(d) dcache_clean_expired((d), &STAMP)
extern void dcache_free (dcache_t *) ;
extern int dcache_save (dcache_t const *, char const *) ;