You need to sign in or sign up before continuing.
reconnect.c 1.2 KB
Newer Older
1
#include <config.h>
2

3 4
#include <stdio.h>
#include <stdlib.h>
R
Richard W.M. Jones 已提交
5

6 7
#include "libvirt/libvirt.h"
#include "libvirt/virterror.h"
8
#include "internal.h"
9

10 11
static void errorHandler(void *userData ATTRIBUTE_UNUSED,
			 virErrorPtr error ATTRIBUTE_UNUSED) {
12
}
13 14 15

int main(void) {
    int id = 0;
16
    int ro = 0;
17 18 19
    virConnectPtr conn;
    virDomainPtr dom;

20 21 22 23 24 25 26
    virSetErrorFunc(NULL, errorHandler);

    conn = virConnectOpen(NULL);
    if (conn == NULL) {
        ro = 1;
	conn = virConnectOpenReadOnly(NULL);
    }
27 28 29 30 31 32 33 34 35 36 37
    if (conn == NULL) {
        fprintf(stderr, "First virConnectOpen() failed\n");
	exit(1);
    }
    dom = virDomainLookupByID(conn, id);
    if (dom == NULL) {
        fprintf(stderr, "First lookup for domain %d failed\n", id);
	exit(1);
    }
    virDomainFree(dom);
    virConnectClose(conn);
38 39 40 41
    if (ro == 1)
	conn = virConnectOpenReadOnly(NULL);
    else
	conn = virConnectOpen(NULL);
42 43 44 45 46 47 48 49 50 51 52 53 54
    if (conn == NULL) {
        fprintf(stderr, "Second virConnectOpen() failed\n");
	exit(1);
    }
    dom = virDomainLookupByID(conn, id);
    if (dom == NULL) {
        fprintf(stderr, "Second lookup for domain %d failed\n", id);
	exit(1);
    }
    virDomainFree(dom);
    virConnectClose(conn);
    printf("OK\n");
    exit(0);
55

56 57
}