From mboxrd@z Thu Jan 1 00:00:00 1970 From: Osier Yang Subject: Re: [PATCH] kvm tools: Add method to stop ipc thread Date: Tue, 25 Oct 2011 19:08:01 +0800 Message-ID: <4EA69891.6030002@redhat.com> References: <1319539236-874-1-git-send-email-levinsasha928@gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=GB2312 Content-Transfer-Encoding: QUOTED-PRINTABLE Cc: penberg@cs.helsinki.fi, kvm@vger.kernel.org, mingo@elte.hu, asias.hejun@gmail.com, gorcunov@gmail.com To: Sasha Levin Return-path: Received: from mx1.redhat.com ([209.132.183.28]:1029 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754490Ab1JYLIm (ORCPT ); Tue, 25 Oct 2011 07:08:42 -0400 In-Reply-To: <1319539236-874-1-git-send-email-levinsasha928@gmail.com> Sender: kvm-owner@vger.kernel.org List-ID: =D3=DA 2011=C4=EA10=D4=C225=C8=D5 18:40, Sasha Levin =D0=B4=B5=C0: > Stop the ipc thread when shutting down the hypervisor. > > This solves a bug where the .sock files weren't removed upon shutdown= =2E I tested the patch, the socket is still there. (I'm testing with "--sdl", and shutdown the vm window). And the guest starting succeeded even if a socket with the name same as the specified guest name exists, which means I can start two guests with same socket, which is not right. > > Signed-off-by: Sasha Levin > --- > tools/kvm/include/kvm/kvm-ipc.h | 1 + > tools/kvm/kvm-ipc.c | 33 +++++++++++++++++++++++++++++= ---- > tools/kvm/kvm.c | 1 + > 3 files changed, 31 insertions(+), 4 deletions(-) > > diff --git a/tools/kvm/include/kvm/kvm-ipc.h b/tools/kvm/include/kvm/= kvm-ipc.h > index c932052..731767f 100644 > --- a/tools/kvm/include/kvm/kvm-ipc.h > +++ b/tools/kvm/include/kvm/kvm-ipc.h > @@ -22,5 +22,6 @@ enum { > int kvm_ipc__register_handler(u32 type, void (*cb)(int fd, u32 type,= u32 len, u8 *msg)); > int kvm_ipc__handle(int fd, struct kvm_ipc_msg *msg); > int kvm_ipc__start(int sock); > +int kvm_ipc__stop(void); > =20 > #endif > diff --git a/tools/kvm/kvm-ipc.c b/tools/kvm/kvm-ipc.c > index f05e926..65209e1 100644 > --- a/tools/kvm/kvm-ipc.c > +++ b/tools/kvm/kvm-ipc.c > @@ -7,12 +7,14 @@ > #include > #include > #include > +#include > =20 > #define KVM_IPC_MAX_MSGS 16 > =20 > static void (*msgs[KVM_IPC_MAX_MSGS])(int fd, u32 type, u32 len, u8 = *msg); > static DECLARE_RWSEM(msgs_rwlock); > -static int epoll_fd, server_fd; > +static int epoll_fd, server_fd, stop_fd; > +static pthread_t thread; > =20 > int kvm_ipc__register_handler(u32 type, void (*cb)(int fd, u32 type,= u32 len, u8 *msg)) > { > @@ -109,8 +111,11 @@ static void *kvm_ipc__thread(void *param) > nfds =3D epoll_wait(epoll_fd, &event, 1, -1); > if (nfds > 0) { > int fd =3D event.data.fd; > - > - if (fd =3D=3D server_fd) { > + printf("bleh\n"); > + if (fd =3D=3D stop_fd) { > + printf("Got stop signal\n"); > + break; > + } else if (fd =3D=3D server_fd) { > int client; > =20 > client =3D kvm_ipc__new_conn(fd); > @@ -128,7 +133,6 @@ static void *kvm_ipc__thread(void *param) > =20 > int kvm_ipc__start(int sock) > { > - pthread_t thread; > struct epoll_event ev; > =20 > server_fd =3D sock; > @@ -140,8 +144,29 @@ int kvm_ipc__start(int sock) > if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, sock, &ev) < 0) > die("Failed starting IPC thread"); > =20 > + stop_fd =3D eventfd(0, 0); > + ev.events =3D EPOLLIN | EPOLLOUT | EPOLLRDHUP | EPOLLET; > + ev.data.fd =3D stop_fd; > + if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, stop_fd, &ev) < 0) > + die("Failed adding stop event to epoll"); > + > if (pthread_create(&thread, NULL, kvm_ipc__thread, NULL) !=3D 0) > die("Failed starting IPC thread"); > =20 > return 0; > } > + > +int kvm_ipc__stop(void) > +{ > + u64 val =3D 1; > + int ret; > + > + ret =3D write(stop_fd, &val, sizeof(val)); > + if (ret < 0) > + return ret; > + > + close(server_fd); > + close(epoll_fd); > + > + return ret; > +} > diff --git a/tools/kvm/kvm.c b/tools/kvm/kvm.c > index 40ae6a5..8d6b5e1 100644 > --- a/tools/kvm/kvm.c > +++ b/tools/kvm/kvm.c > @@ -237,6 +237,7 @@ void kvm__delete(struct kvm *kvm) > kvm__stop_timer(kvm); > =20 > munmap(kvm->ram_start, kvm->ram_size); > + kvm_ipc__stop(); > kvm__remove_socket(kvm->name); > free(kvm); > }