[libvirt] [RFC PATCH 1/4] driver.h: Add macro VIR_DRV_CONN_CHECK_SERVER

2018-07-07 Thread Marcos Paulo de Souza
This new macro will check if the server was passed to connectOpen
function. Some drivers as esx, hyperv and phyp need a server address to
connect to.

Signed-off-by: Marcos Paulo de Souza 
---
 src/driver.h | 9 +
 1 file changed, 9 insertions(+)

diff --git a/src/driver.h b/src/driver.h
index 0b1f7a2269..1f9d7fd26b 100644
--- a/src/driver.h
+++ b/src/driver.h
@@ -60,6 +60,15 @@ typedef enum {
 ((drv)->connectSupportsFeature ? \
 (drv)->connectSupportsFeature((conn), (feature)) > 0 : 0)
 
+/* Check if the connection of the driver has filled the server address */
+# define VIR_DRV_CONN_CHECK_SERVER \
+do { \
+if (!conn->uri->server) { \
+virReportError(VIR_ERR_INVALID_ARG, "%s", \
+   _("URI is missing the server part")); \
+return VIR_DRV_OPEN_ERROR; \
+} \
+} while (0)
 
 # define __VIR_DRIVER_H_INCLUDES___
 
-- 
2.17.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [RFC PATCH 2/4] esx_driver: Use VIR_DRV_CONN_CHECK_SERVER macro

2018-07-07 Thread Marcos Paulo de Souza
ESX needs a server to connect to, so use this macro in order to reuse
code.

Signed-off-by: Marcos Paulo de Souza 
---
 src/esx/esx_driver.c | 7 +--
 1 file changed, 1 insertion(+), 6 deletions(-)

diff --git a/src/esx/esx_driver.c b/src/esx/esx_driver.c
index 06e1238385..89ac7c430f 100644
--- a/src/esx/esx_driver.c
+++ b/src/esx/esx_driver.c
@@ -854,12 +854,7 @@ esxConnectOpen(virConnectPtr conn, virConnectAuthPtr auth,
  conn->uri->path, conn->uri->scheme);
 }
 
-/* Require server part */
-if (!conn->uri->server) {
-virReportError(VIR_ERR_INVALID_ARG, "%s",
-   _("URI is missing the server part"));
-return VIR_DRV_OPEN_ERROR;
-}
+VIR_DRV_CONN_CHECK_SERVER;
 
 /* Require auth */
 if (!auth || !auth->cb) {
-- 
2.17.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [RFC PATCH 0/4] Add new macro to check for existent server

2018-07-07 Thread Marcos Paulo de Souza
Hello,

I am trying to add a generic way to check for server, specially in drivers that
needs the server to be declared. If there is better way to put the check, or a
better way to check this setting, let me know.

Thanks in advance!

Marcos Paulo de Souza (4):
  driver.h: Add macro VIR_DRV_CONN_CHECK_SERVER
  esx_driver: Use VIR_DRV_CONN_CHECK_SERVER macro
  hyperv_driver: Use VIR_DRV_CONN_CHECK_SERVER macro
  phyp_driver: Use VIR_DRV_CONN_CHECK_SERVER macro

 src/driver.h   | 9 +
 src/esx/esx_driver.c   | 7 +--
 src/hyperv/hyperv_driver.c | 7 +--
 src/phyp/phyp_driver.c | 6 +-
 4 files changed, 12 insertions(+), 17 deletions(-)

-- 
2.17.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [RFC PATCH 4/4] phyp_driver: Use VIR_DRV_CONN_CHECK_SERVER macro

2018-07-07 Thread Marcos Paulo de Souza
phyp needs a server to connect to, so use this macro in order to reuse
code.

Signed-off-by: Marcos Paulo de Souza 
---
 src/phyp/phyp_driver.c | 6 +-
 1 file changed, 1 insertion(+), 5 deletions(-)

diff --git a/src/phyp/phyp_driver.c b/src/phyp/phyp_driver.c
index 67ce7903ba..beca1c6fc7 100644
--- a/src/phyp/phyp_driver.c
+++ b/src/phyp/phyp_driver.c
@@ -1141,11 +1141,7 @@ phypConnectOpen(virConnectPtr conn,
 
 virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);
 
-if (conn->uri->server == NULL) {
-virReportError(VIR_ERR_INTERNAL_ERROR,
-   "%s", _("Missing server name in phyp:// URI"));
-return VIR_DRV_OPEN_ERROR;
-}
+VIR_DRV_CONN_CHECK_SERVER;
 
 if (VIR_ALLOC(phyp_driver) < 0)
 goto failure;
-- 
2.17.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [RFC PATCH 3/4] hyperv_driver: Use VIR_DRV_CONN_CHECK_SERVER macro

2018-07-07 Thread Marcos Paulo de Souza
hyperv needs a server to connect to, so use this macro in order to reuse
code.

Signed-off-by: Marcos Paulo de Souza 
---
 src/hyperv/hyperv_driver.c | 7 +--
 1 file changed, 1 insertion(+), 6 deletions(-)

diff --git a/src/hyperv/hyperv_driver.c b/src/hyperv/hyperv_driver.c
index 95496bdf73..a419f0ccee 100644
--- a/src/hyperv/hyperv_driver.c
+++ b/src/hyperv/hyperv_driver.c
@@ -128,12 +128,7 @@ hypervConnectOpen(virConnectPtr conn, virConnectAuthPtr 
auth,
 
 virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);
 
-/* Require server part */
-if (conn->uri->server == NULL) {
-virReportError(VIR_ERR_INVALID_ARG, "%s",
-   _("URI is missing the server part"));
-return VIR_DRV_OPEN_ERROR;
-}
+VIR_DRV_CONN_CHECK_SERVER;
 
 /* Require auth */
 if (auth == NULL || auth->cb == NULL) {
-- 
2.17.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


Re: [libvirt] [PATCH 1/2] virTypedParamsSerialize: minor fixes

2018-07-07 Thread John Ferlan


On 07/06/2018 05:11 AM, Marc Hartmayer wrote:
> On Wed, Jul 04, 2018 at 11:24 AM +0200, Marc Hartmayer 
>  wrote:
>> On Tue, Jul 03, 2018 at 09:14 PM +0200, John Ferlan  
>> wrote:
>>> On 07/03/2018 06:32 AM, Marc Hartmayer wrote:
 On Thu, Jun 21, 2018 at 03:47 PM +0200, Marc Hartmayer 
  wrote:
> On Wed, Jun 13, 2018 at 03:11 PM +0200, John Ferlan  
> wrote:
>> On 06/07/2018 08:17 AM, Marc Hartmayer wrote:
>>> On Wed, May 09, 2018 at 09:51 PM +0200, John Ferlan 
>>>  wrote:
 On 05/07/2018 11:24 AM, Marc Hartmayer wrote:
> On Mon, Apr 30, 2018 at 03:20 PM +0200, John Ferlan 
>  wrote:
>> On 04/25/2018 11:55 AM, Marc Hartmayer wrote:
>>> 1. Don't allocate if there is nothing that needs to be
>>>allocated. Especially as the result of calling calloc(0, ...) is
>>>implementation-defined.

 uh oh - another memory recollection challenge ;-)

>>
>> Following VIR_ALLOC_N one finds :
>>
>> VIR_ALLOC_N(params_val, nparams)
>>
>> which equates to
>>
>> # define VIR_ALLOC_N(ptr, count) \
>>  virAllocN(&(ptr), sizeof(*(ptr)), (count), true, \
>>VIR_FROM_THIS, __FILE__, __FUNCTION__, __LINE__)
>>
>> or
>>
>> virAllocN(_val, sizeof(params_val), nparams, true, ...)
>>
>> and eventually/essentially
>>
>> *params_val = calloc(sizeof(params_val), nparams)
>>
>> If the returned value is NULL then we error w/ OOM (4th param=true).
>>
>> So, unless @params_val had no elements, it won't be calloc(0,...) and
>
> I'm talking about the case that nparams == 0 => calloc(sizeof(…), 0).
>

 And I was thinking is there a specific consumer/caller of
 virTypedParamsSerialize that was passing something incorrectly.

>> thus the question becomes is there a more specific path you are
>> referencing here?
>
> It’s a “generic” serializer so it should work for every (valid)
> case. What happens, for example, if params == NULL and nparams == 0? 
> In
> that case I would expect *remote_params_val = NULL and
> *remote_params_len = 0.
>

 Going through the callers to virTypedParamsSerialize can/does anyone
 pass params == NULL and nparams == 0?  Would that be reasonable and/or
 expected?

 My look didn't find any - either some caller checks that the passed
 @params != NULL (usually via virCheckNonNullArgGoto in the external API
 call) or @params is built up on the fly and wouldn't be NULL because
 there'd be an error causing failure beforehand. Although I'll admit the
 migration ones are also crazy to look at.

 I could have made a mistake too; hence, the is there a specific 
 instance
 that I missed? Or perhaps this is a result of some branched/private 
 code
 that had that error which I don't have access to?
>>>
>>> It was the result of private code but actually it was intended and no
>>> error :)
>>>

 To answer your "What happens, for example, if params == NULL and 
 nparams
 == 0?", well supposedly "VIR_ALLOC_N(params_val, 0)" should return 
 NULL,
 so params_val and thus *remote_params_val == NULL.
