3 * initvm - init for qemu, setup binfmt_misc launch build
9 * This is the kernel init script for virtual machines which will
10 * be running executables for an embedded (non-native)
11 * architecture. It registers binfmt_misc handlers for qemu and
12 * executes the build script, and tests many assumptions.
16 * text file with lines to stuff into the binfmt_misc
17 * filesystem registration file
19 * build script to execute once binfmts are set up
22 * James Perkins <james.perkins@linuxfoundation.org>
25 #include <sys/mount.h>
32 /* to enable debugging, compile with -DDEBUG */
34 #define DBG(x) do { x; } while(0)
39 /* function return codes */
40 enum okfail { FAIL=0, OK=1 };
42 /* qemu registration fields, see kernel/Documentation/binfmt_misc.txt */
43 enum fields { ignore=0, name, type, offset, magic, mask, interpreter, flags };
44 const char * const fieldnames[] = {
45 "ignore", "name", "type", "offset",
46 "magic", "mask", "interpreter", "flags"
48 const int n_fields = 8;
50 /* files in useful places */
51 #define SYSFS_BINFMT_MISC "/proc/sys/fs/binfmt_misc"
52 #define SYSFS_BINFMT_MISC_REG "/proc/sys/fs/binfmt_misc/register"
53 #define SYSFS_BINFMT_MISC_STAT "/proc/sys/fs/binfmt_misc/status"
55 /* /usr/lib/build/x paths are copied to /.build inside a virtual machine */
56 #define BINFMT_REGF_0 "/.build/qemu-reg"
57 #define BINFMT_REGF_1 "/usr/lib/build/qemu-reg"
58 #define BUILD "/.build/build"
60 /* useful constant arrays */
61 static char *rx_files[] = { "/proc", "/proc/sys", "/proc/sys/fs",
62 SYSFS_BINFMT_MISC, NULL };
63 static char *w_files[] = { SYSFS_BINFMT_MISC_REG, NULL };
65 static char* const args[] = { BUILD, NULL };
67 /* test access modes for files, return OK or FAIL */
68 enum okfail test_access_files(char *files[], int mode, const char *errstr)
72 for (i = 0; files[i] != NULL; i++) {
73 if (access(files[i], mode) != 0) {
74 fprintf(stderr, "%s: %s: fails test\n",
83 /* find a string in the given file, return OK or FAIL */
84 enum okfail strfile(const char *filename, const char *string)
88 enum okfail found = FAIL;
90 fp = fopen(filename, "r");
96 while (fgets(buf, sizeof(buf), fp) != NULL)
98 if (strcmp(buf, string) == 0) {
109 /* write the file with given string, return OK or FAIL */
110 enum okfail write_file_string(const char *filename, const char *string)
114 if ((fd = open(filename, O_WRONLY)) == -1)
120 if (write(fd, string, strlen(string)) == -1)
123 fprintf(stderr, "%s: write failed\n", filename);
133 /* dump contents of the file to stderr, return OK or FAIL */
134 enum okfail dump_file(char *path)
139 fp = fopen(path, "r");
145 while (fgets(buf, sizeof(buf), fp) != NULL)
155 /* parse datafile and register (to regfile) all binary formats found */
156 enum okfail binfmt_register(char *datafile, char *regfile)
162 fp = fopen(datafile, "r");
169 for (line = 1; fgets(buf, sizeof(buf), fp) != NULL; line++)
173 char *f[n_fields]; /* field content pointers */
174 int n; /* current field */
177 if (buf[0] != ':') /* non-data input line */
182 /* copy buf and tokenize :-seperated fields into f[] */
184 for (n = 0; s != NULL && n < n_fields; n++)
186 f[n] = strsep(&s, ":");
191 fprintf(stderr, "DEBUG: line %d, fields %d:\n", line, n);
192 for (i = name; i < n; i++)
194 fprintf(stderr, " %s %s\n", fieldnames[i], f[i]);
198 if (n == n_fields && s != NULL)
200 fprintf(stderr, "%s: line %d: extra fields, ignoring."
201 " Content: %s", datafile, line, buf);
207 fprintf(stderr, "%s: line %d: missing fields, ignoring."
208 " Content: %s", datafile, line, buf);
213 if (access(f[interpreter], X_OK) != 0) {
215 "%s: line %d: interpreter '%s' not found,"
216 " ignoring\n", datafile, line, f[interpreter]);
220 if (!write_file_string(regfile, buf)) {
221 fprintf(stderr, "%s: line %d: write failed."
222 " Content: %s\n", datafile, line, buf);
227 /* verify registration completed correctly */
228 snprintf(path, sizeof(path), SYSFS_BINFMT_MISC "/%s", f[name]);
230 if (access(path, R_OK) != 0) {
232 "%s: line %d: binfmt path not created, content '%s'\n",
238 DBG(fprintf(stderr, "dumping: %s\n", path));
239 DBG(dump_file(path));
251 /* set up/verify binfmt FS support, program more binfmts, and launch build */
252 int main(int argc, char* argv[], char* env[])
256 /* mount proc filesystem if it isn't already */
257 if (mount("proc", "/proc", "proc", MS_MGC_VAL, NULL) == -1) {
258 if (errno != EBUSY) {
259 perror("mount: /proc");
264 /* try to load binfmt module if present, no big deal if it fails */
265 if ((retval = system("/sbin/modprobe binfmt_misc")) != 0) {
266 DBG(fprintf(stderr, "modprobe binfmt_misc exit code %d\n",
270 /* mount binfmt filesystem */
271 if (mount("binfmt_misc", SYSFS_BINFMT_MISC, "binfmt_misc", MS_MGC_VAL,
273 if (errno != EBUSY) {
274 perror("mount: binfmt_misc, " SYSFS_BINFMT_MISC);
278 /* verify all paths resulting from this are OK */
279 if (!test_access_files(rx_files, R_OK|X_OK, "read/search")) {
282 if (!test_access_files(w_files, W_OK, "write")) {
286 if (!strfile("/proc/filesystems", "nodev\tbinfmt_misc\n")) {
288 "/proc/filesystems: binfmt_misc support missing\n");
292 if (!strfile(SYSFS_BINFMT_MISC_STAT, "enabled\n")) {
294 "%s: binfmt_misc filesystem support not enabled\n",
295 SYSFS_BINFMT_MISC_STAT);
299 /* setup all done, do the registration */
300 if (!binfmt_register(BINFMT_REGF_0, SYSFS_BINFMT_MISC_REG)) {
301 fprintf(stderr, "%s: failed. Trying alternate binfmt file\n",
303 if (!binfmt_register(BINFMT_REGF_1, SYSFS_BINFMT_MISC_REG)) {
304 fprintf(stderr, "%s: binfmt registration failed\n",
310 /* if we are the init process, start build */
313 if (access(BUILD, F_OK) != 0) {
314 fprintf(stderr, "%s: build executable missing\n",
318 if (access(BUILD, X_OK) != 0) {
319 fprintf(stderr, "%s: not executable\n", BUILD);
322 execve(BUILD, args, env);