event-test.c 14.4 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

#if HAVE_SYS_POLL_H
9 10 11
# include <sys/types.h>
# include <sys/poll.h>
# include <libvirt/libvirt.h>
12

13
# define DEBUG0(fmt) printf("%s:%d :: " fmt "\n", \
14
        __func__, __LINE__)
15
# define DEBUG(fmt, ...) printf("%s:%d: " fmt "\n", \
16
        __func__, __LINE__, __VA_ARGS__)
17
# define STREQ(a,b) (strcmp(a,b) == 0)
18

19 20 21
# ifndef ATTRIBUTE_UNUSED
#  define ATTRIBUTE_UNUSED __attribute__((__unused__))
# endif
22 23 24 25 26

/* handle globals */
int h_fd = 0;
virEventHandleType h_event = 0;
virEventHandleCallback h_cb = NULL;
27
virFreeCallback h_ff = NULL;
28 29 30
void *h_opaque = NULL;

/* timeout globals */
31
# define TIMEOUT_MS 1000
32 33 34
int t_active = 0;
int t_timeout = -1;
virEventTimeoutCallback t_cb = NULL;
35
virFreeCallback t_ff = NULL;
36 37 38 39 40 41
void *t_opaque = NULL;


/* Prototypes */
const char *eventToString(int event);
int myEventAddHandleFunc  (int fd, int event,
42 43 44
                           virEventHandleCallback cb,
                           void *opaque,
                           virFreeCallback ff);
45 46
void myEventUpdateHandleFunc(int watch, int event);
int  myEventRemoveHandleFunc(int watch);
47

48 49 50 51
int myEventAddTimeoutFunc(int timeout,
                          virEventTimeoutCallback cb,
                          void *opaque,
                          virFreeCallback ff);
52 53 54 55 56 57 58 59 60 61 62
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) {
63
    const char *ret = "";
64
    switch(event) {
65 66
        case VIR_DOMAIN_EVENT_DEFINED:
            ret ="Defined";
67
            break;
68 69
        case VIR_DOMAIN_EVENT_UNDEFINED:
            ret ="Undefined";
70 71 72 73 74 75 76 77 78 79 80 81 82
            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;
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
    }
    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";
99
            break;
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
        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)
115
                ret = "Paused";
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
            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;
            }
146 147 148 149 150
            break;
    }
    return ret;
}

151 152 153 154 155
static int myDomainEventCallback1(virConnectPtr conn ATTRIBUTE_UNUSED,
                                  virDomainPtr dom,
                                  int event,
                                  int detail,
                                  void *opaque ATTRIBUTE_UNUSED)
156
{
157
    printf("%s EVENT: Domain %s(%d) %s %s\n", __func__, virDomainGetName(dom),
158 159
           virDomainGetID(dom), eventToString(event),
           eventDetailToString(event, detail));
160 161 162
    return 0;
}

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

175 176 177 178 179 180 181 182 183 184
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;
}

185 186 187 188 189 190 191 192 193 194 195
static int myDomainEventRTCChangeCallback(virConnectPtr conn ATTRIBUTE_UNUSED,
                                          virDomainPtr dom,
                                          long long offset,
                                          void *opaque ATTRIBUTE_UNUSED)
{
    printf("%s EVENT: Domain %s(%d) rtc change %lld\n", __func__, virDomainGetName(dom),
           virDomainGetID(dom), offset);

    return 0;
}

196 197 198 199 200 201 202 203 204 205 206
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;
}

207 208 209 210 211 212 213 214 215 216 217 218 219
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;
}

220 221 222
static void myFreeFunc(void *opaque)
{
    char *str = opaque;
223
    printf("%s: Freeing [%s]\n", __func__, str);
224 225 226
    free(str);
}

227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257

