* [Qemu-devel] [PATCH] linux-user: added fake open() for /proc/self/cmdline
@ 2014-06-17 3:16 lists
2014-06-17 3:16 ` lists
0 siblings, 1 reply; 6+ messages in thread
From: lists @ 2014-06-17 3:16 UTC (permalink / raw)
To: qemu-devel; +Cc: alex.bennee
A piece of software I use depends on /proc/self/cmdline to determine the command with which it was invoked.
When using linux-user, that file would read:
$ hd /proc/self/cmdline
00000000 2f 75 73 72 2f 62 69 6e 2f 71 65 6d 75 2d 61 72 |/usr/bin/qemu-ar|
00000010 6d 2d 73 74 61 74 69 63 00 2f 75 73 72 2f 62 69 |m-static./usr/bi|
00000020 6e 2f 68 64 00 2f 70 72 6f 63 2f 73 65 6c 66 2f |n/hd./proc/self/|
00000030 63 6d 64 6c 69 6e 65 00 |cmdline.|
With this patch, the first word is omitted from the process's own cmdline entry, removing the emulator path from the file:
$ hd /proc/self/cmdline
00000000 2f 75 73 72 2f 62 69 6e 2f 68 64 00 2f 70 72 6f |/usr/bin/hd./pro|
00000010 63 2f 73 65 6c 66 2f 63 6d 64 6c 69 6e 65 00 |c/self/cmdline.|
0000001f
Kind regards,
Wim
^ permalink raw reply [flat|nested] 6+ messages in thread
* [Qemu-devel] [PATCH] linux-user: added fake open() for /proc/self/cmdline
2014-06-17 3:16 [Qemu-devel] [PATCH] linux-user: added fake open() for /proc/self/cmdline lists
@ 2014-06-17 3:16 ` lists
2014-06-17 21:09 ` Eric Blake
0 siblings, 1 reply; 6+ messages in thread
From: lists @ 2014-06-17 3:16 UTC (permalink / raw)
To: qemu-devel; +Cc: Wim Vander Schelden, alex.bennee
From: Wim Vander Schelden <wim@fixnum.org>
---
linux-user/syscall.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 49 insertions(+)
diff --git a/linux-user/syscall.c b/linux-user/syscall.c
index c134c32..f9fed3e 100644
--- a/linux-user/syscall.c
+++ b/linux-user/syscall.c
@@ -4947,6 +4947,54 @@ 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(nb_read == 0)
+ break;
+
+ if(! word_skipped) {
+ // Skip the first string, which is the path to qemu-*-static instead of the actual command.
+ size_t command_length = strnlen(buf, sizeof(buf));
+ if(command_length != sizeof(buf)) {
+ // Null byte found, skip one string
+ nb_read -= command_length + 1;
+ cp_buf += command_length + 1;
+ 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 +5196,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
^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [Qemu-devel] [PATCH] linux-user: added fake open() for /proc/self/cmdline
2014-06-17 3:16 ` lists
@ 2014-06-17 21:09 ` Eric Blake
2014-06-18 9:02 ` lists
0 siblings, 1 reply; 6+ messages in thread
From: Eric Blake @ 2014-06-17 21:09 UTC (permalink / raw)
To: lists, qemu-devel; +Cc: Wim Vander Schelden, alex.bennee
[-- Attachment #1: Type: text/plain, Size: 2257 bytes --]
On 06/16/2014 09:16 PM, lists@fixnum.org wrote:
> From: Wim Vander Schelden <wim@fixnum.org>
>
> ---
Missing a Signed-off-by declaration; without that legal hurdle, we are
unwilling to take the patch.
For more details: http://wiki.qemu.org/Contribute/SubmitAPatch
> linux-user/syscall.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++++
> 1 file changed, 49 insertions(+)
Run your patch through scripts/checkpatch.pl to flag many of the
complaints below.
>
> diff --git a/linux-user/syscall.c b/linux-user/syscall.c
> index c134c32..f9fed3e 100644
> --- a/linux-user/syscall.c
> +++ b/linux-user/syscall.c
> @@ -4947,6 +4947,54 @@ 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);
Why a double space?
> + if(fd_orig < 0) {
Wrong style - we use space after 'if'
> + return fd_orig;
> + }
> +
> + while(true) {
and space after 'while'
> + if(! word_skipped) {
> + // Skip the first string, which is the path to qemu-*-static instead of the actual command.
/* */ comments instead of //; fit 80 columns.
> + size_t command_length = strnlen(buf, sizeof(buf));
> + if(command_length != sizeof(buf)) {
> + // Null byte found, skip one string
> + nb_read -= command_length + 1;
> + cp_buf += command_length + 1;
> + word_skipped = true;
This feels a bit complicated. Why not just use memchr() to locate the
first NUL byte, rather than messing with strnlen()?
> @@ -5148,6 +5196,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},
Match the style of the lines nearby (space before '}')
--
Eric Blake eblake redhat com +1-919-301-3266
Libvirt virtualization library http://libvirt.org
[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 604 bytes --]
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [Qemu-devel] [PATCH] linux-user: added fake open() for /proc/self/cmdline
2014-06-17 21:09 ` Eric Blake
@ 2014-06-18 9:02 ` lists
2014-06-18 9:02 ` lists
0 siblings, 1 reply; 6+ messages in thread
From: lists @ 2014-06-18 9:02 UTC (permalink / raw)
To: qemu-devel; +Cc: alex.bennee
Hi Eric,
Thanks for your feedback.
I've addressed the issues you mentioned and verified the patch with checkpatch.pl.
The updated patch follows.
Kind regards,
Wim
^ permalink raw reply [flat|nested] 6+ messages in thread
* [Qemu-devel] [PATCH] linux-user: added fake open() for /proc/self/cmdline
2014-06-18 9:02 ` lists
@ 2014-06-18 9:02 ` lists
2014-06-20 12:37 ` Riku Voipio
0 siblings, 1 reply; 6+ messages in thread
From: lists @ 2014-06-18 9:02 UTC (permalink / raw)
To: qemu-devel; +Cc: alex.bennee, Wim Vander Schelden
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(+)
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
^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [Qemu-devel] [PATCH] linux-user: added fake open() for /proc/self/cmdline
2014-06-18 9:02 ` lists
@ 2014-06-20 12:37 ` Riku Voipio
0 siblings, 0 replies; 6+ messages in thread
From: Riku Voipio @ 2014-06-20 12:37 UTC (permalink / raw)
To: lists; +Cc: Wim Vander Schelden, alex.bennee, qemu-devel
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
>
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2014-06-20 12:38 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-06-17 3:16 [Qemu-devel] [PATCH] linux-user: added fake open() for /proc/self/cmdline lists
2014-06-17 3:16 ` lists
2014-06-17 21:09 ` Eric Blake
2014-06-18 9:02 ` lists
2014-06-18 9:02 ` lists
2014-06-20 12:37 ` Riku Voipio
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).