event-test.c 16.6 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", __func__, __LINE__)
12
#define STREQ(a,b) (strcmp(a,b) == 0)
13

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

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

27 28 29 30
int myEventAddTimeoutFunc(int timeout,
                          virEventTimeoutCallback cb,
                          void *opaque,
                          virFreeCallback ff);
31 32 33 34 35 36 37 38 39 40 41
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) {
42
    const char *ret = "";
43
    switch(event) {
44 45
        case VIR_DOMAIN_EVENT_DEFINED:
            ret ="Defined";
46
            break;
47 48
        case VIR_DOMAIN_EVENT_UNDEFINED:
            ret ="Undefined";
49 50 51 52 53 54 55 56 57 58 59 60 61
            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;
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
    }
    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";
78
            break;
79 80 81 82 83 84 85 86 87 88 89
        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;
90 91 92
            case VIR_DOMAIN_EVENT_STARTED_FROM_SNAPSHOT:
                ret = "Snapshot";
                break;
93 94 95
            }
            break;
        case VIR_DOMAIN_EVENT_SUSPENDED:
96 97
            switch (detail) {
            case VIR_DOMAIN_EVENT_SUSPENDED_PAUSED:
98
                ret = "Paused";
99 100
                break;
            case VIR_DOMAIN_EVENT_SUSPENDED_MIGRATED:
101
                ret = "Migrated";
102 103 104 105 106 107 108 109 110 111 112 113 114 115
                break;
            case VIR_DOMAIN_EVENT_SUSPENDED_IOERROR:
                ret = "I/O Error";
                break;
            case VIR_DOMAIN_EVENT_SUSPENDED_WATCHDOG:
                ret = "Watchdog";
                break;
            case VIR_DOMAIN_EVENT_SUSPENDED_RESTORED:
                ret = "Restored";
                break;
            case VIR_DOMAIN_EVENT_SUSPENDED_FROM_SNAPSHOT:
                ret = "Snapshot";
                break;
            }
116 117
            break;
        case VIR_DOMAIN_EVENT_RESUMED:
118 119
            switch (detail) {
            case VIR_DOMAIN_EVENT_RESUMED_UNPAUSED:
120
                ret = "Unpaused";
121 122
                break;
            case VIR_DOMAIN_EVENT_RESUMED_MIGRATED:
123
                ret = "Migrated";
124 125 126 127 128
                break;
            case VIR_DOMAIN_EVENT_RESUMED_FROM_SNAPSHOT:
                ret = "Snapshot";
                break;
            }
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
            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;
150 151 152
            case VIR_DOMAIN_EVENT_STOPPED_FROM_SNAPSHOT:
                ret = "Snapshot";
                break;
153
            }
154 155 156 157 158
            break;
    }
    return ret;
}

159 160 161 162 163
static int myDomainEventCallback1(virConnectPtr conn ATTRIBUTE_UNUSED,
                                  virDomainPtr dom,
                                  int event,
                                  int detail,
                                  void *opaque ATTRIBUTE_UNUSED)
164
{
165
    printf("%s EVENT: Domain %s(%d) %s %s\n", __func__, virDomainGetName(dom),
166 167
           virDomainGetID(dom), eventToString(event),
           eventDetailToString(event, detail));
168 169 170
    return 0;
}

171 172 173 174 175
static int myDomainEventCallback2(virConnectPtr conn ATTRIBUTE_UNUSED,
                                  virDomainPtr dom,
                                  int event,
                                  int detail,
                                  void *opaque ATTRIBUTE_UNUSED)
176
{
177
    printf("%s EVENT: Domain %s(%d) %s %s\n", __func__, virDomainGetName(dom),
178 179
           virDomainGetID(dom), eventToString(event),
           eventDetailToString(event, detail));
180 181 182
    return 0;
}

183 184 185 186 187 188 189 190 191 192
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;
}

193 194 195 196 197
static int myDomainEventRTCChangeCallback(virConnectPtr conn ATTRIBUTE_UNUSED,
                                          virDomainPtr dom,
                                          long long offset,
                                          void *opaque ATTRIBUTE_UNUSED)
{
198 199 200 201 202 203 204 205 206 207
    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);
