2010-12-01 16:35:50 +00:00
|
|
|
/*
|
|
|
|
* virnetclient.c: generic network RPC client
|
|
|
|
*
|
2014-03-17 09:38:38 +00:00
|
|
|
* Copyright (C) 2006-2014 Red Hat, Inc.
|
2010-12-01 16:35:50 +00:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2012-09-20 22:30:55 +00:00
|
|
|
* License along with this library. If not, see
|
2012-07-21 10:06:23 +00:00
|
|
|
* <http://www.gnu.org/licenses/>.
|
2010-12-01 16:35:50 +00:00
|
|
|
*
|
|
|
|
* Author: Daniel P. Berrange <berrange@redhat.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <poll.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
|
|
|
#include "virnetclient.h"
|
|
|
|
#include "virnetsocket.h"
|
2011-09-22 12:59:06 +00:00
|
|
|
#include "virkeepalive.h"
|
2012-12-12 18:06:53 +00:00
|
|
|
#include "viralloc.h"
|
2012-12-13 15:49:48 +00:00
|
|
|
#include "virthread.h"
|
2011-07-19 18:32:58 +00:00
|
|
|
#include "virfile.h"
|
2012-12-12 17:59:27 +00:00
|
|
|
#include "virlog.h"
|
2012-12-13 17:44:57 +00:00
|
|
|
#include "virutil.h"
|
2012-12-13 18:21:53 +00:00
|
|
|
#include "virerror.h"
|
2014-02-27 13:41:11 +00:00
|
|
|
#include "virprobe.h"
|
2013-05-03 12:47:53 +00:00
|
|
|
#include "virstring.h"
|
2010-12-01 16:35:50 +00:00
|
|
|
|
|
|
|
#define VIR_FROM_THIS VIR_FROM_RPC
|
|
|
|
|
2014-02-28 12:16:17 +00:00
|
|
|
VIR_LOG_INIT("rpc.netclient");
|
|
|
|
|
2010-12-01 16:35:50 +00:00
|
|
|
typedef struct _virNetClientCall virNetClientCall;
|
|
|
|
typedef virNetClientCall *virNetClientCallPtr;
|
|
|
|
|
|
|
|
enum {
|
|
|
|
VIR_NET_CLIENT_MODE_WAIT_TX,
|
|
|
|
VIR_NET_CLIENT_MODE_WAIT_RX,
|
|
|
|
VIR_NET_CLIENT_MODE_COMPLETE,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct _virNetClientCall {
|
|
|
|
int mode;
|
|
|
|
|
|
|
|
virNetMessagePtr msg;
|
|
|
|
bool expectReply;
|
2011-11-08 09:13:27 +00:00
|
|
|
bool nonBlock;
|
|
|
|
bool haveThread;
|
2010-12-01 16:35:50 +00:00
|
|
|
|
|
|
|
virCond cond;
|
|
|
|
|
|
|
|
virNetClientCallPtr next;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
struct _virNetClient {
|
2013-01-09 21:27:28 +00:00
|
|
|
virObjectLockable parent;
|
2010-12-01 16:35:50 +00:00
|
|
|
|
|
|
|
virNetSocketPtr sock;
|
2012-08-07 11:09:51 +00:00
|
|
|
bool asyncIO;
|
2010-12-01 16:35:50 +00:00
|
|
|
|
2013-01-08 21:02:05 +00:00
|
|
|
#if WITH_GNUTLS
|
2010-12-01 16:35:50 +00:00
|
|
|
virNetTLSSessionPtr tls;
|
2013-01-07 14:54:18 +00:00
|
|
|
#endif
|
2010-12-01 16:35:50 +00:00
|
|
|
char *hostname;
|
|
|
|
|
|
|
|
virNetClientProgramPtr *programs;
|
|
|
|
size_t nprograms;
|
|
|
|
|
|
|
|
/* For incoming message packets */
|
|
|
|
virNetMessage msg;
|
|
|
|
|
2012-09-20 11:58:29 +00:00
|
|
|
#if WITH_SASL
|
2010-12-01 16:35:50 +00:00
|
|
|
virNetSASLSessionPtr sasl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Self-pipe to wakeup threads waiting in poll() */
|
|
|
|
int wakeupSendFD;
|
|
|
|
int wakeupReadFD;
|
|
|
|
|
2011-11-08 09:13:27 +00:00
|
|
|
/*
|
|
|
|
* List of calls currently waiting for dispatch
|
|
|
|
* The calls should all have threads waiting for
|
|
|
|
* them, except possibly the first call in the list
|
|
|
|
* which might be a partially sent non-blocking call.
|
|
|
|
*/
|
2010-12-01 16:35:50 +00:00
|
|
|
virNetClientCallPtr waitDispatch;
|
2011-11-11 15:28:41 +00:00
|
|
|
/* True if a thread holds the buck */
|
|
|
|
bool haveTheBuck;
|
2010-12-01 16:35:50 +00:00
|
|
|
|
|
|
|
size_t nstreams;
|
|
|
|
virNetClientStreamPtr *streams;
|
2011-09-22 13:47:29 +00:00
|
|
|
|
2011-09-22 12:59:06 +00:00
|
|
|
virKeepAlivePtr keepalive;
|
2011-09-22 13:47:29 +00:00
|
|
|
bool wantClose;
|
2012-07-19 10:21:54 +00:00
|
|
|
int closeReason;
|
2015-09-11 15:07:56 +00:00
|
|
|
virErrorPtr error;
|
2012-07-18 16:10:22 +00:00
|
|
|
|
|
|
|
virNetClientCloseFunc closeCb;
|
|
|
|
void *closeOpaque;
|
|
|
|
virFreeCallback closeFf;
|
2010-12-01 16:35:50 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-07-30 09:14:56 +00:00
|
|
|
static virClassPtr virNetClientClass;
|
|
|
|
static void virNetClientDispose(void *obj);
|
|
|
|
|
|
|
|
static int virNetClientOnceInit(void)
|
|
|
|
{
|
2013-01-17 17:22:13 +00:00
|
|
|
if (!(virNetClientClass = virClassNew(virClassForObjectLockable(),
|
2013-01-09 17:37:27 +00:00
|
|
|
"virNetClient",
|
2012-07-30 09:14:56 +00:00
|
|
|
sizeof(virNetClient),
|
|
|
|
virNetClientDispose)))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
VIR_ONCE_GLOBAL_INIT(virNetClient)
|
|
|
|
|
2012-06-08 12:21:00 +00:00
|
|
|
static void virNetClientIOEventLoopPassTheBuck(virNetClientPtr client,
|
|
|
|
virNetClientCallPtr thiscall);
|
2012-06-12 07:01:49 +00:00
|
|
|
static int virNetClientQueueNonBlocking(virNetClientPtr client,
|
|
|
|
virNetMessagePtr msg);
|
2012-07-19 10:21:54 +00:00
|
|
|
static void virNetClientCloseInternal(virNetClientPtr client,
|
|
|
|
int reason);
|
2012-06-08 12:21:00 +00:00
|
|
|
|
|
|
|
|
2012-07-18 16:10:22 +00:00
|
|
|
void virNetClientSetCloseCallback(virNetClientPtr client,
|
|
|
|
virNetClientCloseFunc cb,
|
|
|
|
void *opaque,
|
|
|
|
virFreeCallback ff)
|
|
|
|
{
|
2013-01-09 21:27:28 +00:00
|
|
|
virObjectLock(client);
|
2012-07-18 16:10:22 +00:00
|
|
|
client->closeCb = cb;
|
|
|
|
client->closeOpaque = opaque;
|
|
|
|
client->closeFf = ff;
|
2013-01-09 21:27:28 +00:00
|
|
|
virObjectUnlock(client);
|
2012-07-18 16:10:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-12-01 16:35:50 +00:00
|
|
|
static void virNetClientIncomingEvent(virNetSocketPtr sock,
|
|
|
|
int events,
|
|
|
|
void *opaque);
|
|
|
|
|
2011-11-11 15:26:16 +00:00
|
|
|
/* Append a call to the end of the list */
|
|
|
|
static void virNetClientCallQueue(virNetClientCallPtr *head,
|
|
|
|
virNetClientCallPtr call)
|
|
|
|
{
|
|
|
|
virNetClientCallPtr tmp = *head;
|
2014-11-13 14:29:21 +00:00
|
|
|
while (tmp && tmp->next)
|
2011-11-11 15:26:16 +00:00
|
|
|
tmp = tmp->next;
|
|
|
|
if (tmp)
|
|
|
|
tmp->next = call;
|
|
|
|
else
|
|
|
|
*head = call;
|
|
|
|
call->next = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
/* Obtain a call from the head of the list */
|
|
|
|
static virNetClientCallPtr virNetClientCallServe(virNetClientCallPtr *head)
|
|
|
|
{
|
|
|
|
virNetClientCallPtr tmp = *head;
|
|
|
|
if (tmp)
|
|
|
|
*head = tmp->next;
|
|
|
|
else
|
|
|
|
*head = NULL;
|
|
|
|
tmp->next = NULL;
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Remove a call from anywhere in the list */
|
|
|
|
static void virNetClientCallRemove(virNetClientCallPtr *head,
|
|
|
|
virNetClientCallPtr call)
|
|
|
|
{
|
|
|
|
virNetClientCallPtr tmp = *head;
|
|
|
|
virNetClientCallPtr prev = NULL;
|
|
|
|
while (tmp) {
|
|
|
|
if (tmp == call) {
|
|
|
|
if (prev)
|
|
|
|
prev->next = tmp->next;
|
|
|
|
else
|
|
|
|
*head = tmp->next;
|
|
|
|
tmp->next = NULL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
prev = tmp;
|
|
|
|
tmp = tmp->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Predicate returns true if matches */
|
|
|
|
typedef bool (*virNetClientCallPredicate)(virNetClientCallPtr call, void *opaque);
|
|
|
|
|
|
|
|
/* Remove a list of calls from the list based on a predicate */
|
|
|
|
static void virNetClientCallRemovePredicate(virNetClientCallPtr *head,
|
|
|
|
virNetClientCallPredicate pred,
|
|
|
|
void *opaque)
|
|
|
|
{
|
|
|
|
virNetClientCallPtr tmp = *head;
|
|
|
|
virNetClientCallPtr prev = NULL;
|
|
|
|
while (tmp) {
|
|
|
|
virNetClientCallPtr next = tmp->next;
|
|
|
|
tmp->next = NULL; /* Temp unlink */
|
|
|
|
if (pred(tmp, opaque)) {
|
|
|
|
if (prev)
|
|
|
|
prev->next = next;
|
|
|
|
else
|
|
|
|
*head = next;
|
|
|
|
} else {
|
|
|
|
tmp->next = next; /* Reverse temp unlink */
|
|
|
|
prev = tmp;
|
|
|
|
}
|
|
|
|
tmp = next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Returns true if the predicate matches at least one call in the list */
|
|
|
|
static bool virNetClientCallMatchPredicate(virNetClientCallPtr head,
|
|
|
|
virNetClientCallPredicate pred,
|
|
|
|
void *opaque)
|
|
|
|
{
|
|
|
|
virNetClientCallPtr tmp = head;
|
|
|
|
while (tmp) {
|
2014-11-13 14:29:21 +00:00
|
|
|
if (pred(tmp, opaque))
|
2011-11-11 15:26:16 +00:00
|
|
|
return true;
|
|
|
|
tmp = tmp->next;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-22 12:59:06 +00:00
|
|
|
bool
|
|
|
|
virNetClientKeepAliveIsSupported(virNetClientPtr client)
|
|
|
|
{
|
|
|
|
bool supported;
|
|
|
|
|
2013-01-09 21:27:28 +00:00
|
|
|
virObjectLock(client);
|
2011-09-22 12:59:06 +00:00
|
|
|
supported = !!client->keepalive;
|
2013-01-09 21:27:28 +00:00
|
|
|
virObjectUnlock(client);
|
2011-09-22 12:59:06 +00:00
|
|
|
|
|
|
|
return supported;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
virNetClientKeepAliveStart(virNetClientPtr client,
|
|
|
|
int interval,
|
|
|
|
unsigned int count)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2013-01-09 21:27:28 +00:00
|
|
|
virObjectLock(client);
|
2011-09-22 12:59:06 +00:00
|
|
|
ret = virKeepAliveStart(client->keepalive, interval, count);
|
2013-01-09 21:27:28 +00:00
|
|
|
virObjectUnlock(client);
|
2011-09-22 12:59:06 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-04-24 14:38:41 +00:00
|
|
|
void
|
|
|
|
virNetClientKeepAliveStop(virNetClientPtr client)
|
|
|
|
{
|
2013-01-09 21:27:28 +00:00
|
|
|
virObjectLock(client);
|
2012-06-12 06:58:04 +00:00
|
|
|
virKeepAliveStop(client->keepalive);
|
2013-01-09 21:27:28 +00:00
|
|
|
virObjectUnlock(client);
|
2012-04-24 14:38:41 +00:00
|
|
|
}
|
|
|
|
|
2011-09-22 12:59:06 +00:00
|
|
|
static void
|
|
|
|
virNetClientKeepAliveDeadCB(void *opaque)
|
|
|
|
{
|
2012-07-19 10:21:54 +00:00
|
|
|
virNetClientCloseInternal(opaque, VIR_CONNECT_CLOSE_REASON_KEEPALIVE);
|
2011-09-22 12:59:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
virNetClientKeepAliveSendCB(void *opaque,
|
|
|
|
virNetMessagePtr msg)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = virNetClientSendNonBlock(opaque, msg);
|
|
|
|
if (ret != -1 && ret != 1)
|
|
|
|
virNetMessageFree(msg);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-12-01 16:35:50 +00:00
|
|
|
static virNetClientPtr virNetClientNew(virNetSocketPtr sock,
|
|
|
|
const char *hostname)
|
|
|
|
{
|
2011-06-29 18:28:57 +00:00
|
|
|
virNetClientPtr client = NULL;
|
2010-12-01 16:35:50 +00:00
|
|
|
int wakeupFD[2] = { -1, -1 };
|
|
|
|
|
2012-07-30 09:14:56 +00:00
|
|
|
if (virNetClientInitialize() < 0)
|
|
|
|
return NULL;
|
|
|
|
|
2010-12-01 16:35:50 +00:00
|
|
|
if (pipe2(wakeupFD, O_CLOEXEC) < 0) {
|
|
|
|
virReportSystemError(errno, "%s",
|
|
|
|
_("unable to make pipe"));
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2013-01-09 21:27:28 +00:00
|
|
|
if (!(client = virObjectLockableNew(virNetClientClass)))
|
2012-07-30 09:14:56 +00:00
|
|
|
goto error;
|
2010-12-01 16:35:50 +00:00
|
|
|
|
|
|
|
client->sock = sock;
|
|
|
|
client->wakeupReadFD = wakeupFD[0];
|
|
|
|
client->wakeupSendFD = wakeupFD[1];
|
|
|
|
wakeupFD[0] = wakeupFD[1] = -1;
|
|
|
|
|
2013-05-03 12:47:53 +00:00
|
|
|
if (VIR_STRDUP(client->hostname, hostname) < 0)
|
|
|
|
goto error;
|
2010-12-01 16:35:50 +00:00
|
|
|
|
Rewrite all the DTrace/SystemTAP probing
The libvirtd daemon had a few crude system tap probes. Some of
these were broken during the RPC rewrite. The new modular RPC
code is structured in a way that allows much more effective
tracing. Instead of trying to hook up the original probes,
define a new set of probes for the RPC and event code.
The master probes file is now src/probes.d. This contains
probes for virNetServerClientPtr, virNetClientPtr, virSocketPtr
virNetTLSContextPtr and virNetTLSSessionPtr modules. Also add
probes for the poll event loop.
The src/dtrace2systemtap.pl script can convert the probes.d
file into a libvirt_probes.stp file to make use from systemtap
much simpler.
The src/rpc/gensystemtap.pl script can generate a set of
systemtap functions for translating RPC enum values into
printable strings. This works for all RPC header enums (program,
type, status, procedure) and also the authentication enum
The PROBE macro will automatically generate a VIR_DEBUG
statement, so any place with a PROBE can remove any existing
manual DEBUG statements.
* daemon/libvirtd.stp, daemon/probes.d: Remove obsolete probing
* daemon/libvirtd.h: Remove probe macros
* daemon/Makefile.am: Remove all probe buildings/install
* daemon/remote.c: Update authentication probes
* src/dtrace2systemtap.pl, src/rpc/gensystemtap.pl: Scripts
to generate STP files
* src/internal.h: Add probe macros
* src/probes.d: Master list of probes
* src/rpc/virnetclient.c, src/rpc/virnetserverclient.c,
src/rpc/virnetsocket.c, src/rpc/virnettlscontext.c,
src/util/event_poll.c: Insert probe points, removing any
DEBUG statements that duplicate the info
2011-09-30 13:40:23 +00:00
|
|
|
PROBE(RPC_CLIENT_NEW,
|
2012-07-30 09:14:56 +00:00
|
|
|
"client=%p sock=%p",
|
|
|
|
client, client->sock);
|
2010-12-01 16:35:50 +00:00
|
|
|
return client;
|
|
|
|
|
2014-03-25 06:52:31 +00:00
|
|
|
error:
|
2010-12-01 16:35:50 +00:00
|
|
|
VIR_FORCE_CLOSE(wakeupFD[0]);
|
|
|
|
VIR_FORCE_CLOSE(wakeupFD[1]);
|
2012-07-30 09:14:56 +00:00
|
|
|
virObjectUnref(client);
|
2010-12-01 16:35:50 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
virNetClientPtr virNetClientNewUNIX(const char *path,
|
|
|
|
bool spawnDaemon,
|
|
|
|
const char *binary)
|
|
|
|
{
|
|
|
|
virNetSocketPtr sock;
|
|
|
|
|
|
|
|
if (virNetSocketNewConnectUNIX(path, spawnDaemon, binary, &sock) < 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return virNetClientNew(sock, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
virNetClientPtr virNetClientNewTCP(const char *nodename,
|
2015-05-21 14:51:28 +00:00
|
|
|
const char *service,
|
|
|
|
int family)
|
2010-12-01 16:35:50 +00:00
|
|
|
{
|
|
|
|
virNetSocketPtr sock;
|
|
|
|
|
2015-05-21 14:51:28 +00:00
|
|
|
if (virNetSocketNewConnectTCP(nodename, service,
|
|
|
|
family,
|
|
|
|
&sock) < 0)
|
2010-12-01 16:35:50 +00:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return virNetClientNew(sock, nodename);
|
|
|
|
}
|
|
|
|
|
|
|
|
virNetClientPtr virNetClientNewSSH(const char *nodename,
|
|
|
|
const char *service,
|
|
|
|
const char *binary,
|
|
|
|
const char *username,
|
|
|
|
bool noTTY,
|
2011-07-11 19:50:31 +00:00
|
|
|
bool noVerify,
|
2010-12-01 16:35:50 +00:00
|
|
|
const char *netcat,
|
2011-07-19 17:52:21 +00:00
|
|
|
const char *keyfile,
|
2010-12-01 16:35:50 +00:00
|
|
|
const char *path)
|
|
|
|
{
|
|
|
|
virNetSocketPtr sock;
|
|
|
|
|
2011-07-19 17:52:21 +00:00
|
|
|
if (virNetSocketNewConnectSSH(nodename, service, binary, username, noTTY,
|
|
|
|
noVerify, netcat, keyfile, path, &sock) < 0)
|
2010-12-01 16:35:50 +00:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return virNetClientNew(sock, NULL);
|
|
|
|
}
|
|
|
|
|
2011-11-14 15:12:53 +00:00
|
|
|
#define DEFAULT_VALUE(VAR, VAL) \
|
|
|
|
if (!VAR) \
|
|
|
|
VAR = VAL;
|
|
|
|
virNetClientPtr virNetClientNewLibSSH2(const char *host,
|
|
|
|
const char *port,
|
2015-05-21 14:51:28 +00:00
|
|
|
int family,
|
2011-11-14 15:12:53 +00:00
|
|
|
const char *username,
|
|
|
|
const char *privkeyPath,
|
|
|
|
const char *knownHostsPath,
|
|
|
|
const char *knownHostsVerify,
|
|
|
|
const char *authMethods,
|
|
|
|
const char *netcatPath,
|
|
|
|
const char *socketPath,
|
2013-07-09 14:46:32 +00:00
|
|
|
virConnectAuthPtr authPtr,
|
|
|
|
virURIPtr uri)
|
2011-11-14 15:12:53 +00:00
|
|
|
{
|
|
|
|
virNetSocketPtr sock = NULL;
|
|
|
|
virNetClientPtr ret = NULL;
|
|
|
|
|
|
|
|
virBuffer buf = VIR_BUFFER_INITIALIZER;
|
|
|
|
char *nc = NULL;
|
|
|
|
char *command = NULL;
|
|
|
|
|
|
|
|
char *homedir = virGetUserDirectory();
|
2012-08-21 15:54:26 +00:00
|
|
|
char *confdir = virGetUserConfigDirectory();
|
2011-11-14 15:12:53 +00:00
|
|
|
char *knownhosts = NULL;
|
|
|
|
char *privkey = NULL;
|
|
|
|
|
|
|
|
/* Use default paths for known hosts an public keys if not provided */
|
2012-08-21 15:54:26 +00:00
|
|
|
if (confdir) {
|
2011-11-14 15:12:53 +00:00
|
|
|
if (!knownHostsPath) {
|
2012-08-21 15:54:26 +00:00
|
|
|
if (virFileExists(confdir)) {
|
|
|
|
virBufferAsprintf(&buf, "%s/known_hosts", confdir);
|
|
|
|
if (!(knownhosts = virBufferContentAndReset(&buf)))
|
|
|
|
goto no_memory;
|
|
|
|
}
|
2011-11-14 15:12:53 +00:00
|
|
|
} else {
|
2013-05-03 12:47:53 +00:00
|
|
|
if (VIR_STRDUP(knownhosts, knownHostsPath) < 0)
|
|
|
|
goto cleanup;
|
2011-11-14 15:12:53 +00:00
|
|
|
}
|
2012-08-21 15:54:26 +00:00
|
|
|
}
|
2011-11-14 15:12:53 +00:00
|
|
|
|
2012-08-21 15:54:26 +00:00
|
|
|
if (homedir) {
|
2011-11-14 15:12:53 +00:00
|
|
|
if (!privkeyPath) {
|
|
|
|
/* RSA */
|
|
|
|
virBufferAsprintf(&buf, "%s/.ssh/id_rsa", homedir);
|
|
|
|
if (!(privkey = virBufferContentAndReset(&buf)))
|
|
|
|
goto no_memory;
|
|
|
|
|
|
|
|
if (!(virFileExists(privkey)))
|
|
|
|
VIR_FREE(privkey);
|
|
|
|
/* DSA */
|
|
|
|
if (!privkey) {
|
|
|
|
virBufferAsprintf(&buf, "%s/.ssh/id_dsa", homedir);
|
|
|
|
if (!(privkey = virBufferContentAndReset(&buf)))
|
|
|
|
goto no_memory;
|
|
|
|
|
|
|
|
if (!(virFileExists(privkey)))
|
|
|
|
VIR_FREE(privkey);
|
|
|
|
}
|
|
|
|
} else {
|
2013-05-03 12:47:53 +00:00
|
|
|
if (VIR_STRDUP(privkey, privkeyPath) < 0)
|
|
|
|
goto cleanup;
|
2011-11-14 15:12:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!authMethods) {
|
|
|
|
if (privkey)
|
2013-07-09 14:46:32 +00:00
|
|
|
authMethods = "agent,privkey,password,keyboard-interactive";
|
2011-11-14 15:12:53 +00:00
|
|
|
else
|
2013-07-09 14:46:32 +00:00
|
|
|
authMethods = "agent,password,keyboard-interactive";
|
2011-11-14 15:12:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DEFAULT_VALUE(host, "localhost");
|
|
|
|
DEFAULT_VALUE(port, "22");
|
|
|
|
DEFAULT_VALUE(username, "root");
|
|
|
|
DEFAULT_VALUE(netcatPath, "nc");
|
|
|
|
DEFAULT_VALUE(knownHostsVerify, "normal");
|
|
|
|
|
|
|
|
virBufferEscapeShell(&buf, netcatPath);
|
|
|
|
if (!(nc = virBufferContentAndReset(&buf)))
|
|
|
|
goto no_memory;
|
|
|
|
|
|
|
|
virBufferAsprintf(&buf,
|
|
|
|
"sh -c "
|
|
|
|
"'if '%s' -q 2>&1 | grep \"requires an argument\" >/dev/null 2>&1; then "
|
|
|
|
"ARG=-q0;"
|
|
|
|
"else "
|
|
|
|
"ARG=;"
|
|
|
|
"fi;"
|
|
|
|
"'%s' $ARG -U %s'",
|
|
|
|
nc, nc, socketPath);
|
|
|
|
|
|
|
|
if (!(command = virBufferContentAndReset(&buf)))
|
|
|
|
goto no_memory;
|
|
|
|
|
2015-05-21 14:51:28 +00:00
|
|
|
if (virNetSocketNewConnectLibSSH2(host, port,
|
|
|
|
family,
|
|
|
|
username, privkey,
|
2011-11-14 15:12:53 +00:00
|
|
|
knownhosts, knownHostsVerify, authMethods,
|
2013-07-09 14:46:32 +00:00
|
|
|
command, authPtr, uri, &sock) != 0)
|
2011-11-14 15:12:53 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
if (!(ret = virNetClientNew(sock, NULL)))
|
|
|
|
goto cleanup;
|
|
|
|
sock = NULL;
|
|
|
|
|
2014-03-25 06:52:31 +00:00
|
|
|
cleanup:
|
2011-11-14 15:12:53 +00:00
|
|
|
VIR_FREE(command);
|
|
|
|
VIR_FREE(privkey);
|
|
|
|
VIR_FREE(knownhosts);
|
|
|
|
VIR_FREE(homedir);
|
2012-08-21 15:54:26 +00:00
|
|
|
VIR_FREE(confdir);
|
2011-11-14 15:12:53 +00:00
|
|
|
VIR_FREE(nc);
|
|
|
|
virObjectUnref(sock);
|
|
|
|
return ret;
|
|
|
|
|
2014-03-25 06:52:31 +00:00
|
|
|
no_memory:
|
2011-11-14 15:12:53 +00:00
|
|
|
virReportOOMError();
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
#undef DEFAULT_VALUE
|
|
|
|
|
2010-12-01 16:35:50 +00:00
|
|
|
virNetClientPtr virNetClientNewExternal(const char **cmdargv)
|
|
|
|
{
|
|
|
|
virNetSocketPtr sock;
|
|
|
|
|
|
|
|
if (virNetSocketNewConnectExternal(cmdargv, &sock) < 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return virNetClientNew(sock, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-08-07 11:09:51 +00:00
|
|
|
int virNetClientRegisterAsyncIO(virNetClientPtr client)
|
|
|
|
{
|
|
|
|
if (client->asyncIO)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Set up a callback to listen on the socket data */
|
|
|
|
virObjectRef(client);
|
|
|
|
if (virNetSocketAddIOCallback(client->sock,
|
|
|
|
VIR_EVENT_HANDLE_READABLE,
|
|
|
|
virNetClientIncomingEvent,
|
|
|
|
client,
|
|
|
|
virObjectFreeCallback) < 0) {
|
|
|
|
virObjectUnref(client);
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("Unable to register async IO callback"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
client->asyncIO = true;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int virNetClientRegisterKeepAlive(virNetClientPtr client)
|
|
|
|
{
|
|
|
|
virKeepAlivePtr ka;
|
|
|
|
|
|
|
|
if (client->keepalive)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (!client->asyncIO) {
|
|
|
|
virReportError(VIR_ERR_OPERATION_INVALID, "%s",
|
|
|
|
_("Unable to enable keepalives without async IO support"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Keepalive protocol consists of async messages so it can only be used
|
|
|
|
* if the client supports them */
|
|
|
|
if (!(ka = virKeepAliveNew(-1, 0, client,
|
|
|
|
virNetClientKeepAliveSendCB,
|
|
|
|
virNetClientKeepAliveDeadCB,
|
|
|
|
virObjectFreeCallback)))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* keepalive object has a reference to client */
|
|
|
|
virObjectRef(client);
|
|
|
|
|
|
|
|
client->keepalive = ka;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-07-07 14:17:21 +00:00
|
|
|
int virNetClientGetFD(virNetClientPtr client)
|
|
|
|
{
|
|
|
|
int fd;
|
2013-01-09 21:27:28 +00:00
|
|
|
virObjectLock(client);
|
2011-07-07 14:17:21 +00:00
|
|
|
fd = virNetSocketGetFD(client->sock);
|
2013-01-09 21:27:28 +00:00
|
|
|
virObjectUnlock(client);
|
2011-07-07 14:17:21 +00:00
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int virNetClientDupFD(virNetClientPtr client, bool cloexec)
|
|
|
|
{
|
|
|
|
int fd;
|
2013-01-09 21:27:28 +00:00
|
|
|
virObjectLock(client);
|
2011-07-07 14:17:21 +00:00
|
|
|
fd = virNetSocketDupFD(client->sock, cloexec);
|
2013-01-09 21:27:28 +00:00
|
|
|
virObjectUnlock(client);
|
2011-07-07 14:17:21 +00:00
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-10-21 10:48:03 +00:00
|
|
|
bool virNetClientHasPassFD(virNetClientPtr client)
|
|
|
|
{
|
|
|
|
bool hasPassFD;
|
2013-01-09 21:27:28 +00:00
|
|
|
virObjectLock(client);
|
2011-10-21 10:48:03 +00:00
|
|
|
hasPassFD = virNetSocketHasPassFD(client->sock);
|
2013-01-09 21:27:28 +00:00
|
|
|
virObjectUnlock(client);
|
2011-10-21 10:48:03 +00:00
|
|
|
return hasPassFD;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-30 09:14:56 +00:00
|
|
|
void virNetClientDispose(void *obj)
|
2010-12-01 16:35:50 +00:00
|
|
|
{
|
2012-07-30 09:14:56 +00:00
|
|
|
virNetClientPtr client = obj;
|
Convert 'int i' to 'size_t i' in src/rpc/ files
Convert the type of loop iterators named 'i', 'j', k',
'ii', 'jj', 'kk', to be 'size_t' instead of 'int' or
'unsigned int', also santizing 'ii', 'jj', 'kk' to use
the normal 'i', 'j', 'k' naming
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2013-07-08 14:09:33 +00:00
|
|
|
size_t i;
|
2010-12-01 16:35:50 +00:00
|
|
|
|
2013-03-13 19:17:32 +00:00
|
|
|
PROBE(RPC_CLIENT_DISPOSE,
|
|
|
|
"client=%p", client);
|
|
|
|
|
2012-07-18 16:10:22 +00:00
|
|
|
if (client->closeFf)
|
|
|
|
client->closeFf(client->closeOpaque);
|
|
|
|
|
2013-05-21 07:59:54 +00:00
|
|
|
for (i = 0; i < client->nprograms; i++)
|
2012-07-30 09:14:56 +00:00
|
|
|
virObjectUnref(client->programs[i]);
|
2010-12-01 16:35:50 +00:00
|
|
|
VIR_FREE(client->programs);
|
|
|
|
|
|
|
|
VIR_FORCE_CLOSE(client->wakeupSendFD);
|
|
|
|
VIR_FORCE_CLOSE(client->wakeupReadFD);
|
|
|
|
|
|
|
|
VIR_FREE(client->hostname);
|
|
|
|
|
2011-07-19 13:13:32 +00:00
|
|
|
if (client->sock)
|
|
|
|
virNetSocketRemoveIOCallback(client->sock);
|
2012-07-11 13:35:51 +00:00
|
|
|
virObjectUnref(client->sock);
|
2013-01-08 21:02:05 +00:00
|
|
|
#if WITH_GNUTLS
|
2012-07-11 13:35:48 +00:00
|
|
|
virObjectUnref(client->tls);
|
2013-01-07 14:54:18 +00:00
|
|
|
#endif
|
2012-09-20 11:58:29 +00:00
|
|
|
#if WITH_SASL
|
2012-07-11 13:35:49 +00:00
|
|
|
virObjectUnref(client->sasl);
|
2010-12-01 16:35:50 +00:00
|
|
|
#endif
|
2012-08-03 14:50:16 +00:00
|
|
|
|
|
|
|
virNetMessageClear(&client->msg);
|
2010-12-01 16:35:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-19 10:21:54 +00:00
|
|
|
static void
|
|
|
|
virNetClientMarkClose(virNetClientPtr client,
|
|
|
|
int reason)
|
|
|
|
{
|
|
|
|
VIR_DEBUG("client=%p, reason=%d", client, reason);
|
2015-09-11 15:07:56 +00:00
|
|
|
|
2012-08-27 08:59:25 +00:00
|
|
|
if (client->sock)
|
|
|
|
virNetSocketRemoveIOCallback(client->sock);
|
2015-09-11 15:07:56 +00:00
|
|
|
|
2014-11-30 19:09:08 +00:00
|
|
|
/* Don't override reason that's already set. */
|
|
|
|
if (!client->wantClose) {
|
2015-09-11 15:07:56 +00:00
|
|
|
if (!client->error)
|
|
|
|
client->error = virSaveLastError();
|
2014-11-30 19:09:08 +00:00
|
|
|
client->wantClose = true;
|
|
|
|
client->closeReason = reason;
|
|
|
|
}
|
2012-07-19 10:21:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-22 13:47:29 +00:00
|
|
|
static void
|
|
|
|
virNetClientCloseLocked(virNetClientPtr client)
|
2011-07-19 13:13:32 +00:00
|
|
|
{
|
2011-09-22 12:59:06 +00:00
|
|
|
virKeepAlivePtr ka;
|
|
|
|
|
2012-07-19 10:21:54 +00:00
|
|
|
VIR_DEBUG("client=%p, sock=%p, reason=%d", client, client->sock, client->closeReason);
|
2011-09-22 13:47:29 +00:00
|
|
|
|
|
|
|
if (!client->sock)
|
2011-07-20 14:20:18 +00:00
|
|
|
return;
|
|
|
|
|
2012-07-11 13:35:51 +00:00
|
|
|
virObjectUnref(client->sock);
|
2011-07-19 13:13:32 +00:00
|
|
|
client->sock = NULL;
|
2013-01-08 21:02:05 +00:00
|
|
|
#if WITH_GNUTLS
|
2012-07-11 13:35:48 +00:00
|
|
|
virObjectUnref(client->tls);
|
2011-07-19 13:13:32 +00:00
|
|
|
client->tls = NULL;
|
2013-01-07 14:54:18 +00:00
|
|
|
#endif
|
2012-09-20 11:58:29 +00:00
|
|
|
#if WITH_SASL
|
2012-07-11 13:35:49 +00:00
|
|
|
virObjectUnref(client->sasl);
|
2011-07-19 13:13:32 +00:00
|
|
|
client->sasl = NULL;
|
|
|
|
#endif
|
2011-09-22 12:59:06 +00:00
|
|
|
ka = client->keepalive;
|
|
|
|
client->keepalive = NULL;
|
2011-09-22 13:47:29 +00:00
|
|
|
client->wantClose = false;
|
2011-09-22 12:59:06 +00:00
|
|
|
|
2015-09-11 15:07:56 +00:00
|
|
|
virFreeError(client->error);
|
|
|
|
client->error = NULL;
|
|
|
|
|
2012-07-18 16:10:22 +00:00
|
|
|
if (ka || client->closeCb) {
|
|
|
|
virNetClientCloseFunc closeCb = client->closeCb;
|
|
|
|
void *closeOpaque = client->closeOpaque;
|
|
|
|
int closeReason = client->closeReason;
|
2012-07-30 09:14:56 +00:00
|
|
|
virObjectRef(client);
|
2013-01-09 21:27:28 +00:00
|
|
|
virObjectUnlock(client);
|
2011-09-22 12:59:06 +00:00
|
|
|
|
2012-07-18 16:10:22 +00:00
|
|
|
if (ka) {
|
|
|
|
virKeepAliveStop(ka);
|
2012-07-11 13:35:50 +00:00
|
|
|
virObjectUnref(ka);
|
2012-07-18 16:10:22 +00:00
|
|
|
}
|
|
|
|
if (closeCb)
|
|
|
|
closeCb(client, closeReason, closeOpaque);
|
2011-09-22 12:59:06 +00:00
|
|
|
|
2013-01-09 21:27:28 +00:00
|
|
|
virObjectLock(client);
|
2012-07-30 09:14:56 +00:00
|
|
|
virObjectUnref(client);
|
2011-09-22 12:59:06 +00:00
|
|
|
}
|
2011-09-22 13:47:29 +00:00
|
|
|
}
|
|
|
|
|
2012-07-19 10:21:54 +00:00
|
|
|
static void virNetClientCloseInternal(virNetClientPtr client,
|
|
|
|
int reason)
|
2011-09-22 13:47:29 +00:00
|
|
|
{
|
2012-07-18 16:10:22 +00:00
|
|
|
VIR_DEBUG("client=%p wantclose=%d", client, client ? client->wantClose : false);
|
2012-03-02 18:57:27 +00:00
|
|
|
|
2011-09-22 13:47:29 +00:00
|
|
|
if (!client)
|
|
|
|
return;
|
|
|
|
|
2012-07-19 10:21:54 +00:00
|
|
|
if (!client->sock ||
|
|
|
|
client->wantClose)
|
|
|
|
return;
|
|
|
|
|
2013-01-09 21:27:28 +00:00
|
|
|
virObjectLock(client);
|
2011-09-22 13:47:29 +00:00
|
|
|
|
2012-07-19 10:21:54 +00:00
|
|
|
virNetClientMarkClose(client, reason);
|
2012-06-08 12:21:00 +00:00
|
|
|
|
|
|
|
/* If there is a thread polling for data on the socket, wake the thread up
|
|
|
|
* otherwise try to pass the buck to a possibly waiting thread. If no
|
|
|
|
* thread is waiting, virNetClientIOEventLoopPassTheBuck will clean the
|
|
|
|
* queue and close the client because we set client->wantClose.
|
2011-09-22 13:47:29 +00:00
|
|
|
*/
|
2012-06-08 12:21:00 +00:00
|
|
|
if (client->haveTheBuck) {
|
2011-09-22 13:47:29 +00:00
|
|
|
char ignore = 1;
|
|
|
|
size_t len = sizeof(ignore);
|
|
|
|
|
|
|
|
if (safewrite(client->wakeupSendFD, &ignore, len) != len)
|
|
|
|
VIR_ERROR(_("failed to wake up polling thread"));
|
|
|
|
} else {
|
2012-06-08 12:21:00 +00:00
|
|
|
virNetClientIOEventLoopPassTheBuck(client, NULL);
|
2011-09-22 13:47:29 +00:00
|
|
|
}
|
|
|
|
|
2013-01-09 21:27:28 +00:00
|
|
|
virObjectUnlock(client);
|
2011-07-19 13:13:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-19 10:21:54 +00:00
|
|
|
void virNetClientClose(virNetClientPtr client)
|
|
|
|
{
|
|
|
|
virNetClientCloseInternal(client, VIR_CONNECT_CLOSE_REASON_CLIENT);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-09-20 11:58:29 +00:00
|
|
|
#if WITH_SASL
|
2010-12-01 16:35:50 +00:00
|
|
|
void virNetClientSetSASLSession(virNetClientPtr client,
|
|
|
|
virNetSASLSessionPtr sasl)
|
|
|
|
{
|
2013-01-09 21:27:28 +00:00
|
|
|
virObjectLock(client);
|
2012-07-11 13:35:49 +00:00
|
|
|
client->sasl = virObjectRef(sasl);
|
2010-12-01 16:35:50 +00:00
|
|
|
virNetSocketSetSASLSession(client->sock, client->sasl);
|
2013-01-09 21:27:28 +00:00
|
|
|
virObjectUnlock(client);
|
2010-12-01 16:35:50 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2013-01-08 21:02:05 +00:00
|
|
|
#if WITH_GNUTLS
|
2010-12-01 16:35:50 +00:00
|
|
|
int virNetClientSetTLSSession(virNetClientPtr client,
|
|
|
|
virNetTLSContextPtr tls)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
char buf[1];
|
|
|
|
int len;
|
|
|
|
struct pollfd fds[1];
|
|
|
|
sigset_t oldmask, blockedsigs;
|
|
|
|
|
2012-10-17 09:23:12 +00:00
|
|
|
sigemptyset(&blockedsigs);
|
2013-01-07 14:54:18 +00:00
|
|
|
# ifdef SIGWINCH
|
2012-10-17 09:23:12 +00:00
|
|
|
sigaddset(&blockedsigs, SIGWINCH);
|
2013-01-07 14:54:18 +00:00
|
|
|
# endif
|
|
|
|
# ifdef SIGCHLD
|
2012-10-17 09:23:12 +00:00
|
|
|
sigaddset(&blockedsigs, SIGCHLD);
|
2013-01-07 14:54:18 +00:00
|
|
|
# endif
|
2012-10-17 09:23:12 +00:00
|
|
|
sigaddset(&blockedsigs, SIGPIPE);
|
2010-12-01 16:35:50 +00:00
|
|
|
|
2013-01-09 21:27:28 +00:00
|
|
|
virObjectLock(client);
|
2010-12-01 16:35:50 +00:00
|
|
|
|
|
|
|
if (!(client->tls = virNetTLSSessionNew(tls,
|
|
|
|
client->hostname)))
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
virNetSocketSetTLSSession(client->sock, client->tls);
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
ret = virNetTLSSessionHandshake(client->tls);
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
goto error;
|
|
|
|
if (ret == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
fds[0].fd = virNetSocketGetFD(client->sock);
|
|
|
|
fds[0].revents = 0;
|
|
|
|
if (virNetTLSSessionGetHandshakeStatus(client->tls) ==
|
|
|
|
VIR_NET_TLS_HANDSHAKE_RECVING)
|
|
|
|
fds[0].events = POLLIN;
|
|
|
|
else
|
|
|
|
fds[0].events = POLLOUT;
|
|
|
|
|
|
|
|
/* Block SIGWINCH from interrupting poll in curses programs,
|
|
|
|
* then restore the original signal mask again immediately
|
|
|
|
* after the call (RHBZ#567931). Same for SIGCHLD and SIGPIPE
|
|
|
|
* at the suggestion of Paolo Bonzini and Daniel Berrange.
|
|
|
|
*/
|
|
|
|
ignore_value(pthread_sigmask(SIG_BLOCK, &blockedsigs, &oldmask));
|
|
|
|
|
|
|
|
repoll:
|
|
|
|
ret = poll(fds, ARRAY_CARDINALITY(fds), -1);
|
2012-08-08 20:59:41 +00:00
|
|
|
if (ret < 0 && (errno == EAGAIN || errno == EINTR))
|
2010-12-01 16:35:50 +00:00
|
|
|
goto repoll;
|
|
|
|
|
2014-03-19 17:10:34 +00:00
|
|
|
ignore_value(pthread_sigmask(SIG_SETMASK, &oldmask, NULL));
|
2010-12-01 16:35:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = virNetTLSContextCheckCertificate(tls, client->tls);
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
/* At this point, the server is verifying _our_ certificate, IP address,
|
|
|
|
* etc. If we make the grade, it will send us a '\1' byte.
|
|
|
|
*/
|
|
|
|
|
|
|
|
fds[0].fd = virNetSocketGetFD(client->sock);
|
|
|
|
fds[0].revents = 0;
|
|
|
|
fds[0].events = POLLIN;
|
|
|
|
|
|
|
|
/* Block SIGWINCH from interrupting poll in curses programs */
|
|
|
|
ignore_value(pthread_sigmask(SIG_BLOCK, &blockedsigs, &oldmask));
|
|
|
|
|
|
|
|
repoll2:
|
|
|
|
ret = poll(fds, ARRAY_CARDINALITY(fds), -1);
|
2012-08-08 20:59:41 +00:00
|
|
|
if (ret < 0 && (errno == EAGAIN || errno == EINTR))
|
2010-12-01 16:35:50 +00:00
|
|
|
goto repoll2;
|
|
|
|
|
2014-03-19 17:10:34 +00:00
|
|
|
ignore_value(pthread_sigmask(SIG_SETMASK, &oldmask, NULL));
|
2010-12-01 16:35:50 +00:00
|
|
|
|
|
|
|
len = virNetTLSSessionRead(client->tls, buf, 1);
|
2011-07-15 10:40:35 +00:00
|
|
|
if (len < 0 && errno != ENOMSG) {
|
2010-12-01 16:35:50 +00:00
|
|
|
virReportSystemError(errno, "%s",
|
|
|
|
_("Unable to read TLS confirmation"));
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
if (len != 1 || buf[0] != '\1') {
|
2012-07-18 10:41:47 +00:00
|
|
|
virReportError(VIR_ERR_RPC, "%s",
|
|
|
|
_("server verification (of our certificate or IP "
|
|
|
|
"address) failed"));
|
2010-12-01 16:35:50 +00:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2013-01-09 21:27:28 +00:00
|
|
|
virObjectUnlock(client);
|
2010-12-01 16:35:50 +00:00
|
|
|
return 0;
|
|
|
|
|
2014-03-25 06:52:31 +00:00
|
|
|
error:
|
2012-07-11 13:35:48 +00:00
|
|
|
virObjectUnref(client->tls);
|
2010-12-01 16:35:50 +00:00
|
|
|
client->tls = NULL;
|
2013-01-09 21:27:28 +00:00
|
|
|
virObjectUnlock(client);
|
2010-12-01 16:35:50 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2013-01-07 14:54:18 +00:00
|
|
|
#endif
|
2010-12-01 16:35:50 +00:00
|
|
|
|
|
|
|
bool virNetClientIsEncrypted(virNetClientPtr client)
|
|
|
|
{
|
|
|
|
bool ret = false;
|
2013-01-09 21:27:28 +00:00
|
|
|
virObjectLock(client);
|
2013-01-08 21:02:05 +00:00
|
|
|
#if WITH_GNUTLS
|
2010-12-01 16:35:50 +00:00
|
|
|
if (client->tls)
|
|
|
|
ret = true;
|
2013-01-07 14:54:18 +00:00
|
|
|
#endif
|
2012-09-20 11:58:29 +00:00
|
|
|
#if WITH_SASL
|
2010-12-01 16:35:50 +00:00
|
|
|
if (client->sasl)
|
|
|
|
ret = true;
|
|
|
|
#endif
|
2013-01-09 21:27:28 +00:00
|
|
|
virObjectUnlock(client);
|
2010-12-01 16:35:50 +00:00
|
|
|
return ret;
|
2011-09-23 06:56:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool virNetClientIsOpen(virNetClientPtr client)
|
|
|
|
{
|
|
|
|
bool ret;
|
|
|
|
|
|
|
|
if (!client)
|
|
|
|
return false;
|
|
|
|
|
2013-01-09 21:27:28 +00:00
|
|
|
virObjectLock(client);
|
2011-09-23 06:56:13 +00:00
|
|
|
ret = client->sock && !client->wantClose;
|
2013-01-09 21:27:28 +00:00
|
|
|
virObjectUnlock(client);
|
2011-09-23 06:56:13 +00:00
|
|
|
return ret;
|
2010-12-01 16:35:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int virNetClientAddProgram(virNetClientPtr client,
|
|
|
|
virNetClientProgramPtr prog)
|
|
|
|
{
|
2013-01-09 21:27:28 +00:00
|
|
|
virObjectLock(client);
|
2010-12-01 16:35:50 +00:00
|
|
|
|
|
|
|
if (VIR_EXPAND_N(client->programs, client->nprograms, 1) < 0)
|
2013-07-04 10:15:05 +00:00
|
|
|
goto error;
|
2010-12-01 16:35:50 +00:00
|
|
|
|
2012-07-30 09:14:56 +00:00
|
|
|
client->programs[client->nprograms-1] = virObjectRef(prog);
|
2010-12-01 16:35:50 +00:00
|
|
|
|
2013-01-09 21:27:28 +00:00
|
|
|
virObjectUnlock(client);
|
2010-12-01 16:35:50 +00:00
|
|
|
return 0;
|
|
|
|
|
2014-03-25 06:52:31 +00:00
|
|
|
error:
|
2013-01-09 21:27:28 +00:00
|
|
|
virObjectUnlock(client);
|
2010-12-01 16:35:50 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int virNetClientAddStream(virNetClientPtr client,
|
|
|
|
virNetClientStreamPtr st)
|
|
|
|
{
|
2013-01-09 21:27:28 +00:00
|
|
|
virObjectLock(client);
|
2010-12-01 16:35:50 +00:00
|
|
|
|
|
|
|
if (VIR_EXPAND_N(client->streams, client->nstreams, 1) < 0)
|
2013-07-04 10:15:05 +00:00
|
|
|
goto error;
|
2010-12-01 16:35:50 +00:00
|
|
|
|
2012-07-30 09:14:56 +00:00
|
|
|
client->streams[client->nstreams-1] = virObjectRef(st);
|
2010-12-01 16:35:50 +00:00
|
|
|
|
2013-01-09 21:27:28 +00:00
|
|
|
virObjectUnlock(client);
|
2010-12-01 16:35:50 +00:00
|
|
|
return 0;
|
|
|
|
|
2014-03-25 06:52:31 +00:00
|
|
|
error:
|
2013-01-09 21:27:28 +00:00
|
|
|
virObjectUnlock(client);
|
2010-12-01 16:35:50 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void virNetClientRemoveStream(virNetClientPtr client,
|
|
|
|
virNetClientStreamPtr st)
|
|
|
|
{
|
2013-01-09 21:27:28 +00:00
|
|
|
virObjectLock(client);
|
2010-12-01 16:35:50 +00:00
|
|
|
size_t i;
|
2013-05-21 07:59:54 +00:00
|
|
|
for (i = 0; i < client->nstreams; i++) {
|
2010-12-01 16:35:50 +00:00
|
|
|
if (client->streams[i] == st)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (i == client->nstreams)
|
|
|
|
goto cleanup;
|
|
|
|
|
2014-03-07 08:33:31 +00:00
|
|
|
VIR_DELETE_ELEMENT(client->streams, i, client->nstreams);
|
2012-07-30 09:14:56 +00:00
|
|
|
virObjectUnref(st);
|
2010-12-01 16:35:50 +00:00
|
|
|
|
2014-03-25 06:52:31 +00:00
|
|
|
cleanup:
|
2013-01-09 21:27:28 +00:00
|
|
|
virObjectUnlock(client);
|
2010-12-01 16:35:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-06-23 20:31:20 +00:00
|
|
|
const char *virNetClientLocalAddrStringSASL(virNetClientPtr client)
|
2010-12-01 16:35:50 +00:00
|
|
|
{
|
2016-06-20 14:01:50 +00:00
|
|
|
return virNetSocketLocalAddrStringSASL(client->sock);
|
2010-12-01 16:35:50 +00:00
|
|
|
}
|
|
|
|
|
2016-06-23 20:31:20 +00:00
|
|
|
const char *virNetClientRemoteAddrStringSASL(virNetClientPtr client)
|
2010-12-01 16:35:50 +00:00
|
|
|
{
|
2016-06-20 14:01:50 +00:00
|
|
|
return virNetSocketRemoteAddrStringSASL(client->sock);
|
2010-12-01 16:35:50 +00:00
|
|
|
}
|
|
|
|
|
2013-01-08 21:02:05 +00:00
|
|
|
#if WITH_GNUTLS
|
2010-12-01 16:35:50 +00:00
|
|
|
int virNetClientGetTLSKeySize(virNetClientPtr client)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
2013-01-09 21:27:28 +00:00
|
|
|
virObjectLock(client);
|
2010-12-01 16:35:50 +00:00
|
|
|
if (client->tls)
|
|
|
|
ret = virNetTLSSessionGetKeySize(client->tls);
|
2013-01-09 21:27:28 +00:00
|
|
|
virObjectUnlock(client);
|
2010-12-01 16:35:50 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2013-01-07 14:54:18 +00:00
|
|
|
#endif
|
2010-12-01 16:35:50 +00:00
|
|
|
|
|
|
|
static int
|
|
|
|
virNetClientCallDispatchReply(virNetClientPtr client)
|
|
|
|
{
|
|
|
|
virNetClientCallPtr thecall;
|
|
|
|
|
|
|
|
/* Ok, definitely got an RPC reply now find
|
2011-11-08 09:13:27 +00:00
|
|
|
out which waiting call is associated with it */
|
2010-12-01 16:35:50 +00:00
|
|
|
thecall = client->waitDispatch;
|
|
|
|
while (thecall &&
|
|
|
|
!(thecall->msg->header.prog == client->msg.header.prog &&
|
|
|
|
thecall->msg->header.vers == client->msg.header.vers &&
|
|
|
|
thecall->msg->header.serial == client->msg.header.serial))
|
|
|
|
thecall = thecall->next;
|
|
|
|
|
|
|
|
if (!thecall) {
|
2012-07-18 10:41:47 +00:00
|
|
|
virReportError(VIR_ERR_RPC,
|
|
|
|
_("no call waiting for reply with prog %d vers %d serial %d"),
|
|
|
|
client->msg.header.prog, client->msg.header.vers, client->msg.header.serial);
|
2010-12-01 16:35:50 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-07-04 10:15:05 +00:00
|
|
|
if (VIR_REALLOC_N(thecall->msg->buffer, client->msg.bufferLength) < 0)
|
2012-04-26 15:21:24 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
memcpy(thecall->msg->buffer, client->msg.buffer, client->msg.bufferLength);
|
2010-12-01 16:35:50 +00:00
|
|
|
memcpy(&thecall->msg->header, &client->msg.header, sizeof(client->msg.header));
|
|
|
|
thecall->msg->bufferLength = client->msg.bufferLength;
|
|
|
|
thecall->msg->bufferOffset = client->msg.bufferOffset;
|
|
|
|
|
2012-12-21 16:49:12 +00:00
|
|
|
thecall->msg->nfds = client->msg.nfds;
|
|
|
|
thecall->msg->fds = client->msg.fds;
|
|
|
|
client->msg.nfds = 0;
|
|
|
|
client->msg.fds = NULL;
|
|
|
|
|
2010-12-01 16:35:50 +00:00
|
|
|
thecall->mode = VIR_NET_CLIENT_MODE_COMPLETE;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int virNetClientCallDispatchMessage(virNetClientPtr client)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
virNetClientProgramPtr prog = NULL;
|
|
|
|
|
2013-05-21 07:59:54 +00:00
|
|
|
for (i = 0; i < client->nprograms; i++) {
|
2010-12-01 16:35:50 +00:00
|
|
|
if (virNetClientProgramMatches(client->programs[i],
|
|
|
|
&client->msg)) {
|
|
|
|
prog = client->programs[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!prog) {
|
|
|
|
VIR_DEBUG("No program found for event with prog=%d vers=%d",
|
|
|
|
client->msg.header.prog, client->msg.header.vers);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
virNetClientProgramDispatch(prog, client, &client->msg);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int virNetClientCallDispatchStream(virNetClientPtr client)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
virNetClientStreamPtr st = NULL;
|
|
|
|
virNetClientCallPtr thecall;
|
|
|
|
|
|
|
|
/* First identify what stream this packet is directed at */
|
2013-05-21 07:59:54 +00:00
|
|
|
for (i = 0; i < client->nstreams; i++) {
|
2010-12-01 16:35:50 +00:00
|
|
|
if (virNetClientStreamMatches(client->streams[i],
|
|
|
|
&client->msg)) {
|
|
|
|
st = client->streams[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!st) {
|
|
|
|
VIR_DEBUG("No stream found for packet with prog=%d vers=%d serial=%u proc=%u",
|
|
|
|
client->msg.header.prog, client->msg.header.vers,
|
|
|
|
client->msg.header.serial, client->msg.header.proc);
|
2011-11-09 13:54:01 +00:00
|
|
|
/* Don't return -1, because we expect to see further stream packets
|
|
|
|
* after we've shut it down sometimes */
|
|
|
|
return 0;
|
2010-12-01 16:35:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Finish/Abort are synchronous, so also see if there's an
|
|
|
|
* (optional) call waiting for this stream packet */
|
|
|
|
thecall = client->waitDispatch;
|
|
|
|
while (thecall &&
|
|
|
|
!(thecall->msg->header.prog == client->msg.header.prog &&
|
|
|
|
thecall->msg->header.vers == client->msg.header.vers &&
|
|
|
|
thecall->msg->header.serial == client->msg.header.serial))
|
|
|
|
thecall = thecall->next;
|
|
|
|
|
|
|
|
VIR_DEBUG("Found call %p", thecall);
|
|
|
|
|
|
|
|
/* Status is either
|
2016-04-04 14:44:27 +00:00
|
|
|
* - VIR_NET_OK - no payload for streams
|
|
|
|
* - VIR_NET_ERROR - followed by a remote_error struct
|
|
|
|
* - VIR_NET_CONTINUE - followed by a raw data packet
|
2010-12-01 16:35:50 +00:00
|
|
|
*/
|
|
|
|
switch (client->msg.header.status) {
|
|
|
|
case VIR_NET_CONTINUE: {
|
|
|
|
if (virNetClientStreamQueuePacket(st, &client->msg) < 0)
|
|
|
|
return -1;
|
2011-09-21 13:51:33 +00:00
|
|
|
|
|
|
|
if (thecall && thecall->expectReply) {
|
|
|
|
if (thecall->msg->header.status == VIR_NET_CONTINUE) {
|
|
|
|
VIR_DEBUG("Got a synchronous confirm");
|
|
|
|
thecall->mode = VIR_NET_CLIENT_MODE_COMPLETE;
|
|
|
|
} else {
|
|
|
|
VIR_DEBUG("Not completing call with status %d", thecall->msg->header.status);
|
|
|
|
}
|
|
|
|
}
|
2010-12-01 16:35:50 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
case VIR_NET_OK:
|
|
|
|
if (thecall && thecall->expectReply) {
|
|
|
|
VIR_DEBUG("Got a synchronous confirm");
|
|
|
|
thecall->mode = VIR_NET_CLIENT_MODE_COMPLETE;
|
|
|
|
} else {
|
|
|
|
VIR_DEBUG("Got unexpected async stream finish confirmation");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case VIR_NET_ERROR:
|
|
|
|
/* No call, so queue the error against the stream */
|
|
|
|
if (virNetClientStreamSetError(st, &client->msg) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (thecall && thecall->expectReply) {
|
|
|
|
VIR_DEBUG("Got a synchronous error");
|
|
|
|
/* Raise error now, so that this call will see it immediately */
|
2011-07-04 02:26:05 +00:00
|
|
|
if (!virNetClientStreamRaiseError(st))
|
|
|
|
VIR_DEBUG("unable to raise synchronous error");
|
2010-12-01 16:35:50 +00:00
|
|
|
thecall->mode = VIR_NET_CLIENT_MODE_COMPLETE;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
default:
|
|
|
|
VIR_WARN("Stream with unexpected serial=%d, proc=%d, status=%d",
|
|
|
|
client->msg.header.serial, client->msg.header.proc,
|
|
|
|
client->msg.header.status);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
virNetClientCallDispatch(virNetClientPtr client)
|
|
|
|
{
|
2012-06-12 07:01:49 +00:00
|
|
|
virNetMessagePtr response = NULL;
|
|
|
|
|
Rewrite all the DTrace/SystemTAP probing
The libvirtd daemon had a few crude system tap probes. Some of
these were broken during the RPC rewrite. The new modular RPC
code is structured in a way that allows much more effective
tracing. Instead of trying to hook up the original probes,
define a new set of probes for the RPC and event code.
The master probes file is now src/probes.d. This contains
probes for virNetServerClientPtr, virNetClientPtr, virSocketPtr
virNetTLSContextPtr and virNetTLSSessionPtr modules. Also add
probes for the poll event loop.
The src/dtrace2systemtap.pl script can convert the probes.d
file into a libvirt_probes.stp file to make use from systemtap
much simpler.
The src/rpc/gensystemtap.pl script can generate a set of
systemtap functions for translating RPC enum values into
printable strings. This works for all RPC header enums (program,
type, status, procedure) and also the authentication enum
The PROBE macro will automatically generate a VIR_DEBUG
statement, so any place with a PROBE can remove any existing
manual DEBUG statements.
* daemon/libvirtd.stp, daemon/probes.d: Remove obsolete probing
* daemon/libvirtd.h: Remove probe macros
* daemon/Makefile.am: Remove all probe buildings/install
* daemon/remote.c: Update authentication probes
* src/dtrace2systemtap.pl, src/rpc/gensystemtap.pl: Scripts
to generate STP files
* src/internal.h: Add probe macros
* src/probes.d: Master list of probes
* src/rpc/virnetclient.c, src/rpc/virnetserverclient.c,
src/rpc/virnetsocket.c, src/rpc/virnettlscontext.c,
src/util/event_poll.c: Insert probe points, removing any
DEBUG statements that duplicate the info
2011-09-30 13:40:23 +00:00
|
|
|
PROBE(RPC_CLIENT_MSG_RX,
|
|
|
|
"client=%p len=%zu prog=%u vers=%u proc=%u type=%u status=%u serial=%u",
|
|
|
|
client, client->msg.bufferLength,
|
|
|
|
client->msg.header.prog, client->msg.header.vers, client->msg.header.proc,
|
|
|
|
client->msg.header.type, client->msg.header.status, client->msg.header.serial);
|
2010-12-01 16:35:50 +00:00
|
|
|
|
2012-06-12 07:01:49 +00:00
|
|
|
if (virKeepAliveCheckMessage(client->keepalive, &client->msg, &response)) {
|
|
|
|
if (response &&
|
|
|
|
virNetClientQueueNonBlocking(client, response) < 0) {
|
|
|
|
VIR_WARN("Could not queue keepalive response");
|
|
|
|
virNetMessageFree(response);
|
|
|
|
}
|
2011-09-22 12:59:06 +00:00
|
|
|
return 0;
|
2012-06-12 07:01:49 +00:00
|
|
|
}
|
2011-09-22 12:59:06 +00:00
|
|
|
|
2010-12-01 16:35:50 +00:00
|
|
|
switch (client->msg.header.type) {
|
|
|
|
case VIR_NET_REPLY: /* Normal RPC replies */
|
2011-10-21 10:48:03 +00:00
|
|
|
case VIR_NET_REPLY_WITH_FDS: /* Normal RPC replies with FDs */
|
|
|
|
return virNetClientCallDispatchReply(client);
|
|
|
|
|
2010-12-01 16:35:50 +00:00
|
|
|
case VIR_NET_MESSAGE: /* Async notifications */
|
|
|
|
return virNetClientCallDispatchMessage(client);
|
|
|
|
|
|
|
|
case VIR_NET_STREAM: /* Stream protocol */
|
|
|
|
return virNetClientCallDispatchStream(client);
|
|
|
|
|
|
|
|
default:
|
2012-07-18 10:41:47 +00:00
|
|
|
virReportError(VIR_ERR_RPC,
|
|
|
|
_("got unexpected RPC call prog %d vers %d proc %d type %d"),
|
|
|
|
client->msg.header.prog, client->msg.header.vers,
|
|
|
|
client->msg.header.proc, client->msg.header.type);
|
2010-12-01 16:35:50 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static ssize_t
|
|
|
|
virNetClientIOWriteMessage(virNetClientPtr client,
|
|
|
|
virNetClientCallPtr thecall)
|
|
|
|
{
|
2011-11-04 16:02:14 +00:00
|
|
|
ssize_t ret = 0;
|
2010-12-01 16:35:50 +00:00
|
|
|
|
2011-11-04 16:02:14 +00:00
|
|
|
if (thecall->msg->bufferOffset < thecall->msg->bufferLength) {
|
|
|
|
ret = virNetSocketWrite(client->sock,
|
|
|
|
thecall->msg->buffer + thecall->msg->bufferOffset,
|
|
|
|
thecall->msg->bufferLength - thecall->msg->bufferOffset);
|
|
|
|
if (ret <= 0)
|
|
|
|
return ret;
|
2010-12-01 16:35:50 +00:00
|
|
|
|
2011-11-04 16:02:14 +00:00
|
|
|
thecall->msg->bufferOffset += ret;
|
|
|
|
}
|
2010-12-01 16:35:50 +00:00
|
|
|
|
|
|
|
if (thecall->msg->bufferOffset == thecall->msg->bufferLength) {
|
2011-10-21 10:48:03 +00:00
|
|
|
size_t i;
|
2013-05-21 07:59:54 +00:00
|
|
|
for (i = thecall->msg->donefds; i < thecall->msg->nfds; i++) {
|
2011-11-04 16:02:14 +00:00
|
|
|
int rv;
|
|
|
|
if ((rv = virNetSocketSendFD(client->sock, thecall->msg->fds[i])) < 0)
|
2011-10-21 10:48:03 +00:00
|
|
|
return -1;
|
2011-11-04 16:02:14 +00:00
|
|
|
if (rv == 0) /* Blocking */
|
|
|
|
return 0;
|
|
|
|
thecall->msg->donefds++;
|
2011-10-21 10:48:03 +00:00
|
|
|
}
|
2016-04-27 22:02:22 +00:00
|
|
|
virNetMessageClearPayload(thecall->msg);
|
2010-12-01 16:35:50 +00:00
|
|
|
if (thecall->expectReply)
|
|
|
|
thecall->mode = VIR_NET_CLIENT_MODE_WAIT_RX;
|
|
|
|
else
|
|
|
|
thecall->mode = VIR_NET_CLIENT_MODE_COMPLETE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static ssize_t
|
|
|
|
virNetClientIOHandleOutput(virNetClientPtr client)
|
|
|
|
{
|
|
|
|
virNetClientCallPtr thecall = client->waitDispatch;
|
|
|
|
|
|
|
|
while (thecall &&
|
|
|
|
thecall->mode != VIR_NET_CLIENT_MODE_WAIT_TX)
|
|
|
|
thecall = thecall->next;
|
|
|
|
|
|
|
|
if (!thecall)
|
Fix unwanted closing of libvirt client connection
e5a1bee07 introduced a regression in Boxes: when Boxes is left idle
(it's still doing some libvirt calls in the background), the
libvirt connection gets closed after a few minutes. What happens is
that this code in virNetClientIOHandleOutput gets triggered:
if (!thecall)
return -1; /* Shouldn't happen, but you never know... */
and after the changes in e5a1bee07, this causes the libvirt connection
to be closed.
Upon further investigation, what happens is that
virNetClientIOHandleOutput is called from gvir_event_handle_dispatch
in libvirt-glib, which is triggered because the client fd became
writable. However, between the times gvir_event_handle_dispatch
is called, and the time the client lock is grabbed and
virNetClientIOHandleOutput is called, another thread runs and
completes the current call. 'thecall' is then NULL when the first
thread gets to run virNetClientIOHandleOutput.
After describing this situation on IRC, danpb suggested this:
11:37 < danpb> In that case I think the correct thing would be to change
'return -1' above to 'return 0' since that's not actually an
error - its a rare, but expected event
which is what this patch is doing. I've tested it against master
libvirt, and I didn't get disconnected in ~10 minutes while this
happens in less than 5 minutes without this patch.
2012-09-10 10:17:07 +00:00
|
|
|
return 0; /* This can happen if another thread raced with us and
|
|
|
|
* completed the call between the time this thread woke
|
|
|
|
* up from poll()ing and the time we locked the client
|
|
|
|
*/
|
2010-12-01 16:35:50 +00:00
|
|
|
|
|
|
|
while (thecall) {
|
|
|
|
ssize_t ret = virNetClientIOWriteMessage(client, thecall);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
if (thecall->mode == VIR_NET_CLIENT_MODE_WAIT_TX)
|
|
|
|
return 0; /* Blocking write, to back to event loop */
|
|
|
|
|
|
|
|
thecall = thecall->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0; /* No more calls to send, all done */
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t
|
|
|
|
virNetClientIOReadMessage(virNetClientPtr client)
|
|
|
|
{
|
|
|
|
size_t wantData;
|
|
|
|
ssize_t ret;
|
|
|
|
|
|
|
|
/* Start by reading length word */
|
2012-04-26 15:21:24 +00:00
|
|
|
if (client->msg.bufferLength == 0) {
|
2010-12-01 16:35:50 +00:00
|
|
|
client->msg.bufferLength = 4;
|
2013-07-04 10:15:05 +00:00
|
|
|
if (VIR_ALLOC_N(client->msg.buffer, client->msg.bufferLength) < 0)
|
2012-04-26 15:21:24 +00:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
2010-12-01 16:35:50 +00:00
|
|
|
|
|
|
|
wantData = client->msg.bufferLength - client->msg.bufferOffset;
|
|
|
|
|
|
|
|
ret = virNetSocketRead(client->sock,
|
|
|
|
client->msg.buffer + client->msg.bufferOffset,
|
|
|
|
wantData);
|
|
|
|
if (ret <= 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
client->msg.bufferOffset += ret;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static ssize_t
|
|
|
|
virNetClientIOHandleInput(virNetClientPtr client)
|
|
|
|
{
|
|
|
|
/* Read as much data as is available, until we get
|
|
|
|
* EAGAIN
|
|
|
|
*/
|
|
|
|
for (;;) {
|
2011-11-04 16:02:14 +00:00
|
|
|
ssize_t ret;
|
2010-12-01 16:35:50 +00:00
|
|
|
|
2011-11-04 16:02:14 +00:00
|
|
|
if (client->msg.nfds == 0) {
|
|
|
|
ret = virNetClientIOReadMessage(client);
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
return -1;
|
|
|
|
if (ret == 0)
|
|
|
|
return 0; /* Blocking on read */
|
|
|
|
}
|
2010-12-01 16:35:50 +00:00
|
|
|
|
|
|
|
/* Check for completion of our goal */
|
|
|
|
if (client->msg.bufferOffset == client->msg.bufferLength) {
|
|
|
|
if (client->msg.bufferOffset == 4) {
|
|
|
|
ret = virNetMessageDecodeLength(&client->msg);
|
|
|
|
if (ret < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We'll carry on around the loop to immediately
|
|
|
|
* process the message body, because it has probably
|
|
|
|
* already arrived. Worst case, we'll get EAGAIN on
|
|
|
|
* next iteration.
|
|
|
|
*/
|
|
|
|
} else {
|
2011-11-04 16:02:14 +00:00
|
|
|
if (virNetMessageDecodeHeader(&client->msg) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (client->msg.header.type == VIR_NET_REPLY_WITH_FDS) {
|
|
|
|
size_t i;
|
2012-12-21 16:49:12 +00:00
|
|
|
|
|
|
|
if (client->msg.nfds == 0 &&
|
|
|
|
virNetMessageDecodeNumFDs(&client->msg) < 0)
|
2011-11-04 16:02:14 +00:00
|
|
|
return -1;
|
|
|
|
|
2013-05-21 07:59:54 +00:00
|
|
|
for (i = client->msg.donefds; i < client->msg.nfds; i++) {
|
2011-11-04 16:02:14 +00:00
|
|
|
int rv;
|
|
|
|
if ((rv = virNetSocketRecvFD(client->sock, &(client->msg.fds[i]))) < 0)
|
|
|
|
return -1;
|
|
|
|
if (rv == 0) /* Blocking */
|
|
|
|
break;
|
|
|
|
client->msg.donefds++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (client->msg.donefds < client->msg.nfds) {
|
|
|
|
/* Because DecodeHeader/NumFDs reset bufferOffset, we
|
|
|
|
* put it back to what it was, so everything works
|
|
|
|
* again next time we run this method
|
|
|
|
*/
|
|
|
|
client->msg.bufferOffset = client->msg.bufferLength;
|
|
|
|
return 0; /* Blocking on more fds */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-01 16:35:50 +00:00
|
|
|
ret = virNetClientCallDispatch(client);
|
2012-12-21 16:49:12 +00:00
|
|
|
virNetMessageClear(&client->msg);
|
2010-12-01 16:35:50 +00:00
|
|
|
/*
|
|
|
|
* We've completed one call, but we don't want to
|
|
|
|
* spin around the loop forever if there are many
|
|
|
|
* incoming async events, or replies for other
|
|
|
|
* thread's RPC calls. We want to get out & let
|
|
|
|
* any other thread take over as soon as we've
|
|
|
|
* got our reply. When SASL is active though, we
|
|
|
|
* may have read more data off the wire than we
|
|
|
|
* initially wanted & cached it in memory. In this
|
|
|
|
* case, poll() would not detect that there is more
|
|
|
|
* ready todo.
|
|
|
|
*
|
|
|
|
* So if SASL is active *and* some SASL data is
|
|
|
|
* already cached, then we'll process that now,
|
|
|
|
* before returning.
|
|
|
|
*/
|
|
|
|
if (ret == 0 &&
|
|
|
|
virNetSocketHasCachedData(client->sock))
|
|
|
|
continue;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-11 15:26:16 +00:00
|
|
|
static bool virNetClientIOEventLoopPollEvents(virNetClientCallPtr call,
|
|
|
|
void *opaque)
|
|
|
|
{
|
|
|
|
struct pollfd *fd = opaque;
|
|
|
|
|
|
|
|
if (call->mode == VIR_NET_CLIENT_MODE_WAIT_RX)
|
|
|
|
fd->events |= POLLIN;
|
|
|
|
if (call->mode == VIR_NET_CLIENT_MODE_WAIT_TX)
|
|
|
|
fd->events |= POLLOUT;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static bool virNetClientIOEventLoopRemoveDone(virNetClientCallPtr call,
|
|
|
|
void *opaque)
|
|
|
|
{
|
|
|
|
virNetClientCallPtr thiscall = opaque;
|
|
|
|
|
|
|
|
if (call == thiscall)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (call->mode != VIR_NET_CLIENT_MODE_COMPLETE)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/*
|
2011-11-08 09:13:27 +00:00
|
|
|
* ...if the call being removed from the list
|
|
|
|
* still has a thread, then wake that thread up,
|
|
|
|
* otherwise free the call. The latter should
|
|
|
|
* only happen for calls without replies.
|
|
|
|
*
|
|
|
|
* ...the threads won't actually wakeup until
|
2011-11-11 15:26:16 +00:00
|
|
|
* we release our mutex a short while
|
|
|
|
* later...
|
|
|
|
*/
|
2011-11-08 09:13:27 +00:00
|
|
|
if (call->haveThread) {
|
|
|
|
VIR_DEBUG("Waking up sleep %p", call);
|
|
|
|
virCondSignal(&call->cond);
|
|
|
|
} else {
|
2011-11-22 14:14:43 +00:00
|
|
|
VIR_DEBUG("Removing completed call %p", call);
|
2011-11-08 09:13:27 +00:00
|
|
|
if (call->expectReply)
|
|
|
|
VIR_WARN("Got a call expecting a reply but without a waiting thread");
|
2013-02-07 14:03:17 +00:00
|
|
|
virCondDestroy(&call->cond);
|
2011-11-08 09:13:27 +00:00
|
|
|
VIR_FREE(call->msg);
|
|
|
|
VIR_FREE(call);
|
|
|
|
}
|
2011-11-11 15:26:16 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-11-11 15:34:08 +00:00
|
|
|
|
2012-06-11 12:24:06 +00:00
|
|
|
static void
|
|
|
|
virNetClientIODetachNonBlocking(virNetClientCallPtr call)
|
2011-11-08 09:13:27 +00:00
|
|
|
{
|
2012-06-11 12:24:06 +00:00
|
|
|
VIR_DEBUG("Keeping unfinished non-blocking call %p in the queue", call);
|
|
|
|
call->haveThread = false;
|
2011-11-08 09:13:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-08 12:21:00 +00:00
|
|
|
static bool
|
|
|
|
virNetClientIOEventLoopRemoveAll(virNetClientCallPtr call,
|
|
|
|
void *opaque)
|
2011-09-22 13:47:29 +00:00
|
|
|
{
|
2012-06-08 12:21:00 +00:00
|
|
|
virNetClientCallPtr thiscall = opaque;
|
2011-09-22 13:47:29 +00:00
|
|
|
|
2012-06-08 12:21:00 +00:00
|
|
|
if (call == thiscall)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
VIR_DEBUG("Removing call %p", call);
|
2013-02-07 14:03:17 +00:00
|
|
|
virCondDestroy(&call->cond);
|
2012-06-08 12:21:00 +00:00
|
|
|
VIR_FREE(call->msg);
|
|
|
|
VIR_FREE(call);
|
|
|
|
return true;
|
2011-09-22 13:47:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-08 12:21:00 +00:00
|
|
|
static void
|
|
|
|
virNetClientIOEventLoopPassTheBuck(virNetClientPtr client,
|
|
|
|
virNetClientCallPtr thiscall)
|
2011-11-11 15:34:08 +00:00
|
|
|
{
|
|
|
|
VIR_DEBUG("Giving up the buck %p", thiscall);
|
|
|
|
virNetClientCallPtr tmp = client->waitDispatch;
|
|
|
|
/* See if someone else is still waiting
|
|
|
|
* and if so, then pass the buck ! */
|
|
|
|
while (tmp) {
|
2011-11-08 09:13:27 +00:00
|
|
|
if (tmp != thiscall && tmp->haveThread) {
|
2011-11-11 15:34:08 +00:00
|
|
|
VIR_DEBUG("Passing the buck to %p", tmp);
|
|
|
|
virCondSignal(&tmp->cond);
|
2011-11-22 14:10:06 +00:00
|
|
|
return;
|
2011-11-11 15:34:08 +00:00
|
|
|
}
|
|
|
|
tmp = tmp->next;
|
|
|
|
}
|
2011-12-07 14:46:39 +00:00
|
|
|
client->haveTheBuck = false;
|
2011-09-22 13:47:29 +00:00
|
|
|
|
2011-11-08 09:13:27 +00:00
|
|
|
VIR_DEBUG("No thread to pass the buck to");
|
2011-09-22 13:47:29 +00:00
|
|
|
if (client->wantClose) {
|
|
|
|
virNetClientCloseLocked(client);
|
2012-06-08 12:21:00 +00:00
|
|
|
virNetClientCallRemovePredicate(&client->waitDispatch,
|
|
|
|
virNetClientIOEventLoopRemoveAll,
|
|
|
|
thiscall);
|
2011-09-22 13:47:29 +00:00
|
|
|
}
|
2011-11-08 09:13:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-12-01 16:35:50 +00:00
|
|
|
/*
|
|
|
|
* Process all calls pending dispatch/receive until we
|
|
|
|
* get a reply to our own call. Then quit and pass the buck
|
|
|
|
* to someone else.
|
2011-11-08 09:13:27 +00:00
|
|
|
*
|
2012-06-08 13:24:06 +00:00
|
|
|
* Returns 1 if the call was queued and will be completed later (only
|
2014-03-17 09:38:38 +00:00
|
|
|
* for nonBlock == true), 0 if the call was completed and -1 on error.
|
2010-12-01 16:35:50 +00:00
|
|
|
*/
|
|
|
|
static int virNetClientIOEventLoop(virNetClientPtr client,
|
|
|
|
virNetClientCallPtr thiscall)
|
|
|
|
{
|
|
|
|
struct pollfd fds[2];
|
2015-09-15 14:45:41 +00:00
|
|
|
bool error = false;
|
2010-12-01 16:35:50 +00:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
fds[0].fd = virNetSocketGetFD(client->sock);
|
|
|
|
fds[1].fd = client->wakeupReadFD;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
char ignore;
|
|
|
|
sigset_t oldmask, blockedsigs;
|
|
|
|
int timeout = -1;
|
2012-06-12 12:32:27 +00:00
|
|
|
virNetMessagePtr msg = NULL;
|
2010-12-01 16:35:50 +00:00
|
|
|
|
2011-09-22 13:47:29 +00:00
|
|
|
/* If we have existing SASL decoded data we don't want to sleep in
|
|
|
|
* the poll(), just check if any other FDs are also ready.
|
|
|
|
* If the connection is going to be closed, we don't want to sleep in
|
|
|
|
* poll() either.
|
2010-12-01 16:35:50 +00:00
|
|
|
*/
|
2011-09-22 13:47:29 +00:00
|
|
|
if (virNetSocketHasCachedData(client->sock) || client->wantClose)
|
2010-12-01 16:35:50 +00:00
|
|
|
timeout = 0;
|
|
|
|
|
2012-06-11 12:24:06 +00:00
|
|
|
/* If we are non-blocking, then we don't want to sleep in poll() */
|
|
|
|
if (thiscall->nonBlock)
|
2011-11-08 09:13:27 +00:00
|
|
|
timeout = 0;
|
|
|
|
|
2012-06-12 12:32:27 +00:00
|
|
|
/* Limit timeout so that we can send keepalive request in time */
|
|
|
|
if (timeout == -1)
|
|
|
|
timeout = virKeepAliveTimeout(client->keepalive);
|
|
|
|
|
2010-12-01 16:35:50 +00:00
|
|
|
fds[0].events = fds[0].revents = 0;
|
|
|
|
fds[1].events = fds[1].revents = 0;
|
|
|
|
|
|
|
|
fds[1].events = POLLIN;
|
|
|
|
|
2011-11-11 15:26:16 +00:00
|
|
|
/* Calculate poll events for calls */
|
|
|
|
virNetClientCallMatchPredicate(client->waitDispatch,
|
|
|
|
virNetClientIOEventLoopPollEvents,
|
|
|
|
&fds[0]);
|
2010-12-01 16:35:50 +00:00
|
|
|
|
|
|
|
/* We have to be prepared to receive stream data
|
|
|
|
* regardless of whether any of the calls waiting
|
|
|
|
* for dispatch are for streams.
|
|
|
|
*/
|
|
|
|
if (client->nstreams)
|
|
|
|
fds[0].events |= POLLIN;
|
|
|
|
|
|
|
|
/* Release lock while poll'ing so other threads
|
|
|
|
* can stuff themselves on the queue */
|
2013-01-09 21:27:28 +00:00
|
|
|
virObjectUnlock(client);
|
2010-12-01 16:35:50 +00:00
|
|
|
|
|
|
|
/* Block SIGWINCH from interrupting poll in curses programs,
|
|
|
|
* then restore the original signal mask again immediately
|
|
|
|
* after the call (RHBZ#567931). Same for SIGCHLD and SIGPIPE
|
|
|
|
* at the suggestion of Paolo Bonzini and Daniel Berrange.
|
|
|
|
*/
|
2012-10-17 09:23:12 +00:00
|
|
|
sigemptyset(&blockedsigs);
|
2011-07-06 14:46:15 +00:00
|
|
|
#ifdef SIGWINCH
|
2012-10-17 09:23:12 +00:00
|
|
|
sigaddset(&blockedsigs, SIGWINCH);
|
2011-07-06 14:46:15 +00:00
|
|
|
#endif
|
|
|
|
#ifdef SIGCHLD
|
2012-10-17 09:23:12 +00:00
|
|
|
sigaddset(&blockedsigs, SIGCHLD);
|
2011-07-06 14:46:15 +00:00
|
|
|
#endif
|
2012-10-17 09:23:12 +00:00
|
|
|
sigaddset(&blockedsigs, SIGPIPE);
|
2010-12-01 16:35:50 +00:00
|
|
|
ignore_value(pthread_sigmask(SIG_BLOCK, &blockedsigs, &oldmask));
|
|
|
|
|
|
|
|
repoll:
|
|
|
|
ret = poll(fds, ARRAY_CARDINALITY(fds), timeout);
|
2012-08-08 20:59:41 +00:00
|
|
|
if (ret < 0 && (errno == EAGAIN || errno == EINTR))
|
2010-12-01 16:35:50 +00:00
|
|
|
goto repoll;
|
|
|
|
|
|
|
|
ignore_value(pthread_sigmask(SIG_SETMASK, &oldmask, NULL));
|
|
|
|
|
2013-01-09 21:27:28 +00:00
|
|
|
virObjectLock(client);
|
2010-12-01 16:35:50 +00:00
|
|
|
|
2012-06-13 09:54:02 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
virReportSystemError(errno,
|
|
|
|
"%s", _("poll on socket failed"));
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2012-06-12 12:32:27 +00:00
|
|
|
if (virKeepAliveTrigger(client->keepalive, &msg)) {
|
2012-07-19 10:21:54 +00:00
|
|
|
virNetClientMarkClose(client, VIR_CONNECT_CLOSE_REASON_KEEPALIVE);
|
2012-06-12 12:32:27 +00:00
|
|
|
} else if (msg && virNetClientQueueNonBlocking(client, msg) < 0) {
|
|
|
|
VIR_WARN("Could not queue keepalive request");
|
|
|
|
virNetMessageFree(msg);
|
|
|
|
}
|
|
|
|
|
2010-12-01 16:35:50 +00:00
|
|
|
/* If we have existing SASL decoded data, pretend
|
|
|
|
* the socket became readable so we consume it
|
|
|
|
*/
|
2014-11-13 14:29:21 +00:00
|
|
|
if (virNetSocketHasCachedData(client->sock))
|
2010-12-01 16:35:50 +00:00
|
|
|
fds[0].revents |= POLLIN;
|
|
|
|
|
2015-09-15 14:45:41 +00:00
|
|
|
/* If wantClose flag is set, pretend there was an error on the socket,
|
|
|
|
* but still read and process any data we received so far.
|
2011-09-22 13:47:29 +00:00
|
|
|
*/
|
|
|
|
if (client->wantClose)
|
2015-09-15 14:45:41 +00:00
|
|
|
error = true;
|
2011-09-22 13:47:29 +00:00
|
|
|
|
2010-12-01 16:35:50 +00:00
|
|
|
if (fds[1].revents) {
|
|
|
|
VIR_DEBUG("Woken up from poll by other thread");
|
|
|
|
if (saferead(client->wakeupReadFD, &ignore, sizeof(ignore)) != sizeof(ignore)) {
|
|
|
|
virReportSystemError(errno, "%s",
|
|
|
|
_("read on wakeup fd failed"));
|
2012-07-19 10:21:54 +00:00
|
|
|
virNetClientMarkClose(client, VIR_CONNECT_CLOSE_REASON_ERROR);
|
2015-09-15 14:45:41 +00:00
|
|
|
error = true;
|
|
|
|
/* Fall through to process any pending data. */
|
2010-12-01 16:35:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fds[0].revents & POLLOUT) {
|
2012-07-19 10:21:54 +00:00
|
|
|
if (virNetClientIOHandleOutput(client) < 0) {
|
|
|
|
virNetClientMarkClose(client, VIR_CONNECT_CLOSE_REASON_ERROR);
|
2015-09-15 14:45:41 +00:00
|
|
|
error = true;
|
|
|
|
/* Fall through to process any pending data. */
|
2012-07-19 10:21:54 +00:00
|
|
|
}
|
2010-12-01 16:35:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (fds[0].revents & POLLIN) {
|
2012-07-19 10:21:54 +00:00
|
|
|
if (virNetClientIOHandleInput(client) < 0) {
|
|
|
|
virNetClientMarkClose(client, VIR_CONNECT_CLOSE_REASON_ERROR);
|
2015-09-15 14:45:41 +00:00
|
|
|
error = true;
|
|
|
|
/* Fall through to process any pending data. */
|
2012-07-19 10:21:54 +00:00
|
|
|
}
|
2010-12-01 16:35:50 +00:00
|
|
|
}
|
|
|
|
|
2011-11-08 09:13:27 +00:00
|
|
|
/* Iterate through waiting calls and if any are
|
2012-06-08 12:21:00 +00:00
|
|
|
* complete, remove them from the dispatch list.
|
2010-12-01 16:35:50 +00:00
|
|
|
*/
|
2011-11-11 15:26:16 +00:00
|
|
|
virNetClientCallRemovePredicate(&client->waitDispatch,
|
|
|
|
virNetClientIOEventLoopRemoveDone,
|
|
|
|
thiscall);
|
2010-12-01 16:35:50 +00:00
|
|
|
|
|
|
|
/* Now see if *we* are done */
|
|
|
|
if (thiscall->mode == VIR_NET_CLIENT_MODE_COMPLETE) {
|
2011-11-11 15:26:16 +00:00
|
|
|
virNetClientCallRemove(&client->waitDispatch, thiscall);
|
2011-11-11 15:34:08 +00:00
|
|
|
virNetClientIOEventLoopPassTheBuck(client, thiscall);
|
2012-06-08 13:24:06 +00:00
|
|
|
return 0;
|
2010-12-01 16:35:50 +00:00
|
|
|
}
|
|
|
|
|
2012-06-08 12:21:00 +00:00
|
|
|
/* We're not done, but we're non-blocking; keep the call queued */
|
2011-11-08 09:13:27 +00:00
|
|
|
if (thiscall->nonBlock) {
|
2012-06-11 12:24:06 +00:00
|
|
|
virNetClientIODetachNonBlocking(thiscall);
|
2011-11-08 09:13:27 +00:00
|
|
|
virNetClientIOEventLoopPassTheBuck(client, thiscall);
|
2012-06-08 12:21:00 +00:00
|
|
|
return 1;
|
2011-11-08 09:13:27 +00:00
|
|
|
}
|
2010-12-01 16:35:50 +00:00
|
|
|
|
2015-09-15 14:45:41 +00:00
|
|
|
if (error)
|
|
|
|
goto error;
|
|
|
|
|
2010-12-01 16:35:50 +00:00
|
|
|
if (fds[0].revents & (POLLHUP | POLLERR)) {
|
2012-07-19 10:21:54 +00:00
|
|
|
virNetClientMarkClose(client, VIR_CONNECT_CLOSE_REASON_EOF);
|
2012-07-18 10:41:47 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("received hangup / error event on socket"));
|
2010-12-01 16:35:50 +00:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-25 06:52:31 +00:00
|
|
|
error:
|
2015-09-11 15:07:56 +00:00
|
|
|
if (client->error) {
|
|
|
|
VIR_DEBUG("error on socket: %s", client->error->message);
|
|
|
|
virSetError(client->error);
|
|
|
|
}
|
2011-11-11 15:26:16 +00:00
|
|
|
virNetClientCallRemove(&client->waitDispatch, thiscall);
|
2011-11-11 15:34:08 +00:00
|
|
|
virNetClientIOEventLoopPassTheBuck(client, thiscall);
|
2010-12-01 16:35:50 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-12 06:41:59 +00:00
|
|
|
static bool
|
|
|
|
virNetClientIOUpdateEvents(virNetClientCallPtr call,
|
|
|
|
void *opaque)
|
|
|
|
{
|
|
|
|
int *events = opaque;
|
|
|
|
|
|
|
|
if (call->mode == VIR_NET_CLIENT_MODE_WAIT_TX)
|
|
|
|
*events |= VIR_EVENT_HANDLE_WRITABLE;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-11 15:45:44 +00:00
|
|
|
static void virNetClientIOUpdateCallback(virNetClientPtr client,
|
|
|
|
bool enableCallback)
|
|
|
|
{
|
|
|
|
int events = 0;
|
2012-06-12 06:41:59 +00:00
|
|
|
|
2012-07-19 10:21:54 +00:00
|
|
|
if (client->wantClose)
|
|
|
|
return;
|
|
|
|
|
2012-06-12 06:41:59 +00:00
|
|
|
if (enableCallback) {
|
2011-11-11 15:45:44 +00:00
|
|
|
events |= VIR_EVENT_HANDLE_READABLE;
|
2012-06-12 06:41:59 +00:00
|
|
|
virNetClientCallMatchPredicate(client->waitDispatch,
|
|
|
|
virNetClientIOUpdateEvents,
|
|
|
|
&events);
|
|
|
|
}
|
2011-11-11 15:45:44 +00:00
|
|
|
|
|
|
|
virNetSocketUpdateIOCallback(client->sock, events);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-12-01 16:35:50 +00:00
|
|
|
/*
|
|
|
|
* This function sends a message to remote server and awaits a reply
|
|
|
|
*
|
|
|
|
* NB. This does not free the args structure (not desirable, since you
|
|
|
|
* often want this allocated on the stack or else it contains strings
|
|
|
|
* which come from the user). It does however free any intermediate
|
|
|
|
* results, eg. the error structure if there is one.
|
|
|
|
*
|
2012-03-29 09:52:04 +00:00
|
|
|
* NB(2). Make sure to memset (&ret, 0, sizeof(ret)) before calling,
|
2010-12-01 16:35:50 +00:00
|
|
|
* else Bad Things will happen in the XDR code.
|
|
|
|
*
|
|
|
|
* NB(3) You must have the client lock before calling this
|
|
|
|
*
|
|
|
|
* NB(4) This is very complicated. Multiple threads are allowed to
|
|
|
|
* use the client for RPC at the same time. Obviously only one of
|
|
|
|
* them can. So if someone's using the socket, other threads are put
|
|
|
|
* to sleep on condition variables. The existing thread may completely
|
|
|
|
* send & receive their RPC call/reply while they're asleep. Or it
|
|
|
|
* may only get around to dealing with sending the call. Or it may
|
|
|
|
* get around to neither. So upon waking up from slumber, the other
|
|
|
|
* thread may or may not have more work todo.
|
|
|
|
*
|
|
|
|
* We call this dance 'passing the buck'
|
|
|
|
*
|
|
|
|
* http://en.wikipedia.org/wiki/Passing_the_buck
|
|
|
|
*
|
|
|
|
* "Buck passing or passing the buck is the action of transferring
|
|
|
|
* responsibility or blame unto another person. It is also used as
|
|
|
|
* a strategy in power politics when the actions of one country/
|
|
|
|
* nation are blamed on another, providing an opportunity for war."
|
|
|
|
*
|
2011-11-08 09:13:27 +00:00
|
|
|
* NB(5) If the 'thiscall' has the 'nonBlock' flag set, the caller
|
|
|
|
* must *NOT* free it, if this returns '1' (ie partial send).
|
|
|
|
*
|
|
|
|
* NB(6) The following input states are valid if *no* threads
|
|
|
|
* are currently executing this method
|
|
|
|
*
|
|
|
|
* - waitDispatch == NULL,
|
|
|
|
* - waitDispatch != NULL, waitDispatch.nonBlock == true
|
|
|
|
*
|
|
|
|
* The following input states are valid, if n threads are currently
|
|
|
|
* executing
|
|
|
|
*
|
|
|
|
* - waitDispatch != NULL
|
|
|
|
* - 0 or 1 waitDispatch.nonBlock == false, without any threads
|
|
|
|
* - 0 or more waitDispatch.nonBlock == false, with threads
|
|
|
|
*
|
|
|
|
* The following output states are valid when all threads are done
|
|
|
|
*
|
|
|
|
* - waitDispatch == NULL,
|
|
|
|
* - waitDispatch != NULL, waitDispatch.nonBlock == true
|
|
|
|
*
|
|
|
|
* NB(7) Don't Panic!
|
|
|
|
*
|
2012-06-08 13:24:06 +00:00
|
|
|
* Returns 1 if the call was queued and will be completed later (only
|
2014-03-17 09:38:38 +00:00
|
|
|
* for nonBlock == true), 0 if the call was completed and -1 on error.
|
2010-12-01 16:35:50 +00:00
|
|
|
*/
|
|
|
|
static int virNetClientIO(virNetClientPtr client,
|
|
|
|
virNetClientCallPtr thiscall)
|
|
|
|
{
|
|
|
|
int rv = -1;
|
|
|
|
|
2011-08-16 16:48:03 +00:00
|
|
|
VIR_DEBUG("Outgoing message prog=%u version=%u serial=%u proc=%d type=%d length=%zu dispatch=%p",
|
2010-12-01 16:35:50 +00:00
|
|
|
thiscall->msg->header.prog,
|
|
|
|
thiscall->msg->header.vers,
|
|
|
|
thiscall->msg->header.serial,
|
|
|
|
thiscall->msg->header.proc,
|
|
|
|
thiscall->msg->header.type,
|
|
|
|
thiscall->msg->bufferLength,
|
|
|
|
client->waitDispatch);
|
|
|
|
|
2011-11-11 15:28:41 +00:00
|
|
|
/* Stick ourselves on the end of the wait queue */
|
|
|
|
virNetClientCallQueue(&client->waitDispatch, thiscall);
|
|
|
|
|
2010-12-01 16:35:50 +00:00
|
|
|
/* Check to see if another thread is dispatching */
|
2011-11-11 15:28:41 +00:00
|
|
|
if (client->haveTheBuck) {
|
2010-12-01 16:35:50 +00:00
|
|
|
char ignore = 1;
|
|
|
|
|
|
|
|
/* Force other thread to wakeup from poll */
|
|
|
|
if (safewrite(client->wakeupSendFD, &ignore, sizeof(ignore)) != sizeof(ignore)) {
|
2011-11-11 15:26:16 +00:00
|
|
|
virNetClientCallRemove(&client->waitDispatch, thiscall);
|
2010-12-01 16:35:50 +00:00
|
|
|
virReportSystemError(errno, "%s",
|
|
|
|
_("failed to wake up polling thread"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-06-11 12:24:06 +00:00
|
|
|
/* If we are non-blocking, detach the thread and keep the call in the
|
|
|
|
* queue. */
|
|
|
|
if (thiscall->nonBlock) {
|
|
|
|
virNetClientIODetachNonBlocking(thiscall);
|
|
|
|
rv = 1;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2012-06-08 11:41:25 +00:00
|
|
|
VIR_DEBUG("Going to sleep head=%p call=%p",
|
|
|
|
client->waitDispatch, thiscall);
|
2010-12-01 16:35:50 +00:00
|
|
|
/* Go to sleep while other thread is working... */
|
2013-01-09 21:27:28 +00:00
|
|
|
if (virCondWait(&thiscall->cond, &client->parent.lock) < 0) {
|
2011-11-11 15:26:16 +00:00
|
|
|
virNetClientCallRemove(&client->waitDispatch, thiscall);
|
2012-07-18 10:41:47 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("failed to wait on condition"));
|
2010-12-01 16:35:50 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-06-08 11:41:25 +00:00
|
|
|
VIR_DEBUG("Woken up from sleep head=%p call=%p",
|
|
|
|
client->waitDispatch, thiscall);
|
2011-11-08 09:13:27 +00:00
|
|
|
/* Three reasons we can be woken up
|
2010-12-01 16:35:50 +00:00
|
|
|
* 1. Other thread has got our reply ready for us
|
|
|
|
* 2. Other thread is all done, and it is our turn to
|
|
|
|
* be the dispatcher to finish waiting for
|
|
|
|
* our reply
|
|
|
|
*/
|
|
|
|
if (thiscall->mode == VIR_NET_CLIENT_MODE_COMPLETE) {
|
2012-06-08 13:24:06 +00:00
|
|
|
rv = 0;
|
2010-12-01 16:35:50 +00:00
|
|
|
/*
|
|
|
|
* We avoided catching the buck and our reply is ready !
|
|
|
|
* We've already had 'thiscall' removed from the list
|
|
|
|
* so just need to (maybe) handle errors & free it
|
|
|
|
*/
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2013-04-19 20:18:14 +00:00
|
|
|
/* Grr, someone passed the buck to us ... */
|
2011-12-07 14:46:39 +00:00
|
|
|
} else {
|
|
|
|
client->haveTheBuck = true;
|
2010-12-01 16:35:50 +00:00
|
|
|
}
|
|
|
|
|
2012-06-08 11:41:25 +00:00
|
|
|
VIR_DEBUG("We have the buck head=%p call=%p",
|
|
|
|
client->waitDispatch, thiscall);
|
2011-11-11 15:28:41 +00:00
|
|
|
|
2010-12-01 16:35:50 +00:00
|
|
|
/*
|
|
|
|
* The buck stops here!
|
|
|
|
*
|
|
|
|
* At this point we're about to own the dispatch
|
|
|
|
* process...
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Avoid needless wake-ups of the event loop in the
|
|
|
|
* case where this call is being made from a different
|
|
|
|
* thread than the event loop. These wake-ups would
|
|
|
|
* cause the event loop thread to be blocked on the
|
|
|
|
* mutex for the duration of the call
|
|
|
|
*/
|
2011-11-11 15:45:44 +00:00
|
|
|
virNetClientIOUpdateCallback(client, false);
|
2010-12-01 16:35:50 +00:00
|
|
|
|
2011-06-27 20:38:00 +00:00
|
|
|
virResetLastError();
|
2010-12-01 16:35:50 +00:00
|
|
|
rv = virNetClientIOEventLoop(client, thiscall);
|
|
|
|
|
2011-09-22 13:47:29 +00:00
|
|
|
if (client->sock)
|
|
|
|
virNetClientIOUpdateCallback(client, true);
|
2010-12-01 16:35:50 +00:00
|
|
|
|
2011-06-27 20:38:00 +00:00
|
|
|
if (rv == 0 &&
|
|
|
|
virGetLastError())
|
|
|
|
rv = -1;
|
|
|
|
|
2014-03-25 06:52:31 +00:00
|
|
|
cleanup:
|
2012-06-08 11:41:25 +00:00
|
|
|
VIR_DEBUG("All done with our call head=%p call=%p rv=%d",
|
|
|
|
client->waitDispatch, thiscall, rv);
|
2010-12-01 16:35:50 +00:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void virNetClientIncomingEvent(virNetSocketPtr sock,
|
|
|
|
int events,
|
|
|
|
void *opaque)
|
|
|
|
{
|
|
|
|
virNetClientPtr client = opaque;
|
|
|
|
|
2013-01-09 21:27:28 +00:00
|
|
|
virObjectLock(client);
|
2010-12-01 16:35:50 +00:00
|
|
|
|
2012-07-19 10:21:54 +00:00
|
|
|
VIR_DEBUG("client=%p wantclose=%d", client, client ? client->wantClose : false);
|
|
|
|
|
2011-07-19 13:13:32 +00:00
|
|
|
if (!client->sock)
|
|
|
|
goto done;
|
|
|
|
|
2011-09-22 13:47:29 +00:00
|
|
|
if (client->haveTheBuck || client->wantClose)
|
2010-12-01 16:35:50 +00:00
|
|
|
goto done;
|
|
|
|
|
|
|
|
VIR_DEBUG("Event fired %p %d", sock, events);
|
|
|
|
|
2012-06-12 06:41:59 +00:00
|
|
|
if (events & VIR_EVENT_HANDLE_WRITABLE) {
|
|
|
|
if (virNetClientIOHandleOutput(client) < 0)
|
2012-07-19 10:21:54 +00:00
|
|
|
virNetClientMarkClose(client, VIR_CONNECT_CLOSE_REASON_ERROR);
|
2012-06-12 06:41:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (events & VIR_EVENT_HANDLE_READABLE) {
|
|
|
|
if (virNetClientIOHandleInput(client) < 0)
|
2012-07-19 10:21:54 +00:00
|
|
|
virNetClientMarkClose(client, VIR_CONNECT_CLOSE_REASON_ERROR);
|
2011-07-15 10:19:40 +00:00
|
|
|
}
|
2010-12-01 16:35:50 +00:00
|
|
|
|
2012-07-18 15:50:02 +00:00
|
|
|
if (events & (VIR_EVENT_HANDLE_HANGUP | VIR_EVENT_HANDLE_ERROR)) {
|
|
|
|
VIR_DEBUG("VIR_EVENT_HANDLE_HANGUP or "
|
|
|
|
"VIR_EVENT_HANDLE_ERROR encountered");
|
|
|
|
virNetClientMarkClose(client,
|
|
|
|
(events & VIR_EVENT_HANDLE_HANGUP) ?
|
|
|
|
VIR_CONNECT_CLOSE_REASON_EOF :
|
|
|
|
VIR_CONNECT_CLOSE_REASON_ERROR);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2012-06-12 06:41:59 +00:00
|
|
|
/* Remove completed calls or signal their threads. */
|
|
|
|
virNetClientCallRemovePredicate(&client->waitDispatch,
|
|
|
|
virNetClientIOEventLoopRemoveDone,
|
|
|
|
NULL);
|
2012-06-12 07:01:49 +00:00
|
|
|
virNetClientIOUpdateCallback(client, true);
|
|
|
|
|
2014-03-25 06:52:31 +00:00
|
|
|
done:
|
2013-03-26 14:50:38 +00:00
|
|
|
if (client->wantClose && !client->haveTheBuck) {
|
2012-07-19 10:21:54 +00:00
|
|
|
virNetClientCloseLocked(client);
|
2013-03-26 14:50:38 +00:00
|
|
|
virNetClientCallRemovePredicate(&client->waitDispatch,
|
|
|
|
virNetClientIOEventLoopRemoveAll,
|
|
|
|
NULL);
|
|
|
|
}
|
2013-01-09 21:27:28 +00:00
|
|
|
virObjectUnlock(client);
|
2010-12-01 16:35:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-12 06:47:13 +00:00
|
|
|
static virNetClientCallPtr
|
|
|
|
virNetClientCallNew(virNetMessagePtr msg,
|
|
|
|
bool expectReply,
|
|
|
|
bool nonBlock)
|
2010-12-01 16:35:50 +00:00
|
|
|
{
|
2012-06-12 06:47:13 +00:00
|
|
|
virNetClientCallPtr call = NULL;
|
Rewrite all the DTrace/SystemTAP probing
The libvirtd daemon had a few crude system tap probes. Some of
these were broken during the RPC rewrite. The new modular RPC
code is structured in a way that allows much more effective
tracing. Instead of trying to hook up the original probes,
define a new set of probes for the RPC and event code.
The master probes file is now src/probes.d. This contains
probes for virNetServerClientPtr, virNetClientPtr, virSocketPtr
virNetTLSContextPtr and virNetTLSSessionPtr modules. Also add
probes for the poll event loop.
The src/dtrace2systemtap.pl script can convert the probes.d
file into a libvirt_probes.stp file to make use from systemtap
much simpler.
The src/rpc/gensystemtap.pl script can generate a set of
systemtap functions for translating RPC enum values into
printable strings. This works for all RPC header enums (program,
type, status, procedure) and also the authentication enum
The PROBE macro will automatically generate a VIR_DEBUG
statement, so any place with a PROBE can remove any existing
manual DEBUG statements.
* daemon/libvirtd.stp, daemon/probes.d: Remove obsolete probing
* daemon/libvirtd.h: Remove probe macros
* daemon/Makefile.am: Remove all probe buildings/install
* daemon/remote.c: Update authentication probes
* src/dtrace2systemtap.pl, src/rpc/gensystemtap.pl: Scripts
to generate STP files
* src/internal.h: Add probe macros
* src/probes.d: Master list of probes
* src/rpc/virnetclient.c, src/rpc/virnetserverclient.c,
src/rpc/virnetsocket.c, src/rpc/virnettlscontext.c,
src/util/event_poll.c: Insert probe points, removing any
DEBUG statements that duplicate the info
2011-09-30 13:40:23 +00:00
|
|
|
|
2011-08-16 23:22:44 +00:00
|
|
|
if (expectReply &&
|
2011-09-21 13:51:33 +00:00
|
|
|
(msg->bufferLength != 0) &&
|
2011-08-16 23:22:44 +00:00
|
|
|
(msg->header.status == VIR_NET_CONTINUE)) {
|
2012-07-18 10:41:47 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("Attempt to send an asynchronous message with"
|
|
|
|
" a synchronous reply"));
|
2012-06-12 06:47:13 +00:00
|
|
|
goto error;
|
2011-08-16 23:22:44 +00:00
|
|
|
}
|
|
|
|
|
2011-11-08 09:13:27 +00:00
|
|
|
if (expectReply && nonBlock) {
|
2012-07-18 10:41:47 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("Attempt to send a non-blocking message with"
|
|
|
|
" a synchronous reply"));
|
2012-06-12 06:47:13 +00:00
|
|
|
goto error;
|
2011-11-08 09:13:27 +00:00
|
|
|
}
|
|
|
|
|
2013-07-04 10:15:05 +00:00
|
|
|
if (VIR_ALLOC(call) < 0)
|
2012-06-12 06:47:13 +00:00
|
|
|
goto error;
|
2011-09-22 13:47:29 +00:00
|
|
|
|
2010-12-01 16:35:50 +00:00
|
|
|
if (virCondInit(&call->cond) < 0) {
|
2012-07-18 10:41:47 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("cannot initialize condition variable"));
|
2012-06-12 06:47:13 +00:00
|
|
|
goto error;
|
2010-12-01 16:35:50 +00:00
|
|
|
}
|
|
|
|
|
2011-11-04 16:02:14 +00:00
|
|
|
msg->donefds = 0;
|
2010-12-01 16:35:50 +00:00
|
|
|
if (msg->bufferLength)
|
|
|
|
call->mode = VIR_NET_CLIENT_MODE_WAIT_TX;
|
|
|
|
else
|
|
|
|
call->mode = VIR_NET_CLIENT_MODE_WAIT_RX;
|
|
|
|
call->msg = msg;
|
|
|
|
call->expectReply = expectReply;
|
2011-11-08 09:13:27 +00:00
|
|
|
call->nonBlock = nonBlock;
|
2010-12-01 16:35:50 +00:00
|
|
|
|
2012-06-12 06:47:13 +00:00
|
|
|
VIR_DEBUG("New call %p: msg=%p, expectReply=%d, nonBlock=%d",
|
|
|
|
call, msg, expectReply, nonBlock);
|
|
|
|
|
|
|
|
return call;
|
|
|
|
|
2014-03-25 06:52:31 +00:00
|
|
|
error:
|
2012-06-12 06:47:13 +00:00
|
|
|
VIR_FREE(call);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-12 07:01:49 +00:00
|
|
|
static int
|
|
|
|
virNetClientQueueNonBlocking(virNetClientPtr client,
|
|
|
|
virNetMessagePtr msg)
|
|
|
|
{
|
|
|
|
virNetClientCallPtr call;
|
|
|
|
|
|
|
|
PROBE(RPC_CLIENT_MSG_TX_QUEUE,
|
|
|
|
"client=%p len=%zu prog=%u vers=%u proc=%u"
|
|
|
|
" type=%u status=%u serial=%u",
|
|
|
|
client, msg->bufferLength,
|
|
|
|
msg->header.prog, msg->header.vers, msg->header.proc,
|
|
|
|
msg->header.type, msg->header.status, msg->header.serial);
|
|
|
|
|
|
|
|
if (!(call = virNetClientCallNew(msg, false, true)))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
virNetClientCallQueue(&client->waitDispatch, call);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-12 06:47:13 +00:00
|
|
|
/*
|
|
|
|
* Returns 1 if the call was queued and will be completed later (only
|
2014-03-17 09:38:38 +00:00
|
|
|
* for nonBlock == true), 0 if the call was completed and -1 on error.
|
2012-06-12 06:47:13 +00:00
|
|
|
*/
|
|
|
|
static int virNetClientSendInternal(virNetClientPtr client,
|
|
|
|
virNetMessagePtr msg,
|
|
|
|
bool expectReply,
|
|
|
|
bool nonBlock)
|
|
|
|
{
|
|
|
|
virNetClientCallPtr call;
|
|
|
|
int ret = -1;
|
|
|
|
|
|
|
|
PROBE(RPC_CLIENT_MSG_TX_QUEUE,
|
|
|
|
"client=%p len=%zu prog=%u vers=%u proc=%u type=%u status=%u serial=%u",
|
|
|
|
client, msg->bufferLength,
|
|
|
|
msg->header.prog, msg->header.vers, msg->header.proc,
|
|
|
|
msg->header.type, msg->header.status, msg->header.serial);
|
|
|
|
|
|
|
|
if (!client->sock || client->wantClose) {
|
2012-07-18 10:41:47 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("client socket is closed"));
|
2012-06-12 06:47:13 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-07-04 10:15:05 +00:00
|
|
|
if (!(call = virNetClientCallNew(msg, expectReply, nonBlock)))
|
2012-06-12 06:47:13 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
call->haveThread = true;
|
2010-12-01 16:35:50 +00:00
|
|
|
ret = virNetClientIO(client, call);
|
|
|
|
|
2012-06-08 12:21:00 +00:00
|
|
|
/* If queued, the call will be finished and freed later by another thread;
|
|
|
|
* we're done. */
|
|
|
|
if (ret == 1)
|
|
|
|
return 1;
|
|
|
|
|
2013-02-07 14:03:17 +00:00
|
|
|
virCondDestroy(&call->cond);
|
2012-06-08 12:21:00 +00:00
|
|
|
VIR_FREE(call);
|
2010-12-01 16:35:50 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2011-11-11 15:42:46 +00:00
|
|
|
|
2011-11-08 09:13:27 +00:00
|
|
|
|
2011-11-11 15:42:46 +00:00
|
|
|
/*
|
|
|
|
* @msg: a message allocated on heap or stack
|
|
|
|
*
|
|
|
|
* Send a message synchronously, and wait for the reply synchronously
|
|
|
|
*
|
|
|
|
* The caller is responsible for free'ing @msg if it was allocated
|
|
|
|
* on the heap
|
|
|
|
*
|
|
|
|
* Returns 0 on success, -1 on failure
|
|
|
|
*/
|
|
|
|
int virNetClientSendWithReply(virNetClientPtr client,
|
|
|
|
virNetMessagePtr msg)
|
|
|
|
{
|
2012-01-10 15:57:30 +00:00
|
|
|
int ret;
|
2013-01-09 21:27:28 +00:00
|
|
|
virObjectLock(client);
|
2012-01-10 15:57:30 +00:00
|
|
|
ret = virNetClientSendInternal(client, msg, true, false);
|
2013-01-09 21:27:28 +00:00
|
|
|
virObjectUnlock(client);
|
2011-11-11 15:42:46 +00:00
|
|
|
if (ret < 0)
|
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @msg: a message allocated on heap or stack
|
|
|
|
*
|
|
|
|
* Send a message synchronously, without any reply
|
|
|
|
*
|
|
|
|
* The caller is responsible for free'ing @msg if it was allocated
|
|
|
|
* on the heap
|
|
|
|
*
|
|
|
|
* Returns 0 on success, -1 on failure
|
|
|
|
*/
|
|
|
|
int virNetClientSendNoReply(virNetClientPtr client,
|
|
|
|
virNetMessagePtr msg)
|
|
|
|
{
|
2012-01-10 15:57:30 +00:00
|
|
|
int ret;
|
2013-01-09 21:27:28 +00:00
|
|
|
virObjectLock(client);
|
2012-01-10 15:57:30 +00:00
|
|
|
ret = virNetClientSendInternal(client, msg, false, false);
|
2013-01-09 21:27:28 +00:00
|
|
|
virObjectUnlock(client);
|
2011-11-11 15:42:46 +00:00
|
|
|
if (ret < 0)
|
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
2011-11-08 09:13:27 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* @msg: a message allocated on the heap.
|
|
|
|
*
|
|
|
|
* Send a message asynchronously, without any reply
|
|
|
|
*
|
|
|
|
* The caller is responsible for free'ing @msg, *except* if
|
2011-11-22 14:12:05 +00:00
|
|
|
* this method returns 1.
|
2011-11-08 09:13:27 +00:00
|
|
|
*
|
2012-06-08 13:24:06 +00:00
|
|
|
* Returns 1 if the message was queued and will be completed later (only
|
2014-03-17 09:38:38 +00:00
|
|
|
* for nonBlock == true), 0 if the message was completed and -1 on error.
|
2011-11-08 09:13:27 +00:00
|
|
|
*/
|
|
|
|
int virNetClientSendNonBlock(virNetClientPtr client,
|
|
|
|
virNetMessagePtr msg)
|
|
|
|
{
|
2012-01-10 15:57:30 +00:00
|
|
|
int ret;
|
2013-01-09 21:27:28 +00:00
|
|
|
virObjectLock(client);
|
2012-01-10 15:57:30 +00:00
|
|
|
ret = virNetClientSendInternal(client, msg, false, true);
|
2013-01-09 21:27:28 +00:00
|
|
|
virObjectUnlock(client);
|
2012-01-10 15:57:30 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @msg: a message allocated on heap or stack
|
|
|
|
*
|
|
|
|
* Send a message synchronously, and wait for the reply synchronously
|
|
|
|
*
|
|
|
|
* The caller is responsible for free'ing @msg if it was allocated
|
|
|
|
* on the heap
|
|
|
|
*
|
|
|
|
* Returns 0 on success, -1 on failure
|
|
|
|
*/
|
|
|
|
int virNetClientSendWithReplyStream(virNetClientPtr client,
|
|
|
|
virNetMessagePtr msg,
|
|
|
|
virNetClientStreamPtr st)
|
|
|
|
{
|
|
|
|
int ret;
|
2013-01-09 21:27:28 +00:00
|
|
|
virObjectLock(client);
|
2012-01-10 15:57:30 +00:00
|
|
|
/* Other thread might have already received
|
|
|
|
* stream EOF so we don't want sent anything.
|
|
|
|
* Server won't respond anyway.
|
|
|
|
*/
|
|
|
|
if (virNetClientStreamEOF(st)) {
|
2013-01-09 21:27:28 +00:00
|
|
|
virObjectUnlock(client);
|
2012-01-10 15:57:30 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = virNetClientSendInternal(client, msg, true, false);
|
2013-01-09 21:27:28 +00:00
|
|
|
virObjectUnlock(client);
|
2012-01-10 15:57:30 +00:00
|
|
|
if (ret < 0)
|
|
|
|
return -1;
|
|
|
|
return 0;
|
2011-11-08 09:13:27 +00:00
|
|
|
}
|