qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: aladjev.andrew@gmail.com
To: qemu-devel@nongnu.org
Cc: Andrew Aladjev <aladjev.andrew@gmail.com>
Subject: [PATCH 1/4] linux user: make execfd global (like exec path) and keep it open
Date: Thu, 25 Feb 2021 23:54:45 +0300	[thread overview]
Message-ID: <20210225205448.10624-1-aladjev.andrew@gmail.com> (raw)

From: Andrew Aladjev <aladjev.andrew@gmail.com>

User opens /proc/self/exe symlink, than kernel should create /proc/self/fd/<execfd> symlink. We should be able to detect both exe and fd/<execfd> symlinks to provide common behaviour. The easiest solution is to make execfd global and keep it open. This solution looks acceptable because exec_path is already global. PS load_flt_binary is not closing bprm->fd, so load_elf_binary may not close it too (used symmetrically in loader_exec).
---
 linux-user/elfload.c |  3 ++-
 linux-user/exit.c    |  2 ++
 linux-user/main.c    |  2 +-
 linux-user/qemu.h    |  1 +
 linux-user/syscall.c | 16 ++++++++++++----
 5 files changed, 18 insertions(+), 6 deletions(-)

diff --git a/linux-user/elfload.c b/linux-user/elfload.c
index bab4237..4c347b0 100644
--- a/linux-user/elfload.c
+++ b/linux-user/elfload.c
@@ -2600,6 +2600,7 @@ static bool parse_elf_properties(int image_fd,
 
    IMAGE_NAME is the filename of the image, to use in error messages.
    IMAGE_FD is the open file descriptor for the image.
+   WARNING: IMAGE_FD won't be closed.
 
    BPRM_BUF is a copy of the beginning of the file; this of course
    contains the elf file header at offset 0.  It is assumed that this
@@ -2910,7 +2911,6 @@ static void load_elf_image(const char *image_name, int image_fd,
 
     mmap_unlock();
 
-    close(image_fd);
     return;
 
  exit_read:
@@ -2953,6 +2953,7 @@ static void load_elf_interp(const char *filename, struct image_info *info,
     }
 
     load_elf_image(filename, fd, info, NULL, bprm_buf);
+    close(fd);
 }
 
 static int symfind(const void *s0, const void *s1)
diff --git a/linux-user/exit.c b/linux-user/exit.c
index 70b3440..cc9cf38 100644
--- a/linux-user/exit.c
+++ b/linux-user/exit.c
@@ -28,6 +28,8 @@ extern void __gcov_dump(void);
 
 void preexit_cleanup(CPUArchState *env, int code)
 {
+    close(execfd);
+
 #ifdef CONFIG_GPROF
         _mcleanup();
 #endif
diff --git a/linux-user/main.c b/linux-user/main.c
index 81f48ff..d365335 100644
--- a/linux-user/main.c
+++ b/linux-user/main.c
@@ -50,6 +50,7 @@
 #include "crypto/init.h"
 
 char *exec_path;
+int execfd;
 
 int singlestep;
 static const char *argv0;
@@ -628,7 +629,6 @@ int main(int argc, char **argv, char **envp)
     int target_argc;
     int i;
     int ret;
-    int execfd;
     int log_mask;
     unsigned long max_reserved_va;
 
diff --git a/linux-user/qemu.h b/linux-user/qemu.h
index 52c9817..ec26730 100644
--- a/linux-user/qemu.h
+++ b/linux-user/qemu.h
@@ -160,6 +160,7 @@ typedef struct TaskState {
 } __attribute__((aligned(16))) TaskState;
 
 extern char *exec_path;
+extern int execfd;
 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 389ec09..c171dea 100644
--- a/linux-user/syscall.c
+++ b/linux-user/syscall.c
@@ -8110,8 +8110,7 @@ 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);
-        return execfd ? execfd : safe_openat(dirfd, exec_path, flags, mode);
+        return execfd;
     }
 
     for (fake_open = fakes; fake_open->filename; fake_open++) {
@@ -8369,8 +8368,17 @@ static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1,
         return ret;
 #endif
     case TARGET_NR_close:
-        fd_trans_unregister(arg1);
-        return get_errno(close(arg1));
+        {
+            int fd = arg1;
+
+            /* We don't need to close execfd, it will be closed on QEMU exit. */
+            if (fd == execfd) {
+                return 0;
+            }
+
+            fd_trans_unregister(fd);
+            return get_errno(close(fd));
+        }
 
     case TARGET_NR_brk:
         return do_brk(arg1);
-- 
2.26.2



             reply	other threads:[~2021-02-25 20:56 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-02-25 20:54 aladjev.andrew [this message]
2021-02-25 20:54 ` [PATCH 2/4] linux user: moved is proc functions to separate file aladjev.andrew
2021-02-25 20:54 ` [PATCH 3/4] linux user: refactored is proc myself, added support for fd/<execfd> aladjev.andrew
2021-02-25 20:54 ` [PATCH 4/4] linux user: added tests for proc myself aladjev.andrew

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=20210225205448.10624-1-aladjev.andrew@gmail.com \
    --to=aladjev.andrew@gmail.com \
    --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).