2010-09-22 18:32:21 +00:00
|
|
|
/*
|
2012-02-23 11:54:18 +00:00
|
|
|
* fdstream.c: generic streams impl for file descriptors
|
2010-09-22 18:32:21 +00:00
|
|
|
*
|
2014-02-20 00:32:19 +00:00
|
|
|
* Copyright (C) 2009-2012, 2014 Red Hat, Inc.
|
2010-09-22 18:32:21 +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-09-22 18:32:21 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/socket.h>
|
2011-02-22 12:05:20 +00:00
|
|
|
#include <sys/wait.h>
|
2010-09-22 18:32:21 +00:00
|
|
|
#if HAVE_SYS_UN_H
|
|
|
|
# include <sys/un.h>
|
|
|
|
#endif
|
|
|
|
#include <netinet/in.h>
|
2014-03-10 12:32:51 +00:00
|
|
|
#include <termios.h>
|
2010-09-22 18:32:21 +00:00
|
|
|
|
|
|
|
#include "fdstream.h"
|
2012-12-13 18:21:53 +00:00
|
|
|
#include "virerror.h"
|
2010-09-22 18:32:21 +00:00
|
|
|
#include "datatypes.h"
|
2012-12-12 17:59:27 +00:00
|
|
|
#include "virlog.h"
|
2012-12-12 18:06:53 +00:00
|
|
|
#include "viralloc.h"
|
2012-12-13 17:44:57 +00:00
|
|
|
#include "virutil.h"
|
2011-07-19 18:32:58 +00:00
|
|
|
#include "virfile.h"
|
2011-02-22 12:05:20 +00:00
|
|
|
#include "configmake.h"
|
2013-04-03 10:36:23 +00:00
|
|
|
#include "virstring.h"
|
2010-09-22 18:32:21 +00:00
|
|
|
|
|
|
|
#define VIR_FROM_THIS VIR_FROM_STREAMS
|
|
|
|
|
2014-02-28 12:16:17 +00:00
|
|
|
VIR_LOG_INIT("fdstream");
|
|
|
|
|
2010-09-22 18:32:21 +00:00
|
|
|
/* Tunnelled migration stream support */
|
|
|
|
struct virFDStreamData {
|
|
|
|
int fd;
|
2011-02-22 12:05:20 +00:00
|
|
|
int errfd;
|
|
|
|
virCommandPtr cmd;
|
|
|
|
unsigned long long offset;
|
|
|
|
unsigned long long length;
|
2010-09-22 18:32:21 +00:00
|
|
|
|
|
|
|
int watch;
|
2012-02-23 11:54:18 +00:00
|
|
|
int events; /* events the stream callback is subscribed for */
|
2011-06-28 14:44:22 +00:00
|
|
|
bool cbRemoved;
|
|
|
|
bool dispatching;
|
|
|
|
bool closed;
|
2010-09-22 18:32:21 +00:00
|
|
|
virStreamEventCallback cb;
|
|
|
|
void *opaque;
|
|
|
|
virFreeCallback ff;
|
|
|
|
|
2012-02-23 11:54:18 +00:00
|
|
|
/* don't call the abort callback more than once */
|
|
|
|
bool abortCallbackCalled;
|
|
|
|
bool abortCallbackDispatching;
|
|
|
|
|
2012-02-23 12:45:25 +00:00
|
|
|
/* internal callback, as the regular one (from generic streams) gets
|
|
|
|
* eaten up by the server stream driver */
|
|
|
|
virFDStreamInternalCloseCb icbCb;
|
|
|
|
virFDStreamInternalCloseCbFreeOpaque icbFreeOpaque;
|
|
|
|
void *icbOpaque;
|
|
|
|
|
2010-09-22 18:32:21 +00:00
|
|
|
virMutex lock;
|
|
|
|
};
|
|
|
|
|
2013-05-10 17:12:20 +00:00
|
|
|
|
2010-09-22 18:32:21 +00:00
|
|
|
static int virFDStreamRemoveCallback(virStreamPtr stream)
|
|
|
|
{
|
|
|
|
struct virFDStreamData *fdst = stream->privateData;
|
|
|
|
int ret = -1;
|
|
|
|
|
|
|
|
if (!fdst) {
|
2012-07-18 14:49:46 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
"%s", _("stream is not open"));
|
2010-09-22 18:32:21 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
virMutexLock(&fdst->lock);
|
|
|
|
if (fdst->watch == 0) {
|
2012-07-18 14:49:46 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
"%s", _("stream does not have a callback registered"));
|
2010-09-22 18:32:21 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
virEventRemoveHandle(fdst->watch);
|
|
|
|
if (fdst->dispatching)
|
2011-06-28 14:44:22 +00:00
|
|
|
fdst->cbRemoved = true;
|
2010-09-22 18:32:21 +00:00
|
|
|
else if (fdst->ff)
|
|
|
|
(fdst->ff)(fdst->opaque);
|
|
|
|
|
|
|
|
fdst->watch = 0;
|
|
|
|
fdst->ff = NULL;
|
|
|
|
fdst->cb = NULL;
|
2012-02-23 11:54:18 +00:00
|
|
|
fdst->events = 0;
|
2010-09-22 18:32:21 +00:00
|
|
|
fdst->opaque = NULL;
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
|
2014-03-25 06:57:22 +00:00
|
|
|
cleanup:
|
2010-09-22 18:32:21 +00:00
|
|
|
virMutexUnlock(&fdst->lock);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int virFDStreamUpdateCallback(virStreamPtr stream, int events)
|
|
|
|
{
|
|
|
|
struct virFDStreamData *fdst = stream->privateData;
|
|
|
|
int ret = -1;
|
|
|
|
|
|
|
|
if (!fdst) {
|
2012-07-18 14:49:46 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
"%s", _("stream is not open"));
|
2010-09-22 18:32:21 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
virMutexLock(&fdst->lock);
|
|
|
|
if (fdst->watch == 0) {
|
2012-07-18 14:49:46 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
"%s", _("stream does not have a callback registered"));
|
2010-09-22 18:32:21 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
virEventUpdateHandle(fdst->watch, events);
|
2012-02-23 11:54:18 +00:00
|
|
|
fdst->events = events;
|
2010-09-22 18:32:21 +00:00
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
|
2014-03-25 06:57:22 +00:00
|
|
|
cleanup:
|
2010-09-22 18:32:21 +00:00
|
|
|
virMutexUnlock(&fdst->lock);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void virFDStreamEvent(int watch ATTRIBUTE_UNUSED,
|
|
|
|
int fd ATTRIBUTE_UNUSED,
|
|
|
|
int events,
|
|
|
|
void *opaque)
|
|
|
|
{
|
|
|
|
virStreamPtr stream = opaque;
|
|
|
|
struct virFDStreamData *fdst = stream->privateData;
|
|
|
|
virStreamEventCallback cb;
|
|
|
|
void *cbopaque;
|
|
|
|
virFreeCallback ff;
|
2011-06-28 14:44:22 +00:00
|
|
|
bool closed;
|
2010-09-22 18:32:21 +00:00
|
|
|
|
|
|
|
if (!fdst)
|
|
|
|
return;
|
|
|
|
|
|
|
|
virMutexLock(&fdst->lock);
|
|
|
|
if (!fdst->cb) {
|
|
|
|
virMutexUnlock(&fdst->lock);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
cb = fdst->cb;
|
|
|
|
cbopaque = fdst->opaque;
|
|
|
|
ff = fdst->ff;
|
2011-06-28 14:44:22 +00:00
|
|
|
fdst->dispatching = true;
|
2010-09-22 18:32:21 +00:00
|
|
|
virMutexUnlock(&fdst->lock);
|
|
|
|
|
|
|
|
cb(stream, events, cbopaque);
|
|
|
|
|
|
|
|
virMutexLock(&fdst->lock);
|
2011-06-28 14:44:22 +00:00
|
|
|
fdst->dispatching = false;
|
2010-09-22 18:32:21 +00:00
|
|
|
if (fdst->cbRemoved && ff)
|
|
|
|
(ff)(cbopaque);
|
2011-06-28 14:44:22 +00:00
|
|
|
closed = fdst->closed;
|
2010-09-22 18:32:21 +00:00
|
|
|
virMutexUnlock(&fdst->lock);
|
2011-06-28 14:44:22 +00:00
|
|
|
|
|
|
|
if (closed) {
|
|
|
|
virMutexDestroy(&fdst->lock);
|
|
|
|
VIR_FREE(fdst);
|
|
|
|
}
|
2010-09-22 18:32:21 +00:00
|
|
|
}
|
|
|
|
|
2011-06-30 12:12:56 +00:00
|
|
|
static void virFDStreamCallbackFree(void *opaque)
|
|
|
|
{
|
|
|
|
virStreamPtr st = opaque;
|
|
|
|
virStreamFree(st);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-09-22 18:32:21 +00:00
|
|
|
static int
|
|
|
|
virFDStreamAddCallback(virStreamPtr st,
|
|
|
|
int events,
|
|
|
|
virStreamEventCallback cb,
|
|
|
|
void *opaque,
|
|
|
|
virFreeCallback ff)
|
|
|
|
{
|
|
|
|
struct virFDStreamData *fdst = st->privateData;
|
|
|
|
int ret = -1;
|
|
|
|
|
|
|
|
if (!fdst) {
|
2012-07-18 14:49:46 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
"%s", _("stream is not open"));
|
2010-09-22 18:32:21 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
virMutexLock(&fdst->lock);
|
|
|
|
if (fdst->watch != 0) {
|
2012-07-18 14:49:46 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
"%s", _("stream already has a callback registered"));
|
2010-09-22 18:32:21 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((fdst->watch = virEventAddHandle(fdst->fd,
|
|
|
|
events,
|
|
|
|
virFDStreamEvent,
|
|
|
|
st,
|
2011-06-30 12:12:56 +00:00
|
|
|
virFDStreamCallbackFree)) < 0) {
|
2012-07-18 14:49:46 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
"%s", _("cannot register file watch on stream"));
|
2010-09-22 18:32:21 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2011-06-28 14:44:22 +00:00
|
|
|
fdst->cbRemoved = false;
|
2010-09-22 18:32:21 +00:00
|
|
|
fdst->cb = cb;
|
|
|
|
fdst->opaque = opaque;
|
|
|
|
fdst->ff = ff;
|
2012-02-23 11:54:18 +00:00
|
|
|
fdst->events = events;
|
|
|
|
fdst->abortCallbackCalled = false;
|
2010-09-22 18:32:21 +00:00
|
|
|
virStreamRef(st);
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
|
2014-03-25 06:57:22 +00:00
|
|
|
cleanup:
|
2010-09-22 18:32:21 +00:00
|
|
|
virMutexUnlock(&fdst->lock);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-05-13 19:32:00 +00:00
|
|
|
|
|
|
|
static int
|
2012-02-23 11:54:18 +00:00
|
|
|
virFDStreamCloseInt(virStreamPtr st, bool streamAbort)
|
2010-09-22 18:32:21 +00:00
|
|
|
{
|
2012-02-23 11:54:18 +00:00
|
|
|
struct virFDStreamData *fdst;
|
|
|
|
virStreamEventCallback cb;
|
|
|
|
void *opaque;
|
2010-09-22 18:32:21 +00:00
|
|
|
int ret;
|
2011-05-13 19:32:00 +00:00
|
|
|
|
|
|
|
VIR_DEBUG("st=%p", st);
|
|
|
|
|
2012-02-23 11:54:18 +00:00
|
|
|
if (!st || !(fdst = st->privateData) || fdst->abortCallbackDispatching)
|
2011-05-13 19:32:00 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
virMutexLock(&fdst->lock);
|
|
|
|
|
2012-02-23 11:54:18 +00:00
|
|
|
/* aborting the stream, ensure the callback is called if it's
|
|
|
|
* registered for stream error event */
|
|
|
|
if (streamAbort &&
|
|
|
|
fdst->cb &&
|
|
|
|
(fdst->events & (VIR_STREAM_EVENT_READABLE |
|
|
|
|
VIR_STREAM_EVENT_WRITABLE))) {
|
|
|
|
/* don't enter this function accidentally from the callback again */
|
|
|
|
if (fdst->abortCallbackCalled) {
|
|
|
|
virMutexUnlock(&fdst->lock);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
fdst->abortCallbackCalled = true;
|
|
|
|
fdst->abortCallbackDispatching = true;
|
|
|
|
|
|
|
|
/* cache the pointers */
|
|
|
|
cb = fdst->cb;
|
|
|
|
opaque = fdst->opaque;
|
|
|
|
virMutexUnlock(&fdst->lock);
|
|
|
|
|
|
|
|
/* call failure callback, poll reports nothing on closed fd */
|
|
|
|
(cb)(st, VIR_STREAM_EVENT_ERROR, opaque);
|
|
|
|
|
|
|
|
virMutexLock(&fdst->lock);
|
|
|
|
fdst->abortCallbackDispatching = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* mutex locked */
|
2010-09-22 18:32:21 +00:00
|
|
|
ret = VIR_CLOSE(fdst->fd);
|
2011-02-22 12:05:20 +00:00
|
|
|
if (fdst->cmd) {
|
|
|
|
char buf[1024];
|
|
|
|
ssize_t len;
|
|
|
|
int status;
|
|
|
|
if ((len = saferead(fdst->errfd, buf, sizeof(buf)-1)) < 0)
|
|
|
|
buf[0] = '\0';
|
|
|
|
else
|
|
|
|
buf[len] = '\0';
|
|
|
|
|
2014-02-20 00:32:19 +00:00
|
|
|
virCommandRawStatus(fdst->cmd);
|
2011-02-22 12:05:20 +00:00
|
|
|
if (virCommandWait(fdst->cmd, &status) < 0) {
|
|
|
|
ret = -1;
|
|
|
|
} else if (status != 0) {
|
|
|
|
if (buf[0] == '\0') {
|
|
|
|
if (WIFEXITED(status)) {
|
2012-07-18 14:49:46 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("I/O helper exited with status %d"),
|
|
|
|
WEXITSTATUS(status));
|
2011-02-22 12:05:20 +00:00
|
|
|
} else {
|
2012-07-18 14:49:46 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("I/O helper exited abnormally"));
|
2011-02-22 12:05:20 +00:00
|
|
|
}
|
|
|
|
} else {
|
2012-07-18 14:49:46 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
buf);
|
2011-02-22 12:05:20 +00:00
|
|
|
}
|
|
|
|
ret = -1;
|
|
|
|
}
|
|
|
|
virCommandFree(fdst->cmd);
|
2011-06-28 14:44:22 +00:00
|
|
|
fdst->cmd = NULL;
|
2011-02-22 12:05:20 +00:00
|
|
|
}
|
2011-10-13 11:47:58 +00:00
|
|
|
|
|
|
|
if (VIR_CLOSE(fdst->errfd) < 0)
|
|
|
|
VIR_DEBUG("ignoring failed close on fd %d", fdst->errfd);
|
|
|
|
|
2010-09-22 18:32:21 +00:00
|
|
|
st->privateData = NULL;
|
|
|
|
|
2012-02-23 12:45:25 +00:00
|
|
|
/* call the internal stream closing callback */
|
|
|
|
if (fdst->icbCb) {
|
|
|
|
/* the mutex is not accessible anymore, as private data is null */
|
|
|
|
(fdst->icbCb)(st, fdst->icbOpaque);
|
|
|
|
if (fdst->icbFreeOpaque)
|
|
|
|
(fdst->icbFreeOpaque)(fdst->icbOpaque);
|
|
|
|
}
|
|
|
|
|
2011-06-28 14:44:22 +00:00
|
|
|
if (fdst->dispatching) {
|
|
|
|
fdst->closed = true;
|
|
|
|
virMutexUnlock(&fdst->lock);
|
|
|
|
} else {
|
|
|
|
virMutexUnlock(&fdst->lock);
|
|
|
|
virMutexDestroy(&fdst->lock);
|
|
|
|
VIR_FREE(fdst);
|
|
|
|
}
|
2010-09-22 18:32:21 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-02-23 11:54:18 +00:00
|
|
|
static int
|
|
|
|
virFDStreamClose(virStreamPtr st)
|
|
|
|
{
|
|
|
|
return virFDStreamCloseInt(st, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
virFDStreamAbort(virStreamPtr st)
|
|
|
|
{
|
|
|
|
return virFDStreamCloseInt(st, true);
|
|
|
|
}
|
|
|
|
|
2010-09-22 18:32:21 +00:00
|
|
|
static int virFDStreamWrite(virStreamPtr st, const char *bytes, size_t nbytes)
|
|
|
|
{
|
|
|
|
struct virFDStreamData *fdst = st->privateData;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (nbytes > INT_MAX) {
|
|
|
|
virReportSystemError(ERANGE, "%s",
|
|
|
|
_("Too many bytes to write to stream"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!fdst) {
|
2012-07-18 14:49:46 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
"%s", _("stream is not open"));
|
2010-09-22 18:32:21 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
virMutexLock(&fdst->lock);
|
|
|
|
|
2011-02-22 12:05:20 +00:00
|
|
|
if (fdst->length) {
|
|
|
|
if (fdst->length == fdst->offset) {
|
|
|
|
virReportSystemError(ENOSPC, "%s",
|
|
|
|
_("cannot write to stream"));
|
|
|
|
virMutexUnlock(&fdst->lock);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((fdst->length - fdst->offset) < nbytes)
|
|
|
|
nbytes = fdst->length - fdst->offset;
|
|
|
|
}
|
|
|
|
|
2014-03-25 06:57:22 +00:00
|
|
|
retry:
|
2010-09-22 18:32:21 +00:00
|
|
|
ret = write(fdst->fd, bytes, nbytes);
|
|
|
|
if (ret < 0) {
|
|
|
|
if (errno == EAGAIN || errno == EWOULDBLOCK) {
|
|
|
|
ret = -2;
|
|
|
|
} else if (errno == EINTR) {
|
|
|
|
goto retry;
|
|
|
|
} else {
|
|
|
|
ret = -1;
|
|
|
|
virReportSystemError(errno, "%s",
|
|
|
|
_("cannot write to stream"));
|
|
|
|
}
|
2011-02-22 12:05:20 +00:00
|
|
|
} else if (fdst->length) {
|
|
|
|
fdst->offset += ret;
|
2010-09-22 18:32:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virMutexUnlock(&fdst->lock);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int virFDStreamRead(virStreamPtr st, char *bytes, size_t nbytes)
|
|
|
|
{
|
|
|
|
struct virFDStreamData *fdst = st->privateData;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (nbytes > INT_MAX) {
|
|
|
|
virReportSystemError(ERANGE, "%s",
|
|
|
|
_("Too many bytes to read from stream"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!fdst) {
|
2012-07-18 14:49:46 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
"%s", _("stream is not open"));
|
2010-09-22 18:32:21 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
virMutexLock(&fdst->lock);
|
|
|
|
|
2011-02-22 12:05:20 +00:00
|
|
|
if (fdst->length) {
|
|
|
|
if (fdst->length == fdst->offset) {
|
|
|
|
virMutexUnlock(&fdst->lock);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((fdst->length - fdst->offset) < nbytes)
|
|
|
|
nbytes = fdst->length - fdst->offset;
|
|
|
|
}
|
|
|
|
|
2014-03-25 06:57:22 +00:00
|
|
|
retry:
|
2010-09-22 18:32:21 +00:00
|
|
|
ret = read(fdst->fd, bytes, nbytes);
|
|
|
|
if (ret < 0) {
|
|
|
|
if (errno == EAGAIN || errno == EWOULDBLOCK) {
|
|
|
|
ret = -2;
|
|
|
|
} else if (errno == EINTR) {
|
|
|
|
goto retry;
|
|
|
|
} else {
|
|
|
|
ret = -1;
|
|
|
|
virReportSystemError(errno, "%s",
|
|
|
|
_("cannot read from stream"));
|
|
|
|
}
|
2011-02-22 12:05:20 +00:00
|
|
|
} else if (fdst->length) {
|
|
|
|
fdst->offset += ret;
|
2010-09-22 18:32:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virMutexUnlock(&fdst->lock);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static virStreamDriver virFDStreamDrv = {
|
|
|
|
.streamSend = virFDStreamWrite,
|
|
|
|
.streamRecv = virFDStreamRead,
|
|
|
|
.streamFinish = virFDStreamClose,
|
2012-02-23 11:54:18 +00:00
|
|
|
.streamAbort = virFDStreamAbort,
|
2013-04-22 17:26:01 +00:00
|
|
|
.streamEventAddCallback = virFDStreamAddCallback,
|
|
|
|
.streamEventUpdateCallback = virFDStreamUpdateCallback,
|
|
|
|
.streamEventRemoveCallback = virFDStreamRemoveCallback
|
2010-09-22 18:32:21 +00:00
|
|
|
};
|
|
|
|
|
2011-02-22 12:05:20 +00:00
|
|
|
static int virFDStreamOpenInternal(virStreamPtr st,
|
|
|
|
int fd,
|
|
|
|
virCommandPtr cmd,
|
|
|
|
int errfd,
|
|
|
|
unsigned long long length)
|
2010-09-22 18:32:21 +00:00
|
|
|
{
|
|
|
|
struct virFDStreamData *fdst;
|
|
|
|
|
2011-02-22 12:05:20 +00:00
|
|
|
VIR_DEBUG("st=%p fd=%d cmd=%p errfd=%d length=%llu",
|
|
|
|
st, fd, cmd, errfd, length);
|
|
|
|
|
2010-09-22 18:32:21 +00:00
|
|
|
if ((st->flags & VIR_STREAM_NONBLOCK) &&
|
2014-08-15 04:55:15 +00:00
|
|
|
virSetNonBlock(fd) < 0) {
|
|
|
|
virReportSystemError(errno, "%s", _("Unable to set non-blocking mode"));
|
2010-09-22 18:32:21 +00:00
|
|
|
return -1;
|
2014-08-15 04:55:15 +00:00
|
|
|
}
|
2010-09-22 18:32:21 +00:00
|
|
|
|
2013-07-04 10:20:00 +00:00
|
|
|
if (VIR_ALLOC(fdst) < 0)
|
2010-09-22 18:32:21 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
fdst->fd = fd;
|
2011-02-22 12:05:20 +00:00
|
|
|
fdst->cmd = cmd;
|
|
|
|
fdst->errfd = errfd;
|
|
|
|
fdst->length = length;
|
2010-09-22 18:32:21 +00:00
|
|
|
if (virMutexInit(&fdst->lock) < 0) {
|
|
|
|
VIR_FREE(fdst);
|
2012-07-18 14:49:46 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("Unable to initialize mutex"));
|
2010-09-22 18:32:21 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
st->driver = &virFDStreamDrv;
|
|
|
|
st->privateData = fdst;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-02-22 12:05:20 +00:00
|
|
|
int virFDStreamOpen(virStreamPtr st,
|
|
|
|
int fd)
|
|
|
|
{
|
|
|
|
return virFDStreamOpenInternal(st, fd, NULL, -1, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-09-22 18:32:21 +00:00
|
|
|
#if HAVE_SYS_UN_H
|
|
|
|
int virFDStreamConnectUNIX(virStreamPtr st,
|
|
|
|
const char *path,
|
|
|
|
bool abstract)
|
|
|
|
{
|
|
|
|
struct sockaddr_un sa;
|
Convert 'int i' to 'size_t i' in src/ 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 = 0;
|
2010-09-22 18:32:21 +00:00
|
|
|
int timeout = 3;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
int fd = socket(AF_UNIX, SOCK_STREAM, 0);
|
|
|
|
if (fd < 0) {
|
|
|
|
virReportSystemError(errno, "%s", _("Unable to open UNIX socket"));
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(&sa, 0, sizeof(sa));
|
|
|
|
sa.sun_family = AF_UNIX;
|
|
|
|
if (abstract) {
|
|
|
|
if (virStrcpy(sa.sun_path+1, path, sizeof(sa.sun_path)-1) == NULL)
|
|
|
|
goto error;
|
|
|
|
sa.sun_path[0] = '\0';
|
|
|
|
} else {
|
|
|
|
if (virStrcpy(sa.sun_path, path, sizeof(sa.sun_path)) == NULL)
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
|
|
|
ret = connect(fd, (struct sockaddr *)&sa, sizeof(sa));
|
|
|
|
if (ret == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (errno == ENOENT || errno == ECONNREFUSED) {
|
|
|
|
/* ENOENT : Socket may not have shown up yet
|
|
|
|
* ECONNREFUSED : Leftover socket hasn't been removed yet */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
goto error;
|
|
|
|
} while ((++i <= timeout*5) && (usleep(.2 * 1000000) <= 0));
|
|
|
|
|
2011-02-22 12:05:20 +00:00
|
|
|
if (virFDStreamOpenInternal(st, fd, NULL, -1, 0) < 0)
|
2010-09-22 18:32:21 +00:00
|
|
|
goto error;
|
|
|
|
return 0;
|
|
|
|
|
2014-03-25 06:57:22 +00:00
|
|
|
error:
|
2010-09-22 18:32:21 +00:00
|
|
|
VIR_FORCE_CLOSE(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
int virFDStreamConnectUNIX(virStreamPtr st ATTRIBUTE_UNUSED,
|
|
|
|
const char *path ATTRIBUTE_UNUSED,
|
|
|
|
bool abstract ATTRIBUTE_UNUSED)
|
|
|
|
{
|
|
|
|
virReportSystemError(ENOSYS, "%s",
|
|
|
|
_("UNIX domain sockets are not supported on this platform"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-02-22 12:05:20 +00:00
|
|
|
static int
|
|
|
|
virFDStreamOpenFileInternal(virStreamPtr st,
|
|
|
|
const char *path,
|
|
|
|
unsigned long long offset,
|
|
|
|
unsigned long long length,
|
2011-07-07 17:57:43 +00:00
|
|
|
int oflags,
|
2014-08-15 08:11:35 +00:00
|
|
|
int mode,
|
|
|
|
bool forceIOHelper)
|
2010-09-22 18:32:21 +00:00
|
|
|
{
|
2011-02-22 12:05:20 +00:00
|
|
|
int fd = -1;
|
2012-05-30 15:20:37 +00:00
|
|
|
int childfd = -1;
|
2010-09-22 18:32:21 +00:00
|
|
|
struct stat sb;
|
2011-02-22 12:05:20 +00:00
|
|
|
virCommandPtr cmd = NULL;
|
|
|
|
int errfd = -1;
|
2014-03-25 08:23:15 +00:00
|
|
|
char *iohelper_path = NULL;
|
2010-09-22 18:32:21 +00:00
|
|
|
|
2011-08-02 17:19:53 +00:00
|
|
|
VIR_DEBUG("st=%p path=%s oflags=%x offset=%llu length=%llu mode=%o",
|
|
|
|
st, path, oflags, offset, length, mode);
|
2010-09-22 18:32:21 +00:00
|
|
|
|
2013-09-24 09:57:38 +00:00
|
|
|
oflags |= O_NOCTTY | O_BINARY;
|
2013-03-13 17:22:28 +00:00
|
|
|
|
2011-07-07 17:57:43 +00:00
|
|
|
if (oflags & O_CREAT)
|
|
|
|
fd = open(path, oflags, mode);
|
2011-02-22 12:05:20 +00:00
|
|
|
else
|
2011-07-07 17:57:43 +00:00
|
|
|
fd = open(path, oflags);
|
2011-02-22 12:05:20 +00:00
|
|
|
if (fd < 0) {
|
2010-09-22 18:32:21 +00:00
|
|
|
virReportSystemError(errno,
|
|
|
|
_("Unable to open stream for '%s'"),
|
|
|
|
path);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fstat(fd, &sb) < 0) {
|
|
|
|
virReportSystemError(errno,
|
|
|
|
_("Unable to access stream for '%s'"),
|
|
|
|
path);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2011-07-12 15:17:52 +00:00
|
|
|
if (offset &&
|
|
|
|
lseek(fd, offset, SEEK_SET) != offset) {
|
|
|
|
virReportSystemError(errno,
|
|
|
|
_("Unable to seek %s to %llu"),
|
|
|
|
path, offset);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2010-09-22 18:32:21 +00:00
|
|
|
/* Thanks to the POSIX i/o model, we can't reliably get
|
|
|
|
* non-blocking I/O on block devs/regular files. To
|
2011-07-12 15:17:52 +00:00
|
|
|
* support those we need to fork a helper process to do
|
2010-09-22 18:32:21 +00:00
|
|
|
* the I/O so we just have a fifo. Or use AIO :-(
|
|
|
|
*/
|
|
|
|
if ((st->flags & VIR_STREAM_NONBLOCK) &&
|
2014-08-15 08:11:35 +00:00
|
|
|
((!S_ISCHR(sb.st_mode) &&
|
|
|
|
!S_ISFIFO(sb.st_mode)) || forceIOHelper)) {
|
2012-05-30 15:20:37 +00:00
|
|
|
int fds[2] = { -1, -1 };
|
2011-02-22 12:05:20 +00:00
|
|
|
|
2011-07-12 15:17:52 +00:00
|
|
|
if ((oflags & O_ACCMODE) == O_RDWR) {
|
2012-07-18 14:49:46 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("%s: Cannot request read and write flags together"),
|
|
|
|
path);
|
2011-02-22 12:05:20 +00:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pipe(fds) < 0) {
|
|
|
|
virReportSystemError(errno, "%s",
|
|
|
|
_("Unable to create pipe"));
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2014-03-25 08:23:15 +00:00
|
|
|
if (!(iohelper_path = virFileFindResource("libvirt_iohelper",
|
|
|
|
"src",
|
|
|
|
LIBEXECDIR)))
|
|
|
|
goto error;
|
|
|
|
|
2013-05-10 17:12:20 +00:00
|
|
|
cmd = virCommandNewArgList(iohelper_path,
|
2011-02-22 12:05:20 +00:00
|
|
|
path,
|
|
|
|
NULL);
|
2014-03-25 08:23:15 +00:00
|
|
|
|
|
|
|
VIR_FREE(iohelper_path);
|
|
|
|
|
2011-02-22 12:05:20 +00:00
|
|
|
virCommandAddArgFormat(cmd, "%llu", length);
|
2013-07-11 10:31:56 +00:00
|
|
|
virCommandPassFD(cmd, fd,
|
|
|
|
VIR_COMMAND_PASS_FD_CLOSE_PARENT);
|
2011-07-12 15:17:52 +00:00
|
|
|
virCommandAddArgFormat(cmd, "%d", fd);
|
2011-02-22 12:05:20 +00:00
|
|
|
|
2013-05-10 13:45:05 +00:00
|
|
|
if ((oflags & O_ACCMODE) == O_RDONLY) {
|
2011-02-22 12:05:20 +00:00
|
|
|
childfd = fds[1];
|
|
|
|
fd = fds[0];
|
|
|
|
virCommandSetOutputFD(cmd, &childfd);
|
|
|
|
} else {
|
|
|
|
childfd = fds[0];
|
|
|
|
fd = fds[1];
|
|
|
|
virCommandSetInputFD(cmd, childfd);
|
|
|
|
}
|
|
|
|
virCommandSetErrorFD(cmd, &errfd);
|
|
|
|
|
2011-07-12 18:07:01 +00:00
|
|
|
if (virCommandRunAsync(cmd, NULL) < 0)
|
2011-02-22 12:05:20 +00:00
|
|
|
goto error;
|
|
|
|
|
|
|
|
VIR_FORCE_CLOSE(childfd);
|
2010-09-22 18:32:21 +00:00
|
|
|
}
|
|
|
|
|
2011-02-22 12:05:20 +00:00
|
|
|
if (virFDStreamOpenInternal(st, fd, cmd, errfd, length) < 0)
|
2010-09-22 18:32:21 +00:00
|
|
|
goto error;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
2014-03-25 06:57:22 +00:00
|
|
|
error:
|
2011-02-22 12:05:20 +00:00
|
|
|
virCommandFree(cmd);
|
2011-01-28 21:22:39 +00:00
|
|
|
VIR_FORCE_CLOSE(fd);
|
2012-05-30 15:20:37 +00:00
|
|
|
VIR_FORCE_CLOSE(childfd);
|
2012-05-30 09:20:46 +00:00
|
|
|
VIR_FORCE_CLOSE(errfd);
|
2014-03-25 08:23:15 +00:00
|
|
|
VIR_FREE(iohelper_path);
|
2011-08-02 17:19:53 +00:00
|
|
|
if (oflags & O_CREAT)
|
|
|
|
unlink(path);
|
2010-09-22 18:32:21 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-02-22 12:05:20 +00:00
|
|
|
int virFDStreamOpenFile(virStreamPtr st,
|
|
|
|
const char *path,
|
|
|
|
unsigned long long offset,
|
|
|
|
unsigned long long length,
|
2011-08-02 17:19:53 +00:00
|
|
|
int oflags)
|
2010-09-22 18:32:21 +00:00
|
|
|
{
|
2011-07-07 17:57:43 +00:00
|
|
|
if (oflags & O_CREAT) {
|
2012-07-18 14:49:46 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Attempt to create %s without specifying mode"),
|
|
|
|
path);
|
2011-02-22 12:05:20 +00:00
|
|
|
return -1;
|
2010-09-22 18:32:21 +00:00
|
|
|
}
|
2011-02-22 12:05:20 +00:00
|
|
|
return virFDStreamOpenFileInternal(st, path,
|
|
|
|
offset, length,
|
2014-08-15 08:11:35 +00:00
|
|
|
oflags, 0, false);
|
2011-02-22 12:05:20 +00:00
|
|
|
}
|
2010-09-22 18:32:21 +00:00
|
|
|
|
2011-02-22 12:05:20 +00:00
|
|
|
int virFDStreamCreateFile(virStreamPtr st,
|
|
|
|
const char *path,
|
|
|
|
unsigned long long offset,
|
|
|
|
unsigned long long length,
|
2011-07-07 17:57:43 +00:00
|
|
|
int oflags,
|
2011-08-02 17:19:53 +00:00
|
|
|
mode_t mode)
|
2011-02-22 12:05:20 +00:00
|
|
|
{
|
|
|
|
return virFDStreamOpenFileInternal(st, path,
|
|
|
|
offset, length,
|
2014-08-15 08:11:35 +00:00
|
|
|
oflags | O_CREAT, mode,
|
|
|
|
false);
|
2010-09-22 18:32:21 +00:00
|
|
|
}
|
2012-02-23 12:45:25 +00:00
|
|
|
|
2014-03-10 12:32:51 +00:00
|
|
|
#ifdef HAVE_CFMAKERAW
|
|
|
|
int virFDStreamOpenPTY(virStreamPtr st,
|
|
|
|
const char *path,
|
|
|
|
unsigned long long offset,
|
|
|
|
unsigned long long length,
|
|
|
|
int oflags)
|
|
|
|
{
|
|
|
|
struct virFDStreamData *fdst = NULL;
|
|
|
|
struct termios rawattr;
|
|
|
|
|
|
|
|
if (virFDStreamOpenFileInternal(st, path,
|
|
|
|
offset, length,
|
2014-08-15 08:11:35 +00:00
|
|
|
oflags | O_CREAT, 0,
|
|
|
|
false) < 0)
|
2014-03-10 12:32:51 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
fdst = st->privateData;
|
|
|
|
|
|
|
|
if (tcgetattr(fdst->fd, &rawattr) < 0) {
|
|
|
|
virReportSystemError(errno,
|
|
|
|
_("unable to get tty attributes: %s"),
|
|
|
|
path);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
cfmakeraw(&rawattr);
|
|
|
|
|
|
|
|
if (tcsetattr(fdst->fd, TCSANOW, &rawattr) < 0) {
|
|
|
|
virReportSystemError(errno,
|
|
|
|
_("unable to set tty attributes: %s"),
|
|
|
|
path);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
2014-03-25 06:57:22 +00:00
|
|
|
cleanup:
|
2014-03-10 12:32:51 +00:00
|
|
|
virFDStreamClose(st);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#else /* !HAVE_CFMAKERAW */
|
|
|
|
int virFDStreamOpenPTY(virStreamPtr st,
|
|
|
|
const char *path,
|
|
|
|
unsigned long long offset,
|
|
|
|
unsigned long long length,
|
|
|
|
int oflags)
|
|
|
|
{
|
|
|
|
return virFDStreamOpenFileInternal(st, path,
|
|
|
|
offset, length,
|
2014-08-25 07:44:32 +00:00
|
|
|
oflags | O_CREAT, 0,
|
|
|
|
false);
|
2014-03-10 12:32:51 +00:00
|
|
|
}
|
|
|
|
#endif /* !HAVE_CFMAKERAW */
|
|
|
|
|
2014-08-15 08:11:35 +00:00
|
|
|
int virFDStreamOpenBlockDevice(virStreamPtr st,
|
|
|
|
const char *path,
|
|
|
|
unsigned long long offset,
|
|
|
|
unsigned long long length,
|
|
|
|
int oflags)
|
|
|
|
{
|
|
|
|
return virFDStreamOpenFileInternal(st, path,
|
|
|
|
offset, length,
|
|
|
|
oflags, 0, true);
|
|
|
|
}
|
|
|
|
|
2012-02-23 12:45:25 +00:00
|
|
|
int virFDStreamSetInternalCloseCb(virStreamPtr st,
|
|
|
|
virFDStreamInternalCloseCb cb,
|
|
|
|
void *opaque,
|
|
|
|
virFDStreamInternalCloseCbFreeOpaque fcb)
|
|
|
|
{
|
|
|
|
struct virFDStreamData *fdst = st->privateData;
|
|
|
|
|
|
|
|
virMutexLock(&fdst->lock);
|
|
|
|
|
|
|
|
if (fdst->icbFreeOpaque)
|
|
|
|
(fdst->icbFreeOpaque)(fdst->icbOpaque);
|
|
|
|
|
|
|
|
fdst->icbCb = cb;
|
|
|
|
fdst->icbOpaque = opaque;
|
|
|
|
fdst->icbFreeOpaque = fcb;
|
|
|
|
|
|
|
|
virMutexUnlock(&fdst->lock);
|
|
|
|
return 0;
|
|
|
|
}
|