208 209 210 211

    return 0;
}

212 213 214 215 216 217 218 219 220 221 222
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;
}

223 224 225 226 227 228 229 230 231 232 233 234 235
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;
}

236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
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;
}

277 278 279 280 281 282 283 284 285 286 287
static int myDomainEventControlErrorCallback(virConnectPtr conn ATTRIBUTE_UNUSED,
                                             virDomainPtr dom,
                                             void *opaque ATTRIBUTE_UNUSED)
{
    printf("%s EVENT: Domain %s(%d) control error\n", __func__, virDomainGetName(dom),
           virDomainGetID(dom));

    return 0;
}


288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
const char *diskChangeReasonStrings[] = {
    "startupPolicy", /* 0 */
    /* add new reason here */
};
static int myDomainEventDiskChangeCallback(virConnectPtr conn ATTRIBUTE_UNUSED,
                                           virDomainPtr dom,
                                           const char *oldSrcPath,
                                           const char *newSrcPath,
                                           const char *devAlias,
                                           int reason,
                                           void *opaque ATTRIBUTE_UNUSED)
{
    printf("%s EVENT: Domain %s(%d) disk change oldSrcPath: %s newSrcPath: %s devAlias: %s reason: %s\n",
           __func__, virDomainGetName(dom), virDomainGetID(dom),
           oldSrcPath, newSrcPath, devAlias, diskChangeReasonStrings[reason]);
    return 0;
}


307 308 309
static void myFreeFunc(void *opaque)
{
    char *str = opaque;
310
    printf("%s: Freeing [%s]\n", __func__, str);
311 312 313
    free(str);
}

314 315 316 317 318 319 320 321

/* main test functions */

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

322 323 324 325 326 327 328 329 330
int run = 1;

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


