From ae0dcbc413a0af79340210ac745e028241d2a759 Mon Sep 17 00:00:00 2001 From: Osier Yang Date: Thu, 4 Aug 2011 16:54:58 +0800 Subject: [PATCH] daemon: Unlink unix socket paths on shutdown This patch introduces a internal RPC API "virNetServerClose", which is standalone with "virNetServerFree". it closes all the socket fds, and unlinks the unix socket paths, regardless of whether the socket is still referenced or not. This is to address regression bug: https://bugzilla.redhat.com/show_bug.cgi?id=725702 --- daemon/libvirtd.c | 1 + src/rpc/virnetserver.c | 16 ++++++++++++++++ src/rpc/virnetserver.h | 1 + src/rpc/virnetserverservice.c | 12 ++++++++++++ src/rpc/virnetserverservice.h | 2 ++ src/rpc/virnetsocket.c | 22 ++++++++++++++++++++++ src/rpc/virnetsocket.h | 1 + 7 files changed, 55 insertions(+) diff --git a/daemon/libvirtd.c b/daemon/libvirtd.c index 9e044e2cc9..53f1002b87 100644 --- a/daemon/libvirtd.c +++ b/daemon/libvirtd.c @@ -1558,6 +1558,7 @@ int main(int argc, char **argv) { cleanup: virNetServerProgramFree(remoteProgram); virNetServerProgramFree(qemuProgram); + virNetServerClose(srv); virNetServerFree(srv); if (statuswrite != -1) { if (ret != 0) { diff --git a/src/rpc/virnetserver.c b/src/rpc/virnetserver.c index 5e4826bc3e..1a49dbbd8a 100644 --- a/src/rpc/virnetserver.c +++ b/src/rpc/virnetserver.c @@ -798,3 +798,19 @@ void virNetServerFree(virNetServerPtr srv) virMutexDestroy(&srv->lock); VIR_FREE(srv); } + +void virNetServerClose(virNetServerPtr srv) +{ + int i; + + if (!srv) + return; + + virNetServerLock(srv); + + for (i = 0; i < srv->nservices; i++) { + virNetServerServiceClose(srv->services[i]); + } + + virNetServerUnlock(srv); +} diff --git a/src/rpc/virnetserver.h b/src/rpc/virnetserver.h index 810d8a3a96..324cfb7186 100644 --- a/src/rpc/virnetserver.h +++ b/src/rpc/virnetserver.h @@ -85,5 +85,6 @@ void virNetServerQuit(virNetServerPtr srv); void virNetServerFree(virNetServerPtr srv); +void virNetServerClose(virNetServerPtr srv); #endif diff --git a/src/rpc/virnetserverservice.c b/src/rpc/virnetserverservice.c index d5648dc81f..8c9ed1eb55 100644 --- a/src/rpc/virnetserverservice.c +++ b/src/rpc/virnetserverservice.c @@ -280,3 +280,15 @@ void virNetServerServiceToggle(virNetServerServicePtr svc, VIR_EVENT_HANDLE_READABLE : 0); } + +void virNetServerServiceClose(virNetServerServicePtr svc) +{ + int i; + + if (!svc) + return; + + for (i = 0; i < svc->nsocks; i++) { + virNetSocketClose(svc->socks[i]); + } +} diff --git a/src/rpc/virnetserverservice.h b/src/rpc/virnetserverservice.h index 935759821b..8540bd9356 100644 --- a/src/rpc/virnetserverservice.h +++ b/src/rpc/virnetserverservice.h @@ -66,4 +66,6 @@ void virNetServerServiceFree(virNetServerServicePtr svc); void virNetServerServiceToggle(virNetServerServicePtr svc, bool enabled); +void virNetServerServiceClose(virNetServerServicePtr svc); + #endif diff --git a/src/rpc/virnetsocket.c b/src/rpc/virnetsocket.c index 41b691a6aa..992e33aa20 100644 --- a/src/rpc/virnetsocket.c +++ b/src/rpc/virnetsocket.c @@ -1222,3 +1222,25 @@ void virNetSocketRemoveIOCallback(virNetSocketPtr sock) virMutexUnlock(&sock->lock); } + +void virNetSocketClose(virNetSocketPtr sock) +{ + if (!sock) + return; + + virMutexLock(&sock->lock); + + VIR_FORCE_CLOSE(sock->fd); + +#ifdef HAVE_SYS_UN_H + /* If a server socket, then unlink UNIX path */ + if (!sock->client && + sock->localAddr.data.sa.sa_family == AF_UNIX && + sock->localAddr.data.un.sun_path[0] != '\0') { + if (unlink(sock->localAddr.data.un.sun_path) == 0) + sock->localAddr.data.un.sun_path[0] = '\0'; + } +#endif + + virMutexUnlock(&sock->lock); +} diff --git a/src/rpc/virnetsocket.h b/src/rpc/virnetsocket.h index dfb3c5d031..1e1c63c6e5 100644 --- a/src/rpc/virnetsocket.h +++ b/src/rpc/virnetsocket.h @@ -118,6 +118,7 @@ void virNetSocketUpdateIOCallback(virNetSocketPtr sock, void virNetSocketRemoveIOCallback(virNetSocketPtr sock); +void virNetSocketClose(virNetSocketPtr sock); #endif /* __VIR_NET_SOCKET_H__ */