summary refs log tree commit diff stats
path: root/util/oslib-posix.c
diff options
context:
space:
mode:
authorMichal Privoznik <mprivozn@redhat.com>2016-09-27 17:24:56 +0200
committerPaolo Bonzini <pbonzini@redhat.com>2016-10-04 10:00:27 +0200
commit7dc9ae4339faa97e89daadb2e1098147ab4aadc8 (patch)
tree14fe0cb005009fd3de9ac1cb78dfb9f6765f6d95 /util/oslib-posix.c
parent6a7b2b21008f271e7a91e937e521e22f94579bb9 (diff)
downloadfocaccia-qemu-7dc9ae4339faa97e89daadb2e1098147ab4aadc8.tar.gz
focaccia-qemu-7dc9ae4339faa97e89daadb2e1098147ab4aadc8.zip
util: Introduce qemu_get_pid_name
This is a small helper that tries to fetch binary name for given
PID.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Message-Id: <4d75d475c1884f8e94ee8b1e57273ddf3ed68bf7.1474987617.git.mprivozn@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Diffstat (limited to 'util/oslib-posix.c')
-rw-r--r--util/oslib-posix.c27
1 files changed, 27 insertions, 0 deletions
diff --git a/util/oslib-posix.c b/util/oslib-posix.c
index f2d4e9e592..8c1e8d6841 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,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
+
+    return name;
+}
+
+
 pid_t qemu_fork(Error **errp)
 {
     sigset_t oldmask, newmask;