Example #1
0
/*
 * Process a finish handshake from the client.
 *
 * Returns a VIR_NET_OK confirmation if successful, or a VIR_NET_ERROR
 * if there was a stream error
 *
 * Returns 0 if successfully sent RPC reply, -1 upon fatal error
 */
static int
daemonStreamHandleFinish(virNetServerClientPtr client,
                         daemonClientStream *stream,
                         virNetMessagePtr msg)
{
    int ret;

    VIR_DEBUG("client=%p, stream=%p, proc=%d, serial=%d",
              client, stream, msg->header.proc, msg->header.serial);

    stream->closed = 1;
    virStreamEventRemoveCallback(stream->st);
    ret = virStreamFinish(stream->st);

    if (ret < 0) {
        virNetMessageError rerr;
        memset(&rerr, 0, sizeof(rerr));
        return virNetServerProgramSendReplyError(stream->prog,
                                                 client,
                                                 msg,
                                                 &rerr,
                                                 &msg->header);
    } else {
        /* Send zero-length confirm */
        return virNetServerProgramSendStreamData(stream->prog,
                                                 client,
                                                 msg,
                                                 stream->procedure,
                                                 stream->serial,
                                                 NULL, 0);
    }
}
Example #2
0
/*
 * Process an finish handshake from the client.
 *
 * Returns a REMOTE_OK confirmation if successful, or a REMOTE_ERROR
 * if there was a stream error
 *
 * Returns 0 if successfully sent RPC reply, -1 upon fatal error
 */
static int
remoteStreamHandleFinish(struct qemud_client *client,
                         struct qemud_client_stream *stream,
                         struct qemud_client_message *msg)
{
    remote_error rerr;
    int ret;

    VIR_DEBUG("stream=%p proc=%d serial=%d",
          stream, msg->hdr.proc, msg->hdr.serial);

    memset(&rerr, 0, sizeof rerr);

    stream->closed = 1;
    virStreamEventRemoveCallback(stream->st);
    ret = virStreamFinish(stream->st);

    if (ret < 0) {
        remoteDispatchError(&rerr);
        return remoteSerializeReplyError(client, &rerr, &msg->hdr);
    } else {
        /* Send zero-length confirm */
        if (remoteSendStreamData(client, stream, NULL, 0) < 0)
            return -1;
    }

