⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 test_bindings.c

📁 xen虚拟机源代码安装包
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * Copyright (c) 2006-2007 XenSource, Inc. * * 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 * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA */#define _GNU_SOURCE#include <assert.h>#include <inttypes.h>#include <stdlib.h>#include <stdio.h>#include <string.h>#include <libxml/parser.h>#include <curl/curl.h>#include <xen/api/xen_all.h>//#define PRINT_XMLstatic void usage(){    fprintf(stderr,"Usage:\n""\n""    test_bindings <url> <username> <password>\n""\n""where\n""        <url>      is a fragment of the server's URL, e.g. localhost:8005/RPC2;\n""        <username> is the username to use at the server; and\n""        <password> is the password.\n");    exit(EXIT_FAILURE);}static char *url;typedef struct{    xen_result_func func;    void *handle;} xen_comms;static xen_vm create_new_vm(xen_session *session, bool hvm);static void print_session_info(xen_session *session);static void print_methods(xen_session *session);static void print_vm_power_state(xen_session *session, xen_vm vm);static void print_vm_metrics(xen_session *session, xen_vm vm);static size_twrite_func(void *ptr, size_t size, size_t nmemb, xen_comms *comms){    size_t n = size * nmemb;#ifdef PRINT_XML    printf("\n\n---Result from server -----------------------\n");    printf("%s\n",((char*) ptr));    fflush(stdout);#endif    return comms->func(ptr, n, comms->handle) ? n : 0;}static intcall_func(const void *data, size_t len, void *user_handle,          void *result_handle, xen_result_func result_func){    (void)user_handle;#ifdef PRINT_XML    printf("\n\n---Data to server: -----------------------\n");    printf("%s\n",((char*) data));    fflush(stdout);#endif    CURL *curl = curl_easy_init();    if (!curl) {        return -1;    }    xen_comms comms = {        .func = result_func,        .handle = result_handle    };    curl_easy_setopt(curl, CURLOPT_URL, url);    curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1);    curl_easy_setopt(curl, CURLOPT_MUTE, 1);    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &write_func);    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &comms);    curl_easy_setopt(curl, CURLOPT_POST, 1);    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);    curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, len);    CURLcode result = curl_easy_perform(curl);    curl_easy_cleanup(curl);    return result;}static void print_error(xen_session *session){    fprintf(stderr, "Error: %d", session->error_description_count);    for (int i = 0; i < session->error_description_count; i++)    {        fprintf(stderr, "%s ", session->error_description[i]);    }    fprintf(stderr, "\n");}int main(int argc, char **argv){    if (argc != 4)    {        usage();    }    url = argv[1];    char *username = argv[2];    char *password = argv[3];    xmlInitParser();    xen_init();    curl_global_init(CURL_GLOBAL_ALL);#define CLEANUP                                 \    do {                                        \        xen_session_logout(session);            \        curl_global_cleanup();                  \        xen_fini();                             \        xmlCleanupParser();                     \    } while(0)                                  \        xen_session *session =        xen_session_login_with_password(call_func, NULL, username, password);    print_session_info(session);    if (!session->ok)    {        /* Error has been logged, just clean up. */        CLEANUP;        return 1;    }    print_methods(session);    if (!session->ok)    {        /* Error has been logged, just clean up. */        CLEANUP;        return 1;    }    xen_vm vm;    if (!xen_vm_get_by_uuid(session, &vm,                            "00000000-0000-0000-0000-000000000000"))    {        print_error(session);        CLEANUP;        return 1;    }    char *vm_uuid;    if (!xen_vm_get_uuid(session, &vm_uuid, vm))    {        print_error(session);        xen_vm_free(vm);        CLEANUP;        return 1;    }    char *vm_uuid_bytes;    if (!xen_uuid_string_to_bytes(vm_uuid, &vm_uuid_bytes))    {        fprintf(stderr, "xen_uuid_string_to_bytes failed.\n");        xen_uuid_free(vm_uuid);        xen_vm_free(vm);        CLEANUP;        return 1;    }    xen_vm_record *vm_record;    if (!xen_vm_get_record(session, &vm_record, vm))    {        print_error(session);        xen_uuid_bytes_free(vm_uuid_bytes);        xen_uuid_free(vm_uuid);        xen_vm_free(vm);        CLEANUP;        return 1;    }    xen_host host;    if (!xen_session_get_this_host(session, &host, session))    {        print_error(session);        xen_vm_record_free(vm_record);        xen_uuid_bytes_free(vm_uuid_bytes);        xen_uuid_free(vm_uuid);        xen_vm_free(vm);        CLEANUP;        return 1;    }    xen_string_string_map *versions;    if (!xen_host_get_software_version(session, &versions, host))    {        print_error(session);        xen_host_free(host);        xen_vm_record_free(vm_record);        xen_uuid_bytes_free(vm_uuid_bytes);        xen_uuid_free(vm_uuid);        xen_vm_free(vm);        CLEANUP;        return 1;    }    char *dmesg;    if (!xen_host_dmesg(session, &dmesg, host))    {        print_error(session);        xen_string_string_map_free(versions);        xen_host_free(host);        xen_vm_record_free(vm_record);        xen_uuid_bytes_free(vm_uuid_bytes);        xen_uuid_free(vm_uuid);        xen_vm_free(vm);        CLEANUP;        return 1;    }    xen_string_set *supported_bootloaders;    if (!xen_host_get_supported_bootloaders(session, &supported_bootloaders,                                            host))    {        print_error(session);        free(dmesg);        xen_string_string_map_free(versions);        xen_host_free(host);        xen_vm_record_free(vm_record);        xen_uuid_bytes_free(vm_uuid_bytes);        xen_uuid_free(vm_uuid);        xen_vm_free(vm);        CLEANUP;        return 1;    }    xen_string_set *capabilities;    if (!xen_host_get_capabilities(session, &capabilities, host))    {        print_error(session);        free(dmesg);        xen_string_set_free(supported_bootloaders);        xen_string_string_map_free(versions);        xen_host_free(host);        xen_vm_record_free(vm_record);        xen_uuid_bytes_free(vm_uuid_bytes);        xen_uuid_free(vm_uuid);        xen_vm_free(vm);        CLEANUP;        return 1;    }    xen_string_string_map *cpu_configuration;    if (!xen_host_get_cpu_configuration(session, &cpu_configuration, host))    {        print_error(session);        free(dmesg);        xen_string_set_free(capabilities);        xen_string_set_free(supported_bootloaders);        xen_string_string_map_free(versions);        xen_host_free(host);        xen_vm_record_free(vm_record);        xen_uuid_bytes_free(vm_uuid_bytes);        xen_uuid_free(vm_uuid);        xen_vm_free(vm);        CLEANUP;        return 1;    }    char *sched_policy;    if (!xen_host_get_sched_policy(session, &sched_policy, host))    {        print_error(session);        xen_string_string_map_free(cpu_configuration);        xen_string_set_free(capabilities);        xen_string_set_free(supported_bootloaders);        xen_string_string_map_free(versions);        xen_host_free(host);        xen_vm_record_free(vm_record);        xen_uuid_bytes_free(vm_uuid_bytes);        xen_uuid_free(vm_uuid);        xen_vm_free(vm);        CLEANUP;        return 1;    }    printf("%s.\n", vm_uuid);    printf("In bytes, the VM UUID is ");    for (int i = 0; i < 15; i++)    {        printf("%x, ", (unsigned int)vm_uuid_bytes[i]);    }    printf("%x.\n", (unsigned int)vm_uuid_bytes[15]);    printf("%zd.\n", versions->size);    for (size_t i = 0; i < versions->size; i++)    {        printf("%s -> %s.\n", versions->contents[i].key,               versions->contents[i].val);    }    printf("Host dmesg follows:\n%s\n\n", dmesg);    printf("Host supports the following bootloaders:");    for (size_t i = 0; i < supported_bootloaders->size; i++)    {        printf(" %s", supported_bootloaders->contents[i]);    }    printf("\n");    printf("Host has the following capabilities:");    for (size_t i = 0; i < capabilities->size; i++)    {        printf(" %s", capabilities->contents[i]);    }    printf("\n");    printf("Host has the following CPU configuration:\n");    for (size_t i = 0; i < cpu_configuration->size; i++)    {        printf("  %s -> %s.\n", cpu_configuration->contents[i].key,               cpu_configuration->contents[i].val);    }    printf("Current scheduler policy: %s.\n\n", sched_policy);    printf("%s.\n", vm_record->uuid);    printf("Resident on %s.\n", (char *)vm_record->resident_on->u.handle);    printf("%s.\n", xen_vm_power_state_to_string(vm_record->power_state));    xen_uuid_bytes_free(vm_uuid_bytes);    xen_uuid_free(vm_uuid);    xen_vm_record_free(vm_record);    xen_host_free(host);    xen_string_string_map_free(versions);    free(dmesg);    xen_string_set_free(supported_bootloaders);    xen_string_set_free(capabilities);    xen_string_string_map_free(cpu_configuration);    free(sched_policy);    print_vm_metrics(session, vm);    if (!session->ok)    {        /* Error has been logged, just clean up. */        xen_vm_free(vm);        CLEANUP;        return 1;    }    xen_vm_free(vm);    xen_vm new_vm = create_new_vm(session, true);    if (!session->ok)    {        /* Error has been logged, just clean up. */        CLEANUP;        return 1;    }    print_vm_power_state(session, new_vm);    if (!session->ok)    {        /* Error has been logged, just clean up. */        xen_vm_free(new_vm);        CLEANUP;        return 1;    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -