1
0
Fork 0
mirror of https://github.com/pygos/init.git synced 2024-11-22 03:09:46 +01:00

cleanup: let rdline_init open the file

Signed-off-by: David Oberhollenzer <david.oberhollenzer@tele2.at>
This commit is contained in:
David Oberhollenzer 2018-11-04 15:02:11 +01:00
parent 074fe20a47
commit c0e8c7e245
8 changed files with 38 additions and 43 deletions

View file

@ -8,7 +8,7 @@ runsvc_SOURCES = cmd/runsvc/runsvc.c cmd/runsvc/env.c cmd/runsvc/runsvc.h
runsvc_CPPFLAGS = $(AM_CPPFLAGS) runsvc_CPPFLAGS = $(AM_CPPFLAGS)
runsvc_CFLAGS = $(AM_CFLAGS) runsvc_CFLAGS = $(AM_CFLAGS)
runsvc_LDFLAGS = $(AM_LDFLAGS) 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_SOURCES = cmd/killall5.c
killall5_CPPFLAGS = $(AM_CPPFLAGS) killall5_CPPFLAGS = $(AM_CPPFLAGS)
@ -24,7 +24,7 @@ service_SOURCES += $(SRVHEADERS)
service_CPPFLAGS = $(AM_CPPFLAGS) service_CPPFLAGS = $(AM_CPPFLAGS)
service_CFLAGS = $(AM_CFLAGS) service_CFLAGS = $(AM_CFLAGS)
service_LDFLAGS = $(AM_LDFLAGS) service_LDFLAGS = $(AM_LDFLAGS)
service_LDADD = libinit.a libutil.a libcfg.a service_LDADD = libinit.a libcfg.a libutil.a
if GCROND if GCROND
service_SOURCES += cmd/service/schedule.c service_SOURCES += cmd/service/schedule.c

View file

@ -86,17 +86,12 @@ static struct entry *list_from_file(void)
{ {
struct entry *list; struct entry *list;
rdline_t rd; rdline_t rd;
int fd;
fd = open(ENVFILE, O_RDONLY); if (rdline_init(&rd, AT_FDCWD, ENVFILE, 0, NULL))
if (fd < 0) {
perror(ENVFILE);
return NULL; return NULL;
}
rdline_init(&rd, fd, ENVFILE, 0, NULL);
list = parse_list(&rd); list = parse_list(&rd);
close(fd); rdline_cleanup(&rd);
return list; return list;
} }

View file

@ -3,7 +3,7 @@ gcrond_SOURCES = crond/main.c crond/gcrond.h crond/runjob.c
gcrond_CPPFLAGS = $(AM_CPPFLAGS) gcrond_CPPFLAGS = $(AM_CPPFLAGS)
gcrond_CFLAGS = $(AM_CFLAGS) gcrond_CFLAGS = $(AM_CFLAGS)
gcrond_LDFLAGS = $(AM_LDFLAGS) gcrond_LDFLAGS = $(AM_LDFLAGS)
gcrond_LDADD = libcron.a libutil.a libcfg.a gcrond_LDADD = libcron.a libcfg.a libutil.a
sbin_PROGRAMS += gcrond sbin_PROGRAMS += gcrond
endif endif

View file

@ -3,6 +3,6 @@ init_SOURCES += initd/status.c initd/supervisor.c
init_CPPFLAGS = $(AM_CPPFLAGS) init_CPPFLAGS = $(AM_CPPFLAGS)
init_CFLAGS = $(AM_CFLAGS) init_CFLAGS = $(AM_CFLAGS)
init_LDFLAGS = $(AM_LDFLAGS) init_LDFLAGS = $(AM_LDFLAGS)
init_LDADD = libinit.a libutil.a libcfg.a init_LDADD = libinit.a libcfg.a libutil.a
sbin_PROGRAMS += init sbin_PROGRAMS += init

View file

