qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Paolo Bonzini <pbonzini@redhat.com>
To: Michal Privoznik <mprivozn@redhat.com>, qemu-devel@nongnu.org
Cc: peter.maydell@linaro.org, armbru@redhat.com
Subject: Re: [Qemu-devel] [PATCH 1/2] util: Introduce qemu_get_pid_name
Date: Tue, 20 Sep 2016 17:02:02 +0200	[thread overview]
Message-ID: <6662b2cf-a8d0-afb2-06b8-a42df114ea89@redhat.com> (raw)
In-Reply-To: <0e7c2539c6b9dfcb6f90314c041c87ac9baa1b21.1474383125.git.mprivozn@redhat.com>



On 20/09/2016 16:54, 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   | 36 ++++++++++++++++++++++++++++++++++++
>  util/oslib-win32.c   |  7 +++++++
>  3 files changed, 53 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..9fe5ba9 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,41 @@ int qemu_read_password(char *buf, int buf_size)
>  }
>  
>  
> +char *qemu_get_pid_name(pid_t pid)
> +{
> +    char *name = NULL;
> +    FILE *f;
> +    char pid_path[PATH_MAX];

Please use a char * and g_strdup_printf.  Also the four declaration
starting at FILE *f must be moved under the #else.

Paolo

> +    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 */
> +    snprintf(pid_path, sizeof(pid_path), "/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);
> +
> +#endif
> +
> +    return name;
> +}
> +
> +
>  pid_t qemu_fork(Error **errp)
>  {
>      sigset_t oldmask, newmask;
> diff --git a/util/oslib-win32.c b/util/oslib-win32.c
> index 4c1dcf1..128c6b8 100644
> --- a/util/oslib-win32.c
> +++ b/util/oslib-win32.c
> @@ -575,6 +575,13 @@ int qemu_read_password(char *buf, int buf_size)
>  }
>  
>  
> +char *qemu_get_pid_name(pid_t pid)
> +{
> +    /* XXX Implement me */
> +    return NULL;

Just abort, it's not reachable because os-win32.c never calls
qemu_system_killed.

Paolo

> +}
> +
> +
>  pid_t qemu_fork(Error **errp)
>  {
>      errno = ENOSYS;
> 

  reply	other threads:[~2016-09-20 15:02 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-09-20 14:54 [Qemu-devel] [PATCH 0/2] Produce better termination message Michal Privoznik
2016-09-20 14:54 ` [Qemu-devel] [PATCH 1/2] util: Introduce qemu_get_pid_name Michal Privoznik
2016-09-20 15:02   ` Paolo Bonzini [this message]
2016-09-20 14:54 ` [Qemu-devel] [PATCH 2/2] qemu_kill_report: Report PID name too Michal Privoznik
2016-09-20 15:07   ` Eric Blake
2016-09-20 17:00 ` [Qemu-devel] [PATCH 0/2] Produce better termination message no-reply
2016-09-20 20:38 ` no-reply

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=6662b2cf-a8d0-afb2-06b8-a42df114ea89@redhat.com \
    --to=pbonzini@redhat.com \
    --cc=armbru@redhat.com \
    --cc=mprivozn@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 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).