event-test.c 13.1 KB
Newer Older
1
#include <config.h>
2

3
#include <stdio.h>
4
#include <stdlib.h>
5
#include <string.h>
6
#include <signal.h>
7

8 9
#include <libvirt/libvirt.h>
#include <libvirt/virterror.h>
10

11
#define VIR_DEBUG(fmt, ...) printf("%s:%d: " fmt "\n", \
12
        __func__, __LINE__, ##__VA_ARGS__)
13
#define STREQ(a,b) (strcmp(a,b) == 0)
14

15 16 17
#ifndef ATTRIBUTE_UNUSED
# define ATTRIBUTE_UNUSED __attribute__((__unused__))
#endif
18 19 20 21

/* Prototypes */
const char *eventToString(int event);
int myEventAddHandleFunc  (int fd, int event,
22 23 24
                           virEventHandleCallback cb,
                           void *opaque,
                           virFreeCallback ff);
25 26
void myEventUpdateHandleFunc(int watch, int event);
int  myEventRemoveHandleFunc(int watch);
27

28 29 30 31
int myEventAddTimeoutFunc(int timeout,
                          virEventTimeoutCallback cb,
                          void *opaque,
                          virFreeCallback ff);
32 33 34 35 36 37 38 39 40 41 42
void myEventUpdateTimeoutFunc(int timer, int timout);
int myEventRemoveTimeoutFunc(int timer);

int myEventHandleTypeToPollEvent(virEventHandleType events);
virEventHandleType myPollEventToEventHandleType(int events);

void usage(const char *pname);

/* Callback functions */

const char *eventToString(int event) {
43
    const char *ret = "";
44
    switch(event) {
45 46
        case VIR_DOMAIN_EVENT_DEFINED:
            ret ="Defined";
47
            break;
48 49
        case VIR_DOMAIN_EVENT_UNDEFINED:
            ret ="Undefined";
50 51 52 53 54 55 56 57 58 59 60 61 62
            break;
        case VIR_DOMAIN_EVENT_STARTED:
            ret ="Started";
            break;
        case VIR_DOMAIN_EVENT_SUSPENDED:
            ret ="Suspended";
            break;
        case VIR_DOMAIN_EVENT_RESUMED:
            ret ="Resumed";
            break;
        case VIR_DOMAIN_EVENT_STOPPED:
            ret ="Stopped";
            break;
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
    }
    return ret;
}

static const char *eventDetailToString(int event, int detail) {
    const char *ret = "";
    switch(event) {
        case VIR_DOMAIN_EVENT_DEFINED:
            if (detail == VIR_DOMAIN_EVENT_DEFINED_ADDED)
                ret = "Added";
            else if (detail == VIR_DOMAIN_EVENT_DEFINED_UPDATED)
                ret = "Updated";
            break;
        case VIR_DOMAIN_EVENT_UNDEFINED:
            if (detail == VIR_DOMAIN_EVENT_UNDEFINED_REMOVED)
                ret = "Removed";
79
            break;
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
        case VIR_DOMAIN_EVENT_STARTED:
            switch (detail) {
            case VIR_DOMAIN_EVENT_STARTED_BOOTED:
                ret = "Booted";
                break;
            case VIR_DOMAIN_EVENT_STARTED_MIGRATED:
                ret = "Migrated";
                break;
            case VIR_DOMAIN_EVENT_STARTED_RESTORED:
                ret = "Restored";
                break;
            }
            break;
        case VIR_DOMAIN_EVENT_SUSPENDED:
            if (detail == VIR_DOMAIN_EVENT_SUSPENDED_PAUSED)
95
                ret = "Paused";
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
            else if (detail == VIR_DOMAIN_EVENT_SUSPENDED_MIGRATED)
                ret = "Migrated";
            break;
        case VIR_DOMAIN_EVENT_RESUMED:
            if (detail == VIR_DOMAIN_EVENT_RESUMED_UNPAUSED)
                ret = "Unpaused";
            else if (detail == VIR_DOMAIN_EVENT_RESUMED_MIGRATED)
                ret = "Migrated";
            break;
        case VIR_DOMAIN_EVENT_STOPPED:
            switch (detail) {
            case VIR_DOMAIN_EVENT_STOPPED_SHUTDOWN:
                ret = "Shutdown";
                break;
            case VIR_DOMAIN_EVENT_STOPPED_DESTROYED:
                ret = "Destroyed";
                break;
            case VIR_DOMAIN_EVENT_STOPPED_CRASHED:
                ret = "Crashed";
                break;
            case VIR_DOMAIN_EVENT_STOPPED_MIGRATED:
                ret = "Migrated";
                break;
            case VIR_DOMAIN_EVENT_STOPPED_SAVED:
                ret = "Failed";
                break;
            case VIR_DOMAIN_EVENT_STOPPED_FAILED:
                ret = "Failed";
                break;
            }
126 127 128 129 130
            break;
    }
    return ret;
}

