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"
|
|
|
|
#include "util.h"
|
|
|
|
|
2018-03-25 12:14:06 +02:00
|
|
|
static int svc_desc(service_t *svc, char *arg,
|
2018-02-25 14:33:19 +01:00
|
|
|
const char *filename, size_t lineno)
|
|
|
|
{
|
|
|
|
(void)filename; (void)lineno;
|
2018-03-25 12:14:06 +02:00
|
|
|
svc->desc = arg;
|
2018-02-25 14:33:19 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-03-25 12:14:06 +02:00
|
|
|
static int svc_tty(service_t *svc, char *arg,
|
2018-02-25 14:33:19 +01:00
|
|
|
const char *filename, size_t lineno)
|
|
|
|
{
|
|
|
|
(void)filename; (void)lineno;
|
2018-03-25 12:14:06 +02:00
|
|
|
svc->ctty = arg;
|
2018-02-25 14:33:19 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-03-25 12:14:06 +02:00
|
|
|
static int svc_exec(service_t *svc, char *arg,
|
2018-02-25 14:33:19 +01:00
|
|
|
const char *filename, size_t lineno)
|
|
|
|
{
|
2018-03-25 12:14:06 +02:00
|
|
|
char **new = realloc(svc->exec, sizeof(char*) * (svc->num_exec + 1));
|
2018-02-25 14:33:19 +01:00
|
|
|
|
|
|
|
if (new == NULL) {
|
|
|
|
fprintf(stderr, "%s: %zu: out of memory\n", filename, lineno);
|
|
|
|
free(arg);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-03-25 12:14:06 +02:00
|
|
|
svc->exec = new;
|
|
|
|
svc->exec[svc->num_exec++] = arg;
|
2018-02-25 14:33:19 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-03-25 12:14:06 +02:00
|
|
|
static int svc_before(service_t *svc, char *arg,
|
2018-02-25 14:33:19 +01:00
|
|
|
const char *filename, size_t lineno)
|
|
|
|
{
|
2018-03-25 12:14:06 +02:00
|
|
|
char **new = realloc(svc->before,
|
|
|
|
sizeof(char*) * (svc->num_before + 1));
|
2018-02-25 14:33:19 +01:00
|
|
|
|
|
|
|
if (new == NULL) {
|
|
|
|
fprintf(stderr, "%s: %zu: out of memory\n", filename, lineno);
|
|
|
|
free(arg);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-03-25 12:14:06 +02:00
|
|
|
svc->before = new;
|
|
|
|
svc->before[svc->num_before++] = arg;
|
2018-02-25 14:33:19 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-03-25 12:14:06 +02:00
|
|
|
static int svc_after(service_t *svc, char *arg,
|
2018-02-25 14:33:19 +01:00
|
|
|
const char *filename, size_t lineno)
|
|
|
|
{
|
2018-03-25 12:14:06 +02:00
|
|
|
char **new = realloc(svc->after, sizeof(char*) * (svc->num_after + 1));
|
2018-02-25 14:33:19 +01:00
|
|
|
|
|
|
|
if (new == NULL) {
|
|
|
|
fprintf(stderr, "%s: %zu: out of memory\n", filename, lineno);
|
|
|
|
free(arg);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-03-25 12:14:06 +02:00
|
|
|
svc->after = new;
|
|
|
|
svc->after[svc->num_after++] = arg;
|
2018-02-25 14:33:19 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-03-25 12:14:06 +02:00
|
|
|
static int svc_type(service_t *svc, char *arg,
|
2018-02-25 14:33:19 +01:00
|
|
|
const char *filename, size_t lineno)
|
|
|
|
{
|
2018-03-25 01:55:08 +01:00
|
|
|
int type = svc_type_from_string(arg);
|
2018-02-25 14:33:19 +01:00
|
|
|
|
2018-03-25 01:55:08 +01:00
|
|
|
if (type == -1) {
|
2018-02-25 14:33:19 +01:00
|
|
|
fprintf(stderr, "%s: %zu: unknown service type '%s'\n",
|
|
|
|
filename, lineno, arg);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-03-25 12:14:06 +02:00
|
|
|
svc->type = type;
|
2018-02-25 14:33:19 +01:00
|
|
|
free(arg);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-03-25 12:14:06 +02:00
|
|
|
static int svc_target(service_t *svc, char *arg,
|
2018-02-25 14:33:19 +01:00
|
|
|
const char *filename, size_t lineno)
|
|
|
|
{
|
2018-03-25 01:55:08 +01:00
|
|
|
int 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",
|
|
|
|
filename, lineno, arg);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-03-25 12:14:06 +02:00
|
|
|
svc->target = target;
|
2018-02-25 14:33:19 +01:00
|
|
|
free(arg);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-03-26 00:34:00 +02:00
|
|
|
static int svc_rspwn_limit(service_t *svc, char *arg,
|
|
|
|
const char *filename, size_t lineno)
|
|
|
|
{
|
|
|
|
const char *ptr;
|
|
|
|
|
|
|
|
svc->rspwn_limit = 0;
|
|
|
|
|
|
|
|
if (!isdigit(*arg))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
for (ptr = arg; isdigit(*ptr); ++ptr)
|
|
|
|
svc->rspwn_limit = svc->rspwn_limit * 10 + (*ptr - '0');
|
|
|
|
|
|
|
|
if (*ptr != '\0')
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
free(arg);
|
|
|
|
return 0;
|
|
|
|
fail:
|
|
|
|
fprintf(stderr,
|
|
|
|
"%s: %zu: expected numeric argument for respawn limit\n",
|
|
|
|
filename, lineno);
|
|
|
|
free(arg);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-02-25 14:33:19 +01:00
|
|
|
|
|
|
|
static const struct {
|
|
|
|
const char *key;
|
|
|
|
|
2018-03-25 12:14:06 +02:00
|
|
|
int (*handle)(service_t *svc, char *arg,
|
2018-02-25 14:33:19 +01:00
|
|
|
const char *filename, size_t lineno);
|
2018-03-25 12:14:06 +02:00
|
|
|
} svc_params[] = {
|
|
|
|
{ "description", svc_desc },
|
|
|
|
{ "exec", svc_exec },
|
|
|
|
{ "type", svc_type },
|
|
|
|
{ "target", svc_target },
|
|
|
|
{ "tty", svc_tty },
|
|
|
|
{ "before", svc_before },
|
|
|
|
{ "after", svc_after },
|
2018-03-26 00:34:00 +02:00
|
|
|
{ "respawn_limit", svc_rspwn_limit },
|
2018-02-25 14:33:19 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2018-03-25 12:14:06 +02:00
|
|
|
service_t *rdsvc(int dirfd, const char *filename)
|
2018-02-25 14:33:19 +01:00
|
|
|
{
|
2018-04-04 11:55:59 +02:00
|
|
|
const char *arg, *args[1], *error;
|
2018-02-25 14:33:19 +01:00
|
|
|
char *line, *key, *value;
|
|
|
|
size_t i, argc, lineno;
|
2018-03-25 12:14:06 +02:00
|
|
|
service_t *svc;
|
2018-02-25 14:33:19 +01:00
|
|
|
int fd;
|
|
|
|
|
|
|
|
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-03-25 12:14:06 +02:00
|
|
|
svc = calloc(1, sizeof(*svc));
|
|
|
|
if (svc == NULL) {
|
2018-02-25 14:33:19 +01:00
|
|
|
fputs("out of memory\n", stderr);
|
|
|
|
close(fd);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-03-26 01:05:17 +02:00
|
|
|
if (arg != NULL) {
|
|
|
|
svc->name = strndup(filename, arg - filename);
|
|
|
|
} else {
|
|
|
|
svc->name = strdup(filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (svc->name == NULL) {
|
|
|
|
free(svc);
|
|
|
|
fputs("out of memory\n", stderr);
|
|
|
|
close(fd);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-02-25 14:33:19 +01:00
|
|
|
for (lineno = 1; ; ++lineno) {
|
|
|
|
errno = 0;
|
2018-04-04 11:55:59 +02:00
|
|
|
line = rdline(fd, argc, args);
|
2018-02-25 14:33:19 +01:00
|
|
|
|
|
|
|
if (line == NULL) {
|
2018-04-04 11:55:59 +02:00
|
|
|
if (errno == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
switch (errno) {
|
|
|
|
case EINVAL:
|
|
|
|
error = "error in argument expansion";
|
|
|
|
break;
|
|
|
|
case ELOOP:
|
|
|
|
error = "recursive argument expansion";
|
|
|
|
break;
|
|
|
|
case EILSEQ:
|
|
|
|
error = "missing \"";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
error = strerror(errno);
|
|
|
|
break;
|
2018-02-25 14:33:19 +01:00
|
|
|
}
|
2018-04-04 11:55:59 +02:00
|
|
|
|
|
|
|
fprintf(stderr, "%s: %zu: %s\n",
|
|
|
|
filename, lineno, error);
|
|
|
|
goto fail;
|
2018-02-25 14:33:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (splitkv(line, &key, &value)) {
|
|
|
|
if (key == NULL) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"%s: %zu: expected <key> = <value>\n",
|
|
|
|
filename, lineno);
|
|
|
|
} else if (value == NULL) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"%s: %zu: expected value after %s\n",
|
|
|
|
filename, lineno, key);
|
|
|
|
} else {
|
|
|
|
fprintf(stderr,
|
|
|
|
"%s: %zu: unexpected arguments "
|
|
|
|
"after key-value pair\n",
|
|
|
|
filename, lineno);
|
|
|
|
}
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (key == NULL) {
|
|
|
|
free(line);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-03-25 12:14:06 +02:00
|
|
|
for (i = 0; i < ARRAY_SIZE(svc_params); ++i) {
|
|
|
|
if (!strcmp(svc_params[i].key, key))
|
2018-02-25 14:33:19 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-03-25 12:14:06 +02:00
|
|
|
if (i >= ARRAY_SIZE(svc_params)) {
|
2018-02-25 14:33:19 +01:00
|
|
|
fprintf(stderr, "%s: %zu: unknown parameter '%s'\n",
|
|
|
|
filename, lineno, key);
|
|
|
|
goto fail_line;
|
|
|
|
}
|
|
|
|
|
2018-04-04 11:55:59 +02:00
|
|
|
value = strdup(value);
|
2018-02-25 14:33:19 +01:00
|
|
|
if (value == NULL) {
|
|
|
|
fputs("out of memory", stderr);
|
|
|
|
goto fail_line;
|
|
|
|
}
|
|
|
|
|
2018-03-25 12:14:06 +02:00
|
|
|
if (svc_params[i].handle(svc, value, filename, lineno)) {
|
2018-02-25 14:33:19 +01:00
|
|
|
free(value);
|
|
|
|
goto fail_line;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(line);
|
|
|
|
}
|
|
|
|
|
|
|
|
close(fd);
|
2018-03-25 12:14:06 +02:00
|
|
|
return svc;
|
2018-02-25 14:33:19 +01:00
|
|
|
fail_line:
|
|
|
|
free(line);
|
|
|
|
fail:
|
|
|
|
close(fd);
|
2018-03-25 12:14:06 +02:00
|
|
|
delsvc(svc);
|
2018-02-25 14:33:19 +01:00
|
|
|
return NULL;
|
|
|
|
}
|