qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: YAMAMOTO Takashi <yamamoto@midokura.com>
To: qemu-devel@nongnu.org
Cc: YAMAMOTO Takashi <yamamoto@midokura.com>,
	Laurent Vivier <laurent@vivier.eu>
Subject: [PATCH v2 03/11] linux-user: dup the execfd on start up
Date: Mon, 31 May 2021 14:50:10 +0900	[thread overview]
Message-ID: <20210531055019.10149-4-yamamoto@midokura.com> (raw)
In-Reply-To: <20210531055019.10149-1-yamamoto@midokura.com>

So that it can be used for other purposes (e.g. syscall.c)
after the elf loader closed it.

Signed-off-by: YAMAMOTO Takashi <yamamoto@midokura.com>
---
 linux-user/main.c    | 10 +++++++++-
 linux-user/qemu.h    |  2 ++
 linux-user/syscall.c |  5 ++---
 3 files changed, 13 insertions(+), 4 deletions(-)

diff --git a/linux-user/main.c b/linux-user/main.c
index 4dfc47ad3b..a9d02f9583 100644
--- a/linux-user/main.c
+++ b/linux-user/main.c
@@ -55,6 +55,7 @@
 #endif
 
 char *exec_path;
+int exec_fd = -1;
 
 int singlestep;
 static const char *argv0;
@@ -693,7 +694,14 @@ int main(int argc, char **argv, char **envp)
      * Manage binfmt-misc open-binary flag
      */
     execfd = qemu_getauxval(AT_EXECFD);
-    if (execfd == 0) {
+    if (execfd > 0) {
+        /*
+         * dup execfd to a global so that it can be used after loader_exec
+         * closes it.
+         */
+
+        exec_fd = dup(execfd);
+    } else {
         execfd = open(exec_path, O_RDONLY);
         if (execfd < 0) {
             printf("Error while loading %s: %s\n", exec_path, strerror(errno));
diff --git a/linux-user/qemu.h b/linux-user/qemu.h
index 3b0b6b75fe..ee4e9a1779 100644
--- a/linux-user/qemu.h
+++ b/linux-user/qemu.h
@@ -160,6 +160,8 @@ typedef struct TaskState {
 } __attribute__((aligned(16))) TaskState;
 
 extern char *exec_path;
+extern int exec_fd;
+
 void init_task_state(TaskState *ts);
 void task_settid(TaskState *);
 void stop_all_tasks(void);
diff --git a/linux-user/syscall.c b/linux-user/syscall.c
index 14a63518e2..2947e79dc0 100644
--- a/linux-user/syscall.c
+++ b/linux-user/syscall.c
@@ -8117,12 +8117,11 @@ static int do_openat(void *cpu_env, int dirfd, const char *pathname, int flags,
     };
 
     if (is_proc_myself(pathname, "exe")) {
-        int execfd = qemu_getauxval(AT_EXECFD);
-        if (execfd) {
+        if (exec_fd != -1) {
             char filename[PATH_MAX];
             int ret;
 
-            snprintf(filename, sizeof(filename), "/proc/self/fd/%d", execfd);
+            snprintf(filename, sizeof(filename), "/proc/self/fd/%d", exec_fd);
             ret = safe_openat(dirfd, filename, flags, mode);
             if (ret != -1) {
                 return ret;
-- 
2.21.1 (Apple Git-122.3)



  parent reply	other threads:[~2021-05-31  5:53 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-05-31  5:50 [PATCH v2 00/11] linux-user changes to run docker YAMAMOTO Takashi
2021-05-31  5:50 ` [PATCH v2 01/11] linux-user: handle /proc/self/exe for execve YAMAMOTO Takashi
2021-06-20 14:14   ` Laurent Vivier
2021-06-21  2:02     ` Takashi Yamamoto
2021-06-22 13:47       ` Laurent Vivier
2021-05-31  5:50 ` [PATCH v2 02/11] linux-user: Fix the execfd case of /proc/self/exe open YAMAMOTO Takashi
2021-06-20 14:16   ` Laurent Vivier
2021-06-21  1:19     ` Takashi Yamamoto
2021-05-31  5:50 ` YAMAMOTO Takashi [this message]
2021-05-31  5:50 ` [PATCH v2 04/11] linux-user: make exec_path realpath YAMAMOTO Takashi
2021-05-31  5:50 ` [PATCH v2 05/11] linux-user: Implement pivot_root YAMAMOTO Takashi
2021-06-20 14:02   ` Laurent Vivier
2021-06-20 14:05   ` Laurent Vivier
2021-05-31  5:50 ` [PATCH v2 06/11] linux-user: add get_exe_path YAMAMOTO Takashi
2021-05-31  5:50 ` [PATCH v2 07/11] linux-user: simplify is_proc_myself YAMAMOTO Takashi
2021-05-31  5:50 ` [PATCH v2 08/11] linux-user: Implement exec of /proc/$pid/exe of qemu process YAMAMOTO Takashi
2021-05-31  5:50 ` [PATCH v2 09/11] linux-user: Make the qemu detection for /proc/$pid/exe a bit conservative YAMAMOTO Takashi
2021-05-31  5:50 ` [PATCH v2 10/11] linux-user: a crude hack for libcontainer (CLONE_PARENT) [!MERGE] YAMAMOTO Takashi
2021-05-31  5:50 ` [PATCH v2 11/11] linux-user: always assume preserve_argv0 for now [!MERGE] YAMAMOTO Takashi
2021-05-31  6:07 ` [PATCH v2 00/11] linux-user changes to run docker no-reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210531055019.10149-4-yamamoto@midokura.com \
    --to=yamamoto@midokura.com \
    --cc=laurent@vivier.eu \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).