From: "Daniel P. Berrange" <berrange@redhat.com>
To: Michal Privoznik <mprivozn@redhat.com>
Cc: qemu-devel@nongnu.org, pbonzini@redhat.com, armbru@redhat.com,
peter.maydell@linaro.org
Subject: Re: [Qemu-devel] [PATCH v2 1/2] util: Introduce qemu_get_pid_name
Date: Wed, 21 Sep 2016 16:37:40 +0100 [thread overview]
Message-ID: <20160921153740.GX15535@redhat.com> (raw)
In-Reply-To: <d349faac3d20bee419a6034d431c81c7d64303f3.1474456806.git.mprivozn@redhat.com>
On Wed, Sep 21, 2016 at 01:26:02PM +0200, Michal Privoznik wrote:
> This is a small helper that tries to fetch binary name for given
> PID.
>
> Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
> ---
> include/qemu/osdep.h | 10 ++++++++++
> util/oslib-posix.c | 37 +++++++++++++++++++++++++++++++++++++
> util/oslib-win32.c | 7 +++++++
> 3 files changed, 54 insertions(+)
>
> diff --git a/include/qemu/osdep.h b/include/qemu/osdep.h
> index 9e9fa61..384bfe2 100644
> --- a/include/qemu/osdep.h
> +++ b/include/qemu/osdep.h
> @@ -388,6 +388,16 @@ void os_mem_prealloc(int fd, char *area, size_t sz, Error **errp);
> int qemu_read_password(char *buf, int buf_size);
>
> /**
> + * qemu_get_pid_name:
> + * @pid: pid of a process
> + *
> + * For given @pid fetch its name. Caller is responsible for
> + * freeing the string when no longer needed.
> + * Returns allocated string on success, NULL on failure.
> + */
> +char *qemu_get_pid_name(pid_t pid);
> +
> +/**
> * qemu_fork:
> *
> * A version of fork that avoids signal handler race
> diff --git a/util/oslib-posix.c b/util/oslib-posix.c
> index f2d4e9e..0555425 100644
> --- a/util/oslib-posix.c
> +++ b/util/oslib-posix.c
> @@ -46,6 +46,7 @@
>
> #ifdef __FreeBSD__
> #include <sys/sysctl.h>
> +#include <libutil.h>
> #endif
>
> #include "qemu/mmap-alloc.h"
> @@ -430,6 +431,42 @@ int qemu_read_password(char *buf, int buf_size)
> }
>
>
> +char *qemu_get_pid_name(pid_t pid)
> +{
> + char *name = NULL;
> + char *pid_path;
> + char buf[PATH_MAX];
> + size_t len;
> +
> +#if defined(__FreeBSD__)
> + /* BSDs don't have /proc, but they provide a nice substitute */
> + struct kinfo_proc *proc = kinfo_getproc(pid);
> + if (proc) {
> + name = g_strdup(proc->ki_comm);
> + free(proc);
> + }
> +#else
> + /* Assume a system with reasonable procfs */
> + FILE *f;
> +
> + pid_path = g_strdup_printf("/proc/%d/cmdline", pid);
> + f = fopen(pid_path, "r");
> + if (!f) {
> + return NULL;
> + }
> +
> + len = fread(buf, 1, sizeof(buf), f);
> + if (len) {
> + name = g_strdup(buf);
> + }
> + fclose(f);
This use of fixed length buffer and the calling of multiple stdio
functions can be replaced by g_file_get_contents() which will be
much clearer.
Regards,
Daniel
--
|: http://berrange.com -o- http://www.flickr.com/photos/dberrange/ :|
|: http://libvirt.org -o- http://virt-manager.org :|
|: http://autobuild.org -o- http://search.cpan.org/~danberr/ :|
|: http://entangle-photo.org -o- http://live.gnome.org/gtk-vnc :|
next prev parent reply other threads:[~2016-09-21 15:37 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-09-21 11:26 [Qemu-devel] [PATCH v2 0/2] Produce better termination message Michal Privoznik
2016-09-21 11:26 ` [Qemu-devel] [PATCH v2 1/2] util: Introduce qemu_get_pid_name Michal Privoznik
2016-09-21 15:24 ` Paolo Bonzini
2016-09-21 15:37 ` Daniel P. Berrange [this message]
2016-09-21 11:26 ` [Qemu-devel] [PATCH v2 2/2] qemu_kill_report: Report PID name too Michal Privoznik
2016-09-21 12:32 ` [Qemu-devel] [PATCH v2 0/2] Produce better termination message no-reply
2016-09-21 12:32 ` no-reply
2016-09-21 15:25 ` Paolo Bonzini
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=20160921153740.GX15535@redhat.com \
--to=berrange@redhat.com \
--cc=armbru@redhat.com \
--cc=mprivozn@redhat.com \
--cc=pbonzini@redhat.com \
--cc=peter.maydell@linaro.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.