From patchwork Tue Sep 20 14:54:52 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michal Privoznik X-Patchwork-Id: 9341867 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 7D39E6077A for ; Tue, 20 Sep 2016 15:06:13 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 657BA298A3 for ; Tue, 20 Sep 2016 15:06:13 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 5A14E298BC; Tue, 20 Sep 2016 15:06:13 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 2F1DB298A3 for ; Tue, 20 Sep 2016 15:06:12 +0000 (UTC) Received: from localhost ([::1]:35928 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bmMcd-000545-9e for patchwork-qemu-devel@patchwork.kernel.org; Tue, 20 Sep 2016 11:06:11 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:59668) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bmMRt-0004ca-Jc for qemu-devel@nongnu.org; Tue, 20 Sep 2016 10:55:09 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bmMRr-00034V-UW for qemu-devel@nongnu.org; Tue, 20 Sep 2016 10:55:05 -0400 Received: from mx1.redhat.com ([209.132.183.28]:52824) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bmMRr-000340-Gn for qemu-devel@nongnu.org; Tue, 20 Sep 2016 10:55:03 -0400 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id EF83F6651; Tue, 20 Sep 2016 14:55:02 +0000 (UTC) Received: from lisa.brq.redhat.com (dhcp129-142.brq.redhat.com [10.34.129.142]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u8KEt0f2023412; Tue, 20 Sep 2016 10:55:01 -0400 From: Michal Privoznik To: qemu-devel@nongnu.org Date: Tue, 20 Sep 2016 16:54:52 +0200 Message-Id: <0e7c2539c6b9dfcb6f90314c041c87ac9baa1b21.1474383125.git.mprivozn@redhat.com> In-Reply-To: References: In-Reply-To: References: X-Scanned-By: MIMEDefang 2.68 on 10.5.11.22 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.38]); Tue, 20 Sep 2016 14:55:03 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH 1/2] util: Introduce qemu_get_pid_name X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: pbonzini@redhat.com, armbru@redhat.com, peter.maydell@linaro.org Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP 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 | 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 +#include #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]; + 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; +} + + pid_t qemu_fork(Error **errp) { errno = ENOSYS;