>>>
>>> Did you try what 'VIR_ALLOC_N(params_val, 0)' actually returns? At least
>>> for me, it doesn’t return a NULL pointer.
>>>
>>
>> I think I already determined that NULL isn't returned; otherwise, we
>> would have failed w/ OOM. I do recall trying this and seeing a non NULL
>> value returned.
>>
>> IIRC: I found no way into [de]serialize w/ a 0 parameter, so having that
>> guard didn't seem necessary,
>
> I used the libvirt-python APIs for some testing. I called an (own) API
> with 'None' as argument and this resulted in 0 parameters.
>
>> but it's been 2 months since I looked at
>> this and that level of detail has long been flushed out of main memory
>> cache. ;-)
>>
>>> The man page for calloc-posix reads:
>>>
>>> “If either nelem or elsize is 0, then either a null pointer or a unique
>>> pointer value that can be successfully passed to free() shall be
>>> returned.” [1]
>>>
>>> [1] https://www.unix.com/man-page/POSIX/3posix/calloc/
>>>
>>
>> perhaps then we avoid this conundrum and take Daniel's advice in his
>> response:
>>
>>"If there is any problem with this, then we should just change
>> bootstrap.conf to use calloc-gnu instead of calloc-posix, which
>> basically turns calloc(0) into calloc(1) for compat with glibc
>> behaviour."
>
> This would still 

[libvirt] [PATCH v2 7/9] rpc: Alter virNetDaemonQuit processing

2018-07-07 Thread John Ferlan
When virNetDaemonQuit is called from libvirtd's shutdown
handler (daemonShutdownHandler) we need to perform the quit
in multiple steps. The first part is to "request" the quit
and notify the NetServer's of the impending quit which causes
the NetServers to inform their workers that a quit was requested.

Still because we cannot guarantee a quit will happen or it's
possible there's no workers pending, use a virNetDaemonQuitTimer
to not only break the event loop but keep track of how long we're
waiting and we've waited too long, force an ungraceful exit so
that we don't hang waiting forever or cause some sort of SEGV
because something is still pending and we Unref things.

Signed-off-by: John Ferlan 
---
 src/libvirt_remote.syms|  1 +
 src/remote/remote_daemon.c |  1 +
 src/rpc/virnetdaemon.c | 68 +-
 src/rpc/virnetdaemon.h |  4 +++
 4 files changed, 73 insertions(+), 1 deletion(-)