    return 0;
}
// here be a compress func
void compressFunc(virConnectPtr conn)
{
	int fd;
	// открываем некомпрессированный файл
	// компрессуем
	
	virStreamPtr st = virStreamNew(conn, 0);
	fd = open("compressed_file", O_RDONLY);
// открываем компрессированный, считываем, передаём стрим - сенд по указателю на поток СТ, буф+смещение, до гот-смещение
	while (1) {
       		char buf[1024];
       		int got;
		got = read(fd, buf, 1024);
	       	if (got < 0) {
          	virStreamAbort(st);
          	break;
       		}
       	if (got == 0) {
       		virStreamFinish(st);
          	break;
       	}
       	int offset = 0;
       	while (offset < got) {
        	int sent;
          	sent = virStreamSend(st, buf+offset, got-offset);
          	if (sent < 0) {
             	virStreamAbort(st);
             	goto done;
        }
        offset += sent;
       }
   }
   if (virStreamFinish(st) < 0)
   	// обработчик ошибки
done:
   virStreamFree(st);
   close(fd);	
}
Result StorageVolControlThread::uploadStorageVol()
{
    Result result;
    result.name = QString("%1_%2").arg(task.srcConName).arg(currPoolName);
    QString name, path;
    name = task.object;
    path = task.args.path;
    //qDebug()<<path<<"upload";
    if (currStoragePool!=NULL) {
        virStoragePoolFree(currStoragePool);
        currStoragePool = NULL;
    };
    currStoragePool = virStoragePoolLookupByName(
                *task.srcConnPtr, currPoolName.toUtf8().data());
    QFile *f = new QFile(path);
    f->open(QIODevice::ReadOnly);

    bool uploaded = false;
    virStreamPtr stream = virStreamNew(*task.srcConnPtr, 0);
    unsigned long long offset = 0;
    unsigned long long length = f->size();
    // flags: extra flags; not used yet, so callers should always pass 0
    unsigned int flags = 0;
    virStorageVol *storageVol = virStorageVolLookupByName(
                currStoragePool, name.toUtf8().data());
    if ( storageVol!=NULL ) {
        int ret = virStorageVolUpload(
                    storageVol, stream, offset, length, flags);
        if ( ret<0 ) {
            result.err = sendConnErrors();
        } else {
            uploaded = true;
            length = 0;
            int got, saved, step;
            step = 0;
            char buf[BLOCK_SIZE];
            while ( 1 && keep_alive ) {
                got = f->read(buf, BLOCK_SIZE);
                if (got == 0) break;
                if ( got<0 ) {
                    QString msg = QString("ReadError after (%2): %1 bytes")
                            .arg(length).arg(step);
                    emit errorMsg( msg, number );
                    result.err = msg;
                } else {
                    saved = virStreamSend(stream, buf, got);
                    if (saved < 0) {
                        result.err = sendConnErrors();
                        uploaded = false;
                        break;
                    };
                    step++;
                    length += saved;
                    //qDebug()<<"got<>saved:length"<<got<<saved<<step<<length;
                };
            };
            virStreamFinish(stream);
        };
        virStorageVolFree(storageVol);
    } else
        result.err = sendConnErrors();
    if ( stream!=NULL ) virStreamFree(stream);
    f->close();
    delete f; f = 0;
    result.msg.append(
                QString("'<b>%1</b>' StorageVol %2 Uploaded from %3 (%4).")
                .arg(name).arg((uploaded)?"":"don't")
                .arg(path).arg(length));
    result.result = uploaded;
    return result;
}
Example #5
0
static bool
cmdVolDownload(vshControl *ctl, const vshCmd *cmd)
{
    const char *file = NULL;
    virStorageVolPtr vol = NULL;
    bool ret = false;
    int fd = -1;
    virStreamPtr st = NULL;
    const char *name = NULL;
    unsigned long long offset = 0, length = 0;
    bool created = false;

    if (!vshConnectionUsability(ctl, ctl->conn))
        return false;

    if (vshCommandOptULongLong(cmd, "offset", &offset) < 0) {
        vshError(ctl, _("Unable to parse integer"));
        return false;
    }

    if (vshCommandOptULongLong(cmd, "length", &length) < 0) {
        vshError(ctl, _("Unable to parse integer"));
        return false;
    }

    if (!(vol = vshCommandOptVol(ctl, cmd, "vol", "pool", &name)))
        return false;

    if (vshCommandOptString(cmd, "file", &file) < 0) {
        vshError(ctl, _("file must not be empty"));
        goto cleanup;
    }

    if ((fd = open(file, O_WRONLY|O_CREAT|O_EXCL, 0666)) < 0) {
        if (errno != EEXIST ||
            (fd = open(file, O_WRONLY|O_TRUNC, 0666)) < 0) {
            vshError(ctl, _("cannot create %s"), file);
            goto cleanup;
        }
    } else {
        created = true;
    }

    st = virStreamNew(ctl->conn, 0);
    if (virStorageVolDownload(vol, st, offset, length, 0) < 0) {
        vshError(ctl, _("cannot download from volume %s"), name);
        goto cleanup;
    }

    if (virStreamRecvAll(st, vshStreamSink, &fd) < 0) {
        vshError(ctl, _("cannot receive data from volume %s"), name);
        goto cleanup;
    }

    if (VIR_CLOSE(fd) < 0) {
        vshError(ctl, _("cannot close file %s"), file);
        virStreamAbort(st);
        goto cleanup;
    }

    if (virStreamFinish(st) < 0) {
        vshError(ctl, _("cannot close volume %s"), name);
        goto cleanup;
    }

    ret = true;

cleanup:
    VIR_FORCE_CLOSE(fd);
    if (!ret && created)
        unlink(file);
    if (vol)
        virStorageVolFree(vol);
    if (st)
        virStreamFree(st);
    return ret;
}
Example #6
0
static bool
cmdVolUpload(vshControl *ctl, const vshCmd *cmd)
{
    const char *file = NULL;
    virStorageVolPtr vol = NULL;
    bool ret = false;
    int fd = -1;
    virStreamPtr st = NULL;
    const char *name = NULL;
    unsigned long long offset = 0, length = 0;

    if (!vshConnectionUsability(ctl, ctl->conn))
        goto cleanup;

    if (vshCommandOptULongLong(cmd, "offset", &offset) < 0) {
        vshError(ctl, _("Unable to parse integer"));
        return false;
    }

    if (vshCommandOptULongLong(cmd, "length", &length) < 0) {
        vshError(ctl, _("Unable to parse integer"));
        return false;
    }

    if (!(vol = vshCommandOptVol(ctl, cmd, "vol", "pool", &name))) {
        return false;
    }

    if (vshCommandOptString(cmd, "file", &file) < 0) {
        vshError(ctl, _("file must not be empty"));
        goto cleanup;
    }

    if ((fd = open(file, O_RDONLY)) < 0) {
        vshError(ctl, _("cannot read %s"), file);
        goto cleanup;
    }

    st = virStreamNew(ctl->conn, 0);
    if (virStorageVolUpload(vol, st, offset, length, 0) < 0) {
        vshError(ctl, _("cannot upload to volume %s"), name);
        goto cleanup;
    }

    if (virStreamSendAll(st, cmdVolUploadSource, &fd) < 0) {
        vshError(ctl, _("cannot send data to volume %s"), name);
        goto cleanup;
    }

    if (VIR_CLOSE(fd) < 0) {
        vshError(ctl, _("cannot close file %s"), file);
        virStreamAbort(st);
        goto cleanup;
    }

    if (virStreamFinish(st) < 0) {
        vshError(ctl, _("cannot close volume %s"), name);
        goto cleanup;
    }

    ret = true;

cleanup:
    if (vol)
        virStorageVolFree(vol);
    if (st)
        virStreamFree(st);
    VIR_FORCE_CLOSE(fd);
    return ret;
}
Example #7
0
void Server::sendImage()
{
    //screenshot();

    QByteArray block;
    QDataStream out(&block, QIODevice::WriteOnly);
    out.setVersion(QDataStream::Qt_4_0);

    out << (quint32)0;
    /*******************************/
    virConnectPtr conn = NULL; /* the hypervisor connection */
    virDomainPtr dom = NULL; /* the domain to be screenshotted */
    virStreamPtr st = NULL;
    char *mimetype = NULL;

    conn = virConnectOpen("qemu:///system");
    if (conn == NULL) {
        fprintf(stderr, "Failed to connect to hypervisor\n");
        if(st != NULL)
            virStreamFree(st);

        if(mimetype !=NULL)
            free(mimetype);

        if (dom != NULL)
            virDomainFree(dom);

        if (conn != NULL)
            virConnectClose(conn);

        return;
    }

    /* Find the domain of the given id */
    dom = virDomainLookupByName(conn, "win7-1");
    if (dom == NULL) {
        fprintf(stderr, "Failed to connect to hypervisor\n");
        if(st != NULL)
            virStreamFree(st);

        if(mimetype !=NULL)
            free(mimetype);

        if (dom != NULL)
            virDomainFree(dom);

        if (conn != NULL)
            virConnectClose(conn);

        return;
    }

    st = virStreamNew(conn, 0);
    mimetype = virDomainScreenshot(dom, st, 0, 0);
    if(mimetype == NULL) {
        fprintf(stderr, "Failed in virDomainScreenshot funcation\n");
        if(st != NULL)
            virStreamFree(st);

        if(mimetype !=NULL)
            free(mimetype);

        if (dom != NULL)
            virDomainFree(dom);

        if (conn != NULL)
            virConnectClose(conn);

        return;
    }

    if(virStreamRecvAll(st, mysink, &out) < 0) {
        fprintf(stderr, "Failed in virStreamRecvAll funcation\n");
        if(st != NULL)
            virStreamFree(st);

        if(mimetype !=NULL)
            free(mimetype);

        if (dom != NULL)
            virDomainFree(dom);

        if (conn != NULL)
            virConnectClose(conn);

        return;
    }

    if (virStreamFinish(st) < 0) {
        fprintf(stderr, "Failed in virStreamFinish funcation\n");
        if(st != NULL)
            virStreamFree(st);

        if(mimetype !=NULL)
            free(mimetype);

        if (dom != NULL)
            virDomainFree(dom);

        if (conn != NULL)
            virConnectClose(conn);

        return;
    }

    if(st != NULL)
        virStreamFree(st);

    if(mimetype !=NULL)
        free(mimetype);

    if (dom != NULL)
        virDomainFree(dom);

    if (conn != NULL)
        virConnectClose(conn);
    /*******************************/

    out.device()->seek(0);
    out << (quint32)(block.size() - sizeof(quint32));

    QTcpSocket *clientConnection = tcpServer->nextPendingConnection();
    connect(clientConnection, SIGNAL(disconnected()),
            clientConnection, SLOT(deleteLater()));

    clientConnection->write(block);
    clientConnection->disconnectFromHost();

    QTextStream info(stdout);
    info << tr("has sended %1 bytes\n").arg(block.size() - 4);
}
Example #8
0
void Server::screenshot()
{
    virConnectPtr conn = NULL; /* the hypervisor connection */
    virDomainPtr dom = NULL; /* the domain to be screenshotted */
    virStreamPtr st = NULL;
    char *mimetype = NULL;
    FILE *fp = NULL;

    conn = virConnectOpen("qemu:///system");
    if (conn == NULL) {
        fprintf(stderr, "Failed to connect to hypervisor\n");

        if(st != NULL)
            virStreamFree(st);

        if(fp != NULL)
            fclose(fp);

        if(mimetype !=NULL)
            free(mimetype);

        if (dom != NULL)
            virDomainFree(dom);

        if (conn != NULL)
            virConnectClose(conn);

        return;
    }

    /* Find the domain of the given id */
    dom = virDomainLookupByName(conn, "win7-1");
    if (dom == NULL) {
        fprintf(stderr, "Failed to connect to hypervisor\n");

        if(st != NULL)
            virStreamFree(st);

        if(fp != NULL)
            fclose(fp);

        if(mimetype !=NULL)
            free(mimetype);

        if (dom != NULL)
            virDomainFree(dom);

        if (conn != NULL)
            virConnectClose(conn);

        return;
    }

    st = virStreamNew(conn, 0);
    mimetype = virDomainScreenshot(dom, st, 0, 0);
    if(mimetype == NULL) {
        fprintf(stderr, "Failed in virDomainScreenshot funcation\n");

        if(st != NULL)
            virStreamFree(st);

        if(fp != NULL)
            fclose(fp);

        if(mimetype !=NULL)
            free(mimetype);

        if (dom != NULL)
            virDomainFree(dom);

        if (conn != NULL)
            virConnectClose(conn);

        return;
    }

    fp = fopen("shot.ppm", "w");
    if(fp == NULL) {
        fprintf(stderr, "Failed in fopen funcation\n");

        if(st != NULL)
            virStreamFree(st);

        if(fp != NULL)
            fclose(fp);

        if(mimetype !=NULL)
            free(mimetype);

        if (dom != NULL)
            virDomainFree(dom);

        if (conn != NULL)
            virConnectClose(conn);

        return;
    }
    if(virStreamRecvAll(st, mysink, fp) < 0) {
        fprintf(stderr, "Failed in virStreamRecvAll funcation\n");

        if(st != NULL)
            virStreamFree(st);

        if(fp != NULL)
            fclose(fp);

        if(mimetype !=NULL)
            free(mimetype);

        if (dom != NULL)
            virDomainFree(dom);

        if (conn != NULL)
            virConnectClose(conn);

        return;
    }

    if (virStreamFinish(st) < 0) {
        fprintf(stderr, "Failed in virStreamFinish funcation\n");

        if(st != NULL)
            virStreamFree(st);

        if(fp != NULL)
            fclose(fp);

        if(mimetype !=NULL)
            free(mimetype);

        if (dom != NULL)
            virDomainFree(dom);

        if (conn != NULL)
            virConnectClose(conn);

        return;
    }

    if(st != NULL)
        virStreamFree(st);

    if(fp != NULL)
        fclose(fp);

    if(mimetype !=NULL)
        free(mimetype);

    if (dom != NULL)
        virDomainFree(dom);

    if (conn != NULL)
        virConnectClose(conn);
}