aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Oberhollenzer <david.oberhollenzer@tele2.at>2018-11-04 15:02:11 +0100
committerDavid Oberhollenzer <david.oberhollenzer@tele2.at>2018-11-04 15:02:11 +0100
commitc0e8c7e245c09598478302155b37a15f3370571f (patch)
treec2e4d7d62f00c8de12d08d9d469b8c31b3f1e21d
parent074fe20a4761e284af2aa46f978f48410cfa60c5 (diff)
cleanup: let rdline_init open the file
Signed-off-by: David Oberhollenzer <david.oberhollenzer@tele2.at>
-rw-r--r--cmd/Makemodule.am4
-rw-r--r--cmd/runsvc/env.c9
-rw-r--r--crond/Makemodule.am2
-rw-r--r--initd/Makemodule.am2
-rw-r--r--lib/cron/rdcron.c14
-rw-r--r--lib/include/libcfg.h12
-rw-r--r--lib/init/rdsvc.c24
-rw-r--r--lib/libcfg/rdline.c14
8 files changed, 38 insertions, 43 deletions
diff --git a/cmd/Makemodule.am b/cmd/Makemodule.am
index f2c7d5f..78692d1 100644
--- a/cmd/Makemodule.am
+++ b/cmd/Makemodule.am
@@ -8,7 +8,7 @@ runsvc_SOURCES = cmd/runsvc/runsvc.c cmd/runsvc/env.c cmd/runsvc/runsvc.h
runsvc_CPPFLAGS = $(AM_CPPFLAGS)
runsvc_CFLAGS = $(AM_CFLAGS)
runsvc_LDFLAGS = $(AM_LDFLAGS)
-runsvc_LDADD = libinit.a libutil.a libcfg.a
+runsvc_LDADD = libinit.a libcfg.a libutil.a
killall5_SOURCES = cmd/killall5.c
killall5_CPPFLAGS = $(AM_CPPFLAGS)
@@ -24,7 +24,7 @@ service_SOURCES += $(SRVHEADERS)
service_CPPFLAGS = $(AM_CPPFLAGS)
service_CFLAGS = $(AM_CFLAGS)
service_LDFLAGS = $(AM_LDFLAGS)
-service_LDADD = libinit.a libutil.a libcfg.a
+service_LDADD = libinit.a libcfg.a libutil.a
if GCROND
service_SOURCES += cmd/service/schedule.c
diff --git a/cmd/runsvc/env.c b/cmd/runsvc/env.c
index 1e73ee4..e58d803 100644
--- a/cmd/runsvc/env.c
+++ b/cmd/runsvc/env.c
@@ -86,17 +86,12 @@ static struct entry *list_from_file(void)
{
struct entry *list;
rdline_t rd;
- int fd;
- fd = open(ENVFILE, O_RDONLY);
- if (fd < 0) {
- perror(ENVFILE);
+ if (rdline_init(&rd, AT_FDCWD, ENVFILE, 0, NULL))
return NULL;
- }
- rdline_init(&rd, fd, ENVFILE, 0, NULL);
list = parse_list(&rd);
- close(fd);
+ rdline_cleanup(&rd);
return list;
}
diff --git a/crond/Makemodule.am b/crond/Makemodule.am
index 6beeff1..fb52b9a 100644
--- a/crond/Makemodule.am
+++ b/crond/Makemodule.am
@@ -3,7 +3,7 @@ gcrond_SOURCES = crond/main.c crond/gcrond.h crond/runjob.c
gcrond_CPPFLAGS = $(AM_CPPFLAGS)
gcrond_CFLAGS = $(AM_CFLAGS)
gcrond_LDFLAGS = $(AM_LDFLAGS)
-gcrond_LDADD = libcron.a libutil.a libcfg.a
+gcrond_LDADD = libcron.a libcfg.a libutil.a
sbin_PROGRAMS += gcrond
endif
diff --git a/initd/Makemodule.am b/initd/Makemodule.am
index e6260f5..8552e45 100644
--- a/initd/Makemodule.am
+++ b/initd/Makemodule.am
@@ -3,6 +3,6 @@ init_SOURCES += initd/status.c initd/supervisor.c
init_CPPFLAGS = $(AM_CPPFLAGS)
init_CFLAGS = $(AM_CFLAGS)
init_LDFLAGS = $(AM_LDFLAGS)
-init_LDADD = libinit.a libutil.a libcfg.a
+init_LDADD = libinit.a libcfg.a libutil.a
sbin_PROGRAMS += init
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)