From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:60607) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bmkS6-00009C-Ox for qemu-devel@nongnu.org; Wed, 21 Sep 2016 12:32:55 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bmkS4-0002rI-3b for qemu-devel@nongnu.org; Wed, 21 Sep 2016 12:32:54 -0400 Received: from mx1.redhat.com ([209.132.183.28]:33862) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bmkS3-0002r3-R6 for qemu-devel@nongnu.org; Wed, 21 Sep 2016 12:32:52 -0400 References: From: Paolo Bonzini Message-ID: <3091d61c-8e97-2fd0-892b-419174e63d45@redhat.com> Date: Wed, 21 Sep 2016 18:32:47 +0200 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit Subject: Re: [Qemu-devel] [PATCH v3 1/2] util: Introduce qemu_get_pid_name List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Michal Privoznik , qemu-devel@nongnu.org Cc: armbru@redhat.com, peter.maydell@linaro.org, berrange@redhat.com On 21/09/2016 18:27, Michal Privoznik wrote: > This is a small helper that tries to fetch binary name for given > PID. > > Signed-off-by: Michal Privoznik > --- > include/qemu/osdep.h | 10 ++++++++++ > util/oslib-posix.c | 27 +++++++++++++++++++++++++++ > util/oslib-win32.c | 7 +++++++ > 3 files changed, 44 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..8c1e8d6 100644 > --- a/util/oslib-posix.c > +++ b/util/oslib-posix.c > @@ -46,6 +46,7 @@ > > #ifdef __FreeBSD__ > #include > +#include > #endif > > #include "qemu/mmap-alloc.h" > @@ -430,6 +431,32 @@ int qemu_read_password(char *buf, int buf_size) > } > > > +char *qemu_get_pid_name(pid_t pid) > +{ > + char *name = NULL; > + > +#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 */ > + char *pid_path; > + size_t len; > + > + pid_path = g_strdup_printf("/proc/%d/cmdline", pid); > + g_file_get_contents(pid_path, &name, &len, NULL); > + g_free(pid_path); > +#endif Nice, thanks Dan for the suggestion. :) Paolo > + 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..d09863c 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 */ > + abort(); > +} > + > + > pid_t qemu_fork(Error **errp) > { > errno = ENOSYS; >