event-test.c 10.3 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 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
#include <sys/types.h>
#include <sys/poll.h>
#include <libvirt/libvirt.h>

#define DEBUG0(fmt) printf("%s:%d :: " fmt "\n", \
        __FUNCTION__, __LINE__)
#define DEBUG(fmt, ...) printf("%s:%d: " fmt "\n", \
        __FUNCTION__, __LINE__, __VA_ARGS__)
#define STREQ(a,b) (strcmp((a),(b)) == 0)

#ifndef ATTRIBUTE_UNUSED
#define ATTRIBUTE_UNUSED __attribute__((__unused__))
#endif

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

/* timeout globals */
#define TIMEOUT_MS 1000
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 myDomainEventCallback1 (virConnectPtr conn, virDomainPtr dom,
42
                            int event, int detail, void *opaque);
43
int myDomainEventCallback2 (virConnectPtr conn, virDomainPtr dom,
44
                            int event, int detail, void *opaque);
45
int myEventAddHandleFunc  (int fd, int event,
46 47 48
                           virEventHandleCallback cb,
                           void *opaque,
                           virFreeCallback ff);
49 50
void myEventUpdateHandleFunc(int watch, int event);
int  myEventRemoveHandleFunc(int watch);
51

52 53 54 55
int myEventAddTimeoutFunc(int timeout,
                          virEventTimeoutCallback cb,
                          void *opaque,
                          virFreeCallback ff);
56 57 58 59 60 61 62 63 64 65 66
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) {
67
    const char *ret = "";
68
    switch(event) {
69 70
        case VIR_DOMAIN_EVENT_DEFINED:
            ret ="Defined";
71
            break;
72 73
        case VIR_DOMAIN_EVENT_UNDEFINED:
            ret ="Undefined";
74 75 76 77 78 79 80 81 82 83 84 85 86
            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;
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
    }
    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";
103
            break;
104 105 106 107 108 109 110 111 112 113 114 115 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 146 147 148 149
        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)
                ret = "Unpaused";
            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;
            }
150 151 152 153 154 155 156 157
            break;
    }
    return ret;
}

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

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

179 180 181 182 183 184 185
static void myFreeFunc(void *opaque)
{
    char *str = opaque;
    printf("%s: Freeing [%s]\n", __FUNCTION__, str);
    free(str);
}

186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216

/* 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,
217 218 219
                          virEventHandleCallback cb,
                          void *opaque,
                          virFreeCallback ff)
220 221 222 223 224
{
    DEBUG("Add handle %d %d %p %p", fd, event, cb, opaque);
    h_fd = fd;
    h_event = myEventHandleTypeToPollEvent(event);
    h_cb = cb;
225
    h_ff = ff;
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
    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;
241 242
    if (h_ff)
       (h_ff)(h_opaque);
243 244 245
    return 0;
}

246 247 248 249
int myEventAddTimeoutFunc(int timeout,
                          virEventTimeoutCallback cb,
                          void *opaque,
                          virFreeCallback ff)
250 251 252 253 254
{
    DEBUG("Adding Timeout %d %p %p", timeout, cb, opaque);
    t_active = 1;
    t_timeout = timeout;
    t_cb = cb;
255
    t_ff = ff;
256 257 258 259 260 261 262 263 264 265 266 267 268 269
    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;
270 271
   if (t_ff)
       (t_ff)(t_opaque);
272 273 274 275 276 277 278 279 280 281
   return 0;
}

/* main test functions */

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

282 283 284 285 286 287 288 289 290
int run = 1;

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


291 292 293
int main(int argc, char **argv)
{
    int sts;
294 295 296
    struct sigaction action_stop = {
        .sa_handler = stop
    };
297 298 299 300 301

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

303 304 305 306 307 308 309 310
    virEventRegisterImpl( myEventAddHandleFunc,
                          myEventUpdateHandleFunc,
                          myEventRemoveHandleFunc,
                          myEventAddTimeoutFunc,
                          myEventUpdateTimeoutFunc,
                          myEventRemoveTimeoutFunc);

    virConnectPtr dconn = NULL;
311
    dconn = virConnectOpen (argv[1] ? argv[1] : NULL);
312 313 314 315 316
    if (!dconn) {
        printf("error opening\n");
        return -1;
    }

317 318 319
    sigaction(SIGTERM, &action_stop, NULL);
    sigaction(SIGINT, &action_stop, NULL);

320 321 322
    DEBUG0("Registering domain event cbs");

    /* Add 2 callbacks to prove this works with more than just one */
323 324 325 326
    virConnectDomainEventRegister(dconn, myDomainEventCallback1,
                                  strdup("callback 1"), myFreeFunc);
    virConnectDomainEventRegister(dconn, myDomainEventCallback2,
                                  strdup("callback 2"), myFreeFunc);
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352

    while(run) {
        struct pollfd pfd = { .fd = h_fd,
                          .events = h_event,
                          .revents = 0};

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

        /* We are assuming timeout of 0 here - so execute every time */
        if(t_cb && t_active)
            t_cb(t_timeout,t_opaque);

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

        if(h_cb) {
353 354
            h_cb(0,
                 h_fd,
355 356 357 358 359 360
                 myPollEventToEventHandleType(pfd.revents & h_event),
                 h_opaque);
        }

    }

361 362 363 364 365
    DEBUG0("Deregistering event handlers");
    virConnectDomainEventDeregister(dconn, myDomainEventCallback1);
    virConnectDomainEventDeregister(dconn, myDomainEventCallback2);

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

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

374 375 376 377 378 379
#else
int main(void) {
    printf("event-test program not available without sys/poll.h support\n");
    return 1;
}
#endif