mirror of
https://github.com/troglobit/finit.git
synced 2026-10-14 10:19:36 +00:00
Initial support for templating and instantiating services
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
1 parent
334d4a708a
commit
700c32fd73
5 files changed
+139
-13
No files matched your search
+71
-1
@@ -914,11 +914,79 @@ static int parse_dynamic(char *line, struct rlimit rlimit[], char *file)
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Very simple and crude implementation, only supports '%i'
|
||||
*/
|
||||
static char *instantiate(char *line, char *name)
|
||||
{
|
||||
char *ptr, *end = strchr(line, 0);
|
||||
char *pos = line;
|
||||
size_t num = 0;
|
||||
|
||||
if (!name[0] || !end)
|
||||
return line;
|
||||
|
||||
while ((ptr = strchr(pos, '%'))) {
|
||||
num++;
|
||||
pos++;
|
||||
}
|
||||
|
||||
ptr = realloc(line, strlen(line) + num * strlen(name) + 1);
|
||||
if (!ptr)
|
||||
return line;
|
||||
|
||||
pos = line = ptr;
|
||||
while ((ptr = strchr(pos, '%'))) {
|
||||
if (!strncmp(ptr, "%i", 2)) {
|
||||
char *rest = &ptr[2];
|
||||
char *next = ptr + strlen(name);
|
||||
|
||||
memmove(next, rest, strlen(rest) + 1);
|
||||
memcpy(ptr, name, strlen(name));
|
||||
|
||||
pos += strlen(name);
|
||||
} else
|
||||
pos++;
|
||||
}
|
||||
|
||||
return line;
|
||||
}
|
||||
|
||||
static int is_template(const char *file, char *name, size_t len)
|
||||
{
|
||||
char *ptr, *nm;
|
||||
size_t i = 0;
|
||||
|
||||
ptr = strchr(file, '@');
|
||||
if (!ptr)
|
||||
return 0; /* not a template */
|
||||
|
||||
nm = ptr + 1;
|
||||
ptr = strstr(nm, ".conf");
|
||||
if (!strcmp(nm, ".conf") || !ptr)
|
||||
return 1; /* template itself or invalid */
|
||||
|
||||
while (nm < ptr && i < len - 1)
|
||||
name[i++] = *nm++;
|
||||
name[i] = 0;
|
||||
|
||||
return 1; /* instantiated template */
|
||||
}
|
||||
|
||||
static int parse_conf(char *file, int is_rcsd)
|
||||
{
|
||||
struct rlimit rlimit[RLIMIT_NLIMITS];
|
||||
char name[65] = { 0 };
|
||||
FILE *fp;
|
||||
|
||||
if (is_template(file, name, sizeof(name))) {
|
||||
if (!name[0]) {
|
||||
dbg("*** Skipping template file %s", file);
|
||||
return 0;
|
||||
}
|
||||
dbg("*** instantiating %s from %s ...", name, file);
|
||||
}
|
||||
|
||||
fp = fopen(file, "r");
|
||||
if (!fp)
|
||||
return 1;
|
||||
@@ -938,7 +1006,9 @@ static int parse_conf(char *file, int is_rcsd)
|
||||
continue;
|
||||
|
||||
tabstospaces(line);
|
||||
// dbg("%s", line);
|
||||
// dbg("raw: %s", line);
|
||||
line = instantiate(line, name);
|
||||
// dbg("ins: %s", line);
|
||||
|
||||
if (!parse_static(line, is_rcsd))
|
||||
;
|
||||
|
||||
+16
-8
@@ -253,18 +253,18 @@ static char *conf(char *path, size_t len, char *name, int creat)
|
||||
|
||||
int serv_enable(char *arg)
|
||||
{
|
||||
char src[strlen(arg) + 6];
|
||||
size_t arglen, len;
|
||||
char corr[40];
|
||||
char *ptr;
|
||||
|
||||
if (!arg || !arg[0]) {
|
||||
WARNX("missing argument to enable, may be one of:");
|
||||
return serv_list("available");
|
||||
}
|
||||
|
||||
if (!strstr(arg, ".conf")) {
|
||||
snprintf(corr, sizeof(corr), "%s.conf", arg);
|
||||
arg = corr;
|
||||
}
|
||||
strlcpy(src, arg, sizeof(src));
|
||||
if (!strstr(src, ".conf"))
|
||||
strlcat(src, ".conf", sizeof(src));
|
||||
|
||||
if (chdir(finit_rcsd))
|
||||
ERR(72, "failed cd %s", finit_rcsd);
|
||||
@@ -272,13 +272,21 @@ int serv_enable(char *arg)
|
||||
if (icreate && mkdir("enabled", 0755) && EEXIST != errno)
|
||||
ERR(73, "failed creating %s/enabled directory", finit_rcsd);
|
||||
|
||||
len = snprintf(NULL, 0, "%s/available/%s", finit_rcsd, arg);
|
||||
len = snprintf(NULL, 0, "%s/available/%s", finit_rcsd, src);
|
||||
arglen = snprintf(NULL, 0, "%s/enabled/%s", finit_rcsd, arg);
|
||||
char argpath[arglen + 1], path[len + 1];
|
||||
|
||||
snprintf(path, sizeof(path), "%s/available/%s", finit_rcsd, arg);
|
||||
snprintf(path, sizeof(path), "%s/available/%s", finit_rcsd, src);
|
||||
|
||||
/* check for template instatiation */
|
||||
ptr = strchr(path, '@');
|
||||
if (ptr++) {
|
||||
*ptr = 0;
|
||||
strlcat(path, ".conf", sizeof(path));
|
||||
}
|
||||
|
||||
if (!fexist(path))
|
||||
ERRX(72, "cannot find %s", path);
|
||||
ERRX(72, "cannot find %s%s", ptr ? "template ": "", path);
|
||||
|
||||
snprintf(argpath, sizeof(argpath), "%s/enabled/%s", finit_rcsd, arg);
|
||||
if (fexist(argpath))
|
||||
|
||||
Reference in new issue
Block a user