summaryrefslogtreecommitdiff
path: root/src/libs6rc
diff options
context:
space:
mode:
Diffstat (limited to 'src/libs6rc')
-rw-r--r--src/libs6rc/s6rc_db_init.c42
-rw-r--r--src/libs6rc/s6rc_service_recheck_instance.c16
-rw-r--r--src/libs6rc/s6rc_service_resolve.c6
3 files changed, 26 insertions, 38 deletions
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 ;
}