reconnect.c 1.2 KB
Newer Older
1 2 3
#include <stdio.h>
#include <stdlib.h>
#include <libvirt/libvirt.h>
4
#include <libvirt/virterror.h>
5
#include "internal.h"
6

7 8
static void errorHandler(void *userData ATTRIBUTE_UNUSED,
			 virErrorPtr error ATTRIBUTE_UNUSED) {
9
}
10 11 12

int main(void) {
    int id = 0;
13
    int ro = 0;
14 15 16
    virConnectPtr conn;
    virDomainPtr dom;

17 18 19 20 21 22 23
    virSetErrorFunc(NULL, errorHandler);

    conn = virConnectOpen(NULL);
    if (conn == NULL) {
        ro = 1;
	conn = virConnectOpenReadOnly(NULL);
    }
24 25 26 27 28 29 30 31 32 33 34
    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);
35 36 37 38
    if (ro == 1)
	conn = virConnectOpenReadOnly(NULL);
    else
	conn = virConnectOpen(NULL);
39 40 41 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);
    
}