diff options
author | Laurent Bercot <ska-skaware@skarnet.org> | 2023-07-11 12:26:21 +0000 |
---|---|---|
committer | Laurent Bercot <ska@appnovation.com> | 2023-07-11 12:26:21 +0000 |
commit | 8cf671e973a4ea2ef7c9ca1321531a7ceeaa5073 (patch) | |
tree | a84faead6505a91ce91f2a86f07768e3aa318dda /src | |
parent | bce22be61a55a29084c8c0416be942622d2d8f87 (diff) | |
download | s6-dns-8cf671e973a4ea2ef7c9ca1321531a7ceeaa5073.tar.xz |
Good version of hosts.h
Signed-off-by: Laurent Bercot <ska@appnovation.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/include/s6-dns/hosts.h | 6 | ||||
-rw-r--r-- | src/libs6dns/s6dns_hosts_init.c | 46 |
2 files changed, 31 insertions, 21 deletions
diff --git a/src/include/s6-dns/hosts.h b/src/include/s6-dns/hosts.h index 7c11af0..0f85d0b 100644 --- a/src/include/s6-dns/hosts.h +++ b/src/include/s6-dns/hosts.h @@ -13,8 +13,10 @@ extern cdb s6dns_hosts_here ; extern int s6dns_hosts_compile (int, int) ; -extern int s6dns_hosts_init (cdb *) ; -#define s6dns_hosts_free(c) cdb_free(c) +extern int s6dns_hosts_init_r (cdb *, char const *, char const *, char const *) ; +#define s6dns_hosts_init() s6dns_hosts_init_r(&s6dns_hosts_here, "/etc/hosts", "/etc/hosts.cdb", "/tmp/hosts.cdb") +#define s6dns_hosts_free_r(c) cdb_free(c) +#define s6dns_hosts_free() s6dns_hosts_free_r(&s6dns_hosts_here) diff --git a/src/libs6dns/s6dns_hosts_init.c b/src/libs6dns/s6dns_hosts_init.c index d3cd863..762d738 100644 --- a/src/libs6dns/s6dns_hosts_init.c +++ b/src/libs6dns/s6dns_hosts_init.c @@ -1,5 +1,6 @@ /* ISC license. */ +#include <string.h> #include <unistd.h> #include <errno.h> #include <stdlib.h> @@ -11,16 +12,15 @@ #include <s6-dns/hosts.h> -int s6dns_hosts_init (void) +int s6dns_hosts_init_r (cdb *c, char const *txtfile, char const *cdbfile, char const *tmpprefix) { - int fdr, fdw ; - char tmp[24] = "/tmp/hosts.cdb:XXXXXX" ; - int fdc = openc_read("/etc/hosts.cdb") ; + int fdr ; + int fdc = openc_read(cdbfile) ; if (fdc >= 0) { struct stat stc, str ; if (fstat(fdc, &stc) == -1) goto errc ; - if (stat("/etc/hosts", &str) == -1) + if (stat(txtfile, &str) == -1) { if (errno == ENOENT) goto useit ; else goto errc ; @@ -28,21 +28,29 @@ int s6dns_hosts_init (void) if (stc.st_mtim > str.st_mtim) goto useit ; fd_close(fdc) ; } - fdr = openc_read("/etc/hosts") ; + + fdr = openc_read(txtfile) ; if (fdr == -1) return errno == ENOENT ? (errno = 0, 0) : -1 ; - fdw = mkstemp(tmp) ; - if (fdw == -1) goto errr ; - if (!s6dns_hosts_compile(fdr, fdw)) goto errw ; - if (lseek(fdw, 0, SEEK_SET) == -1) goto errw ; - if (!cdb_init_fromfd(&s6dns_hosts_here, fdw)) goto errw ; - fd_close(fdw) ; - unlink_void(tmp) ; - fd_close(fdr) ; - return 1 ; + { + int fdw ; + size_t len = strlen(tmpprefix) ; + char tmp[len + 8] ; + memcpy(tmp, tmpprefix, len) ; + memcpy(tmp + len, ":XXXXXX", 8) ; + fdw = mkstemp(tmp) ; + if (fdw == -1) goto errr ; + if (!s6dns_hosts_compile(fdr, fdw)) goto errw ; + if (lseek(fdw, 0, SEEK_SET) == -1) goto errw ; + if (!cdb_init_fromfd(c, fdw)) goto errw ; + fd_close(fdw) ; + unlink_void(tmp) ; + fd_close(fdr) ; + return 1 ; - errw: - fd_close(fdw) ; - unlink_void(tmp) ; + errw: + fd_close(fdw) ; + unlink_void(tmp) ; + } errr: fd_close(fdr) ; return -1 ; @@ -52,7 +60,7 @@ int s6dns_hosts_init (void) return -1 ; useit: - if (!cdb_init_fromfd(&s6dns_hosts_here, fdc)) + if (!cdb_init_fromfd(c, fdc)) { fd_close(fdc) ; return 0 ; |