2018-03-24 22:31:05 +01:00
|
|
|
/* SPDX-License-Identifier: GPL-3.0-or-later */
|
|
|
|
/*
|
|
|
|
* Copyright (C) 2018 - David Oberhollenzer
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2018-02-25 14:33:19 +01:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2018-03-26 00:34:00 +02:00
|
|
|
#include <ctype.h>
|
2018-02-25 14:33:19 +01:00
|
|
|
|
|
|
|
#include "service.h"
|
2018-05-23 22:26:17 +02:00
|
|
|
#include "libcfg.h"
|
2018-02-25 14:33:19 +01:00
|
|
|
#include "util.h"
|
|
|
|
|
2018-04-11 00:04:33 +02:00
|
|
|
static int try_unescape(char *arg, rdline_t *rd)
|
2018-04-04 14:03:23 +02:00
|
|
|
{
|
|
|
|
if (unescape(arg)) {
|
|
|
|
fprintf(stderr, "%s: %zu: malformed string constant\n",
|
2018-04-11 00:04:33 +02:00
|
|
|
rd->filename, rd->lineno);
|
2018-04-04 14:03:23 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-04-11 00:04:33 +02:00
|
|
|
static char *try_strdup(const char *str, rdline_t *rd)
|
2018-02-25 14:33:19 +01:00
|
|
|
{
|
2018-04-11 00:04:33 +02:00
|
|
|
char *out = strdup(str);
|
|
|
|
|
|
|
|
if (out == NULL) {
|
|
|
|
fprintf(stderr, "%s: %zu: out of memory\n",
|
|
|
|
rd->filename, rd->lineno);
|
|
|
|
}
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2018-04-11 20:12:15 +02:00
|
|
|
static int try_pack_argv(char *str, rdline_t *rd)
|
|
|
|
{
|
|
|
|
int count = pack_argv(str);
|
|
|
|
if (count < 0) {
|
|
|
|
fprintf(stderr, "%s: %zu: malformed string constant\n",
|
|
|
|
rd->filename, rd->lineno);
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2018-04-11 00:04:33 +02:00
|
|
|
static int svc_desc(service_t *svc, char *arg, rdline_t *rd)
|
|
|
|
{
|
|
|
|
if (try_unescape(arg, rd))
|
2018-04-04 14:03:23 +02:00
|
|
|
return -1;
|
2018-04-11 00:04:33 +02:00
|
|
|
svc->desc = try_strdup(arg, rd);
|
|
|
|
return svc->desc == NULL ? -1 : 0;
|
2018-02-25 14:33:19 +01:00
|
|
|
}
|
|
|
|
|
2018-04-11 00:04:33 +02:00
|
|
|
static int svc_tty(service_t *svc, char *arg, rdline_t *rd)
|
2018-02-25 14:33:19 +01:00
|
|
|
{
|
2018-07-22 17:57:13 +02:00
|
|
|
if (strncmp(arg, "truncate", 8) == 0 && isspace(arg[8])) {
|
|
|
|
svc->flags |= SVC_FLAG_TRUNCATE_OUT;
|
|
|
|
arg += 8;
|
|
|
|
while (isspace(*arg))
|
|
|
|
++arg;
|
|
|
|
}
|
|
|
|
|
2018-04-11 00:04:33 +02:00
|
|
|
if (try_unescape(arg, rd))
|
2018-04-04 14:03:23 +02:00
|
|
|
return -1;
|
2018-07-22 17:57:13 +02:00
|
|
|
|
2018-04-11 00:04:33 +02:00
|
|
|
svc->ctty = try_strdup(arg, rd);
|
|
|
|
return svc->ctty == NULL ? -1 : 0;
|
2018-02-25 14:33:19 +01:00
|
|
|
}
|
|
|
|
|
2018-04-11 00:04:33 +02:00
|
|
|
static int svc_exec(service_t *svc, char *arg, rdline_t *rd)
|
2018-02-25 14:33:19 +01:00
|
|
|
{
|
2018-04-04 14:58:01 +02:00
|
|
|
exec_t *e, *end;
|
|
|
|
|
2018-04-11 13:29:13 +02:00
|
|
|
e = calloc(1, sizeof(*e) + strlen(arg) + 1);
|
2018-04-04 14:58:01 +02:00
|
|
|
if (e == NULL) {
|
2018-04-11 00:04:33 +02:00
|
|
|
fprintf(stderr, "%s: %zu: out of memory\n",
|
|
|
|
rd->filename, rd->lineno);
|
2018-02-25 14:33:19 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-04-11 19:45:26 +02:00
|
|
|
strcpy(e->args, arg);
|
2018-04-11 00:04:33 +02:00
|
|
|
|
2018-04-11 20:12:15 +02:00
|
|
|
e->argc = try_pack_argv(e->args, rd);
|
|
|
|
if (e->argc < 0)
|
2018-04-11 00:04:33 +02:00
|
|
|
return -1;
|
2018-04-04 14:58:01 +02:00
|
|
|
|
|
|
|
if (svc->exec == NULL) {
|
|
|
|
svc->exec = e;
|
|
|
|
} else {
|
|
|
|
for (end = svc->exec; end->next != NULL; end = end->next)
|
|
|
|
;
|
|
|
|
end->next = e;
|
|
|
|
}
|
2018-02-25 14:33:19 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-04-11 00:04:33 +02:00
|
|
|
static int svc_before(service_t *svc, char *arg, rdline_t *rd)
|
2018-02-25 14:33:19 +01:00
|
|
|
{
|
2018-04-04 14:58:01 +02:00
|
|
|
if (svc->before != NULL) {
|
|
|
|
fprintf(stderr, "%s: %zu: 'before' dependencies respecified\n",
|
2018-04-11 00:04:33 +02:00
|
|
|
rd->filename, rd->lineno);
|
2018-04-04 14:03:23 +02:00
|
|
|
return -1;
|
2018-04-04 14:58:01 +02:00
|
|
|
}
|
2018-04-04 14:03:23 +02:00
|
|
|
|
2018-04-11 16:07:20 +02:00
|
|
|
svc->before = try_strdup(arg, rd);
|
|
|
|
if (svc->before == NULL)
|
2018-04-11 00:04:33 +02:00
|
|
|
return -1;
|
|
|
|
|
2018-04-11 20:12:15 +02:00
|
|
|
svc->num_before = try_pack_argv(svc->before, rd);
|
|
|
|
return (svc->num_before < 0) ? -1 : 0;
|
2018-02-25 14:33:19 +01:00
|
|
|
}
|
|
|
|
|
2018-04-11 00:04:33 +02:00
|
|
|
static int svc_after(service_t *svc, char *arg, rdline_t *rd)
|
2018-02-25 14:33:19 +01:00
|
|
|
{
|
2018-04-04 14:58:01 +02:00
|
|
|
if (svc->after != NULL) {
|
|
|
|
fprintf(stderr, "%s: %zu: 'after' dependencies respecified\n",
|
2018-04-11 00:04:33 +02:00
|
|
|
rd->filename, rd->lineno);
|
2018-04-04 14:03:23 +02:00
|
|
|
return -1;
|
2018-04-04 14:58:01 +02:00
|
|
|
}
|
2018-04-04 14:03:23 +02:00
|
|
|
|
2018-04-11 16:07:20 +02:00
|
|
|
svc->after = try_strdup(arg, rd);
|
|
|
|
if (svc->after == NULL)
|
2018-04-11 00:04:33 +02:00
|
|
|
return -1;
|
|
|
|
|
2018-04-11 20:12:15 +02:00
|
|
|
svc->num_after = try_pack_argv(svc->after, rd);
|
|
|
|
return (svc->num_after < 0) ? -1 : 0;
|
2018-02-25 14:33:19 +01:00
|
|
|
}
|
|
|
|
|
2018-04-11 00:04:33 +02:00
|
|
|
static int svc_type(service_t *svc, char *arg, rdline_t *rd)
|
2018-02-25 14:33:19 +01:00
|
|
|
{
|
2018-04-11 20:12:15 +02:00
|
|
|
int count = try_pack_argv(arg, rd);
|
2018-04-04 14:03:23 +02:00
|
|
|
|
2018-04-11 20:12:15 +02:00
|
|
|
if (count < 1)
|
|
|
|
return -1;
|
2018-04-04 14:03:23 +02:00
|
|
|
|
2018-04-11 13:29:13 +02:00
|
|
|
svc->type = svc_type_from_string(arg);
|
2018-02-25 14:33:19 +01:00
|
|
|
|
2018-04-11 13:29:13 +02:00
|
|
|
if (svc->type == -1) {
|
2018-02-25 14:33:19 +01:00
|
|
|
fprintf(stderr, "%s: %zu: unknown service type '%s'\n",
|
2018-04-11 13:29:13 +02:00
|
|
|
rd->filename, rd->lineno, arg);
|
2018-02-25 14:33:19 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-04-11 20:12:15 +02:00
|
|
|
if (count > 1) {
|
|
|
|
arg += strlen(arg) + 1;
|
|
|
|
|
2018-04-11 13:29:13 +02:00
|
|
|
switch (svc->type) {
|
2018-04-04 14:03:23 +02:00
|
|
|
case SVC_RESPAWN:
|
2018-04-11 13:29:13 +02:00
|
|
|
if (strcmp(arg, "limit") != 0)
|
2018-04-04 14:03:23 +02:00
|
|
|
goto fail_limit;
|
2018-04-11 20:12:15 +02:00
|
|
|
arg += strlen(arg) + 1;
|
2018-04-04 14:03:23 +02:00
|
|
|
|
2018-04-11 20:12:15 +02:00
|
|
|
if (count > 3)
|
|
|
|
goto fail_args;
|
|
|
|
if (!isdigit(*arg))
|
2018-04-11 13:29:13 +02:00
|
|
|
goto fail_limit;
|
2018-04-11 20:12:15 +02:00
|
|
|
svc->rspwn_limit = atoi(arg);
|
|
|
|
break;
|
2018-04-04 14:03:23 +02:00
|
|
|
default:
|
2018-04-11 20:12:15 +02:00
|
|
|
goto fail_args;
|
2018-04-04 14:03:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-25 14:33:19 +01:00
|
|
|
return 0;
|
2018-04-11 20:12:15 +02:00
|
|
|
fail_args:
|
|
|
|
fprintf(stderr, "%s: %zu: unexpected extra arguments\n",
|
|
|
|
rd->filename, rd->lineno);
|
|
|
|
return -1;
|
2018-04-04 14:03:23 +02:00
|
|
|
fail_limit:
|
|
|
|
fprintf(stderr, "%s: %zu: expected 'limit <value>' after 'respawn'\n",
|
2018-04-11 00:04:33 +02:00
|
|
|
rd->filename, rd->lineno);
|
2018-04-04 14:03:23 +02:00
|
|
|
return -1;
|
2018-02-25 14:33:19 +01:00
|
|
|
}
|
|
|
|
|
2018-04-11 00:04:33 +02:00
|
|
|
static int svc_target(service_t *svc, char *arg, rdline_t *rd)
|
2018-02-25 14:33:19 +01:00
|
|
|
{
|
2018-04-04 14:03:23 +02:00
|
|
|
int target;
|
|
|
|
|
2018-04-11 00:04:33 +02:00
|
|
|
if (try_unescape(arg, rd))
|
2018-04-04 14:03:23 +02:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
target = svc_target_from_string(arg);
|
2018-02-25 14:33:19 +01:00
|
|
|
|
2018-03-25 01:55:08 +01:00
|
|
|
if (target == -1) {
|
2018-02-25 14:33:19 +01:00
|
|
|
fprintf(stderr, "%s: %zu: unknown service target '%s'\n",
|
2018-04-11 00:04:33 +02:00
|
|
|
rd->filename, rd->lineno, arg);
|
2018-02-25 14:33:19 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-03-25 12:14:06 +02:00
|
|
|
svc->target = target;
|
2018-02-25 14:33:19 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-04-08 13:30:20 +02:00
|
|
|
static const struct svc_param {
|
2018-02-25 14:33:19 +01:00
|
|
|
const char *key;
|
|
|
|
|
2018-04-11 23:01:35 +02:00
|
|
|
unsigned int allow_block : 1;
|
|
|
|
|
2018-04-21 22:51:28 +02:00
|
|
|
int flags;
|
|
|
|
|
2018-04-11 00:04:33 +02:00
|
|
|
int (*handle)(service_t *svc, char *arg, rdline_t *rd);
|
2018-03-25 12:14:06 +02:00
|
|
|
} svc_params[] = {
|
2018-04-21 22:51:28 +02:00
|
|
|
{ "description", 0, 0, svc_desc },
|
|
|
|
{ "exec", 1, RDSVC_NO_EXEC, svc_exec },
|
|
|
|
{ "type", 0, 0, svc_type },
|
|
|
|
{ "target", 0, 0, svc_target },
|
|
|
|
{ "tty", 0, RDSVC_NO_CTTY, svc_tty },
|
|
|
|
{ "before", 0, RDSVC_NO_DEPS, svc_before },
|
|
|
|
{ "after", 0, RDSVC_NO_DEPS, svc_after },
|
2018-02-25 14:33:19 +01:00
|
|
|
};
|
|
|
|
|
2018-04-11 00:04:33 +02:00
|
|
|
static int splitkv(rdline_t *rd, char **k, char **v)
|
2018-04-08 13:30:20 +02:00
|
|
|
{
|
2018-04-11 00:04:33 +02:00
|
|
|
char *key = rd->buffer, *value = rd->buffer;
|
2018-04-08 13:30:20 +02:00
|
|
|
|
|
|
|
while (*value != ' ' && *value != '\0') {
|
|
|
|
if (!isalpha(*value)) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"%s: %zu: unexpected '%c' in keyword\n",
|
2018-04-11 00:04:33 +02:00
|
|
|
rd->filename, rd->lineno, *value);
|
2018-04-08 13:30:20 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
++value;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*value != ' ') {
|
|
|
|
fprintf(stderr, "%s: %zu: expected argument after '%s'\n",
|
2018-04-11 00:04:33 +02:00
|
|
|
rd->filename, rd->lineno, key);
|
2018-04-08 13:30:20 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
*(value++) = '\0';
|
|
|
|
|
|
|
|
*k = key;
|
|
|
|
*v = value;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-04-11 00:04:33 +02:00
|
|
|
static const struct svc_param *find_param(rdline_t *rd, const char *name)
|
2018-04-08 13:30:20 +02:00
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE(svc_params); ++i) {
|
|
|
|
if (!strcmp(svc_params[i].key, name))
|
|
|
|
return svc_params + i;
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(stderr, "%s: %zu: unknown keyword '%s'\n",
|
2018-04-11 00:04:33 +02:00
|
|
|
rd->filename, rd->lineno, name);
|
2018-04-08 13:30:20 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-04-21 22:51:28 +02:00
|
|
|
service_t *rdsvc(int dirfd, const char *filename, int flags)
|
2018-02-25 14:33:19 +01:00
|
|
|
{
|
2018-04-08 13:30:20 +02:00
|
|
|
const struct svc_param *p;
|
|
|
|
const char *arg, *args[1];
|
|
|
|
service_t *svc = NULL;
|
2018-04-11 00:04:33 +02:00
|
|
|
char *key, *value;
|
2018-04-11 13:29:13 +02:00
|
|
|
size_t argc, nlen;
|
2018-04-11 00:04:33 +02:00
|
|
|
rdline_t rd;
|
|
|
|
int fd, ret;
|
2018-02-25 14:33:19 +01:00
|
|
|
|
|
|
|
fd = openat(dirfd, filename, O_RDONLY);
|
|
|
|
if (fd < 0) {
|
|
|
|
perror(filename);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
arg = strchr(filename, '@');
|
|
|
|
if (arg != NULL) {
|
|
|
|
args[0] = arg + 1;
|
|
|
|
argc = 1;
|
|
|
|
} else {
|
|
|
|
argc = 0;
|
|
|
|
}
|
|
|
|
|
2018-04-11 00:04:33 +02:00
|
|
|
rdline_init(&rd, fd, filename, argc, args);
|
|
|
|
|
2018-04-11 13:29:13 +02:00
|
|
|
nlen = (arg != NULL) ? (size_t)(arg - filename) : strlen(filename);
|
|
|
|
|
|
|
|
svc = calloc(1, sizeof(*svc) + nlen + 1);
|
2018-04-08 13:30:20 +02:00
|
|
|
if (svc == NULL)
|
|
|
|
goto fail_oom;
|
2018-02-25 14:33:19 +01:00
|
|
|
|
2018-04-21 22:51:28 +02:00
|
|
|
if (!(flags & RDSVC_NO_FNAME)) {
|
|
|
|
svc->fname = strdup(filename);
|
|
|
|
if (svc->fname == NULL)
|
|
|
|
goto fail_oom;
|
|
|
|
}
|
2018-04-21 22:19:43 +02:00
|
|
|
|
2018-04-11 13:29:13 +02:00
|
|
|
memcpy(svc->name, filename, nlen);
|
2018-03-26 01:05:17 +02:00
|
|
|
|
2018-04-11 00:04:33 +02:00
|
|
|
while ((ret = rdline(&rd)) == 0) {
|
|
|
|
if (splitkv(&rd, &key, &value))
|
2018-04-08 13:30:20 +02:00
|
|
|
goto fail;
|
2018-02-25 14:33:19 +01:00
|
|
|
|
2018-04-11 00:04:33 +02:00
|
|
|
p = find_param(&rd, key);
|
2018-04-08 18:04:01 +02:00
|
|
|
if (p == NULL)
|
2018-04-08 13:30:20 +02:00
|
|
|
goto fail;
|
2018-02-25 14:33:19 +01:00
|
|
|
|
2018-04-11 23:01:35 +02:00
|
|
|
if (p->allow_block && *value == '{') {
|
|
|
|
for (++value; *value == ' '; ++value)
|
|
|
|
;
|
2018-04-21 22:51:28 +02:00
|
|
|
|
|
|
|
if (!(flags & p->flags)) {
|
|
|
|
if (*value != '\0' &&
|
|
|
|
p->handle(svc, value, &rd)) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
2018-04-11 23:01:35 +02:00
|
|
|
|
|
|
|
while ((ret = rdline(&rd)) == 0) {
|
|
|
|
if (strcmp(rd.buffer, "}") == 0)
|
|
|
|
break;
|
2018-04-21 22:51:28 +02:00
|
|
|
if (flags & p->flags)
|
|
|
|
continue;
|
2018-04-11 23:01:35 +02:00
|
|
|
if (p->handle(svc, rd.buffer, &rd))
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
goto fail;
|
|
|
|
if (ret > 0)
|
|
|
|
goto fail_bra;
|
2018-04-21 22:51:28 +02:00
|
|
|
} else {
|
|
|
|
if (flags & p->flags)
|
|
|
|
continue;
|
|
|
|
if (p->handle(svc, value, &rd))
|
|
|
|
goto fail;
|
2018-04-11 23:01:35 +02:00
|
|
|
}
|
2018-02-25 14:33:19 +01:00
|
|
|
}
|
|
|
|
|
2018-04-11 00:04:33 +02:00
|
|
|
if (ret < 0)
|
|
|
|
goto fail;
|
|
|
|
|
2018-02-25 14:33:19 +01:00
|
|
|
close(fd);
|
2018-03-25 12:14:06 +02:00
|
|
|
return svc;
|
2018-04-11 23:01:35 +02:00
|
|
|
fail_bra:
|
|
|
|
fprintf(stderr, "%s: missing '}' before end-of-file\n", filename);
|
|
|
|
goto fail;
|
2018-04-08 13:30:20 +02:00
|
|
|
fail_oom:
|
|
|
|
fputs("out of memory\n", stderr);
|
2018-02-25 14:33:19 +01:00
|
|
|
fail:
|
2018-03-25 12:14:06 +02:00
|
|
|
delsvc(svc);
|
2018-04-08 13:30:20 +02:00
|
|
|
close(fd);
|
2018-02-25 14:33:19 +01:00
|
|
|
return NULL;
|
|
|
|
}
|