/* EventImpl Functions */
int myEventHandleTypeToPollEvent(virEventHandleType events)
{
    int ret = 0;
    if(events & VIR_EVENT_HANDLE_READABLE)
        ret |= POLLIN;
    if(events & VIR_EVENT_HANDLE_WRITABLE)
        ret |= POLLOUT;
    if(events & VIR_EVENT_HANDLE_ERROR)
        ret |= POLLERR;
    if(events & VIR_EVENT_HANDLE_HANGUP)
        ret |= POLLHUP;
    return ret;
}

virEventHandleType myPollEventToEventHandleType(int events)
{
    virEventHandleType ret = 0;
    if(events & POLLIN)
        ret |= VIR_EVENT_HANDLE_READABLE;
    if(events & POLLOUT)
        ret |= VIR_EVENT_HANDLE_WRITABLE;
    if(events & POLLERR)
        ret |= VIR_EVENT_HANDLE_ERROR;
    if(events & POLLHUP)
        ret |= VIR_EVENT_HANDLE_HANGUP;
    return ret;
}

int  myEventAddHandleFunc(int fd, int event,
258 259 260
                          virEventHandleCallback cb,
                          void *opaque,
                          virFreeCallback ff)
261 262 263 264 265
{
    DEBUG("Add handle %d %d %p %p", fd, event, cb, opaque);
    h_fd = fd;
    h_event = myEventHandleTypeToPollEvent(event);
    h_cb = cb;
266
    h_ff = ff;
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
    h_opaque = opaque;
    return 0;
}

void myEventUpdateHandleFunc(int fd, int event)
{
    DEBUG("Updated Handle %d %d", fd, event);
    h_event = myEventHandleTypeToPollEvent(event);
    return;
}

int  myEventRemoveHandleFunc(int fd)
{
    DEBUG("Removed Handle %d", fd);
    h_fd = 0;
282 283
    if (h_ff)
       (h_ff)(h_opaque);
284 285 286
    return 0;
}

287 288 289 290
int myEventAddTimeoutFunc(int timeout,
                          virEventTimeoutCallback cb,
                          void *opaque,
                          virFreeCallback ff)
291 292 293 294 295
{
    DEBUG("Adding Timeout %d %p %p", timeout, cb, opaque);
    t_active = 1;
    t_timeout = timeout;
    t_cb = cb;
296
    t_ff = ff;
297 298 299 300 301 302 303 304 305 306 307 308 309 310
    t_opaque = opaque;
    return 0;
}

void myEventUpdateTimeoutFunc(int timer ATTRIBUTE_UNUSED, int timeout)
{
    /*DEBUG("Timeout updated %d %d", timer, timeout);*/
    t_timeout = timeout;
}

int myEventRemoveTimeoutFunc(int timer)
{
   DEBUG("Timeout removed %d", timer);
   t_active = 0;
311 312
   if (t_ff)
       (t_ff)(t_opaque);
313 314 315 316 317 318 319 320 321 322
   return 0;
}

/* main test functions */

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

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

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


