summary refs log tree commit diff
path: root/service.c
blob: 347215167126d24ac6b0b63c8829b6210dcb0338 (plain) (blame)
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
/* Copyright (C) 2020  C. McEnroe <june@causal.agency>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <assert.h>
#include <err.h>
#include <fcntl.h>
#include <limits.h>
#include <paths.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <unistd.h>

#include "daemon.h"

const char *serviceDir = "/";
uid_t serviceUID;
gid_t serviceGID;
char *serviceEnviron[EnvironLen] = {
	[SHELL] = "SHELL=" _PATH_BSHELL,
	[PATH]  = "PATH=" _PATH_DEFPATH,
};

struct Prepend prepend;
struct Services services;

static void serviceFree(struct Service *service) {
	free(service->name);
	free(service->command);
	if (service->outPipe[0] >= 0) {
		close(service->outPipe[0]);
		close(service->outPipe[1]);
	}
	if (service->errPipe[0] >= 0) {
		close(service->errPipe[0]);
		close(service->errPipe[1]);
	}
}

int serviceAdd(const char *name, const char *command) {
	struct Service *service = NULL;
	for (size_t i = 0; i < services.len; ++i) {
		if (!strcmp(services.ptr[i].name, name)) {
			service = &services.ptr[i];
			break;
		}
	}

	if (service) {
		char *dup = strdup(command);
		if (!dup) return -1;
		free(service->command);
		service->command = dup;
		return 0;
	}

	if (services.len == services.cap) {
		size_t cap = (services.cap ? services.cap * 2 : 8);
		void *ptr = realloc(services.ptr, sizeof(*services.ptr) * cap);
		if (!ptr) return -1;
		services.cap = cap;
		services.ptr = ptr;
	}
	service = &services.ptr[services.len];
	*service = (struct Service) {
		.outPipe = { -1, -1 },
		.errPipe = { -1, -1 },
		.restartInterval = restartInterval,
	};

	service->name = strdup(name);
	if (!service->name) goto err;

	service->command = strdup(command);
	if (!service->command) goto err;

	int error = pipe2(service->outPipe, O_CLOEXEC);
	if (error) goto err;

	error = pipe2(service->errPipe, O_CLOEXEC);
	if (error) goto err;

	fcntl(service->outPipe[0], F_SETFL, O_NONBLOCK);
	fcntl(service->errPipe[0], F_SETFL, O_NONBLOCK);

	services.len++;
	return 0;

err:
	serviceFree(service);
	return -1;
}

void serviceStart(struct Service *service) {
	if (service->state == Start) return;
	service->intent = Start;
	// TODO: Reset restart interval.

	service->pid = fork();
	if (service->pid < 0) {
		syslog(LOG_ERR, "fork: %m");
		return;
		// FIXME: Do we treat this as a restart condition?
	}
	if (service->pid) {
		service->state = Start;
		return;
	}

	closelog();
	dup2(service->outPipe[1], STDOUT_FILENO);
	dup2(service->errPipe[1], STDERR_FILENO);

	int error = chdir(serviceDir);
	if (error) err(StopExit, "%s", serviceDir);

	error = setgid(serviceGID);
	if (error) err(StopExit, "setgid");

	error = setuid(serviceUID);
	if (error) err(StopExit, "setuid");

	size_t len = 0;
	char command[ARG_MAX];
	for (size_t i = 0; i < prepend.len; ++i) {
		int n = snprintf(
			&command[len], sizeof(command) - len, "%s;", prepend.commands[i]
		);
		assert(n > 0);
		len += n;
	}
	snprintf(&command[len], sizeof(command) - len, "exec %s", service->command);

	execle(
		_PATH_BSHELL,
		_PATH_BSHELL, "-c", command, service->name, NULL,
		serviceEnviron
	);
	err(StopExit, "%s", _PATH_BSHELL);
}