331 332
int main(int argc, char **argv)
{
333 334
    int callback1ret = -1;
    int callback2ret = -1;
335
    int callback3ret = -1;
336
    int callback4ret = -1;
337
    int callback5ret = -1;
338
    int callback6ret = -1;
339
    int callback7ret = -1;
340
    int callback8ret = -1;
341
    int callback9ret = -1;
342
    struct sigaction action_stop;
343

344 345 346
    memset(&action_stop, 0, sizeof action_stop);

    action_stop.sa_handler = stop;
347

348
    if (argc > 1 && STREQ(argv[1], "--help")) {
349 350 351
        usage(argv[0]);
        return -1;
    }
352

353
    virEventRegisterDefaultImpl();
354 355

    virConnectPtr dconn = NULL;
356 357 358
    dconn = virConnectOpenAuth(argc > 1 ? argv[1] : NULL,
                               virConnectAuthPtrDefault,
                               VIR_CONNECT_RO);
359 360 361 362 363
    if (!dconn) {
        printf("error opening\n");
        return -1;
    }

364 365 366
    sigaction(SIGTERM, &action_stop, NULL);
    sigaction(SIGINT, &action_stop, NULL);

367
    VIR_DEBUG("Registering domain event cbs");
368 369

    /* Add 2 callbacks to prove this works with more than just one */
370 371
    callback1ret = virConnectDomainEventRegister(dconn, myDomainEventCallback1,
                                                 strdup("callback 1"), myFreeFunc);
372 373 374 375 376
    callback2ret = virConnectDomainEventRegisterAny(dconn,
                                                    NULL,
                                                    VIR_DOMAIN_EVENT_ID_LIFECYCLE,
                                                    VIR_DOMAIN_EVENT_CALLBACK(myDomainEventCallback2),
                                                    strdup("callback 2"), myFreeFunc);
377 378 379 380 381
    callback3ret = virConnectDomainEventRegisterAny(dconn,
                                                    NULL,
                                                    VIR_DOMAIN_EVENT_ID_REBOOT,
                                                    VIR_DOMAIN_EVENT_CALLBACK(myDomainEventRebootCallback),
                                                    strdup("callback reboot"), myFreeFunc);
382 383 384 385 386
    callback4ret = virConnectDomainEventRegisterAny(dconn,
                                                    NULL,
                                                    VIR_DOMAIN_EVENT_ID_RTC_CHANGE,
                                                    VIR_DOMAIN_EVENT_CALLBACK(myDomainEventRTCChangeCallback),
                                                    strdup("callback rtcchange"), myFreeFunc);
387 388 389 390 391
    callback5ret = virConnectDomainEventRegisterAny(dconn,
                                                    NULL,
                                                    VIR_DOMAIN_EVENT_ID_WATCHDOG,
                                                    VIR_DOMAIN_EVENT_CALLBACK(myDomainEventWatchdogCallback),
                                                    strdup("callback watchdog"), myFreeFunc);
392 393 394 395 396
    callback6ret = virConnectDomainEventRegisterAny(dconn,
                                                    NULL,
                                                    VIR_DOMAIN_EVENT_ID_IO_ERROR,
                                                    VIR_DOMAIN_EVENT_CALLBACK(myDomainEventIOErrorCallback),
                                                    strdup("callback io error"), myFreeFunc);
397 398 399 400 401
    callback7ret = virConnectDomainEventRegisterAny(dconn,
                                                    NULL,
                                                    VIR_DOMAIN_EVENT_ID_GRAPHICS,
                                                    VIR_DOMAIN_EVENT_CALLBACK(myDomainEventGraphicsCallback),
                                                    strdup("callback graphics"), myFreeFunc);
402 403 404 405 406
    callback8ret = virConnectDomainEventRegisterAny(dconn,
                                                    NULL,
                                                    VIR_DOMAIN_EVENT_ID_CONTROL_ERROR,
                                                    VIR_DOMAIN_EVENT_CALLBACK(myDomainEventControlErrorCallback),
                                                    strdup("callback control error"), myFreeFunc);
407 408 409 410 411
    callback9ret = virConnectDomainEventRegisterAny(dconn,
                                                    NULL,
                                                    VIR_DOMAIN_EVENT_ID_DISK_CHANGE,
                                                    VIR_DOMAIN_EVENT_CALLBACK(myDomainEventDiskChangeCallback),
                                                    strdup("disk change"), myFreeFunc);
412 413

    if ((callback1ret != -1) &&
414
        (callback2ret != -1) &&
415
        (callback3ret != -1) &&
416
        (callback4ret != -1) &&
417
        (callback5ret != -1) &&
418
        (callback6ret != -1) &&
419 420
        (callback7ret != -1) &&
        (callback9ret != -1)) {
421 422 423 424 425 426 427 428
        if (virConnectSetKeepAlive(dconn, 5, 3) < 0) {
            virErrorPtr err = virGetLastError();
            fprintf(stderr, "Failed to start keepalive protocol: %s\n",
                    err && err->message ? err->message : "Unknown error");
            run = 0;
        }

        while (run && virConnectIsAlive(dconn) == 1) {
429 430 431 432
            if (virEventRunDefaultImpl() < 0) {
                virErrorPtr err = virGetLastError();
                fprintf(stderr, "Failed to run event loop: %s\n",
                        err && err->message ? err->message : "Unknown error");
433
            }
434 435
        }

436
        VIR_DEBUG("Deregistering event handlers");
437
        virConnectDomainEventDeregister(dconn, myDomainEventCallback1);
438
        virConnectDomainEventDeregisterAny(dconn, callback2ret);
439
        virConnectDomainEventDeregisterAny(dconn, callback3ret);
440
        virConnectDomainEventDeregisterAny(dconn, callback4ret);
441
        virConnectDomainEventDeregisterAny(dconn, callback5ret);
442
        virConnectDomainEventDeregisterAny(dconn, callback6ret);
443
        virConnectDomainEventDeregisterAny(dconn, callback7ret);
444
        virConnectDomainEventDeregisterAny(dconn, callback9ret);
445 446
        if (callback8ret != -1)
            virConnectDomainEventDeregisterAny(dconn, callback8ret);
447 448
    }

449
    VIR_DEBUG("Closing connection");
450
    if (dconn && virConnectClose(dconn) < 0) {
451 452
        printf("error closing\n");
    }
453

454 455 456
    printf("done\n");
    return 0;
}