131 132 133 134 135
static int myDomainEventCallback1(virConnectPtr conn ATTRIBUTE_UNUSED,
                                  virDomainPtr dom,
                                  int event,
                                  int detail,
                                  void *opaque ATTRIBUTE_UNUSED)
136
{
137
    printf("%s EVENT: Domain %s(%d) %s %s\n", __func__, virDomainGetName(dom),
138 139
           virDomainGetID(dom), eventToString(event),
           eventDetailToString(event, detail));
140 141 142
    return 0;
}

143 144 145 146 147
static int myDomainEventCallback2(virConnectPtr conn ATTRIBUTE_UNUSED,
                                  virDomainPtr dom,
                                  int event,
                                  int detail,
                                  void *opaque ATTRIBUTE_UNUSED)
148
{
149
    printf("%s EVENT: Domain %s(%d) %s %s\n", __func__, virDomainGetName(dom),
150 151
           virDomainGetID(dom), eventToString(event),
           eventDetailToString(event, detail));
152 153 154
    return 0;
}

155 156 157 158 159 160 161 162 163 164
static int myDomainEventRebootCallback(virConnectPtr conn ATTRIBUTE_UNUSED,
                                       virDomainPtr dom,
                                       void *opaque ATTRIBUTE_UNUSED)
{
    printf("%s EVENT: Domain %s(%d) rebooted\n", __func__, virDomainGetName(dom),
           virDomainGetID(dom));

    return 0;
}

165 166 167 168 169
static int myDomainEventRTCChangeCallback(virConnectPtr conn ATTRIBUTE_UNUSED,
                                          virDomainPtr dom,
                                          long long offset,
                                          void *opaque ATTRIBUTE_UNUSED)
{
170 171 172 173 174 175 176 177 178 179
    char *str = NULL;
    /* HACK: use asprintf since we have gnulib's wrapper for %lld on Win32
     * but don't have a printf() replacement with %lld */
    if (asprintf(&str, "%s EVENT: Domain %s(%d) rtc change %lld\n",
                 __func__, virDomainGetName(dom),
                 virDomainGetID(dom), offset) < 0)
        return 0;

    printf("%s", str);
    free(str);
180 181 182 183

    return 0;
}

184 185 186 187 188 189 190 191 192 193 194
static int myDomainEventWatchdogCallback(virConnectPtr conn ATTRIBUTE_UNUSED,
                                         virDomainPtr dom,
                                         int action,
                                         void *opaque ATTRIBUTE_UNUSED)
{
    printf("%s EVENT: Domain %s(%d) watchdog action=%d\n", __func__, virDomainGetName(dom),
           virDomainGetID(dom), action);

    return 0;
}

195 196 197 198 199 200 201 202 203 204 205 206 207
static int myDomainEventIOErrorCallback(virConnectPtr conn ATTRIBUTE_UNUSED,
                                        virDomainPtr dom,
                                        const char *srcPath,
                                        const char *devAlias,
                                        int action,
                                        void *opaque ATTRIBUTE_UNUSED)
{
    printf("%s EVENT: Domain %s(%d) io error path=%s alias=%s action=%d\n", __func__, virDomainGetName(dom),
           virDomainGetID(dom), srcPath, devAlias, action);

    return 0;
}

208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
static int myDomainEventGraphicsCallback(virConnectPtr conn ATTRIBUTE_UNUSED,
                                         virDomainPtr dom,
                                         int phase,
                                         virDomainEventGraphicsAddressPtr local,
                                         virDomainEventGraphicsAddressPtr remote,
                                         const char *authScheme,
                                         virDomainEventGraphicsSubjectPtr subject,
                                         void *opaque ATTRIBUTE_UNUSED)
{
    int i;
    printf("%s EVENT: Domain %s(%d) graphics ", __func__, virDomainGetName(dom),
           virDomainGetID(dom));

    switch (phase) {
    case VIR_DOMAIN_EVENT_GRAPHICS_CONNECT:
        printf("connected ");
        break;
    case VIR_DOMAIN_EVENT_GRAPHICS_INITIALIZE:
        printf("initialized ");
        break;
    case VIR_DOMAIN_EVENT_GRAPHICS_DISCONNECT:
        printf("disconnected ");
        break;
    }

    printf("local: family=%d node=%s service=%s ",
           local->family, local->node, local->service);
    printf("remote: family=%d node=%s service=%s ",
           remote->family, remote->node, remote->service);

    printf("auth: %s ", authScheme);
    for (i = 0 ; i < subject->nidentity ; i++) {
        printf(" identity: %s=%s",
               subject->identities[i].type,
               subject->identities[i].name);
    }
    printf("\n");

    return 0;
}

249 250 251
static void myFreeFunc(void *opaque)
{
    char *str = opaque;
252
    printf("%s: Freeing [%s]\n", __func__, str);
253 254 255
    free(str);
}

256 257 258 259 260 261 262 263

/* main test functions */

void usage(const char *pname)
{
    printf("%s uri\n", pname);
}

264 265 266 267 268 269 270 271 272
int run = 1;

