qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Stacey Son <sson@FreeBSD.org>
To: qemu-devel@nongnu.org
Cc: Stacey Son <sson@FreeBSD.org>
Subject: [Qemu-devel] [PATCH 07/23] bsd-user: find target executable in path when absolute path not given
Date: Sun, 23 Jun 2013 21:03:39 -0500	[thread overview]
Message-ID: <1372039435-41921-8-git-send-email-sson@FreeBSD.org> (raw)
In-Reply-To: <1372039435-41921-1-git-send-email-sson@FreeBSD.org>

If the target executable's path is not absolute then this code will search
the PATH to find it. Save the fullpath to put on to the stack for the
runtime linker.

Signed-off-by: Stacey Son <sson@FreeBSD.org>
---
 bsd-user/bsdload.c |   84 +++++++++++++++++++++++++++++++++++++++++++++++++--
 bsd-user/qemu.h    |    3 +-
 2 files changed, 82 insertions(+), 5 deletions(-)

diff --git a/bsd-user/bsdload.c b/bsd-user/bsdload.c
index cc4f534..c768855 100644
--- a/bsd-user/bsdload.c
+++ b/bsd-user/bsdload.c
@@ -169,19 +169,95 @@ abi_ulong loader_build_argptr(int envc, int argc, abi_ulong sp,
     return sp;
 }
 
