From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:44099) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bmjak-0003k9-KH for qemu-devel@nongnu.org; Wed, 21 Sep 2016 11:37:47 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bmjaj-0004Ar-EO for qemu-devel@nongnu.org; Wed, 21 Sep 2016 11:37:46 -0400 Received: from mx1.redhat.com ([209.132.183.28]:51268) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bmjaj-0004AJ-5P for qemu-devel@nongnu.org; Wed, 21 Sep 2016 11:37:45 -0400 Date: Wed, 21 Sep 2016 16:37:40 +0100 From: "Daniel P. Berrange" Message-ID: <20160921153740.GX15535@redhat.com> Reply-To: "Daniel P. Berrange" References: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: Subject: Re: [Qemu-devel] [PATCH v2 1/2] util: Introduce qemu_get_pid_name List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Michal Privoznik Cc: qemu-devel@nongnu.org, pbonzini@redhat.com, armbru@redhat.com, peter.maydell@linaro.org 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 > --- > 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 > +#include > #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 :|