static void stop(int sig)
{
    printf("Exiting on signal %d\n", sig);
    run = 0;
}


273 274
int main(int argc, char **argv)
{
275 276
    int callback1ret = -1;
    int callback2ret = -1;
277
    int callback3ret = -1;
278
    int callback4ret = -1;
279
    int callback5ret = -1;
280
    int callback6ret = -1;
281
    int callback7ret = -1;
282
    struct sigaction action_stop;
283

284 285 286
    memset(&action_stop, 0, sizeof action_stop);

    action_stop.sa_handler = stop;
287

288
    if (argc > 1 && STREQ(argv[1], "--help")) {
289 290 291
        usage(argv[0]);
        return -1;
    }
292

293
    virEventRegisterDefaultImpl();
294 295

    virConnectPtr dconn = NULL;
296
    dconn = virConnectOpenReadOnly (argv[1] ? argv[1] : NULL);
297 298 299 300 301
    if (!dconn) {
        printf("error opening\n");
        return -1;
    }

302 303 304
    sigaction(SIGTERM, &action_stop, NULL);
    sigaction(SIGINT, &action_stop, NULL);

305
    VIR_DEBUG("Registering domain event cbs");
306 307

    /* Add 2 callbacks to prove this works with more than just one */
308 309
    callback1ret = virConnectDomainEventRegister(dconn, myDomainEventCallback1,
                                                 strdup("callback 1"), myFreeFunc);
310 311 312 313 314
    callback2ret = virConnectDomainEventRegisterAny(dconn,
                                                    NULL,
                                                    VIR_DOMAIN_EVENT_ID_LIFECYCLE,
                                                    VIR_DOMAIN_EVENT_CALLBACK(myDomainEventCallback2),
                                                    strdup("callback 2"), myFreeFunc);
315 316 317 318 319
    callback3ret = virConnectDomainEventRegisterAny(dconn,
                                                    NULL,
                                                    VIR_DOMAIN_EVENT_ID_REBOOT,
                                                    VIR_DOMAIN_EVENT_CALLBACK(myDomainEventRebootCallback),
                                                    strdup("callback reboot"), myFreeFunc);
320 321 322 323 324
    callback4ret = virConnectDomainEventRegisterAny(dconn,
                                                    NULL,
                                                    VIR_DOMAIN_EVENT_ID_RTC_CHANGE,
                                                    VIR_DOMAIN_EVENT_CALLBACK(myDomainEventRTCChangeCallback),
                                                    strdup("callback rtcchange"), myFreeFunc);
325 326 327 328 329
    callback5ret = virConnectDomainEventRegisterAny(dconn,
                                                    NULL,
                                                    VIR_DOMAIN_EVENT_ID_WATCHDOG,
                                                    VIR_DOMAIN_EVENT_CALLBACK(myDomainEventWatchdogCallback),
                                                    strdup("callback watchdog"), myFreeFunc);
330 331 332 333 334
    callback6ret = virConnectDomainEventRegisterAny(dconn,
                                                    NULL,
                                                    VIR_DOMAIN_EVENT_ID_IO_ERROR,
                                                    VIR_DOMAIN_EVENT_CALLBACK(myDomainEventIOErrorCallback),
                                                    strdup("callback io error"), myFreeFunc);
335 336 337 338 339
    callback7ret = virConnectDomainEventRegisterAny(dconn,
                                                    NULL,
                                                    VIR_DOMAIN_EVENT_ID_GRAPHICS,
                                                    VIR_DOMAIN_EVENT_CALLBACK(myDomainEventGraphicsCallback),
                                                    strdup("callback graphics"), myFreeFunc);
340 341

    if ((callback1ret != -1) &&
342
        (callback2ret != -1) &&
343
        (callback3ret != -1) &&
344
        (callback4ret != -1) &&
345
        (callback5ret != -1) &&
346 347
        (callback6ret != -1) &&
        (callback7ret != -1)) {
348 349 350 351 352
        while (run) {
            if (virEventRunDefaultImpl() < 0) {
                virErrorPtr err = virGetLastError();
                fprintf(stderr, "Failed to run event loop: %s\n",
                        err && err->message ? err->message : "Unknown error");
353
            }
354 355
        }

356
        VIR_DEBUG("Deregistering event handlers");
357
        virConnectDomainEventDeregister(dconn, myDomainEventCallback1);
358
        virConnectDomainEventDeregisterAny(dconn, callback2ret);
359
        virConnectDomainEventDeregisterAny(dconn, callback3ret);
360
        virConnectDomainEventDeregisterAny(dconn, callback4ret);
361
        virConnectDomainEventDeregisterAny(dconn, callback5ret);
362
        virConnectDomainEventDeregisterAny(dconn, callback6ret);
363
        virConnectDomainEventDeregisterAny(dconn, callback7ret);
364 365
    }

366
    VIR_DEBUG("Closing connection");
367
    if (dconn && virConnectClose(dconn) < 0) {
368 369
        printf("error closing\n");
    }
370

371 372 373
    printf("done\n");
    return 0;
}