+static int is_there(const char *candidate)
+{
+    struct stat fin;
+
+    /* XXX work around access(2) false positives for superuser */
+    if (access(candidate, X_OK) == 0 && stat(candidate, &fin) == 0 &&
+            S_ISREG(fin.st_mode) && (getuid() != 0 ||
+                (fin.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) != 0)) {
+        return 1;
+    }
+
+    return 0;
+}
+
+static int find_in_path(char *path, const char *filename, char *retpath,
+        size_t rpsize)
+{
+    const char *d;
+    int found;
+
+    if (strchr(filename, '/') != NULL) {
+        if (is_there(filename)) {
+                if (!realpath(filename, retpath)) {
+                    return -1;
+                }
+                return 0;
+        } else {
+            return -1;
+        }
+    }
+
+    found = 0;
+    while ((d = strsep(&path, ":")) != NULL) {
+        if (*d == '\0') {
+            d = ".";
+        }
+        if (snprintf(retpath, rpsize, "%s/%s", d, filename) >= (int)rpsize) {
+            continue;
+        }
+        if (is_there((const char *)retpath)) {
+            found = 1;
+            break;
+        }
+    }
+    return found;
+}
+
 int loader_exec(const char * filename, char ** argv, char ** envp,
              struct target_pt_regs *regs, struct image_info *infop,
              struct bsd_binprm *bprm)
 {
-    int retval;
-    int i;
+    char *p, *path = NULL, fullpath[PATH_MAX];
+    const char *execname = NULL;
+    int retval, i;
 
-    bprm->p = TARGET_PAGE_SIZE*MAX_ARG_PAGES-sizeof(unsigned int);
+    bprm->p = TARGET_PAGE_SIZE * MAX_ARG_PAGES; /* -sizeof(unsigned int); */
     for (i=0 ; i<MAX_ARG_PAGES ; i++)       /* clear page-table */
             bprm->page[i] = NULL;
-    retval = open(filename, O_RDONLY);
+
+    /* Find target executable in path, if not already an absolute path. */
+    p = getenv("PATH");
+    if (p != NULL) {
+        path = g_strdup(p);
+        if (path == NULL) {
+            fprintf(stderr, "Out of memory\n");
+            return -1;
+        }
+        execname = realpath(filename, NULL);
+        if (execname == NULL) {
+            execname = filename;
+        }
+        if (!find_in_path(path, execname, fullpath, sizeof(fullpath))) {
+            retval = open(fullpath, O_RDONLY);
+            bprm->fullpath = g_strdup(fullpath);
+        } else {
+            retval = open(execname, O_RDONLY);
+            bprm->fullpath = NULL;
+        }
+        if (execname) {
+            free((void *)execname);
+        }
+        free(path);
+    } else {
+        retval = open(filename, O_RDONLY);
+        bprm->fullpath = NULL;
+    }
     if (retval < 0)
         return retval;
+
     bprm->fd = retval;
     bprm->filename = (char *)filename;
     bprm->argc = count(argv);
diff --git a/bsd-user/qemu.h b/bsd-user/qemu.h
index a36e9d2..1e2abd5 100644
--- a/bsd-user/qemu.h
+++ b/bsd-user/qemu.h
@@ -128,7 +128,8 @@ struct bsd_binprm {
         int argc, envc;
         char **argv;
         char **envp;
-        char *filename;        /* Name of binary */
+        char *filename;        /* (Given) Name of binary */
+        char *fullpath;        /* Full path of binary */
         int (*core_dump)(int, const CPUArchState *);
 };
 
-- 
1.7.8

  parent reply	other threads:[~2013-06-24 16:48 UTC|newest]

Thread overview: 41+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-06-24  2:03 [Qemu-devel] [PATCH 00/23] bsd-user: FreeBSD support for mips/mips64 and arm Stacey Son
2013-06-24  2:03 ` [Qemu-devel] [PATCH 01/23] bsd-user: initial code clean up Stacey Son
2013-06-24  2:03 ` [Qemu-devel] [PATCH 02/23] bsd-user: add initial support for mips/mips64 Stacey Son
2013-06-24 17:15   ` Peter Maydell
2013-06-24 20:09     ` Stacey Son
2013-06-24  2:03 ` [Qemu-devel] [PATCH 03/23] bsd-user: additional seperation of OS and architecture dependent code Stacey Son
2013-06-24 17:20   ` Peter Maydell
2013-06-24 17:24   ` Peter Maydell
2013-06-24 20:15     ` Stacey Son
2013-06-24  2:03 ` [Qemu-devel] [PATCH 04/23] bsd-user: add bsd signal emulation Stacey Son
2013-06-24  2:03 ` [Qemu-devel] [PATCH 05/23] bsd-user: add bsd_binprm to TaskState for core dumping emulation Stacey Son
2013-06-24  2:03 ` [Qemu-devel] [PATCH 06/23] bsd-user: fix thread initialization and ELF addresses for mips/mips64 Stacey Son
2013-06-24  2:03 ` Stacey Son [this message]
2013-06-24  2:03 ` [Qemu-devel] [PATCH 08/23] bsd-user: initialize stack with signal trampolin code and canary Stacey Son
2013-06-24  2:03 ` [Qemu-devel] [PATCH 09/23] bsd-user: refresh FreeBSD's system call numbers Stacey Son
2013-06-24  2:03 ` [Qemu-devel] [PATCH 10/23] bsd-user: add shims for memory management related syscalls Stacey Son
2013-06-24 17:26   ` Peter Maydell
2013-06-24  2:03 ` [Qemu-devel] [PATCH 11/23] bsd-user: add shims for file related system calls Stacey Son
2013-06-24  2:03 ` [Qemu-devel] [PATCH 12/23] bsd-user: add shims for time " Stacey Son
2013-06-24  2:03 ` [Qemu-devel] [PATCH 13/23] bsd-user: add shims for signal " Stacey Son
2013-06-24  2:03 ` [Qemu-devel] [PATCH 14/23] bsd-user: add shims for process " Stacey Son
2013-06-24  2:03 ` [Qemu-devel] [PATCH 15/23] bsd-user: add shims for socket " Stacey Son
2013-06-24  2:03 ` [Qemu-devel] [PATCH 16/23] bsd-user: add shims for stat and file handle related syscalls Stacey Son
2013-06-24  2:03 ` [Qemu-devel] [PATCH 17/23] bsd-user: add shims for thread related system calls Stacey Son
2013-06-24 17:37   ` Peter Maydell
2013-06-24 20:23     ` Stacey Son
2013-06-24  2:03 ` [Qemu-devel] [PATCH 18/23] bsd-user: add shim for the ioctl system call Stacey Son
2013-06-24  2:03 ` [Qemu-devel] [PATCH 19/23] bsd-user: add shims for sysarch() and sysctl() system calls Stacey Son
2013-06-24  2:03 ` [Qemu-devel] [PATCH 20/23] bsd-user: add shims for extended attributes " Stacey Son
2013-06-24  2:03 ` [Qemu-devel] [PATCH 21/23] bsd-user: add miscellaneous system call shims Stacey Son
2013-06-24  2:03 ` [Qemu-devel] [PATCH 22/23] bsd-user: add more strace formating Stacey Son
2013-06-24 17:41   ` Peter Maydell
2013-06-24 20:55     ` Stacey Son
2013-06-24  2:03 ` [Qemu-devel] [PATCH 23/23] bsd-user: add arm cpu support Stacey Son
2013-06-24 17:18   ` Peter Maydell
2013-06-24 17:49 ` [Qemu-devel] [PATCH 00/23] bsd-user: FreeBSD support for mips/mips64 and arm Peter Maydell
2013-06-24 19:18   ` Stacey Son
2013-06-24 17:55 ` Anthony Liguori
2013-06-24 19:29   ` Stacey Son
2013-06-24 20:07     ` Anthony Liguori
2013-06-24 20:40       ` Stacey Son

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=1372039435-41921-8-git-send-email-sson@FreeBSD.org \
    --to=sson@freebsd.org \
    --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).