@ -473,21 +473,17 @@ static const cfg_param_t cron_params[] = {
crontab_t *rdcron(int dirfd, const char *filename) crontab_t *rdcron(int dirfd, const char *filename)
{ {
crontab_t *cron; crontab_t *cron = NULL;
rdline_t rd; rdline_t rd;
int fd, ret; int ret;
fd = openat(dirfd, filename, O_RDONLY); if (rdline_init(&rd, dirfd, filename, 0, NULL))
if (fd < 0) {
perror(filename);
return NULL; return NULL;
}
cron = calloc(1, sizeof(*cron)); cron = calloc(1, sizeof(*cron));
if (cron == NULL) { if (cron == NULL) {
fputs("out of memory\n", stderr); fputs("out of memory\n", stderr);
close(fd); goto out;
return NULL;
} }
cron->minute = 0xFFFFFFFFFFFFFFFFUL; cron->minute = 0xFFFFFFFFFFFFFFFFUL;
@ -496,12 +492,12 @@ crontab_t *rdcron(int dirfd, const char *filename)
cron->month = 0xFFFF; cron->month = 0xFFFF;
cron->dayofweek = 0xFF; cron->dayofweek = 0xFF;
rdline_init(&rd, fd, filename, 0, NULL);
ret = rdcfg(cron, &rd, cron_params, ARRAY_SIZE(cron_params), 0); ret = rdcfg(cron, &rd, cron_params, ARRAY_SIZE(cron_params), 0);
if (ret) { if (ret) {
delcron(cron); delcron(cron);
cron = NULL; cron = NULL;
} }
out:
rdline_cleanup(&rd); rdline_cleanup(&rd);
return cron; return cron;
} }

View file

@ -49,12 +49,14 @@ typedef struct {
/* /*
Initialize the config line scanner. Initialize the config line scanner.
The scanner reads from the provided fd. The filename is used for The scanner opens the filename relative to the passed dirfd. An
error reporting. An argument count and vector can be set for argument argument count and vector can be set for argument substitution
substitution in rdline. in rdline.
Returns 0 on success.
*/ */
void rdline_init(rdline_t *t, int fd, const char *filename, int rdline_init(rdline_t *t, int dirfd, const char *filename,
int argc, const char *const *argv); int argc, const char *const *argv);
void rdline_cleanup(rdline_t *t); void rdline_cleanup(rdline_t *t);

View file

@ -250,13 +250,6 @@ service_t *rdsvc(int dirfd, const char *filename, int flags)
service_t *svc = NULL; service_t *svc = NULL;
size_t argc, nlen; size_t argc, nlen;
rdline_t rd; rdline_t rd;
int fd;
fd = openat(dirfd, filename, O_RDONLY);
if (fd < 0) {
perror(filename);
return NULL;
}
arg = strchr(filename, '@'); arg = strchr(filename, '@');
if (arg != NULL) { if (arg != NULL) {
@ -266,6 +259,9 @@ service_t *rdsvc(int dirfd, const char *filename, int flags)
argc = 0; argc = 0;
} }
if (rdline_init(&rd, dirfd, filename, argc, args))
return NULL;
nlen = (arg != NULL) ? (size_t)(arg - filename) : strlen(filename); nlen = (arg != NULL) ? (size_t)(arg - filename) : strlen(filename);
svc = calloc(1, sizeof(*svc) + nlen + 1); 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); memcpy(svc->name, filename, nlen);
rdline_init(&rd, fd, filename, argc, args); if (rdcfg(svc, &rd, svc_params, ARRAY_SIZE(svc_params), flags))
goto fail;
if (rdcfg(svc, &rd, svc_params, ARRAY_SIZE(svc_params), flags)) {
delsvc(svc);
svc = NULL;
}
out:
rdline_cleanup(&rd); rdline_cleanup(&rd);
return svc; return svc;
fail_oom: fail_oom:
fputs("out of memory\n", stderr); fputs("out of memory\n", stderr);
fail:
delsvc(svc); delsvc(svc);
close(fd); svc = NULL;
return NULL; goto out;
} }

View file

@ -23,15 +23,23 @@
#include <stdio.h> #include <stdio.h>
#include "libcfg.h" #include "libcfg.h"
#include "util.h"
void rdline_init(rdline_t *t, int fd, const char *filename, int rdline_init(rdline_t *t, int dirfd, const char *filename,
int argc, const char *const *argv) int argc, const char *const *argv)
{ {
memset(t, 0, sizeof(*t)); 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->filename = filename;
t->argc = argc; t->argc = argc;
t->argv = argv; t->argv = argv;
return 0;
} }
void rdline_cleanup(rdline_t *t) void rdline_cleanup(rdline_t *t)