diff --git a/src/libvirt_remote.syms b/src/libvirt_remote.syms
index a8f937f32e..e416cfec44 100644
--- a/src/libvirt_remote.syms
+++ b/src/libvirt_remote.syms
@@ -87,6 +87,7 @@ virNetDaemonPreExecRestart;
 virNetDaemonQuit;
 virNetDaemonRemoveShutdownInhibition;
 virNetDaemonRun;
+virNetDaemonSetQuitTimeout;
 virNetDaemonUpdateServices;
 
 
diff --git a/src/remote/remote_daemon.c b/src/remote/remote_daemon.c
index dee634d7e1..d11adf422f 100644
--- a/src/remote/remote_daemon.c
+++ b/src/remote/remote_daemon.c
@@ -1273,6 +1273,7 @@ int main(int argc, char **argv) {
 ret = VIR_DAEMON_ERR_DRIVER;
 goto cleanup;
 }
+virNetDaemonSetQuitTimeout(dmn, config->quit_timeout);
 
 if (!(srv = virNetServerNew("libvirtd", 1,
 config->min_workers,
diff --git a/src/rpc/virnetdaemon.c b/src/rpc/virnetdaemon.c
index 329f116a6c..c6ed65c8c3 100644
--- a/src/rpc/virnetdaemon.c
+++ b/src/rpc/virnetdaemon.c
@@ -73,6 +73,8 @@ struct _virNetDaemon {
 virHashTablePtr servers;
 virJSONValuePtr srvObject;
 
+unsigned int quitTimeout;
+bool quitRequested;
 bool quit;
 
 unsigned int autoShutdownTimeout;
@@ -153,6 +155,14 @@ virNetDaemonNew(void)
 }
 
 
+void
+virNetDaemonSetQuitTimeout(virNetDaemonPtr dmn,
+   unsigned int quitTimeout)
+{
+dmn->quitTimeout = quitTimeout;
+}
+
+
 int
 virNetDaemonAddServer(virNetDaemonPtr dmn,
   virNetServerPtr srv)
@@ -791,11 +801,50 @@ daemonServerProcessClients(void *payload,
 return 0;
 }
 
+
+static int
+daemonServerWorkerCount(void *payload,
+const void *key ATTRIBUTE_UNUSED,
+void *opaque)
+{
+size_t *workerCount = opaque;
+virNetServerPtr srv = payload;
+
+*workerCount += virNetServerWorkerCount(srv);
+
+return 0;
+}
+
+
+static bool
+daemonServerWorkersDone(virNetDaemonPtr dmn)
+{
+size_t workerCount = 0;
+
+virHashForEach(dmn->servers, daemonServerWorkerCount, );
+
+return workerCount == 0;
+}
+
+
+static void
+virNetDaemonQuitTimer(int timer ATTRIBUTE_UNUSED,
+  void *opaque)
+{
+int *quitCount = opaque;
+
+(*quitCount)++;
+VIR_DEBUG("quitCount=%d", *quitCount);
+}
+
+
 void
 virNetDaemonRun(virNetDaemonPtr dmn)
 {
 int timerid = -1;
 bool timerActive = false;
+int quitTimer = -1;
+int quitCount = 0;
 
 virObjectLock(dmn);
 
@@ -855,10 +904,27 @@ virNetDaemonRun(virNetDaemonPtr dmn)
 virObjectLock(dmn);
 
 virHashForEach(dmn->servers, daemonServerProcessClients, NULL);
+
+/* HACK: Add a dummy timeout to break event loop */
+if (dmn->quitRequested && quitTimer == -1)
+quitTimer = virEventAddTimeout(500, virNetDaemonQuitTimer,
+   , NULL);
+
+if (dmn->quitRequested && daemonServerWorkersDone(dmn)) {
+dmn->quit = true;
+} else {
+/* Firing every 1/2 second and quitTimeout in seconds, force
+ * an exit when there are still worker threads running and we
+ * have waited long enough */
+if (quitCount > dmn->quitTimeout * 2)
+_exit(EXIT_FAILURE);
+}
 }
 
  cleanup:
 virObjectUnlock(dmn);
+if (quitTimer != -1)
+virEventRemoveTimeout(quitTimer);
 }
 
 
@@ -880,7 +946,7 @@ virNetDaemonQuit(virNetDaemonPtr dmn)
 virObjectLock(dmn);
 
 VIR_DEBUG("Quit requested %p", dmn);
-dmn->quit = true;
+dmn->quitRequested = true;
 virHashForEach(dmn->servers, daemonServerQuitRequested, NULL);
 
 virObjectUnlock(dmn);
diff --git a/src/rpc/virnetdaemon.h b/src/rpc/virnetdaemon.h
index 09ed5adf36..8433d6a09d 100644
--- a/src/rpc/virnetdaemon.h
+++ b/src/rpc/virnetdaemon.h
@@ -35,6 +35,10 @@
 
 virNetDaemonPtr virNetDaemonNew(void);
 
+void
+virNetDaemonSetQuitTimeout(virNetDaemonPtr dmn,
+   unsigned int quitTimeout);
+
 int virNetDaemonAddServer(virNetDaemonPtr dmn,
   

[libvirt] [PATCH v2 0/9] Resolve libvirtd hang on termination with connected long running client

2018-07-07 Thread John Ferlan
v1: https://www.redhat.com/archives/libvir-list/2018-January/msg00624.html

Cannot recall differences from v1.

Since so much changed since v1, figured it'd be easier to just repost
to continue the discussion.

John Ferlan (9):
  libvirtd: Alter refcnt processing for domain server objects
  libvirtd: Alter refcnt processing for server program objects
  util: Introduce virThreadPoolQuitRequested
  rpc: Introduce virNetServerQuitRequested
  rpc: Introduce virNetServerWorkerCount
  daemon: Add quit_timeout period
  rpc: Alter virNetDaemonQuit processing
  docs: Add news article for libvirtd issue
  APPLY ONLY FOR TESTING PURPOSES

 docs/news.xml | 12 +
 src/libvirt_private.syms  |  1 +
 src/libvirt_remote.syms   |  3 ++
 src/qemu/qemu_driver.c|  5 ++
 src/remote/libvirtd.aug   |  1 +
 src/remote/libvirtd.conf  |  8 +++
 src/remote/remote_daemon.c| 40 ++-
 src/remote/remote_daemon_config.c |  5 ++
 src/remote/remote_daemon_config.h |  2 +
 src/remote/test_libvirtd.aug.in   |  1 +
 src/rpc/virnetdaemon.c| 82 ++-
 src/rpc/virnetdaemon.h|  4 ++
 src/rpc/virnetserver.c| 50 +++
 src/rpc/virnetserver.h|  4 ++
 src/util/virthreadpool.c  | 51 ---
 src/util/virthreadpool.h  |  2 +
 16 files changed, 250 insertions(+), 21 deletions(-)

-- 
2.17.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v2 4/9] rpc: Introduce virNetServerQuitRequested

2018-07-07 Thread John Ferlan
Create a helper API to disable new connections and request the worker
threads to quit when a quit has been requested such as via a signal handler.

Signed-off-by: John Ferlan 
---
 src/libvirt_remote.syms |  1 +
 src/rpc/virnetdaemon.c  | 13 +
 src/rpc/virnetserver.c  | 24 
 src/rpc/virnetserver.h  |  2 ++
 4 files changed, 40 insertions(+)

diff --git a/src/libvirt_remote.syms b/src/libvirt_remote.syms
index 9a33626ec6..9ee983fdf2 100644
--- a/src/libvirt_remote.syms
+++ b/src/libvirt_remote.syms
@@ -129,6 +129,7 @@ virNetServerNewPostExecRestart;
 virNetServerNextClientID;
 virNetServerPreExecRestart;
 virNetServerProcessClients;
+virNetServerQuitRequested;
 virNetServerSetClientAuthenticated;
 virNetServerSetClientLimits;
 virNetServerSetThreadPoolParameters;
diff --git a/src/rpc/virnetdaemon.c b/src/rpc/virnetdaemon.c
index 31b687de12..329f116a6c 100644
--- a/src/rpc/virnetdaemon.c
+++ b/src/rpc/virnetdaemon.c
@@ -862,6 +862,18 @@ virNetDaemonRun(virNetDaemonPtr dmn)
 }
 
 
+static int
+daemonServerQuitRequested(void *payload,
+  const void *key ATTRIBUTE_UNUSED,
+  void *opaque ATTRIBUTE_UNUSED)
+{
+virNetServerPtr srv = payload;
+
+virNetServerQuitRequested(srv);
+return 0;
+}
+
+
 void
 virNetDaemonQuit(virNetDaemonPtr dmn)
 {
@@ -869,6 +881,7 @@ virNetDaemonQuit(virNetDaemonPtr dmn)
 
 VIR_DEBUG("Quit requested %p", dmn);
 dmn->quit = true;
+virHashForEach(dmn->servers, daemonServerQuitRequested, NULL);
 
 virObjectUnlock(dmn);
 }
diff --git a/src/rpc/virnetserver.c b/src/rpc/virnetserver.c
index 5c7f7dd08f..c426567d86 100644
--- a/src/rpc/virnetserver.c
+++ b/src/rpc/virnetserver.c
@@ -818,6 +818,30 @@ void virNetServerDispose(void *obj)
 virNetServerMDNSFree(srv->mdns);
 }
 
