コード例 #1
0
ファイル: virnetdaemon.c プロジェクト: carriercomm/libvirt
virJSONValuePtr
virNetDaemonPreExecRestart(virNetDaemonPtr dmn)
{
    virJSONValuePtr object, srvArray = NULL;
    size_t i;

    virObjectLock(dmn);

    if (!(object = virJSONValueNewObject()))
        goto error;

    if (!(srvArray = virJSONValueNewArray()) ||
        virJSONValueObjectAppend(object, "servers", srvArray) < 0)
        goto error;

    for (i = 0; i < dmn->nservers; i++) {
        virJSONValuePtr srvJSON = NULL;
        srvJSON = virNetServerPreExecRestart(dmn->servers[i]);

        if (!srvJSON)
            goto error;

        if (virJSONValueArrayAppend(srvArray, srvJSON) < 0) {
            virJSONValueFree(srvJSON);
            goto error;
        }
    }

    virObjectUnlock(dmn);

    return object;

 error:
    virJSONValueFree(object);
    virJSONValueFree(srvArray);
    virObjectUnlock(dmn);
    return NULL;
}
コード例 #2
0
static char *testGenerateJSON(void)
{
    virNetServerPtr srv = NULL;
    virJSONValuePtr json = NULL;
    char *jsonstr = NULL;
    bool has_ipv4, has_ipv6;

    /* Our pre-saved JSON file is created so that each service
     * only has one socket. If we let libvirt bind to IPv4 and
     * IPv6 we might end up with two sockets, so force one or
     * the other based on what's available on thehost
     */
    if (virNetSocketCheckProtocols(&has_ipv4,
                                   &has_ipv6) < 0)
        return NULL;

    if (!has_ipv4 && !has_ipv6)
        return NULL;

    if (!(srv = testCreateServer(
              has_ipv4 ? "127.0.0.1" : "::1",
              has_ipv4 ? AF_INET : AF_INET6)))
        goto cleanup;

    if (!(json = virNetServerPreExecRestart(srv)))
        goto cleanup;

    if (!(jsonstr = virJSONValueToString(json, true)))
        goto cleanup;
    fprintf(stderr, "%s\n", jsonstr);
 cleanup:
    virNetServerClose(srv);
    virObjectUnref(srv);
    virJSONValueFree(json);
    return jsonstr;
}
コード例 #3
0
static int testExecRestart(const void *opaque)
{
    int ret = -1;
    virNetServerPtr srv = NULL;
    const struct testExecRestartData *data = opaque;
    char *infile = NULL, *outfile = NULL;
    char *injsonstr = NULL, *outjsonstr = NULL;
    virJSONValuePtr injson = NULL, outjson = NULL;
    int fdclient[2] = { -1, -1 }, fdserver[2] = { -1, -1 };

    if (socketpair(PF_UNIX, SOCK_STREAM, 0, fdclient) < 0) {
        virReportSystemError(errno, "%s",
                             "Cannot create socket pair");
        goto cleanup;
    }

    if (socketpair(PF_UNIX, SOCK_STREAM, 0, fdserver) < 0) {
        virReportSystemError(errno, "%s",
                             "Cannot create socket pair");
        goto cleanup;
    }

    /* We're blindly assuming the test case isn't using
     * fds 100->103 for something else, which is probably
     * fairly reasonable in general
     */
    dup2(fdserver[0], 100);
    dup2(fdserver[1], 101);
    dup2(fdclient[0], 102);
    dup2(fdclient[1], 103);

    if (virAsprintf(&infile, "%s/virnetserverdata/input-data-%s.json",
                    abs_srcdir, data->jsonfile) < 0)
        goto cleanup;

    if (virAsprintf(&outfile, "%s/virnetserverdata/output-data-%s.json",
                    abs_srcdir, data->jsonfile) < 0)
        goto cleanup;

    if (virFileReadAll(infile, 8192, &injsonstr) < 0)
        goto cleanup;

    if (!(injson = virJSONValueFromString(injsonstr)))
        goto cleanup;

    if (!(srv = virNetServerNewPostExecRestart(injson,
                                               NULL, NULL, NULL,
                                               NULL, NULL)))
        goto cleanup;

    if (!(outjson = virNetServerPreExecRestart(srv)))
        goto cleanup;

    if (!(outjsonstr = virJSONValueToString(outjson, true)))
        goto cleanup;

    if (virtTestCompareToFile(outjsonstr, outfile) < 0)
        goto fail;

    ret = 0;

 cleanup:
    if (ret < 0) {
        virErrorPtr err = virGetLastError();
        /* Rather be safe, we have lot of missing errors */
        if (err)
            fprintf(stderr, "%s\n", err->message);
        else
            fprintf(stderr, "%s\n", "Unknown error");
    }
 fail:
    VIR_FREE(infile);
    VIR_FREE(outfile);
    VIR_FREE(injsonstr);
    VIR_FREE(outjsonstr);
    virJSONValueFree(injson);
    virJSONValueFree(outjson);
    virObjectUnref(srv);
    VIR_FORCE_CLOSE(fdserver[0]);
    VIR_FORCE_CLOSE(fdserver[1]);
    VIR_FORCE_CLOSE(fdclient[0]);
    VIR_FORCE_CLOSE(fdclient[1]);
    return ret;
}