Skip to contentSkip to conversation

kvm tools: Handle only relevant events in epoll

PATCH New Sasha Levin 1 participant 2 messages Oct 28, 2011 KVM

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 kvm tools: Handle only relevant events in epoll

Handle only new incoming data for listener and stop fds, treat all other events as error events which close the socket.

This fixes the bug where a 'kvm list' could have hanged because a close event in the listener fd have been treated as a new connection.

---
 tools/kvm/kvm-ipc.c |    6 +++---
 1 files changed, 3 insertions(+), 3 deletions(-)
tools/kvm/kvm-ipc.c +3 -3
index 3131fe8..40ab457 100644
@@ -112,7 +112,7 @@ static void *kvm_ipc__thread(void *param)
 		if (nfds > 0) {
 			int fd = event.data.fd;
 
-			if (fd == stop_fd) {
+			if (fd == stop_fd && event.events & EPOLLIN) {
 				break;
 			} else if (fd == server_fd) {
 				int client;
@@ -138,13 +138,13 @@ int kvm_ipc__start(int sock)
 
 	epoll_fd = epoll_create(KVM_IPC_MAX_MSGS);
 
-	ev.events = EPOLLIN | EPOLLOUT | EPOLLPRI;
+	ev.events = EPOLLIN | EPOLLET;
 	ev.data.fd = sock;
 	if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, sock, &ev) < 0)
 		die("Failed starting IPC thread");
 
 	stop_fd = eventfd(0, 0);
-	ev.events = EPOLLIN | EPOLLOUT | EPOLLRDHUP | EPOLLET;
+	ev.events = EPOLLIN | EPOLLET;
 	ev.data.fd = stop_fd;
 	if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, stop_fd, &ev) < 0)
 		die("Failed adding stop event to epoll");
Signature · 1 line
1.7.7.1
#1 lore raw

PATCH 2/2 kvm tools: Cleanup ghost socket files

Clean ghost socket files as part of the regular connection process.

This fixes the bug of 'kvm list' failing with "Failed connecting to instance" when ghost socket files existed.

---
 tools/kvm/kvm.c |    9 ++++++++-
 1 files changed, 8 insertions(+), 1 deletions(-)
tools/kvm/kvm.c +8 -1
index d953b94..252bd18 100644
@@ -208,8 +208,13 @@ int kvm__get_sock_by_instance(const char *name)
 	len = strlen(local.sun_path) + sizeof(local.sun_family);
 
 	r = connect(s, &local, len);
-	if (r < 0)
+	if (r < 0 && errno == ECONNREFUSED) {
+		/* Clean ghost socket file */
+		unlink(sock_file);
+		return -1;
+	} else if (r < 0) {
 		die("Failed connecting to instance");
+	}
 
 	return s;
 }
@@ -232,6 +237,8 @@ int kvm__enumerate_instances(int (*callback)(const char *name, int fd))
 		if (entry.d_type == DT_SOCK) {
 			entry.d_name[strlen(entry.d_name)-5] = 0;
 			sock = kvm__get_sock_by_instance(entry.d_name);
+			if (sock < 0)
+				continue;
 			ret = callback(entry.d_name, sock);
 			close(sock);
 			if (ret < 0)
Signature · 1 line
1.7.7.1