+
+/* virNetServerQuitRequested:
+ * @srv: Netserver pointer
+ *
+ * Disable new connections and let the worker threads know that
+ * a quit has been requested.
+ */
+void
+virNetServerQuitRequested(virNetServerPtr srv)
+{
+size_t i;
+
+if (!srv)
+return;
+
+VIR_DEBUG("Quit server requested '%s'", srv->name);
+
+for (i = 0; i < srv->nservices; i++)
+virNetServerServiceToggle(srv->services[i], false);
+
+virThreadPoolQuitRequested(srv->workers);
+}
+
+
 void virNetServerClose(virNetServerPtr srv)
 {
 size_t i;
diff --git a/src/rpc/virnetserver.h b/src/rpc/virnetserver.h
index 26cec43c22..fcaf48fe14 100644
--- a/src/rpc/virnetserver.h
+++ b/src/rpc/virnetserver.h
@@ -58,6 +58,8 @@ virNetServerPtr 
virNetServerNewPostExecRestart(virJSONValuePtr object,
 ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3)
 ATTRIBUTE_NONNULL(4) ATTRIBUTE_NONNULL(5) ATTRIBUTE_NONNULL(6);
 
+void virNetServerQuitRequested(virNetServerPtr srv);
+
 void virNetServerClose(virNetServerPtr srv);
 
 virJSONValuePtr virNetServerPreExecRestart(virNetServerPtr srv);
-- 
2.17.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v2 5/9] rpc: Introduce virNetServerWorkerCount

2018-07-07 Thread John Ferlan
Get a count of the number of workers for all servers that are still
processing a job.

Signed-off-by: John Ferlan 
---
 src/libvirt_remote.syms |  1 +
 src/rpc/virnetserver.c  | 26 ++
 src/rpc/virnetserver.h  |  2 ++
 3 files changed, 29 insertions(+)

diff --git a/src/libvirt_remote.syms b/src/libvirt_remote.syms
index 9ee983fdf2..a8f937f32e 100644
--- a/src/libvirt_remote.syms
+++ b/src/libvirt_remote.syms
@@ -136,6 +136,7 @@ virNetServerSetThreadPoolParameters;
 virNetServerSetTLSContext;
 virNetServerStart;
 virNetServerUpdateServices;
