aboutsummaryrefslogtreecommitdiffstats
path: root/src/afm-launch.c
blob: 12a481ea5d771ff112ca7e0bf35e62000f6ffec5 (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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/*
 Copyright 2015 IoT.bzh

 author: José Bollo <jose.bollo@iot.bzh>

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
*/

#define _GNU_SOURCE

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <assert.h>
#include <sys/stat.h>
#include <sys/types.h>

extern char **environ;

#include "verbose.h"
#include "afm-launch.h"
#include "secmgr-wrap.h"

/*
%I icondir			FWK_ICON_DIR
%P port				params->port
%S secret			params->secret
%D datadir			params->datadir
%r rootdir			desc->path
%h homedir			desc->home
%t tag (smack label)		desc->tag
%a appid			desc->appid
%c content			desc->content
%m mime-type			desc->type
%n name				desc->name
%p plugins			desc->plugins
%W width			desc->width
%H height			desc->height
*/

static const char *launch_master_args[] = {
	"/usr/bin/echo",
	"--alias=/icons:%I",
	"--port=%P",
	"--rootdir=%D",
	"--token=%S",
	NULL
};

static const char *launch_html_args[] = {
	"/usr/bin/chromium",
	"--single-process",
	"--user-data-dir=%D",
	"--data-path=%r",
	"file://%r/%c",
/*
	"http://localhost:%P",
*/
	NULL
};

static const char *launch_bin_args[] = {
	"/usr/bin/echo",
	"BINARY",
	NULL
};

static const char *launch_qml_args[] = {
	"/usr/bin/echo",
	"QML",
	NULL
};

static struct {
	const char *type;
	const char **launch_args;
}
known_launchers[] = {
	{ "text/html",                launch_html_args },
	{ "application/x-executable", launch_bin_args },
	{ "application/octet-stream", launch_bin_args },
	{ "text/vnd.qt.qml",          launch_qml_args }
};

struct launchparam {
	int port;
	const char *secret;
	const char *datadir;
};

static char **instantiate_arguments(const char **args, struct afm_launch_desc *desc, struct launchparam *params)
{
	const char **iter, *p, *v;
	char *data, **result, port[20], width[20], height[20], mini[3], c;
	int n, s, x;

	/* init */
	mini[0] = '%';
	mini[2] = 0;

	/* loop that either compute the size and build the result */
	n = s = x = 0;
	for (;;) {
		iter = args;
		n = 0;
		while (*iter) {
			p = *iter++;
			if (x)
				result[n] = data;
			n++;
			while((c = *p++) != 0) {
				if (c != '%') {
					if (x)
						*data++ = c;
					else
						s++;
				} else {
					c = *p++;
					switch (c) {
					case 'I': v = FWK_ICON_DIR; break;
					case 'P': if(!x) sprintf(port, "%d", params->port); v = port; break;
					case 'S': v = params->secret; break;
					case 'D': v = params->datadir; break;
					case 'r': v = desc->path; break;
					case 'h': v = desc->home; break;
					case 't': v = desc->tag; break;
					case 'a': v = desc->appid; break;
					case 'c': v = desc->content; break;
					case 'm': v = desc->type; break;
					case 'n': v = desc->name; break;
					case 'p': v = "" /*desc->plugins*/; break;
					case 'W': if(!x) sprintf(width, "%d", desc->width); v = width; break;
					case 'H': if(!x) sprintf(height, "%d", desc->height); v = height; break;
					case '%': c = 0;
					default: mini[1] = c; v = mini; break;
					}
					if (x)
						data = stpcpy(data, v);
					else
						s += strlen(v);
				}
			}
			if (x)
				*data++ = 0;
			else
				s++;
		}
		if (x) {
			result[n] = NULL;
			return result;
		}
		/* allocation */
		result = malloc((n+1)*sizeof(char*) + s);
		if (result == NULL) {
			errno = ENOMEM;
			return NULL;
		}
		data = (char*)(&result[n + 1]);
		x = 1;
	}
}

static void mksecret(char buffer[9])
{
	snprintf(buffer, 9, "%08lX", (0xffffffff & random()));
}

static int mkport()
{
	static int port_ring = 12345;
	int port = port_ring;
	if (port < 12345 || port > 15432)
		port = 12345;
	port_ring = port + 1;
	return port;
}

int afm_launch(struct afm_launch_desc *desc, pid_t children[2])
{
	char datadir[PATH_MAX];
	int ikl, nkl, rc;
	char secret[9];
	int port;
	char message[10];
	int mpipe[2];
	int spipe[2];
	struct launchparam params;
	char **args;

	/* what launcher ? */
	ikl = 0;
	if (desc->type != NULL && *desc->type) {
		nkl = sizeof known_launchers / sizeof * known_launchers;
		while (ikl < nkl && strcmp(desc->type, known_launchers[ikl].type))
			ikl++;
		if (ikl == nkl) {
			ERROR("type %s not found!", desc->type);
			errno = ENOENT;
			return -1;
		}
	}

	/* prepare paths */
	rc = snprintf(datadir, sizeof datadir, "%s/%s", desc->home, desc->tag);
	if (rc < 0 || rc >= sizeof datadir) {
		ERROR("overflow for datadir");
		errno = EINVAL;
		return -1;
	}

	/* make the secret and port */
	mksecret(secret);
	port = mkport();

	params.port = port;
	params.secret = secret;
	params.datadir = datadir;

	/* prepare the pipes */
	rc = pipe2(mpipe, O_CLOEXEC);
	if (rc < 0) {
		ERROR("error while calling pipe2: %m");
		return -1;
	}
	rc = pipe2(spipe, O_CLOEXEC);
	if (rc < 0) {
		ERROR("error while calling pipe2: %m");
		close(spipe[0]);
		close(spipe[1]);
		return -1;
	}

	/* fork the master child */
	children[0] = fork();
	if (children[0] < 0) {
		ERROR("master fork failed: %m");
		close(mpipe[0]);
		close(mpipe[1]);
		close(spipe[0]);
		close(spipe[1]);
		return -1;
	}
	if (children[0]) {
		/********* in the parent process ************/
		close(mpipe[1]);
		close(spipe[0]);
		/* wait the ready signal (that transmit the slave pid) */
		rc = read(mpipe[0], &children[1], sizeof children[1]);
		if (rc  < 0) {
			ERROR("reading master pipe failed: %m");
			close(mpipe[0]);
			close(spipe[1]);
			return -1;
		}
		close(mpipe[0]);
		assert(rc == sizeof children[1]);
		/* start the child */
		rc = write(spipe[1], "start", 5);
		if (rc < 0) {
			ERROR("writing slave pipe failed: %m");
			close(spipe[1]);
			return -1;
		}
		assert(rc == 5);
		close(spipe[1]);
		return 0;
	}

	/********* in the master child ************/
	close(mpipe[0]);
	close(spipe[1]);

	/* enter the process group */
	rc = setpgid(0, 0);
	if (rc) {
		ERROR("setpgid failed");
		_exit(1);
	}

	/* enter security mode */
	rc = secmgr_prepare_exec(desc->tag);
	if (rc < 0) {
		ERROR("call to secmgr_prepare_exec failed: %m");
		_exit(1);
	}

	/* enter the datadirectory */
	rc = mkdir(datadir, 0755);
	if (rc && errno != EEXIST) {
		ERROR("creation of datadir %s failed: %m", datadir);
		_exit(1);
	}
	rc = chdir(datadir);
	if (rc) {
		ERROR("can't enter the datadir %s: %m", datadir);
		_exit(1);
	}

	/* fork the slave child */
	children[1] = fork();
	if (children[1] < 0) {
		ERROR("slave fork failed: %m");
		_exit(1);
	}
	if (children[1] == 0) {
		/********* in the slave child ************/
		close(mpipe[0]);
		rc = read(spipe[0], message, sizeof message);
		if (rc < 0) {
			ERROR("reading slave pipe failed: %m");
			_exit(1);
		}

		args = instantiate_arguments(known_launchers[ikl].launch_args, desc, &params);
		if (args == NULL) {
			ERROR("out of memory in slave");
		}
		else {
			rc = execve(args[0], args, environ);
			ERROR("failed to exec slave %s: %m", args[0]);
		}
		_exit(1);
	}

	/********* still in the master child ************/
	close(spipe[1]);
	args = instantiate_arguments(launch_master_args, desc, &params);
	if (args == NULL) {
		ERROR("out of memory in master");
	}
	else {
		rc = write(mpipe[1], &children[1], sizeof children[1]);
		if (rc < 0) {
			ERROR("can't write master pipe: %m");
		}
		else {
			close(mpipe[1]);
			rc = execve(args[0], args, environ);
			ERROR("failed to exec master %s: %m", args[0]);
		}
	}
	_exit(1);
}