diff mbox

linux-user: added fake open() for /proc/self/cmdline

Message ID 1403082159-14710-2-git-send-email-lists@fixnum.org
State New
Headers show

Commit Message

lists@fixnum.org June 18, 2014, 9:02 a.m. UTC
From: Wim Vander Schelden <wim@fixnum.org>

Signed-off-by: Wim Vander Schelden <wim@fixnum.org>
---
 linux-user/syscall.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 46 insertions(+)

Comments

Riku Voipio June 20, 2014, 12:37 p.m. UTC | #1
On Wed, Jun 18, 2014 at 11:02:39AM +0200, lists@fixnum.org wrote:
> From: Wim Vander Schelden <wim@fixnum.org>

Seems to work,
applied to linux-user updates 

> Signed-off-by: Wim Vander Schelden <wim@fixnum.org>
> ---
>  linux-user/syscall.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 46 insertions(+)
> 
> diff --git a/linux-user/syscall.c b/linux-user/syscall.c
> index c134c32..1be0f09 100644
> --- a/linux-user/syscall.c
> +++ b/linux-user/syscall.c
> @@ -4947,6 +4947,51 @@ int host_to_target_waitstatus(int status)
>      return status;
>  }
>  
> +static int open_self_cmdline(void *cpu_env, int fd)
> +{
> +    int fd_orig = -1;
> +    bool word_skipped = false;
> +
> +    fd_orig = open("/proc/self/cmdline", O_RDONLY);
> +    if (fd_orig < 0) {
> +        return fd_orig;
> +    }
> +
> +    while (true) {
> +        ssize_t nb_read;
> +        char buf[128];
> +        char *cp_buf = buf;
> +
> +        nb_read = read(fd_orig, buf, sizeof(buf));
> +        if (nb_read < 0) {
> +            fd_orig = close(fd_orig);
> +            return -1;
> +        } else if (nb_read == 0) {
> +            break;
> +        }
> +
> +        if (!word_skipped) {
> +            /* Skip the first string, which is the path to qemu-*-static
> +               instead of the actual command. */
> +            cp_buf = memchr(buf, 0, sizeof(buf));
> +            if (cp_buf) {
> +                /* Null byte found, skip one string */
> +                cp_buf++;
> +                nb_read -= cp_buf - buf;
> +                word_skipped = true;
> +            }
> +        }
> +
> +        if (word_skipped) {
> +            if (write(fd, cp_buf, nb_read) != nb_read) {
> +                return -1;
> +            }
> +        }
> +    }
> +
> +    return close(fd_orig);
> +}
> +
>  static int open_self_maps(void *cpu_env, int fd)
>  {
>  #if defined(TARGET_ARM) || defined(TARGET_M68K) || defined(TARGET_UNICORE32)
> @@ -5148,6 +5193,7 @@ static int do_open(void *cpu_env, const char *pathname, int flags, mode_t mode)
>          { "maps", open_self_maps, is_proc_myself },
>          { "stat", open_self_stat, is_proc_myself },
>          { "auxv", open_self_auxv, is_proc_myself },
> +        { "cmdline", open_self_cmdline, is_proc_myself },
>  #if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN)
>          { "/proc/net/route", open_net_route, is_proc },
>  #endif
> -- 
> 1.9.1
>
diff mbox

Patch

diff --git a/linux-user/syscall.c b/linux-user/syscall.c
index c134c32..1be0f09 100644
--- a/linux-user/syscall.c
+++ b/linux-user/syscall.c
@@ -4947,6 +4947,51 @@  int host_to_target_waitstatus(int status)
     return status;
 }
 
+static int open_self_cmdline(void *cpu_env, int fd)
+{
+    int fd_orig = -1;
+    bool word_skipped = false;
+
+    fd_orig = open("/proc/self/cmdline", O_RDONLY);
+    if (fd_orig < 0) {
+        return fd_orig;
+    }
+
+    while (true) {
+        ssize_t nb_read;
+        char buf[128];
+        char *cp_buf = buf;
+
+        nb_read = read(fd_orig, buf, sizeof(buf));
+        if (nb_read < 0) {
+            fd_orig = close(fd_orig);
+            return -1;
+        } else if (nb_read == 0) {
+            break;
+        }
+
+        if (!word_skipped) {
+            /* Skip the first string, which is the path to qemu-*-static
+               instead of the actual command. */
+            cp_buf = memchr(buf, 0, sizeof(buf));
+            if (cp_buf) {
+                /* Null byte found, skip one string */
+                cp_buf++;
+                nb_read -= cp_buf - buf;
+                word_skipped = true;
+            }
+        }
+
+        if (word_skipped) {
+            if (write(fd, cp_buf, nb_read) != nb_read) {
+                return -1;
+            }
+        }
+    }
+
+    return close(fd_orig);
+}
+
 static int open_self_maps(void *cpu_env, int fd)
 {
 #if defined(TARGET_ARM) || defined(TARGET_M68K) || defined(TARGET_UNICORE32)
@@ -5148,6 +5193,7 @@  static int do_open(void *cpu_env, const char *pathname, int flags, mode_t mode)
         { "maps", open_self_maps, is_proc_myself },
         { "stat", open_self_stat, is_proc_myself },
         { "auxv", open_self_auxv, is_proc_myself },
+        { "cmdline", open_self_cmdline, is_proc_myself },
 #if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN)
         { "/proc/net/route", open_net_route, is_proc },
 #endif