From 587ca337782bafe84db03fcdc306038a61f0d74a Mon Sep 17 00:00:00 2001 From: Laurent Bercot Date: Sat, 24 Jul 2021 17:24:27 +0000 Subject: Adapt to new cdb interface Signed-off-by: Laurent Bercot --- src/include/s6-rc/db.h | 4 +-- src/libs6rc/s6rc_db_init.c | 42 +++++++++++------------------ src/libs6rc/s6rc_service_recheck_instance.c | 16 +++++------ src/libs6rc/s6rc_service_resolve.c | 6 ++--- 4 files changed, 28 insertions(+), 40 deletions(-) diff --git a/src/include/s6-rc/db.h b/src/include/s6-rc/db.h index 713b76b..7e1ee07 100644 --- a/src/include/s6-rc/db.h +++ b/src/include/s6-rc/db.h @@ -104,9 +104,9 @@ extern void s6rc_service_typenum (uin32_t const *, uint32_t, uint8_t *, uint32_t extern uint8_t s6rc_service_type (uint32_t const *, uint32_t) ; extern uint32_t s6rc_service_num (uint32_t const *, uint32_t) ; -extern int s6rc_service_resolve (cdb_t *, char const *, uint32_t *, char const **) ; +extern int s6rc_service_resolve (s6rc_db_t const *, char const *, uint32_t *, char const **) ; extern s6rc_common_t const *s6rc_service_common (s6rc_db_t const *, uint32_t) ; extern s6rc_common_t const *s6rc_service_common_tn (s6rc_db_t const *, uint8_t, uint32_t) ; -extern int s6rc_service_recheck_instance (s6rc_db_t const *, cdb_t *, uint32_t *, char const **) ; +extern int s6rc_service_recheck_instance (s6rc_db_t const *, uint32_t *, char const **) ; #endif diff --git a/src/libs6rc/s6rc_db_init.c b/src/libs6rc/s6rc_db_init.c index 075d0c3..40c8dcc 100644 --- a/src/libs6rc/s6rc_db_init.c +++ b/src/libs6rc/s6rc_db_init.c @@ -31,12 +31,11 @@ int s6rc_db_init (s6rc_db_t *db, char const *dir) size_t len = strlen(dir) ; buffer b ; int fd ; + int dfd = openc_read(dir) ; char buf[4096] ; - char fn[len + 13] ; - memcpy(fn, dir, len) ; - memcpy(fn + len, "/db_nomap", 10) ; - fd = openc_read(fn) ; - if (fd == -1) return 0 ; + if (dfd == -1) return 0 ; + fd = openc_readat(dfd, "db_nomap") ; + if (fd == -1) goto errd ; buffer_init(&b, &buffer_read, fd, buf, 4096) ; { uint32_t canary ; @@ -61,16 +60,14 @@ int s6rc_db_init (s6rc_db_t *db, char const *dir) } fd_close(fd) ; sha256_update(&ctx, (char *)argvs, nargv * 4) ; - memcpy(fn + len, "/db", 4) ; { struct stat st ; void *map ; - fd = openc_read(fn) ; - if (fd == -1) return 0 ; + fd = openc_readat(dfd, "db") ; + if (fd == -1) goto errd ; if (fstat(fd, &st) == -1) goto err0 ; - if (!S_ISREG(st.st_mode)) goto eproto0 ; - if (st.st_size < 8 * S6RC_STYPE_N) goto eproto0 ; + if (!S_ISREG(st.st_mode) || st.st_size < 8 * S6RC_STYPE_N) goto eproto0 ; map = mmap(0, st.st_size, PROT_READ, MAP_SHARED, fd, 0) ; if (map == MAP_FAILED) goto err0 ; fd_close(fd) ; @@ -83,18 +80,13 @@ int s6rc_db_init (s6rc_db_t *db, char const *dir) errno = EPROTO ; goto err1 ; } - - memcpy(fn + len, "/resolve.cdb", 13) ; - if (!cdb_init(&db->resolve, fn)) goto err1 ; - + if (!cdb_init_at(&db->resolve, dfd, "resolve.cdb")) goto err1 ; { - ssize_t r ; - memcpy(fn + len, "/hash", 6) ; - r = openreadnclose(fn, buf+32, 33) ; + ssize_t r = openreadnclose_at(dfd, "hash", buf+32, 33) ; if (r == 33) goto eproto2 ; if (r < 32) goto err2 ; sha256_update(&ctx, db->map, db->size) ; - sha256_update(&ctx, c->map, c->size) ; + sha256_update(&ctx, db->resolve.map, db->resolve.size) ; sha256_final(&ctx, buf) ; if (memcmp(buf, buf+32, 32)) goto eproto2 ; } @@ -117,6 +109,7 @@ int s6rc_db_init (s6rc_db_t *db, char const *dir) for (uint32_t i = 0 ; i < nargv ; i++) db->argvs[i] = argvs[i] ? db->storage + argvs[i] : 0 ; } + fd_close(dfd) ; return 1 ; eproto2: @@ -124,17 +117,14 @@ int s6rc_db_init (s6rc_db_t *db, char const *dir) err2: cdb_free(&db->resolve) ; err1: - { - int e = errno ; - munmap(db->map, db->size) ; - errno = e ; - } + munmap_void(db->map, db->size) ; db->map = 0 ; - return 0 ; - + goto errd ; eproto0: - errno = EPROTO ; + errno = EPROTO ; err0: fd_close(fd) ; + errd: + fd_close(dfd) ; return 0 ; } diff --git a/src/libs6rc/s6rc_service_recheck_instance.c b/src/libs6rc/s6rc_service_recheck_instance.c index e8cf42c..1c476f7 100644 --- a/src/libs6rc/s6rc_service_recheck_instance.c +++ b/src/libs6rc/s6rc_service_recheck_instance.c @@ -9,25 +9,25 @@ int s6rc_service_recheck_instance (s6rc_db_t const *db, uint32_t *id, char const **param) { - uint8_t type ; uint32_t num ; + uint8_t type ; s6rc_service_typenum(db->n, *id) ; - if (type < S6RC_STYPE_N) return 0 ; + if (type >= S6RC_STYPE_N) { - cdb_reader reader = CDB_READER_ZERO ; - cdb_data data ; - s6rc_common_t *common = s6rc_service_common(db->n, type, num) ; + s6rc_common_t const *common = s6rc_service_common(db->n, type, num) ; size_t namelen = strlen(db->storage + common->name) ; size_t paramlen = strlen(*param) ; + cdb_data data ; int r ; char tmp[namelen + paramlen] ; memcpy(tmp, db->storage + common->name, namelen) ; memcpy(tmp + namelen, *param, paramlen) ; - r = cdb_find(c, &reader, &data, tmp, namelen + paramlen) ; + r = cdb_find(c, &data, tmp, namelen + paramlen) ; if (r <= 0) return r ; if (data.len != 4) return -1 ; uint32_unpack_big(data.s, id) ; + *param = 0 ; + return 1 ; } - *param = 0 ; - return 1 ; + else return 0 ; } diff --git a/src/libs6rc/s6rc_service_resolve.c b/src/libs6rc/s6rc_service_resolve.c index 6868e6b..0bdf670 100644 --- a/src/libs6rc/s6rc_service_resolve.c +++ b/src/libs6rc/s6rc_service_resolve.c @@ -12,17 +12,15 @@ int s6rc_service_resolve (s6rc_db_t const *db, char const *s, uint32_t *id, char { size_t len = strlen(s) ; char const *p = 0 ; - cdb_reader reader = CDB_READER_ZERO ; cdb_data data ; - int r = cdb_find(c, &reader, &data, s, len) ; + int r = cdb_find(c, &data, s, len) ; if (r < 0) return r ; else if (!r) { size_t at = byte_chr(s, len, '@') ; if (at == len) return 0 ; if (at == len - 1) return -1 ; - cdb_findstart(&reader) ; - r = cdb_find(c, &reader, &data, s, at + 1) ; + r = cdb_find(c, &data, s, at + 1) ; if (r <= 0) return r ; p = s + at + 1 ; } -- cgit v1.2.3