public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
To: Steven Rostedt <rostedt@goodmis.org>
Cc: Yoshihiro YUNOMAE <yoshihiro.yunomae.ez@hitachi.com>,
	Aaron Fabbri <aaronx.j.fabbri@intel.com>,
	linux-kernel@vger.kernel.org,
	cti.systems-productivity-manager.ts@hitachi.com,
	Divya Vyas <edivya.vyas@gmail.com>,
	Hidehiro Kawai <hidehiro.kawai.ez@hitachi.com>,
	yoshihiro.yunomae@aktsk.jp
Subject: [PATCH trace-cmd V6 7/7] trace-cmd: Use pid instead of libvirt virt domain name
Date: Tue, 26 May 2015 15:55:36 +0900	[thread overview]
Message-ID: <20150526065536.16023.32916.stgit@localhost.localdomain> (raw)
In-Reply-To: <20150526065522.16023.30813.stgit@localhost.localdomain>

From: Yoshihiro YUNOMAE <yoshihiro.yunomae.ez@hitachi.com>

Use pid instead of domain name if libvirt is not found.
This also fix the trace fifo lookup routine to find it
from /proc/<PID>/fd.
With this change, we can use trace-cmd virt-server for
directly running qemu virtual machines.

Signed-off-by: Yoshihiro YUNOMAE <yoshihiro.yunomae.ez@hitachi.com>
Signed-off-by: Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
---
 trace-listen.c |   47 ++++++++++++++++++++++++++++++++++-------------
 1 file changed, 34 insertions(+), 13 deletions(-)

diff --git a/trace-listen.c b/trace-listen.c
index f6d53d2..f40b3a5 100644
--- a/trace-listen.c
+++ b/trace-listen.c
@@ -35,6 +35,7 @@
 #include <fcntl.h>
 #include <signal.h>
 #include <errno.h>
+#include <fnmatch.h>
 
 #include "trace-local.h"
 #include "trace-msg.h"
@@ -396,20 +397,41 @@ static int open_udp(const char *node, const char *port, int *pid,
 #define TRACE_PATH_DOMAIN_CPU_O	VIRT_DOMAIN_DIR "trace-path-cpu%d.out"
 #define TRACE_PATH_DOMAIN_CPU_I	VIRT_DOMAIN_DIR "trace-path-cpu%d.in"
 
+#define TRACE_PATH_PATTERN	VIRT_DIR "*/trace-path-cpu%d.out"
+
 static int open_virtio_serial_pipe(int *pid, int cpu, int pagesize,
 				   const char *domain, int virtpid)
 {
+	char path[PATH_MAX];
 	char buf[PATH_MAX];
-	int fd;
-
-	snprintf(buf, PATH_MAX, TRACE_PATH_DOMAIN_CPU_O, domain, cpu);
-	fd = open(buf, O_RDONLY | O_NONBLOCK);
-	if (fd < 0) {
-		warning("open %s", buf);
-		return fd;
+	int fd = -ENOENT;
+	DIR *dir;
+	struct dirent *ent;
+	struct stat st;
+
+	snprintf(path, PATH_MAX, "/proc/%d/fd", virtpid);
+	dir = opendir(path);
+	if (!dir)
+		return -errno;
+	while ((ent = readdir(dir)) != NULL) {
+		snprintf(path, PATH_MAX, "/proc/%d/fd/%s",
+			 virtpid, ent->d_name);
+		if (readlink(path, buf, PATH_MAX) < 0)
+			continue;
+		snprintf(path, PATH_MAX, TRACE_PATH_PATTERN, cpu);
+		/* Find the pipe which matchs pattern */
+		if ((fnmatch(path, buf, FNM_PATHNAME) == 0) &&
+		    (stat(buf, &st) == 0) && (S_ISFIFO(st.st_mode))) {
+			fd = open(buf, O_RDONLY | O_NONBLOCK);
+			break;
+		}
 	}
+	closedir(dir);
 
-	fork_virt_reader(fd, pid, cpu, pagesize, domain, virtpid);
+	if (fd < 0)
+		warning("open %s: %d", buf, fd);
+	else
+		fork_virt_reader(fd, pid, cpu, pagesize, domain, virtpid);
 
 	return fd;
 }
@@ -762,6 +784,7 @@ static int do_fork(int cfd)
 	return 0;
 }
 
+/* Get client (VM) pid from unix domain socket */
 static int get_virtpid(int cfd)
 {
 	struct ucred cr;
@@ -790,11 +813,8 @@ static char *get_guest_domain_from_pid(int pid)
 	int fd;
 
 	dir = opendir(LIBVIRT_DOMAIN_PATH);
-	if (!dir) {
-		if (errno == ENOENT)
-			warning("Only support for using libvirt");
+	if (!dir)
 		return NULL;
-	}
 
 	for (dirent = readdir(dir); dirent != NULL; dirent = readdir(dir)) {
 		snprintf(file_name, NAME_MAX, LIBVIRT_DOMAIN_PATH"%s",
@@ -837,7 +857,8 @@ static int do_connection(int cfd, struct sockaddr *peer_addr,
 
 		domain = get_guest_domain_from_pid(virtpid);
 		if (!domain)
-			return -1;
+			if (asprintf(&domain, "%d", virtpid) < 0)
+				return -ENOMEM;
 	}
 
 	ret = do_fork(cfd);


      parent reply	other threads:[~2015-05-26  6:58 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-05-26  6:55 [PATCH trace-cmd V6 0/7] trce-cmd: add virtual machine tracing Masami Hiramatsu
2015-05-26  6:55 ` [PATCH trace-cmd V6 1/7] trace-cmd: Support -N option for trace-cmd extract Masami Hiramatsu
2015-05-26  6:55 ` [PATCH trace-cmd V6 2/7] trace-cmd/listen: Introduce trace-msg protocol (protocol v2) Masami Hiramatsu
2015-05-26  6:55 ` [PATCH trace-cmd V6 3/7] trace-cmd/msg: Use poll(2) to wait for a message Masami Hiramatsu
2015-05-26  6:55 ` [PATCH trace-cmd V6 4/7] trace-cmd/virt-server: Add virt-server mode for a virtualization environment Masami Hiramatsu
2015-05-26  6:55 ` [PATCH trace-cmd V6 5/7] trace-cmd/record: Add --virt option for record mode Masami Hiramatsu
2015-05-26  6:55 ` [PATCH trace-cmd V6 6/7] trace-cmd/virt-server: Add --dom option which makes a domain directory to virt-server Masami Hiramatsu
2015-05-26  6:55 ` Masami Hiramatsu [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20150526065536.16023.32916.stgit@localhost.localdomain \
    --to=masami.hiramatsu.pt@hitachi.com \
    --cc=aaronx.j.fabbri@intel.com \
    --cc=cti.systems-productivity-manager.ts@hitachi.com \
    --cc=edivya.vyas@gmail.com \
    --cc=hidehiro.kawai.ez@hitachi.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=rostedt@goodmis.org \
    --cc=yoshihiro.yunomae.ez@hitachi.com \
    --cc=yoshihiro.yunomae@aktsk.jp \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox