aboutsummaryrefslogtreecommitdiff
path: root/src/com_start.c
blob: 5ad1786aa0e88ed0d7278671331317ee0fb1992b (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
/* This file is part of genrc
Copyryght (C) 2018-2020 Sergey Poznyakoff
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
*/
#include "genrc.h"
#include <syslog.h>
#include <runcap.h>
#include <fcntl.h>

static int
expand_command(char **ret, const char *str, size_t len, char **argv,
	       void *closure)
{
	struct runcap rc;
	int res;

	rc.rc_argv = argv;
	rc.rc_cap[RUNCAP_STDERR].sc_size = 0; /* Disable stderr capturing */
	res = runcap(&rc, 0);
	if (res == -1) {
		if (errno == ENOMEM)
			return WRDSE_NOSPACE;
		else {
			size_t size = 0;
			*ret = NULL;
			if (grecs_asprintf(ret, &size, "can't run %s: %s",
					   argv[0], strerror(errno)))
				return WRDSE_NOSPACE;
			else
				return WRDSE_USERERR;
		}
	}

	if (rc.rc_cap[RUNCAP_STDOUT].sc_leng) {
		char *p = malloc(rc.rc_cap[RUNCAP_STDOUT].sc_leng + 1);
		size_t i;

		if (!p) {
			runcap_free(&rc);
			return WRDSE_NOSPACE;
		}
		*ret = p;
		for (i = 0; i < rc.rc_cap[RUNCAP_STDOUT].sc_leng; i++) {
			res = runcap_getc(&rc, RUNCAP_STDOUT, p);
			if (res == -1) {
				size_t size = 0;

				free(*ret);
				*ret = NULL;
				runcap_free(&rc);
				if (grecs_asprintf(ret, &size,
						   "error capturing output "
						   "from %s: %s",
						   argv[0], strerror(errno)))
					return WRDSE_NOSPACE;
				else
					return WRDSE_USERERR;
			}
			if (res == 0)
				break;
			if (*p == '\n')
				*p = ' ';
			p++;
		}
		*p = 0;
	}

	runcap_free(&rc);
	return WRDSE_OK;
}

extern char **environ;

/* Spawn genrc_command.  If genrc_shell is not NULL, start it via
   "genrc_shell -c".  Otherwise, do a plain exec(3).

   If P is not null, close all file descriptors, open /dev/null as standard
   input, use P[0] as standard output (sic!) and P[1] as standard error.

   The function never returns.
*/
void
spawn(int *p)
{
	char **argv;

	if (genrc_shell) {
		argv = xcalloc(4, sizeof(argv[0]));
		argv[0] = genrc_shell;
		argv[1] = "-c";
		argv[2] = genrc_command;
		argv[3] = NULL;
	} else {
		struct wordsplit ws;
		int wsflags = WRDSF_ENV
			      | WRDSF_ENOMEMABRT
			      | WRDSF_SHOWERR
			      | WRDSF_ERROR
			      ;
		ws.ws_error = genrc_error;
		ws.ws_command = expand_command;
		ws.ws_env = (const char **)environ;
		if (wordsplit(genrc_command, &ws, wsflags))
			exit(127);
		argv = ws.ws_wordv;
	}

	runas();

	if (p) {
		int i;

		close(0);
		close(1);
		close(2);
		open("/dev/null", O_RDWR);
		dup(p[0]);
		dup(p[1]);

		i = sysconf(_SC_OPEN_MAX);
		while (--i > 2)
			close(i);
	}

	execvp(argv[0], argv);

	if (p) {
		genrc_openlog();
		syslog(LOG_CRIT, "failed to exec: %m");
	} else
		system_error(errno, "failed to exec %s", genrc_program);

	exit(127);
}

void
report_exec_error(int rc, char const *program)
{
	if (WIFEXITED(rc)) {
		if (WEXITSTATUS(rc)) {
			genrc_error("%s exited with status %d",
				    program, WEXITSTATUS(rc));
		}
	} else if (WIFSIGNALED(rc)) {
		char const *coremsg = "";
#ifdef WCOREDUMP
		if (WCOREDUMP(rc))
			coremsg = " (core dumped)";
#endif
		genrc_error("%s terminated on signal %d%s",
			    program, WTERMSIG(rc), coremsg);
	} else if (WIFSTOPPED(rc)) {
		genrc_error("%s stopped on signal %d",
			    program, WSTOPSIG(rc));
	} else {
		genrc_error("%s terminated with unrecognized status: %d",
			    program, rc);
	}
}

typedef void (*SIGHANDLER)(int);

void
sigchld(int sig)
{
}

int
timedwaitpid(pid_t pid, int *status)
{
	struct timeval now, stoptime, ttw;
	int rc = -1;
	struct sigaction act, oldact;

	act.sa_handler = sigchld;
	act.sa_flags = 0;
	sigemptyset(&act.sa_mask);
	sigaction(SIGCHLD, &act, &oldact);

	gettimeofday(&stoptime, NULL);
	stoptime.tv_sec += genrc_timeout;
	while (1) {
		pid_t p;
		
		p = waitpid(pid, status, WNOHANG);
		if (p == pid) {
			rc = 0;
			break;
		}
		if (p < 0 && errno != EINTR) {
			system_error(errno, "waitpid");
			break;
		}

		gettimeofday(&now, NULL);
		if (timercmp(&now, &stoptime, >=))
			break;
		timersub(&stoptime, &now, &ttw);
		if (select(0, NULL, NULL, NULL, &ttw) < 0) {
			if (errno != EINTR) {
				system_error(errno, "select");
				break;
			}
		}

	}
	sigaction(SIGCHLD, &oldact, NULL);
	if (rc) {
		kill(pid, SIGKILL);
	}
	return rc;
}

int
com_start(void)
{
	pid_t pid;
	int status;
	PIDLIST pids;
	char *p;
	
	pidlist_init(&pids);
	if (get_pid_list(genrc_pid_closure, &pids) == 0) {
		int running = pids.pidc > 0;
		pidlist_free(&pids);
		if (running) {
			genrc_error("%s is already running", genrc_program);
			return 1;
		}
	}

	if (genrc_verbose)
		printf("Starting %s\n", genrc_program);
	
	if ((p = getenv("GENRC_SENTINEL")) && *p == '1')
		return sentinel();
	
	pid = fork();
	if (pid == -1) {
		system_error(errno, "fork");
		return 1;
	}
	if (pid == 0) {
		spawn(NULL);
	}

	if (timedwaitpid(pid, &status)) {
		genrc_error("timed out waiting for %s to return",
			    genrc_program);
		return 1;
	}
	if (!(WIFEXITED(status) && WEXITSTATUS(status) == 0)) {
		report_exec_error(status, genrc_program);
		return 1;
	}
	return 0;
}

Return to:

Send suggestions and report system problems to the System administrator.