332 333 334
int main(int argc, char **argv)
{
    int sts;
335 336
    int callback1ret = -1;
    int callback2ret = -1;
337
    int callback3ret = -1;
338
    int callback4ret = -1;
339
    int callback5ret = -1;
340
    int callback6ret = -1;
341

342 343 344
    struct sigaction action_stop = {
        .sa_handler = stop
    };
345 346 347 348 349

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

351 352 353 354 355 356 357 358
    virEventRegisterImpl( myEventAddHandleFunc,
                          myEventUpdateHandleFunc,
                          myEventRemoveHandleFunc,
                          myEventAddTimeoutFunc,
                          myEventUpdateTimeoutFunc,
                          myEventRemoveTimeoutFunc);

    virConnectPtr dconn = NULL;
359
    dconn = virConnectOpenReadOnly (argv[1] ? argv[1] : NULL);
360 361 362 363 364
    if (!dconn) {
        printf("error opening\n");
        return -1;
    }

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

368 369 370
    DEBUG0("Registering domain event cbs");

    /* Add 2 callbacks to prove this works with more than just one */
371 372
    callback1ret = virConnectDomainEventRegister(dconn, myDomainEventCallback1,
                                                 strdup("callback 1"), myFreeFunc);
373 374 375 376 377
    callback2ret = virConnectDomainEventRegisterAny(dconn,
                                                    NULL,
                                                    VIR_DOMAIN_EVENT_ID_LIFECYCLE,
                                                    VIR_DOMAIN_EVENT_CALLBACK(myDomainEventCallback2),
                                                    strdup("callback 2"), myFreeFunc);
378 379 380 381 382
    callback3ret = virConnectDomainEventRegisterAny(dconn,
                                                    NULL,
                                                    VIR_DOMAIN_EVENT_ID_REBOOT,
                                                    VIR_DOMAIN_EVENT_CALLBACK(myDomainEventRebootCallback),
                                                    strdup("callback reboot"), myFreeFunc);
383 384 385 386 387
    callback4ret = virConnectDomainEventRegisterAny(dconn,
                                                    NULL,
                                                    VIR_DOMAIN_EVENT_ID_RTC_CHANGE,
                                                    VIR_DOMAIN_EVENT_CALLBACK(myDomainEventRTCChangeCallback),
                                                    strdup("callback rtcchange"), myFreeFunc);
388 389 390 391 392
    callback5ret = virConnectDomainEventRegisterAny(dconn,
                                                    NULL,
                                                    VIR_DOMAIN_EVENT_ID_WATCHDOG,
                                                    VIR_DOMAIN_EVENT_CALLBACK(myDomainEventWatchdogCallback),
                                                    strdup("callback watchdog"), myFreeFunc);
393 394 395 396 397
    callback6ret = virConnectDomainEventRegisterAny(dconn,
                                                    NULL,
                                                    VIR_DOMAIN_EVENT_ID_IO_ERROR,
                                                    VIR_DOMAIN_EVENT_CALLBACK(myDomainEventIOErrorCallback),
                                                    strdup("callback io error"), myFreeFunc);
398 399

    if ((callback1ret != -1) &&
400
        (callback2ret != -1) &&
401
        (callback3ret != -1) &&
402
        (callback4ret != -1) &&
403 404
        (callback5ret != -1) &&
        (callback6ret != -1)) {
405 406 407 408 409 410 411
        while(run) {
            struct pollfd pfd = { .fd = h_fd,
                              .events = h_event,
                              .revents = 0};

            sts = poll(&pfd, 1, TIMEOUT_MS);

412 413
            /* if t_timeout < 0 then t_cb must not be called */
            if (t_cb && t_active && t_timeout >= 0) {
414
                t_cb(t_timeout,t_opaque);
415
            }
416 417 418 419 420 421 422 423 424 425 426 427 428

            if (sts == 0) {
                /* DEBUG0("Poll timeout"); */
                continue;
            }
            if (sts < 0 ) {
                DEBUG0("Poll failed");
                continue;
            }
            if ( pfd.revents & POLLHUP ) {
                DEBUG0("Reset by peer");
                return -1;
            }
429

430 431 432 433 434 435
            if(h_cb) {
                h_cb(0,
                     h_fd,
                     myPollEventToEventHandleType(pfd.revents & h_event),
                     h_opaque);
            }
436 437 438

        }

439 440
        DEBUG0("Deregistering event handlers");
        virConnectDomainEventDeregister(dconn, myDomainEventCallback1);
441
        virConnectDomainEventDeregisterAny(dconn, callback2ret);
442
        virConnectDomainEventDeregisterAny(dconn, callback3ret);
443
        virConnectDomainEventDeregisterAny(dconn, callback4ret);
444
        virConnectDomainEventDeregisterAny(dconn, callback5ret);
445
        virConnectDomainEventDeregisterAny(dconn, callback6ret);
446 447
    }

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

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

457 458 459 460 461 462
#else
int main(void) {
    printf("event-test program not available without sys/poll.h support\n");
    return 1;
}
#endif