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
|
Subject: [PATCH, HACK]: linux-user: handle binfmt-misc P flag as a separate exe name
From: Michael Tokarev <mjt@tls.msk.ru>
Date: Sat, 13 Feb 2021 13:57:52 +0300
A hackish way to distinguish the case when qemu-user binary is executed
using in-kernel binfmt-misc subsystem with P flag (preserve argv).
We register binfmt interpreter under name /usr/libexec/qemu-binfmt/qemu-foo-binfmt-P
(which is just a symlink to ../../bin/qemu-foo), and if run like that,
qemu-user binary will "know" it should interpret argv[1] & argv[2]
in a special way.
diff --git a/linux-user/main.c b/linux-user/main.c
index 24d1eb73ad..5596dab9be 100644
--- a/linux-user/main.c
+++ b/linux-user/main.c
@@ -560,6 +560,27 @@ static int parse_args(int argc, char **argv)
}
}
+ /* HACK alert.
+ * when run as an interpreter using kernel's binfmt-misc mechanism,
+ * we have to know where are we (our own binary), where's the binary being run,
+ * and what it's argv[0] element.
+ * Only with the P interpreter flag kernel passes all 3 elements as first 3 argv[],
+ * but we can't distinguish if we were run with or without this P flag.
+ * So we register a special name with binfmt-misc system, a name which ends up
+ * in "-binfmt-P", and if our argv[0] ends up with that, we assume we were run
+ * from kernel's binfmt with P flag and our first 3 args are from kernel.
+ */
+ if (strlen(argv[0]) > sizeof("binfmt-P") &&
+ strcmp(argv[0] + strlen(argv[0]) - sizeof("binfmt-P"), "-binfmt-P") == 0) {
+ if (argc < 3) {
+ (void) fprintf(stderr, "qemu: %s has to be run using kernel binfmt-misc subsystem\n", argv[0]);
+ exit(EXIT_FAILURE);
+ }
+ exec_path = argv[1];
+ handle_arg_argv0(argv[2]);
+ return 2;
+ }
+
optind = 1;
for (;;) {
if (optind >= argc) {
|