diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/cron/rdcron.c | 14 | ||||
-rw-r--r-- | lib/include/libcfg.h | 12 | ||||
-rw-r--r-- | lib/init/rdsvc.c | 24 | ||||
-rw-r--r-- | lib/libcfg/rdline.c | 14 |
4 files changed, 32 insertions, 32 deletions
diff --git a/lib/cron/rdcron.c b/lib/cron/rdcron.c index 520f969..8781a6f 100644 --- a/lib/cron/rdcron.c +++ b/lib/cron/rdcron.c @@ -473,21 +473,17 @@ static const cfg_param_t cron_params[] = { crontab_t *rdcron(int dirfd, const char *filename) { - crontab_t *cron; + crontab_t *cron = NULL; rdline_t rd; - int fd, ret; + int ret; - fd = openat(dirfd, filename, O_RDONLY); - if (fd < 0) { - perror(filename); + if (rdline_init(&rd, dirfd, filename, 0, NULL)) return NULL; - } cron = calloc(1, sizeof(*cron)); if (cron == NULL) { fputs("out of memory\n", stderr); - close(fd); - return NULL; + goto out; } cron->minute = 0xFFFFFFFFFFFFFFFFUL; @@ -496,12 +492,12 @@ crontab_t *rdcron(int dirfd, const char *filename) cron->month = 0xFFFF; cron->dayofweek = 0xFF; - rdline_init(&rd, fd, filename, 0, NULL); ret = rdcfg(cron, &rd, cron_params, ARRAY_SIZE(cron_params), 0); if (ret) { delcron(cron); cron = NULL; } +out: rdline_cleanup(&rd); return cron; } diff --git a/lib/include/libcfg.h b/lib/include/libcfg.h index 95f91a8..3e631d1 100644 --- a/lib/include/libcfg.h +++ b/lib/include/libcfg.h @@ -49,12 +49,14 @@ typedef struct { /* Initialize the config line scanner. - The scanner reads from the provided fd. The filename is used for - error reporting. An argument count and vector can be set for argument - substitution in rdline. + The scanner opens the filename relative to the passed dirfd. An + argument count and vector can be set for argument substitution + in rdline. + + Returns 0 on success. */ -void rdline_init(rdline_t *t, int fd, const char *filename, - int argc, const char *const *argv); +int rdline_init(rdline_t *t, int dirfd, const char *filename, + int argc, const char *const *argv); void rdline_cleanup(rdline_t *t); diff --git a/lib/init/rdsvc.c b/lib/init/rdsvc.c index ca60731..e8cb530 100644 --- a/lib/init/rdsvc.c +++ b/lib/init/rdsvc.c @@ -250,13 +250,6 @@ service_t *rdsvc(int dirfd, const char *filename, int flags) service_t *svc = NULL; size_t argc, nlen; rdline_t rd; - int fd; - - fd = openat(dirfd, filename, O_RDONLY); - if (fd < 0) { - perror(filename); - return NULL; - } arg = strchr(filename, '@'); if (arg != NULL) { @@ -266,6 +259,9 @@ service_t *rdsvc(int dirfd, const char *filename, int flags) argc = 0; } + if (rdline_init(&rd, dirfd, filename, argc, args)) + return NULL; + nlen = (arg != NULL) ? (size_t)(arg - filename) : strlen(filename); svc = calloc(1, sizeof(*svc) + nlen + 1); @@ -280,18 +276,16 @@ service_t *rdsvc(int dirfd, const char *filename, int flags) memcpy(svc->name, filename, nlen); - rdline_init(&rd, fd, filename, argc, args); - - if (rdcfg(svc, &rd, svc_params, ARRAY_SIZE(svc_params), flags)) { - delsvc(svc); - svc = NULL; - } + if (rdcfg(svc, &rd, svc_params, ARRAY_SIZE(svc_params), flags)) + goto fail; +out: rdline_cleanup(&rd); return svc; fail_oom: fputs("out of memory\n", stderr); +fail: delsvc(svc); - close(fd); - return NULL; + svc = NULL; + goto out; } diff --git a/lib/libcfg/rdline.c b/lib/libcfg/rdline.c index dbb3069..b0cd789 100644 --- a/lib/libcfg/rdline.c +++ b/lib/libcfg/rdline.c @@ -23,15 +23,23 @@ #include <stdio.h> #include "libcfg.h" +#include "util.h" -void rdline_init(rdline_t *t, int fd, const char *filename, - int argc, const char *const *argv) +int rdline_init(rdline_t *t, int dirfd, const char *filename, + int argc, const char *const *argv) { memset(t, 0, sizeof(*t)); - t->fp = fdopen(fd, "r"); + + t->fp = fopenat(dirfd, filename, "r"); + if (t->fp == NULL) { + perror(filename); + return -1; + } + t->filename = filename; t->argc = argc; t->argv = argv; + return 0; } void rdline_cleanup(rdline_t *t) |