+virNetServerWorkerCount;
 
 
 # rpc/virnetserverclient.h
diff --git a/src/rpc/virnetserver.c b/src/rpc/virnetserver.c
index c426567d86..053ef8a5ab 100644
--- a/src/rpc/virnetserver.c
+++ b/src/rpc/virnetserver.c
@@ -842,6 +842,32 @@ virNetServerQuitRequested(virNetServerPtr srv)
 }
 
 
+/* virNetServerWorkerCount:
+ * @srv: Netserver pointer
+ *
+ * Return the number of workers still waiting to be processed. This
+ * allows the quit requested code to wait for all worker jobs to be
+ * completed before actually quitting.
+ */
+size_t
+virNetServerWorkerCount(virNetServerPtr srv)
+{
+size_t workerCount;
+
+virObjectLock(srv);
+
+workerCount = virThreadPoolGetCurrentWorkers(srv->workers) +
+  virThreadPoolGetPriorityWorkers(srv->workers);
+
+if (workerCount > 0)
+VIR_DEBUG("server '%s' still has %zd workers", srv->name, workerCount);
+
+virObjectUnlock(srv);
+
+return workerCount;
+}
+
+
 void virNetServerClose(virNetServerPtr srv)
 {
 size_t i;
diff --git a/src/rpc/virnetserver.h b/src/rpc/virnetserver.h
index fcaf48fe14..3b2582e15b 100644
--- a/src/rpc/virnetserver.h
+++ b/src/rpc/virnetserver.h
@@ -60,6 +60,8 @@ virNetServerPtr 
virNetServerNewPostExecRestart(virJSONValuePtr object,
 
 void virNetServerQuitRequested(virNetServerPtr srv);
 
+size_t virNetServerWorkerCount(virNetServerPtr srv);
+
 void virNetServerClose(virNetServerPtr srv);
 
 virJSONValuePtr virNetServerPreExecRestart(virNetServerPtr srv);
-- 
2.17.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v2 8/9] docs: Add news article for libvirtd issue

2018-07-07 Thread John Ferlan
Signed-off-by: John Ferlan 
---
 docs/news.xml | 12 
 1 file changed, 12 insertions(+)

diff --git a/docs/news.xml b/docs/news.xml
index 773c95b0da..6cacded58f 100644
--- a/docs/news.xml
+++ b/docs/news.xml
@@ -46,6 +46,18 @@
   
 
 
+  
+
+  Fix issues with unexpected libvirtd termination
+
+
+  If there were outstanding worker threads when libvirtd
+  received a quit signal via SIGTERM, SIGINT, or SIGQUIT
+  then, libvirtd may have crashed or hung waiting for a
+  worker job to send a completion notification that cannot
+  be received because the event loop was stopped too soon.
+
+  
 
 
 
-- 
2.17.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v2 9/9] APPLY ONLY FOR TESTING PURPOSES

2018-07-07 Thread John Ferlan
Modify GetAllStats to generate a long enough pause in order to send a
SIGTERM to libvirtd while a client connection is processing.

In order to "set things up":

1. In one terminal window from a local branch built using these
   patches using a root account run libvirtd debug, e.g.:

# ./run gdb src/libvirtd

once running, type a 'c' (e.g. continue) and 

2. Start a domain (or have one running with the current libvirtd)

virsh start $domain

3. Prepare a domstats command for that domain (but don't yet
   hit  in order run it):

virsh domstats $domain

4. Prepare a kill command for the running libvirtd, e.g.:

jferlan   4143  4107  0 09:51 pts/100:00:00 vim +1396 src/libvirtd.c
root 30054 21195  6 11:17 pts/800:00:01 gdb 
/home/jferlan/git/libvirt.work/src/.libs/lt-libvirtd
root 30087 30054  7 11:17 pts/800:00:01 
/home/jferlan/git/libvirt.work/src/.libs/lt-libvirtd
root 30385 19861  0 11:17 pts/17   00:00:00 grep --color=auto libvirtd

but again don't hit  yet.

5. Align your stars perfectly now...

   a. Hit  on your domstats command
   b. Swap to the kill command window and hit 

   This should cause the libvirtd debug window to stop, but since you
   already typed 'c' it'll continue at least briefly, for example:

...
[Thread 0x7fffc3231700 (LWP 30374) exited]
Detaching after fork from child process 30376.
Detaching after fork from child process 30377.
Detaching after fork from child process 30378.
[Thread 0x7fffc4445700 (LWP 30106) exited]
c
2018-01-10 16:18:12.962+: 30094: info : libvirt version: 4.0.0
2018-01-10 16:18:12.962+: 30094: info : hostname: 
unknown4ceb42c824f4.attlocal.net
2018-01-10 16:18:12.962+: 30094: warning : 
qemuConnectGetAllDomainStats:20265 : k = -5340232226128654848

Thread 1 "lt-libvirtd" received signal SIGTERM, Terminated.
0x73ae6d2d in poll () from /lib64/libc.so.6
...
(gdb) c
Continuing.
[Thread 0x7fffc5c48700 (LWP 30103) exited]
[Thread 0x7fffc5447700 (LWP 30104) exited]
[Thread 0x7fffc4c46700 (LWP 30105) exited]
[Thread 0x7fffc6449700 (LWP 30102) exited]
[Thread 0x7fffc6c4a700 (LWP 30101) exited]
[Thread 0x7fffe3b57700 (LWP 30097) exited]
[Thread 0x7fffe4358700 (LWP 30096) exited]
[Thread 0x7fffe2354700 (LWP 30100) exited]
[Thread 0x7fffe3356700 (LWP 30098) exited]
[Thread 0x7fffe2b55700 (LWP 30099) exited]
[Thread 0x7fffe535a700 (LWP 30094) exited]
[Thread 0x7fffe5b5b700 (LWP 30093) exited]
[Thread 0x7fffe635c700 (LWP 30092) exited]
[Thread 0x7fffe6b5d700 (LWP 30091) exited]
2018-01-10 16:18:25.451+: 30095: warning : 
qemuConnectGetAllDomainStats:20265 : k = -5340232226128654848
[Thread 0x7fffe4b59700 (LWP 30095) exited]
[Thread 0x7fffc3a32700 (LWP 30187) exited]
[Inferior 1 (process 30087) exited normally]
(gdb) c
The program is not being run.
(gdb) quit

