summaryrefslogtreecommitdiff
path: root/initvm.c
blob: b5f64cfd31c40e7bf2ebac08d830a1a6f4a1c183 (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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/*
 * NAME
 *	initvm - init for qemu, setup binfmt_misc launch build
 *	
 * SYNOPSIS
 *	initvm
 *
 * DESCRIPTION
 *	This is the kernel init script for virtual machines which will
 *	be running executables for an embedded (non-native)
 *	architecture. It registers binfmt_misc handlers for qemu and
 *	executes the build script, and tests many assumptions.
 *
 * FILES
 *	/.build/qemu-reg
 *		text file with lines to stuff into the binfmt_misc
 *		filesystem registration file
 *	/.build/build
 *		build script to execute once binfmts are set up
 *
 * AUTHOR
 *      Copyright (c) 2012 James Perkins <james.perkins@linuxfoundation.org>
 * 	                   Adrian Schroeter <adrian@suse.de>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 or 3 as
 * published by the Free Software Foundation.
 *
 * 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 (see the file COPYING); if not, write to the
 * Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 */

#include <sys/mount.h>
#include <sys/stat.h>
#include <sys/utsname.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <stdio.h>
#include <unistd.h>

/* to enable debugging, compile with -DDEBUG */
#ifdef DEBUG
#define DBG(x) 		do { x; } while(0)
#else
#define DBG(x)
#endif

/* function return codes */
enum okfail { FAIL=0, OK=1 };

/* qemu registration fields, see kernel/Documentation/binfmt_misc.txt */
enum fields { ignore=0, name, type, offset, magic, mask, interpreter, flags };
const char * const fieldnames[] = {
	"ignore", "name", "type", "offset",
	"magic", "mask", "interpreter", "flags"
};
const int n_fields = 8;

/* files in useful places */
#define SYSFS_BINFMT_MISC	"/proc/sys/fs/binfmt_misc"
#define SYSFS_BINFMT_MISC_REG	"/proc/sys/fs/binfmt_misc/register"
#define SYSFS_BINFMT_MISC_STAT	"/proc/sys/fs/binfmt_misc/status"

/* /usr/lib/build/x paths are copied to /.build inside a virtual machine */
#define BINFMT_REGF_0		"/.build/qemu-reg"
#define BINFMT_REGF_1		"/usr/lib/build/qemu-reg"
#define BUILD			"/.build/build"

/* useful constant arrays */
static char *rx_files[] = { "/proc", "/proc/sys", "/proc/sys/fs",
	 SYSFS_BINFMT_MISC, NULL };
static char *w_files[] = { SYSFS_BINFMT_MISC_REG, NULL };

static char* const args[] = { BUILD, NULL };

/* test access modes for files, return OK or FAIL */
enum okfail test_access_files(char *files[], int mode, const char *errstr)
{
	int i;

	for (i = 0; files[i] != NULL; i++) {
		if (access(files[i], mode) != 0) {
			fprintf(stderr, "%s: %s: fails test\n",
				files[i], errstr);
			return FAIL;
		}
	}

	return OK;
}

/* find a string in the given file, return OK or FAIL */
enum okfail strfile(const char *filename, const char *string)
{
	char buf[BUFSIZ];
	FILE *fp;
	enum okfail found = FAIL;

	fp = fopen(filename, "r");
	if (fp == NULL)
	{
		perror(filename);
		return FAIL;
	}
	while (fgets(buf, sizeof(buf), fp) != NULL)
	{
		if (strcmp(buf, string) == 0) {
			found = OK;
			break;
		}

	}
	(void)fclose(fp);

	return found;
}

/* write the file with given string, return OK or FAIL */
enum okfail write_file_string(const char *filename, const char *string)
{
	int fd;

	if ((fd = open(filename, O_WRONLY)) == -1)
	{
		perror(filename);
		return FAIL;
	}

	if (write(fd, string, strlen(string)) == -1)
	{
		perror("write");
		fprintf(stderr, "%s: write failed\n", filename);
		close(fd);
		return FAIL;
	}

	close(fd);
	return OK;
}

#ifdef DEBUG
/* dump contents of the file to stderr, return OK or FAIL */
enum okfail dump_file(char *path)
{
	FILE *fp;
	char buf[BUFSIZ];

	fp = fopen(path, "r");
	if (fp == NULL) {
		perror(path);
		return FAIL;
	}

	while (fgets(buf, sizeof(buf), fp) != NULL)
	{
		fputs(buf, stderr);
	}

	fclose(fp);
	return OK;
}
#endif /* DEBUG */

/* parse datafile and register (to regfile) all binary formats found */
enum okfail binfmt_register(char *datafile, char *regfile)
{
	char buf[BUFSIZ];
	FILE *fp;
	int line;
	struct utsname myuname;
	uname(&myuname);

	fp = fopen(datafile, "r");
	if (fp == NULL)
	{
		perror(datafile);
		return FAIL;
	}

	for (line = 1; fgets(buf, sizeof(buf), fp) != NULL; line++)
	{
		char tokens[BUFSIZ];
		char *s = tokens;
		char *blacklist;
		char *f[n_fields];	/* field content pointers */
		int n;			/* current field */
		char path[BUFSIZ];

		if (buf[0] != ':')	/* non-data input line */
		{
			continue;
		}
		blacklist = strchr(buf, ' ');
		if (blacklist) {
			int skip = 0;
			char *eol;

			*blacklist = '\0';
			blacklist++;

			eol = strchr(blacklist, '\n');
			if (eol)
				*eol = '\0';

			for (n = 0; blacklist != NULL; n++)
			{
				char *bp = strsep(&blacklist, " ");
				if (!strcmp(bp, myuname.machine)) {
#ifdef DEBUG
					fprintf(stderr, " skipping on hostarch %s line %s\n", bp, buf);
#endif /* DEBUG */
					skip = 1;
					break;
				}
			}
			if (skip)
				continue;
		}

		/* copy buf and tokenize :-seperated fields into f[] */
		strcpy(tokens, buf);
		for (n = 0; s != NULL && n < n_fields; n++)
		{
			f[n] = strsep(&s, ":");
		}

#ifdef DEBUG
		int i;
		fprintf(stderr, "DEBUG: line %d, fields %d:\n",  line, n);
		for (i = name; i < n; i++)
		{
			fprintf(stderr, " %s %s\n", fieldnames[i], f[i]);
		}
#endif /* DEBUG */

		if (n == n_fields && s != NULL)
		{
			fprintf(stderr, "%s: line %d: extra fields, ignoring."
				" Content: %s", datafile, line, buf);
			continue;
		}

		if (n < n_fields)
		{
			fprintf(stderr, "%s: line %d: missing fields, ignoring."
				" Content: %s", datafile, line, buf);
			continue;
		}

		int ret;
                /* Is an interpreter for this arch already registered? */
		snprintf(path, sizeof(path), SYSFS_BINFMT_MISC "/%s", f[name]);
		ret=access(path, F_OK);
		if (ret == 0) {
#ifdef DEBUG
			fprintf(stderr, 
				"interpreter for '%s' already registered, ignoring\n",
				f[name]);
#endif /* DEBUG */
			continue;
		}
#ifdef DEBUG
		fprintf(stderr, 
			"registering interpreter for '%s'...\n",
			f[name]);
#endif /* DEBUG */

        /* Does the interpreter exists? */
		ret=access(f[interpreter], X_OK);
		if (ret != 0) {
			fprintf(stderr, 
				"%s: line %d: interpreter '%s' not found,"
				" ignoring, return %d\n", datafile, line, f[interpreter], ret);
			continue;
		}

		if (!write_file_string(regfile, buf)) {
			fprintf(stderr, "%s: line %d: write failed."
				" Content: %s\n", datafile, line, buf);
			(void)fclose(fp);
			return FAIL;
		}

		/* verify registration completed correctly */
		snprintf(path, sizeof(path), SYSFS_BINFMT_MISC "/%s", f[name]);

		if (access(path, R_OK) != 0) {
			fprintf(stderr, 
				"%s: line %d: binfmt path not created, content '%s'\n",
				path, line, buf);
			(void)fclose(fp);
			return FAIL;
		}

		DBG(fprintf(stderr, "dumping: %s\n", path));
		DBG(dump_file(path));
	}


	(void)fclose(fp);

	return OK;
}

/* set up/verify binfmt FS support, program more binfmts, and launch build */
int main(int argc, char* argv[], char* env[])
{
	int retval;
	char buf[BUFSIZ], *build_dir;

        if (system("mount | grep 'proc on /proc type proc' >/dev/null") != 0) {
	        /* mount proc filesystem if it isn't already. */
		if (mount("proc", "/proc", "proc", MS_MGC_VAL, NULL) == -1) {
			if (errno != EBUSY) {
				perror("mount: /proc");
				exit(1);
			}
		}
        }

        /* need to have binfmt_misc loaded already */
        if (system("mount | grep 'binfmt_misc on /proc/sys/fs/binfmt_misc' >/dev/null") != 0) {
		/* try to load binfmt module if present, no big deal if it fails */
		if ((retval = system("/sbin/modprobe binfmt_misc")) != 0) {
			DBG(fprintf(stderr, "modprobe binfmt_misc exit code %d\n",
				retval));
		}

		/* mount binfmt filesystem */
		if (mount("binfmt_misc", SYSFS_BINFMT_MISC, "binfmt_misc", MS_MGC_VAL,
			NULL) == -1) {
			if (errno != EBUSY) {
				perror("mount: binfmt_misc, " SYSFS_BINFMT_MISC);
			}
		}
        }

	/* verify all paths resulting from this are OK */
	if (!test_access_files(rx_files, R_OK|X_OK, "read/search")) {
		exit(1);
	}
	if (!test_access_files(w_files, W_OK, "write")) {
		exit(1);
	}

	if (!strfile("/proc/filesystems", "nodev\tbinfmt_misc\n")) {
		fprintf(stderr,
			"/proc/filesystems: binfmt_misc support missing\n");
		exit(1);
	}

	if (!strfile(SYSFS_BINFMT_MISC_STAT, "enabled\n")) {
		fprintf(stderr,
			"%s: binfmt_misc filesystem support not enabled\n",
			SYSFS_BINFMT_MISC_STAT);
		exit(1);
	}

	*buf = 0;
	build_dir = getenv("BUILD_DIR");
	if (build_dir && strlen(build_dir) < sizeof(buf) - 10)
		sprintf(buf, "%s/qemu-reg", build_dir);

        if (!*buf || !binfmt_register(buf, SYSFS_BINFMT_MISC_REG)) {
		/* setup all done, do the registration */
		if (!binfmt_register(BINFMT_REGF_0, SYSFS_BINFMT_MISC_REG)) {
			fprintf(stderr, "%s: failed. Trying alternate binfmt file\n",
				BINFMT_REGF_0);
			if (!binfmt_register(BINFMT_REGF_1, SYSFS_BINFMT_MISC_REG)) {
				fprintf(stderr, "%s: binfmt registration failed\n",
					BINFMT_REGF_1);
				exit(1);
			}
		}
	}

	/* if we are the init process, start build */
	if (getpid() == 1)
	{
		if (access(BUILD, F_OK) != 0) {
			fprintf(stderr, "%s: build executable missing\n",
				BUILD);
			exit(1);
		}
		if (access(BUILD, X_OK) != 0) {
			fprintf(stderr, "%s: not executable\n", BUILD);
			exit(1);
		}
		execve(BUILD, args, env);
		perror("execve of "BUILD);
		exit(1);
	}

	/* success! */
	exit(0);
}