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:
parent
074fe20a47
commit
c0e8c7e245
8 changed files with 38 additions and 43 deletions
|
@ -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
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in a new issue