From: Andrew Aladjev <aladjev.and...@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