aboutsummaryrefslogtreecommitdiff
path: root/lib/src/rdsvc.c
blob: ee898c3a9548fc346677de2bda220c916b8c8963 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/* 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/>.
 */
#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>
#include <ctype.h>

#include "service.h"
#include "util.h"

static int svc_desc(service_t *svc, char *arg,
		    const char *filename, size_t lineno)
{
	(void)filename; (void)lineno;
	svc->desc = arg;
	return 0;
}

static int svc_tty(service_t *svc, char *arg,
		   const char *filename, size_t lineno)
{
	(void)filename; (void)lineno;
	svc->ctty = arg;
	return 0;
}

static int svc_exec(service_t *svc, char *arg,
		    const char *filename, size_t lineno)
{
	char **new = realloc(svc->exec, sizeof(char*) * (svc->num_exec + 1));

	if (new == NULL) {
		fprintf(stderr, "%s: %zu: out of memory\n", filename, lineno);
		free(arg);
		return -1;
	}

	svc->exec = new;
	svc->exec[svc->num_exec++] = arg;
	return 0;
}

static int svc_before(service_t *svc, char *arg,
		      const char *filename, size_t lineno)
{
	char **new = realloc(svc->before,
			     sizeof(char*) * (svc->num_before + 1));

	if (new == NULL) {
		fprintf(stderr, "%s: %zu: out of memory\n", filename, lineno);
		free(arg);
		return -1;
	}

	svc->before = new;
	svc->before[svc->num_before++] = arg;
	return 0;
}

static int svc_after(service_t *svc, char *arg,
		     const char *filename, size_t lineno)
{
	char **new = realloc(svc->after, sizeof(char*) * (svc->num_after + 1));

	if (new == NULL) {
		fprintf(stderr, "%s: %zu: out of memory\n", filename, lineno);
		free(arg);
		return -1;
	}

	svc->after = new;
	svc->after[svc->num_after++] = arg;
	return 0;
}

static int svc_type(service_t *svc, char *arg,
		    const char *filename, size_t lineno)
{
	int type = svc_type_from_string(arg);

	if (type == -1) {
		fprintf(stderr, "%s: %zu: unknown service type '%s'\n",
			filename, lineno, arg);
		return -1;
	}

	svc->type = type;
	free(arg);
	return 0;
}

static int svc_target(service_t *svc, char *arg,
		      const char *filename, size_t lineno)
{
	int target = svc_target_from_string(arg);

	if (target == -1) {
		fprintf(stderr, "%s: %zu: unknown service target '%s'\n",
			filename, lineno, arg);
		return -1;
	}

	svc->target = target;
	free(arg);
	return 0;
}

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;
}


static const struct {
	const char *key;

	int (*handle)(service_t *svc, char *arg,
		      const char *filename, size_t lineno);
} svc_params[] = {
	{ "description", svc_desc },
	{ "exec", svc_exec },
	{ "type", svc_type },
	{ "target", svc_target },
	{ "tty", svc_tty },
	{ "before", svc_before },
	{ "after", svc_after },
	{ "respawn_limit", svc_rspwn_limit },
};


service_t *rdsvc(int dirfd, const char *filename)
{
	const char *arg, *args[1];
	char *line, *key, *value;
	size_t i, argc, lineno;
	service_t *svc;
	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;
	}

	svc = calloc(1, sizeof(*svc));
	if (svc == NULL) {
		fputs("out of memory\n", stderr);
		close(fd);
		return NULL;
	}

	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;
	}

	for (lineno = 1; ; ++lineno) {
		errno = 0;
		line = rdline(fd);

		if (line == NULL) {
			if (errno != 0) {
				fprintf(stderr, "read: %s: %zu: %s\n",
					filename, lineno, strerror(errno));
				goto fail;
			}
			break;
		}

		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;
		}

		for (i = 0; i < ARRAY_SIZE(svc_params); ++i) {
			if (!strcmp(svc_params[i].key, key))
				break;
		}

		if (i >= ARRAY_SIZE(svc_params)) {
			fprintf(stderr, "%s: %zu: unknown parameter '%s'\n",
				filename, lineno, key);
			goto fail_line;
		}

		value = strexpand(value, argc, args);
		if (value == NULL) {
			fputs("out of memory", stderr);
			goto fail_line;
		}

		if (svc_params[i].handle(svc, value, filename, lineno)) {
			free(value);
			goto fail_line;
		}

		free(line);
	}

	close(fd);
	return svc;
fail_line:
	free(line);
fail:
	close(fd);
	delsvc(svc);
	return NULL;
}