From: Michal Privoznik Date: Tue, 27 Sep 2016 15:24:56 +0000 (+0200) Subject: util: Introduce qemu_get_pid_name X-Git-Tag: TizenStudio_2.0_p2.3.2~9^2~14^2~5^2~139^2~15 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=7dc9ae4339faa97e89daadb2e1098147ab4aadc8;p=sdk%2Femulator%2Fqemu.git 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 Message-Id: <4d75d475c1884f8e94ee8b1e57273ddf3ed68bf7.1474987617.git.mprivozn@redhat.com> Signed-off-by: Paolo Bonzini --- 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 + + 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;