aboutsummaryrefslogtreecommitdiff
path: root/initd/runsvc.c
blob: 95c26cbd9b5c827acad281c398b2a48e1ac5481a (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
/* SPDX-License-Identifier: ISC */
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdlib.h>
#include <dirent.h>
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <ctype.h>

#include "init.h"

static int setup_env(void)
{
	int status = -1;
	ssize_t ret;
	FILE *fp;

	clearenv();

	fp = fopen(ENVFILE, "r");
	if (fp == NULL) {
		perror(ENVFILE);
		return -1;
	}

	do {
		char *line = NULL;
		size_t n = 0;

		errno = 0;
		ret = getline(&line, &n, fp);

		if (ret < 0) {
			if (errno == 0) {
				status = 0;
			} else {
				perror(ENVFILE);
			}
		} else if (ret > 0 && putenv(line) != 0) {
			perror("putenv");
			ret = -1;
		}

		free(line);
	} while (ret >= 0);

	fclose(fp);
	return status;
}

static int close_all_files(void)
{
	struct dirent *ent;
	int fd, n;
	DIR *dir;

	dir = opendir(PROCFDDIR);

	if (dir == NULL) {
		if (errno != ENOENT) {
			perror(PROCFDDIR);
			return -1;
		}

		n = sysconf(_SC_OPEN_MAX);
		if (n < 0) {
			perror("getting maximum file descriptor count");
			return -1;
		}

		for (fd = STDERR_FILENO + 1; fd < n; ++fd)
			close(fd);
	} else {
		while ((ent = readdir(dir)) != NULL) {
			if (!isdigit(ent->d_name[0]))
				continue;

			fd = atoi(ent->d_name);

			if (fd == STDIN_FILENO || fd == STDOUT_FILENO ||
			    fd == STDERR_FILENO) {
				continue;
			}

			close(fd);
		}

		closedir(dir);
	}

	return 0;
}

static int setup_tty(const char *tty, bool truncate)
{
	int fd;

	if (tty == NULL)
		return 0;

	fd = open(tty, O_RDWR);
	if (fd < 0) {
		perror(tty);
		return -1;
	}

	if (truncate)
		ftruncate(fd, 0);

	setsid();

	dup2(fd, STDIN_FILENO);
	dup2(fd, STDOUT_FILENO);
	dup2(fd, STDERR_FILENO);
	close(fd);
	return 0;
}

static __attribute__((noreturn)) void argv_exec(exec_t *e)
{
	char **argv = alloca(sizeof(char *) * (e->argc + 1)), *ptr;
	int i;

	for (ptr = e->args, i = 0; i < e->argc; ++i, ptr += strlen(ptr) + 1)
		argv[i] = ptr;

	argv[i] = NULL;
	execvp(argv[0], argv);
	perror(argv[0]);
	exit(EXIT_FAILURE);
}

static pid_t current_child;

static void runsvc_sighandler(int signo)
{
	if (current_child != -1)
		kill(current_child, signo);
}

static int run_sequentially(exec_t *list, bool direct_exec_last)
{
	pid_t ret, pid;
	int status;

	current_child = -1;

	for (; list != NULL; list = list->next) {
		if (list->next == NULL && direct_exec_last)
			argv_exec(list);

		pid = fork();

		if (pid == 0)
			argv_exec(list);

		if (pid == -1) {
			perror("fork");
			return EXIT_FAILURE;
		}

		current_child = pid;

		do {
			ret = wait(&status);
		} while (ret != pid);

		current_child = -1;

		if (!WIFEXITED(status))
			return EXIT_FAILURE;

		if (WEXITSTATUS(status) != EXIT_SUCCESS)
			return WEXITSTATUS(status);
	}

	do {
		ret = wait(&status);
	} while (ret != -1 || errno != ECHILD);

	return ret;
}

pid_t runsvc(service_t *svc)
{
	struct sigaction act;
	pid_t pid;
	int ret;

	pid = fork();

	if (pid == -1)
		perror("fork");

	if (pid == 0) {
		cli(NULL);

		memset(&act, 0, sizeof(act));
		act.sa_handler = runsvc_sighandler;
		sigaction(SIGTERM, &act, NULL);
		sigaction(SIGINT, &act, NULL);
		sigaction(SIGHUP, &act, NULL);

		act.sa_handler = SIG_DFL;
		sigaction(SIGCHLD, &act, NULL);
		sigaction(SIGUSR1, &act, NULL);

		if (setup_env())
			exit(EXIT_FAILURE);

		if (close_all_files())
			exit(EXIT_FAILURE);

		if (setup_tty(svc->ctty,
			      (svc->flags & SVC_FLAG_TRUNCATE_OUT) != 0)) {
			exit(EXIT_FAILURE);
		}

		sti(NULL);

		if (svc->flags & SVC_FLAG_SUB_REAPER) {
			prctl(PR_SET_CHILD_SUBREAPER, 1, 0, 0, 0);

			ret = run_sequentially(svc->exec, false);
		} else {
			ret = run_sequentially(svc->exec, true);
		}

		exit(ret);
	}

	return pid;
}