The virsh domstats window will "close" as follows:

error: Disconnected from qemu:///system due to end of file
error: End of file while reading data: Input/output error

If something's wrong, then the libvirtd window may not exit those
final two threads in which case you could interrupt it (^c) and
check the threads (thread apply all bt) which will probably show
some sort of hang... My testing shows that the hang no longer
occurs with all the previous patches applied.

The subsequent patch calling virHashRemoveAll from virNetDaemonClose
does not seem to be necessary, although I suppose it cannot hurt as
the same essential functionality occurs during the Dispose function

Signed-off-by: John Ferlan 
---
 src/qemu/qemu_driver.c   |  5 +
 src/rpc/virnetdaemon.c   |  3 ++-
 src/rpc/virnetserver.c   |  4 ++--
 src/util/virthreadpool.c | 10 +++---
 4 files changed, 16 insertions(+), 6 deletions(-)

diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index 9a35e04a85..fa725131a2 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -20454,6 +20454,7 @@ qemuConnectGetAllDomainStats(virConnectPtr conn,
 bool enforce = !!(flags & VIR_CONNECT_GET_ALL_DOMAINS_STATS_ENFORCE_STATS);
 int nstats = 0;
 size_t i;
+size_t j, k = 0;
 int ret = -1;
 unsigned int privflags = 0;
 unsigned int domflags = 0;
@@ -20492,6 +20493,10 @@ qemuConnectGetAllDomainStats(virConnectPtr conn,
 if (qemuDomainGetStatsNeedMonitor(stats))
 privflags |= QEMU_DOMAIN_STATS_HAVE_JOB;
 
+for (j = 0; j < 100; j++)  // Add one more zero for longer...
+ k = j + k;
+VIR_WARN("k = %zd", k);
+
 for (i = 0; i < nvms; i++) {
 virDomainStatsRecordPtr tmp = NULL;
 domflags = 0;
diff --git a/src/rpc/virnetdaemon.c b/src/rpc/virnetdaemon.c
index c6ed65c8c3..6cce4b7004 100644
--- a/src/rpc/virnetdaemon.c
+++ b/src/rpc/virnetdaemon.c
@@ -834,7 +834,7 @@ virNetDaemonQuitTimer(int timer ATTRIBUTE_UNUSED,
 int *quitCount = opaque;
 
 (*quitCount)++;
-VIR_DEBUG("quitCount=%d", *quitCount);
+VIR_WARN("quitCount=%d", *quitCount);
 }
 
 
@@ -912,6 +912,7 @@ 

[libvirt] [PATCH v2 3/9] util: Introduce virThreadPoolQuitRequested

2018-07-07 Thread John Ferlan
Create a helper API to set the quit flag and wake up the worker
threads when a quit has been requested such as via a signal handler.

Signed-off-by: John Ferlan 
---
 src/libvirt_private.syms |  1 +
 src/util/virthreadpool.c | 43 ++--
 src/util/virthreadpool.h |  2 ++
 3 files changed, 40 insertions(+), 6 deletions(-)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 3e304907b9..af76c29928 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -3001,6 +3001,7 @@ virThreadPoolGetMaxWorkers;
 virThreadPoolGetMinWorkers;
 virThreadPoolGetPriorityWorkers;
 virThreadPoolNewFull;
+virThreadPoolQuitRequested;
 virThreadPoolSendJob;
 virThreadPoolSetParameters;
 
diff --git a/src/util/virthreadpool.c b/src/util/virthreadpool.c
index 10f2bd2c3a..137c5d1746 100644
--- a/src/util/virthreadpool.c
+++ b/src/util/virthreadpool.c
@@ -30,9 +30,12 @@
 #include "viralloc.h"
 #include "virthread.h"
 #include "virerror.h"
+#include "virlog.h"
 
 #define VIR_FROM_THIS VIR_FROM_NONE
 
+VIR_LOG_INIT("util.threadpool");
+
 typedef struct _virThreadPoolJob virThreadPoolJob;
 typedef virThreadPoolJob *virThreadPoolJobPtr;
 
@@ -269,6 +272,18 @@ virThreadPoolNewFull(size_t minWorkers,
 
 }
 
+
+static void
+virThreadPoolSetQuit(virThreadPoolPtr pool)
+{
+pool->quit = true;
+if (pool->nWorkers > 0)
+virCondBroadcast(>cond);
+if (pool->nPrioWorkers > 0)
+virCondBroadcast(>prioCond);
+}
+
+
 void virThreadPoolFree(virThreadPoolPtr pool)
 {
 virThreadPoolJobPtr job;
@@ -278,13 +293,9 @@ void virThreadPoolFree(virThreadPoolPtr pool)
 return;
 
 virMutexLock(>mutex);
-pool->quit = true;
-if (pool->nWorkers > 0)
-virCondBroadcast(>cond);
-if (pool->nPrioWorkers > 0) {
+if (pool->nPrioWorkers > 0)
 priority = true;
-virCondBroadcast(>prioCond);
-}
+virThreadPoolSetQuit(pool);
 
 while (pool->nWorkers > 0 || pool->nPrioWorkers > 0)
 ignore_value(virCondWait(>quit_cond, >mutex));
@@ -307,6 +318,26 @@ void virThreadPoolFree(virThreadPoolPtr pool)
 }
 
 
+/*
+ * virThreadPoolQuitRequested:
+ * @pool: Pointer to thread pool
+ *
+ * When libvirtd quit is requested via the daemonShutdownHandler let's
+ * set the quit flag for current workers and wake them up.
+ */
+void
+virThreadPoolQuitRequested(virThreadPoolPtr pool)
+{
+virMutexLock(>mutex);
+
+VIR_DEBUG("nWorkers=%zd, nPrioWorkers=%zd jobQueueDepth=%zd",
+  pool->nWorkers, pool->nPrioWorkers, pool->jobQueueDepth);
+
+virThreadPoolSetQuit(pool);
+virMutexUnlock(>mutex);
+}
+
+
 size_t virThreadPoolGetMinWorkers(virThreadPoolPtr pool)
 {
 size_t ret;
diff --git a/src/util/virthreadpool.h b/src/util/virthreadpool.h
index e1f362f5bb..f2c8b2ae61 100644
--- a/src/util/virthreadpool.h
+++ b/src/util/virthreadpool.h
@@ -52,6 +52,8 @@ size_t virThreadPoolGetJobQueueDepth(virThreadPoolPtr pool);
 
 void virThreadPoolFree(virThreadPoolPtr pool);
 
+void virThreadPoolQuitRequested(virThreadPoolPtr pool);
+
 int virThreadPoolSendJob(virThreadPoolPtr pool,
  unsigned int priority,
  void *jobdata) ATTRIBUTE_NONNULL(1)
-- 
2.17.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v2 2/9] libvirtd: Alter refcnt processing for server program objects

2018-07-07 Thread John Ferlan
Once virNetServerProgramNew returns, the program objects have
either been added to the @srv or @srvAdm list increasing the
refcnt or there was an error. In either case, we can lower the
refcnt from virNetServerProgramNew and set the object to NULL
since it won't be used any more.

The @srv or @srvAdm dispose function (virNetServerDispose) will
handle the Unref of each object during the virHashFree when the
object is removed from the hash table.

Signed-off-by: John Ferlan 
---
 src/remote/remote_daemon.c | 24 
 1 file changed, 16 insertions(+), 8 deletions(-)

diff --git a/src/remote/remote_daemon.c b/src/remote/remote_daemon.c
index f61d58f3e5..dee634d7e1 100644
--- a/src/remote/remote_daemon.c
+++ b/src/remote/remote_daemon.c
@@ -1317,7 +1317,10 @@ int main(int argc, char **argv) {
 ret = VIR_DAEMON_ERR_INIT;
 goto cleanup;
 }
-if (virNetServerAddProgram(srv, remoteProgram) < 0) {
+
+rc = virNetServerAddProgram(srv, remoteProgram);
+virObjectUnref(remoteProgram);
+if (rc < 0) {
 ret = VIR_DAEMON_ERR_INIT;
 goto cleanup;
 }
@@ -1329,7 +1332,10 @@ int main(int argc, char **argv) {
 ret = VIR_DAEMON_ERR_INIT;
 goto cleanup;
 }
-if (virNetServerAddProgram(srv, lxcProgram) < 0) {
+
+rc = virNetServerAddProgram(srv, lxcProgram);
+virObjectUnref(lxcProgram);
+if (rc < 0) {
 ret = VIR_DAEMON_ERR_INIT;
 goto cleanup;
 }
@@ -1341,7 +1347,10 @@ int main(int argc, char **argv) {
 ret = VIR_DAEMON_ERR_INIT;
 goto cleanup;
 }
-if (virNetServerAddProgram(srv, qemuProgram) < 0) {
+
+rc = virNetServerAddProgram(srv, qemuProgram);
+virObjectUnref(qemuProgram);
+if (rc < 0) {
 ret = VIR_DAEMON_ERR_INIT;
 goto cleanup;
 }
@@ -1379,7 +1388,10 @@ int main(int argc, char **argv) {
 ret = VIR_DAEMON_ERR_INIT;
 goto cleanup;
 }
-if (virNetServerAddProgram(srvAdm, adminProgram) < 0) {
+
+rc = virNetServerAddProgram(srvAdm, adminProgram);
+virObjectUnref(adminProgram);
+if (rc < 0) {
 ret = VIR_DAEMON_ERR_INIT;
 goto cleanup;
 }
@@ -1487,10 +1499,6 @@ int main(int argc, char **argv) {
 virStateCleanup();
 }
 
-virObjectUnref(adminProgram);
-virObjectUnref(qemuProgram);
-virObjectUnref(lxcProgram);
-virObjectUnref(remoteProgram);
 virObjectUnref(dmn);
 
 virNetlinkShutdown();
-- 
2.17.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v2 1/9] libvirtd: Alter refcnt processing for domain server objects

2018-07-07 Thread John Ferlan
Once virNetDaemonAddServer returns, the @srv or @srvAdm have
either been added to the @dmn list increasing the refcnt or
there was an error. In either case, we can lower the refcnt
from virNetServerNew, but not set to NULL. Thus "using" the
hash table reference when adding programs later or allowing
the immediate free of the object on error.

The @dmn dispose function (virNetDaemonDispose) will handle
the Unref of each object during the virHashFree when the
object is removed from the hash table.

Signed-off-by: John Ferlan 
Reviewed-by: Erik Skultety 
Reviewed-by: Marc Hartmayer 
---
 src/remote/remote_daemon.c | 15 +++
 1 file changed, 11 insertions(+), 4 deletions(-)

diff --git a/src/remote/remote_daemon.c b/src/remote/remote_daemon.c
index 9f3a5f38ad..f61d58f3e5 100644
--- a/src/remote/remote_daemon.c
+++ b/src/remote/remote_daemon.c
@@ -1036,6 +1036,7 @@ int main(int argc, char **argv) {
 char *remote_config_file = NULL;
 int statuswrite = -1;
 int ret = 1;
+int rc;
 int pid_file_fd = -1;
 char *pid_file = NULL;
 char *sock_file = NULL;
@@ -1290,7 +1291,11 @@ int main(int argc, char **argv) {
 goto cleanup;
 }
 
-if (virNetDaemonAddServer(dmn, srv) < 0) {
+/* Add @srv to @dmn servers hash table and Unref to allow removal from
+ * hash table at @dmn disposal to decide when to free @srv */
+rc = virNetDaemonAddServer(dmn, srv);
+virObjectUnref(srv);
+if (rc < 0) {
 ret = VIR_DAEMON_ERR_INIT;
 goto cleanup;
 }
@@ -1358,7 +1363,11 @@ int main(int argc, char **argv) {
 goto cleanup;
 }
 
-if (virNetDaemonAddServer(dmn, srvAdm) < 0) {
+/* Add @srvAdm to @dmn servers hash table and Unref to allow removal from
+ * hash table at @dmn disposal to decide when to free @srvAdm */
+rc = virNetDaemonAddServer(dmn, srvAdm);
+virObjectUnref(srvAdm);
+if (rc < 0) {
 ret = VIR_DAEMON_ERR_INIT;
 goto cleanup;
 }
@@ -1479,11 +1488,9 @@ int main(int argc, char **argv) {
 }
 
 virObjectUnref(adminProgram);
-virObjectUnref(srvAdm);
 virObjectUnref(qemuProgram);
 virObjectUnref(lxcProgram);
 virObjectUnref(remoteProgram);
-virObjectUnref(srv);
 virObjectUnref(dmn);
 
 virNetlinkShutdown();
-- 
2.17.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list


[libvirt] [PATCH v2 6/9] daemon: Add quit_timeout period

2018-07-07 Thread John Ferlan
In order to prepare for the possibility to allow long running
thread(s) to complete their task(s) when a quit signal is received
by the daemon, create a configurable quit_timeout period in seconds.

Signed-off-by: John Ferlan 
---
 src/remote/libvirtd.aug   | 1 +
 src/remote/libvirtd.conf  | 8 
 src/remote/remote_daemon_config.c | 5 +
 src/remote/remote_daemon_config.h | 2 ++
 src/remote/test_libvirtd.aug.in   | 1 +
 5 files changed, 17 insertions(+)

diff --git a/src/remote/libvirtd.aug b/src/remote/libvirtd.aug
index 1448a4..2c282e53e5 100644
--- a/src/remote/libvirtd.aug
+++ b/src/remote/libvirtd.aug
@@ -62,6 +62,7 @@ module Libvirtd =
 | int_entry "max_anonymous_clients"
 | int_entry "max_client_requests"
 | int_entry "prio_workers"
+| int_entry "quit_timeout"
 
let admin_processing_entry = int_entry "admin_min_workers"
   | int_entry "admin_max_workers"
diff --git a/src/remote/libvirtd.conf b/src/remote/libvirtd.conf
index 92562ab7ae..cc9647391d 100644
--- a/src/remote/libvirtd.conf
+++ b/src/remote/libvirtd.conf
@@ -321,6 +321,14 @@
 # parameter.
 #max_client_requests = 5
 
+# Approximate number of seconds to wait to allow worker threads to
+# complete when a signal to quit (e.g. SIGINT, SIGQUIT, SIGTERM) is
+# received by libvirtd. This is only for currently processing workers
+# in order to allow them to potentially complete their long running
+# task such as stats collection or migrations. The default is to
+# not wait.
+#quit_timeout = 0
+
 # Same processing controls, but this time for the admin interface.
 # For description of each option, be so kind to scroll few lines
 # upwards.
diff --git a/src/remote/remote_daemon_config.c 
b/src/remote/remote_daemon_config.c
index b1516befb4..d6b3575a9a 100644
--- a/src/remote/remote_daemon_config.c
+++ b/src/remote/remote_daemon_config.c
@@ -155,6 +155,8 @@ daemonConfigNew(bool privileged ATTRIBUTE_UNUSED)
 
 data->max_client_requests = 5;
 
+data->quit_timeout = 0;
+
 data->audit_level = 1;
 data->audit_logging = 0;
 
@@ -350,6 +352,9 @@ daemonConfigLoadOptions(struct daemonConfig *data,
 if (virConfGetValueUInt(conf, "max_client_requests", 
>max_client_requests) < 0)
 goto error;
 
+if (virConfGetValueUInt(conf, "quit_timeout", >quit_timeout) < 0)
+goto error;
+
 if (virConfGetValueUInt(conf, "admin_min_workers", 
>admin_min_workers) < 0)
 goto error;
 if (virConfGetValueUInt(conf, "admin_max_workers", 
>admin_max_workers) < 0)
diff --git a/src/remote/remote_daemon_config.h 
b/src/remote/remote_daemon_config.h
index 49ea80104b..0f27bf44c8 100644
--- a/src/remote/remote_daemon_config.h
+++ b/src/remote/remote_daemon_config.h
@@ -73,6 +73,8 @@ struct daemonConfig {
 
 unsigned int max_client_requests;
 
+unsigned int quit_timeout;
+
 unsigned int log_level;
 char *log_filters;
 char *log_outputs;
diff --git a/src/remote/test_libvirtd.aug.in b/src/remote/test_libvirtd.aug.in
index 527e3d7d0d..c559696f58 100644
--- a/src/remote/test_libvirtd.aug.in
+++ b/src/remote/test_libvirtd.aug.in
@@ -43,6 +43,7 @@ module Test_libvirtd =
 { "max_workers" = "20" }
 { "prio_workers" = "5" }
 { "max_client_requests" = "5" }
+{ "quit_timeout" = "0" }
 { "admin_min_workers" = "1" }
 { "admin_max_workers" = "5" }
 { "admin_max_clients" = "5" }
-- 
2.17.1

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list